gurobi Flashcards
how to make a model
we need to initialize
import gurobipy as gp
model = gp.Model()
how do we add variables?
If many, we add as a matrix.
We can add separate variables in the case where we have few.
We use the addVar() function.
addVar() takes the following arguments:
- lb (lower bound)
- ub (upper bound)
- obj (objective function coefficient)
- vtype (variable type, default is GRB.CONTINUOUS)
- name (name, default is “”)
- column (indicates the set of constraints the variable is included in)
All of these are OPTIONAL. We can add iwthout specifying any
import gurobipy as gp
model = gp.Model()
x1 = model.addVar(name=”Fuck off”)
how do we configure the objective function?
model.setObjective(mathematicalExpression, sense=GRB.MAXIMIZE)
can also use minimize.
the mathemtical expression use the variables we have defined, like this:
x1=model.addVar()
x2=model.addVar()
model.setObjective(x1 + 5x2, sense=GRB.MAXIMIZE)
We could also do it differently:
model.ModelSense=GRB.MAXIMIZE
model.addVar(obj=1)
model.addVar(obj=5)
how do we add constraints to the model+
We use the function “addConstr()”
addConstr() takes two arguments:
1) the mathematical expression as an equality or inequality
2) the name of the constraint
how to run the model?
model.optimize()
Given that the model is solved, how to retreive the information of the solution?
Given that we have the variables defined, we can call on them like this:
optimal_x1 = x1.X
optimal_x2 = x2.X
print(optimal…)
optimal_value = model.ObjVal
How do we retrieve the solution if we have not saved th evariables?
for var in model.getVars():
print(var.X)
how can we add say 10 variables to a model using indices
model = gp.Model()
for i in range(1, 11):
model.addVar(name=f”x_{i}”)
say we have added a list of variables to the model. Elaobrate on the process of doing this, and also adding coefficients to both obj and constraints when these coefficients are stored as lists
we have already done this:
model = gp.Model()
for var in range(indices):
model.addVar(name=f”x_{var}”)
Now we need to add the other coefficients:
Assume we have a list that hold constraint coeff and a list that holds obj coeff:
We want to construct the mathematical objective function:
objective_function = 0
for i in range(n_indices):
objective_function += (objective_coefficients[i] x variables[i])
so, we need to have the list variables as well. We can get this from:
list_of_variables = model.getVars()
NOW ON TO THE CONSTRAINTS
We apply the same logic of constructing the LHS:
constraint_lhs = 0
for i in range(n_indices):
constraint_lhs += (constraint_coefficients[i] x list_of_variables[i])
Now we can simply set the objective:
model.setObjective(objective_function, sense=GRB.MAXIMIZE)
and add the constraint:
model.addConstr(constraint_lhs <= RHS-value, name=”constraint”)
And we can now optimize:
model.optimize()
And get the results:
for i in model.getVars():
print(f”Variable {i}: {i.X}”)
and objective function value:
print(model.ObjVal)
the pvrevious method to add variables used .addVar() for each index in a list. is there another way to do this?
variables = model.addVars(numberOfVariablesToAdd)
Now we have the list of new variables.
WE can use list comprehension with quicksum to make it faster:
model.setObjective(gp.quicksum(objective_coeffs[i] x variables[i] for i in range(len(objective_coeffs))), sense=GRB.MAXIMIZE)
We can do the same with constraints.
the entire point is that this allows us to not build the functions ourselves, but rather use teh quicksum feature of gurobi.
how do we fidn the shadow prices
after optimizing, we access a constraint by:
constraints = model.getConstrs():
for i in constrains:
print(i.Pi)
Pi represent dual price/dual variable
for constraints, how do we get sensitivity informaiton?
typically throughj:
constraint.SARHSLow
constraint.SARHSUp
constraint.RHS for the current
constraint.Pi
constraint.Slack
for variables, how do we get sensitivity informaiton?
for each variable:
variable.VarName
variable.X
variable.RC
variable.Obj
variable.SAObjUp
variable.SAObjLow
elaborate on multidict
Multidict takes a dictionary, and returns dictionaries so that if the original dictionary has multiple values (a list) for each key, this is split up into separate dictionaries.
The point is that instead of having a big dictionary where a single key will access a list of different values, we know have multiple dictionaries so that each key correspond to exactly one object. The differnece is that we have multiple dicts.
how do we cope with integers and binaries in gurobi?
When adding a variable, we must specify the type. GRB.CONTINUOUS is default. we use INTEGER or BINARY when necessary.