change the coefficient of the variables in the objective function
Awaiting user inputI 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

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 alphalike
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 reoptimize the model via optimize. Since, you are changing an objective coefficient only, the optimization will be warmstarted 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
Please sign in to leave a comment.
Comments
10 comments