change the coefficient of the variables in the objective function
AnsweredI have a model that I am solving with callback, I need to change the coefficients of the variables in the objective function from time to time, I have seen the Model.chgCoeff() to change the coefficient of the variables in constraints but how I can do it in the objective function ??
Regards
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
You can change the linear objective coefficient of a variable \(x\) via the variable attribute Obj,e.g.,
x.Obj = 3
Best regards,
Jaromił0 -
Hi Jarmoil
I have the following objective function:
model.setObjective(quicksum(x[i, j] * alpha[i] for (i, j) in E if i != s)
+ quicksum((n * (1 - x[i, j]) - 1) * beta[i, j] for (i, j) in E if j != s) + omega
- (z* lamda))Now I have the values of x and the value of z if I tried to change the coefficient of alpha-like
for i in range(0, n):
temp_x = 0
for (j) in range(0, n + 1):
if (i, j) in E:
temp_x += x[i, j]
alpha[i].Obj = temp_xI got the message error
AttributeError: 'gurobipy.Var' object has no attribute 'Obj'
Regards
0 -
Hi Ahmad,
Note that the Obj attribute changes the linear objective coefficient of a variable.
I assume that \(\texttt{alpha}\) and \(\texttt{x}\) are both optimization variables, which makes your objective a quadratic one. You cannot change the quadratic coefficient of a variable. To change the coefficient of a quadratic term you have to reconstruct the objective function and replace the old one.
Best regards,
Jaromił0 -
Hi Jaromil
Thanks a lot for your answer, yes that is true, in this case how I can replace the objective function?? should I create a new model with a new objective function or there is a way to do it ??
Regards
0 -
Hi Ahmad,
One way would be
import gurobipy as gp
from gurobipy import GRB
m = gp.Model("test")
x = m.addVar()
y = m.addVar()
m.setObjective(x*y)
m.optimize()
# set new objective
m.setObjective(2*x*y)
m.optimize()Best regards,
Jaromił0 -
In my case x[i, j] and z are parameters(alpha, beta, omega, and lamda are variables), actually, these are the values changing every time, so I am changing my objective function based on the new values of x and z, I have tried the way you mentioned as follows :
model.setObjective(quicksum(x[i, j] * alpha[i] for (i, j) in E if i != s)
+ quicksum((n * (1 - x[i, j]) - 1) * beta[i, j] for (i, j) in E if j != s) + omega
- (z* lamda))I got the error message :
gurobipy.GurobiError: Variable not in model
0 -
Ok, so your model is not a quadratic one. Could you please post a minimal working example to reproduce this issue?
0 -
Do I understand correctly that you are trying to change a coefficient in a callback call? If yes, then this is not possible. What you can do is to terminate the optimization process, change the objective coefficients and then re-optimize the model via optimize. Since, you are changing an objective coefficient only, the optimization will be warm-started to improve performance.
Best regards,
Jaromił0 -
Sorry Jaromil, your suggestion is not clear for me, terminating the MP model will terminate the callback which is not what I want, do you mean terminating the DSP model where I am trying to change the coefficients??
0 -
Hi Ahmad,
I was able to execute your code and did not get any errors. You are getting each variable by name and then setting its Obj attribute. This works properly. Could you try to generate a minimal working example?
I just now noticed that you are actually working with a separate model in your \(\texttt{getSubSolution}\) function so please just ignore my previous comment about terminating the optimization.
Best regards,
Jaromił0 -
Hi Jaromil,
How could I reset the coefficient of a specific variable in a specific objective function when I deal with a multi-objectives problem in Gurobi?
Thank you so much!
0 -
How could I reset the coefficient of a specific variable in a specific objective function when I deal with a multi-objectives problem in Gurobi?
You can use the ObjN attribute after setting the ObjNumber.
import gurobipy as gp
from gurobipy import GRB
m = gp.Model()
x = m.addVar(lb = 0, vtype=GRB.CONTINUOUS, name="x")
y = m.addVar(name="y")
m.setObjectiveN(2*x + 2*y, 0, 0) # objective 0
m.setObjectiveN(3*x + 3*y, 1, 1) # objective 1
m.params.ObjNumber = 0
m.update()
x.ObjN = 1 # change objective 0 to x + 2*y
m.params.ObjNumber = 1
m.update()
y.ObjN = 1 # change objective 1 to 3*x + y
m.write("myLP.lp")Best regards,
Jaromił0 -
Thanks!
0
Post is closed for comments.
Comments
14 comments