Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

How to get the values of set of particular variables in the relaxed model

  • 1.  How to get the values of set of particular variables in the relaxed model

    Posted Tue April 11, 2023 06:13 AM
    Hello everybody!

    I am solving a MILP example (details are as follows).

    from docplex.mp.model import Model
    from docplex.mp.relax_linear import LinearRelaxer

    n_xvars=2    # number of 'x' variables
    n_yvars=2    # number of 'y' variables

    #-----Model-----#
    my_MILP=Model(name='MILP example')

    x_vars={i+1:my_MILP.continuous_var(name='x_{}'.format(i+1),lb=-20,ub=20) for i in range(n_xvars)}

    y_vars={i+1:my_MILP.integer_var(name='y_{}'.format(i+1),lb=-20,ub=20) for i in range(n_yvars)}

    # Adding constraints

    my_MILP.add_constraint(x_vars[1]+4*x_vars[2]>=-5)
    my_MILP.add_constraint(x_vars[1]-y_vars[2]<=5)
    my_MILP.add_constraint(x_vars[1]+3*y_vars[1]-10*y_vars[2]<=10)
    my_MILP.add_constraint(x_vars[1]+2*x_vars[2]-3*y_vars[1]+4*y_vars[2]<=2)

    # Defining objective function and solving
    obj=x_vars[1]+5*x_vars[2]+6*y_vars[1]-2*y_vars[2]
    my_MILP.minimize(obj)

    my_MILP.print_information()
    print(my_MILP.export_as_lp_string())

    # Solving and solution
    my_MILP.solve()
    my_MILP.print_solution()


    Then, I want to convert this model into an LP Relax model. For this, I wrote the following code:


    my_MILP_relax = LinearRelaxer.make_relaxed_model(my_MILP)
    my_MILP_relax.print_information()
    print(my_MILP_relax.export_as_lp_string())
    my_MILP_relax.solve()
    #knap_relax.display()
    my_MILP_relax.print_solution()


    Then, I want to print the set of 'y' variables. When I write the following code:

    for i in range( n_yvars):
        print(y_vars[i+1].solution_value)

    It is printing the values of MILP model. However, I want to print the values of a set of y_vars for the LP Relax model.

    I request the community please help me to get those values.



    Thanks,
    Samiullah





  • 2.  RE: How to get the values of set of particular variables in the relaxed model

    Posted Tue April 11, 2023 06:54 AM

    Hello,

    If I don't mistake, make_relaxed_model will return a new model, with new variables.
    So y_vars are still linked to your first MIP model.

    You should use the Solution object that is returned by the solve method to query the variable solutions.
    Another way is to get the hand on the new variables, via 
    relaxed_mdl.get_var_by_index(y_vars[i+1].index) which will return the cloned variable, on which you will be able to call solution_value

    Here are the 2 possible calls:

    s = my_MILP_relax.solve()
    for i in range( n_yvars):
    print(s[y_vars[i+1].name])
    print(my_MILP_relax.get_var_by_index(y_vars[i+1].index).solution_value)





    ------------------------------
    Vincent Beraudier
    ------------------------------