Skip to main content

Passing multiple variables across Python function/methods

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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.
  • Ryuta Tamura
    • 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
    • Gurobi-versary
    • First Question
    • 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

    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
    • Gurobi-versary
    • First Question
    • Conversationalist

    Hi, Ryuta,

    Sweet. Thank you for your explanation!

    Best, Panggah.

    0

Post is closed for comments.