Skip to main content

change the coefficient of the variables in the objective function

Awaiting user input

Comments

10 comments

  • Jaromił Najman
    Gurobi Staff 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ł

    0
  • ahmad alanaqreh
    First Question
    First Comment

    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

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • ahmad alanaqreh
    First Question
    First Comment

    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
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • ahmad alanaqreh
    First Question
    First Comment

    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
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

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

    0
  • Jaromił Najman
    Gurobi Staff 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ł

    0
  • ahmad alanaqreh
    First Question
    First Comment

    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
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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.

Powered by Zendesk