Skip to main content

Passing multiple variables across Python function/methods

Answered

Comments

4 comments

  • Ryuta Tamura
    Gurobi Staff Gurobi Staff

    Hi,

    You can pack variables as a List in the function:

    All_variables = [a, b, c]

    Then, variables can be unpacked outside the function like this:

    a, b, c = All_variables

    Is this the behavior you are looking for?

    Thanks,
    Ryuta

    1
  • Panggah Prabawa
    Curious
    Gurobi-versary
    Conversationalist

    Hi, Ryuta,

    It can be applicable too, however, I will apply the function iteratively so it may not be easy to write down all the variables that are going to be packed.

    Does Gurobi provide a way to automatically look for existing variables in the model or function before optimizing so that it can be called with just one argument?
    For example, using model.getVars(), I can pack all variables. However, unpacking it will be too tedious because it is a list and I can only access it using an index without the information of the variable names and their tuplelist indices.

    Thank you,
    Best, Panggah.

    0
  • Ryuta Tamura
    Gurobi Staff Gurobi Staff

    Hi,

    Unfortunately, Gurobi does not currently provide such a method. Is your goal to have each function take only Model object as arguments and refer to the necessary variable objects in the function? A variable object can be a member of a Model object.

    m._a = a
    m._b = b

    This allows variable objects to be passed to each function along with the model object, so that they can be referenced as needed.

    Thanks,
    Ryuta

    0
  • Panggah Prabawa
    Curious
    Gurobi-versary
    Conversationalist

    Hi, Ryuta,

    Sweet. Thank you for your explanation!

    Best, Panggah.

    0

Please sign in to leave a comment.