Skip to main content

unrelax a model




  • Gwyneth Butera
    Gurobi Staff Gurobi Staff

    When you relax a model in Python, a new model object is returned. So you should still have access to the original "unrelaxed" model. -G

  • ghafour ahani
    First Comment
    First Question

    Correct. But, I am adding new variables to the relaxed model in an iterative process, and from time to time I need to switch back the model to integer form and solve the model.  Therefore, I need to switch back the relaxed model to the integer form, not the original one created at the begnining.

    Any help is appreciated.




  • Eli Towle
    Gurobi Staff Gurobi Staff

    The VType variable attribute gives the type of each variable. You can store the original types of all model variables in a new attribute, then switch all of the variables to continuous to build the relaxed model. Whenever you want to switch back to the integer form, revert all of the variable types to their original values using the new attribute.

    For example:

    # Store original variable types and relax model
    orig_vars = model.getVars()
    for v in orig_vars:
        v._VType = v.VType
        v.VType = GRB.CONTINUOUS

    # Do something with relaxed model here

    # Revert back to original variable types
    for v in orig_vars:
        v.VType = v._VType

Please sign in to leave a comment.