Adding objective functions and variables to a matrix
AnsweredHi,
I have an optimization problem. A parameter will get new values and solve the problem again. How can I define a matrix included objective functions and variables? (python with Gurobi package)
for example: A=[[obj1],[obj2],...], B=[[x1],[x2],...]

I am not sure what exactly you are asking for.
In case you are looking for a Matrix API, please have a look at Gurobi's Matrix API webinar. We also have a Knowledge Base section documenting some Python Matrix API tricks.
If you already have constructed your model and just would like to change a particular objective coefficient, you can set the Obj attribute of a particular variable to set its linear objective coefficient. If you would like to change a coefficient of a variable in a linear constraint, then you can use the chgCoeff method.
Best regards,
Jaromił0 
Thanks for your response,
I mean I have the following problem and I want to solve this problem 3 times, I have a matrix vf (with 3 columns), and I want to solve this problem for each column of this matrix every time.
def solve(col):
m= Model()#constraints and objective function
m.optimize()
return m.objval
if __name__ =="__main__":
vf=#matrix
for s in range(num_cols):
col=vf[:,s]
solve(col)Now I want to put all objective functions (3 objectives) into a matrix, or put variables of 3 problems in a matrix.
How can I define this matrix?
0 
I am still not sure if I fully understand what you are trying to achieve but here is my guess
import gurobipy as gp
m = gp.Model("test")
x = m.addVars(3)
obj = [[1,2,3],[4,5,6],[7,8,9]]
m.addConstr(gp.quicksum(x) == 1)
for i in range(len(obj)):
for j in range(len(obj[i])):
x[j].Obj = obj[i][j]
m.write("myLP%d.lp"%(i))
m.optimize()The above code will solve 3 models. Each of the models has a different objective function. The coefficients of the objective function are set via the variables' Obj attribute. The write method is used to check whether the generated models indeed look like what I expect them to be. Each of the models has a different objective function but they all have the same constraints.
Is this what you are looking for?
Best regards,
Jaromił0 
Sorry for not clarifying it well.
I have a parameter (vf) which is a matrix, this matrix contains two columns and I want to use each column for any problem.
vf= np.array([[1,2],[2,4],[4,4]])
In first problem vf=np.array([[1],[2],[4]])
in second problem vf=np.array([[2],[4],[4]])
I used def(col) as mentioned in my previous comment and I got the results. but in the results:
Set parameter Username
.
.
.
x[3,0] 500
x[3,3] 6000
ob 2.97521e+08Gurobi Optimizer version
.
.
.
x[2,2] 1800
x[3,3] 1000
ob 1.27515e+08As you can see the results are separate for each problem. I am going to define a matrix including objective functions (ob) for all problems together. for instance:
A=[[2.97521e+08],[1.27515e+08 ]]
How can I define this matrix A?
As you can see in my code:
if __name__ =="__main__":
vf=#matrix
for s in range(num_cols):
col=vf[:,s]
solve(col)I added this:
A=np.zeros((2,1))
A[s]=ob
print(A)but did not get A.
Regards,
Arash
0 
This should hopefully work for your problem
import gurobipy as gp
from gurobipy import GRB
import numpy as np
m = gp.Model("test")
x = m.addVars(3)
vf = np.array([[1,2],[2,4],[4,4]])
M = 3
N = 2
A = np.zeros((2,1))
m.addConstr(gp.quicksum(x) == 1)
for i in range(N):
for j in range(M):
x[j].Obj = vf[j][i]
m.optimize()
if m.status == GRB.OPTIMAL:
A[i] = m.ObjVal
print(A)Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
5 comments