• Gurobi Staff

You can change the linear objective coefficient of a variable $$x$$ via the variable attribute Obj,e.g.,

x.Obj = 3

Best regards,
Jaromił

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_x

I got the message error

AttributeError: 'gurobipy.Var' object has no attribute 'Obj'

Regards

• Gurobi Staff

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ł

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

• Gurobi Staff

One way would be

import gurobipy as gpfrom gurobipy import GRBm = gp.Model("test")x = m.addVar()y = m.addVar()m.setObjective(x*y)m.optimize()# set new objectivem.setObjective(2*x*y)m.optimize()

Best regards,
Jaromił

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
• Gurobi Staff

Ok, so your model is not a quadratic one. Could you please post a minimal working example to reproduce this issue?

• Gurobi Staff

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ł

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??

• Gurobi Staff

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.