Skip to main content

Append a variable to a list

Answered

Comments

2 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 Sanaa,

    Please note that your example is not reproducible, because values for Demand and Horizon are missing.

    Moreover, you are adding Plants x Demand x Horizon variable in each loop iteration by using the addVars method. If you want to assign a value to a certain dictionary field, you have to access it via \(\texttt{Prod_values[l,p,d] = ...}\)

    What you are probably looking for is something like

    import gurobipy as gp
    from gurobipy import GRB

    model = gp.Model()

    Prod_values = {}
    obj_vars = []
    Plants = range(4)
    Demand = range(3)
    Horizon = range(2)
    for l in Plants:
        for p in Demand:
            for d in Horizon:
                Prod_values[l,p,d] = model.addVar(lb = 0, vtype =GRB.INTEGER, name ='Prod_engrais[%d,%d,%d]'%(l,p,d))
                obj_vars.append(Prod_values[(l,p,d)])
    model.update()
    print(Prod_values)
    print(obj_vars)

    Best regards, 
    Jaromił

    0

Post is closed for comments.