Skip to main content

Create different models from "pool" of variables and constraints

Answered

Comments

3 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.
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Hi Andres,

    AFAIK both constraints and variables are specific to each model object in Gurobi.

    To get around this, you could for example declare your variables and constraints using only their indices and properties, and then add them to your models as you need.

    For example, you could define your "variables" using the following dict:

    vars_dict = {1: ["I", 0, 5, 2],
                 2: ["C", -5, 5, 0]}

    "constraints" as:

    constr_dict = {"A": [1, 2]}

    and then automate by calling

    m = gp.Model()
    model_specific_vars = dict()
    for index, var_properties in vars_dict.items():
        # some if-statement could come here
        model_specific_vars[index] = m.addVar(vtype=var_properties[0], lb=var_properties[1], ub=var_properties[2], obj=var_properties[3])

    for name, constr in constr_dict.items():
        # here also some if-conditions could be used
        # here I assume that the constraint is a multiple choice constraint
        expr = gp.quicksum(model_specific_vars[i] for i in constr)
        m.addLConstr(expr <= 1, name=name)

    You will find the information about the methods used above here and here.

    Hope this gives you some inspiration. Perhaps someone from Gurobi team can offer a better approach?

    Best regards
    Jonasz

    1
  • Andres Merchan
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks Jonasz Staszek for the suggestion; it definitely gives me good ideas to try a few things and I think puts me in the right track. I'll try to update the post once I find a solution based on your advice.

    0

Post is closed for comments.