• 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 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ł

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!

• Gurobi Staff

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 gpfrom gurobipy import GRBm = 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 0m.setObjectiveN(3*x + 3*y, 1, 1) # objective 1m.params.ObjNumber = 0m.update()x.ObjN = 1 # change objective 0 to x + 2*ym.params.ObjNumber = 1m.update()y.ObjN = 1 # change objective 1 to 3*x + ym.write("myLP.lp")

Best regards,
Jaromił

Thanks!