Skip to main content

value the variable

Answered

Comments

10 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    Hi Niloofar,

    You can access the value of an optimization variable after at least one feasible solution has been found via the X attribute. Example mip1.py shows how to print variable values after a successful optimization.

    Best regards, 
    Jaromił

    0
  • Niloofar Shirvani
    • Gurobi-versary
    • Conversationalist
    • First Question

    Actually I do not want to print variable value, I want to give them value by myself and then compare each solution.

    0
  • Jaromił Najman
    • Gurobi Staff

    If you want to fix a variable to a particular value you can either add an equality constraint fixing it to a constant value or setting a variable's lower and upper bound to the same value.

    # fix via equality constraint
    x = model.addVar(vtype=GRB.BINARY)
    model.addConstr(x==1)
    # fix via bounds
    x.lb = 1
    x.ub = 1
    # [...]
    # optimize model with fixed values to get new solution and objective value
    model.optimize()

    Best regards, 
    Jaromił

    0
  • Niloofar Shirvani
    • Gurobi-versary
    • Conversationalist
    • First Question

    Dear,

    Actually I want to give value 1 to all of x variable but in order.

    for example: at first x(1,0,2)=1 and get model objective, then x(1,0,3)=1 and get model objective by this criteria again. 

    this would be loop for value this variable.

    please give me hand to get the solution.

    0
  • Jaromił Najman
    • Gurobi Staff

    You can use the code from my previous comment and put it in a \(\texttt{for}\)-loop to achieve what you need.

    for j in J:
    # fix variable to desired value
    x[(1,0,j)].lb = 1
    x[(1,0,j)].ub = 1
    # optimize model
    model.optimize()
    # if at least one solution point has been found,
    # set obj to objective value and infinity otherwise
    if (model.SolCount > 0)
    obj = model.ObjVal
    else
    obj = GRB.INFINITY
    # do something with the objective value for this run
    # [...]
    # reset bound of the variable
     x[(1,0,j)].lb = 0
    x[(1,0,j)].ub = 1

    You can find more information about model and variable attributes in the attributes documentation.

    Best regards, 
    Jaromił

    0
  • Niloofar Shirvani
    • Gurobi-versary
    • Conversationalist
    • First Question

    Thank a lot. It works.

    0
  • Niloofar Shirvani
    • Gurobi-versary
    • Conversationalist
    • First Question

    Sorry, I have another question.

    how can I bring the objective value in a dictionary or table with their variable?

    0
  • Jaromił Najman
    • Gurobi Staff

    Note that this is no longer Gurobi related. You can use a standard Python dictionary for this

    d = {}
    for j in J:
    #[..]
    d[(1,0,j)] = obj

    # access objective value for fixed variable (1,0,0)
    print(d[(1,0,0)])
    0
  • Niloofar Shirvani
    • Gurobi-versary
    • Conversationalist
    • First Question

    Thanks again.

    0

Post is closed for comments.