Decision Optimization

 View Only
  • 1.  Getting RHS of a constraint

    Posted Tue February 28, 2023 12:35 AM
    I am defining one LP and the details are as follows:

    from docplex.mp.model import Model

    import numpy as np

    import sys

    #-------------------------------------#

    ## Parameters Details

    tech_coeff=np.array([[1, 1, 1, 1, 1],[0.4, 0.4, 0.4, -0.6, -0.6],[0.5, 0.5, -0.5, 0, 0],[0.06, 0.03, -0.01, 0.01, -0.02]])

    rhs_coeff=np.array([12, 0, 0, 0])

    cost_coeff=np.array([0.026, 0.0509, 0.0864, 0.06875, 0.078])


    ## Creating Model

    mod1=Model(name='my first model')

    ## Defining variables

    x={i:mod1.continuous_var(name='x_{}'.format(i+1)) for i in range(5)}
    x_vars_scope=[]
    for i in x:
       x_vars_scope.append(x[i])
    x_vars=np.array(x_vars_scope)


    ## Adding constraints

    for i in range(len(tech_coeff)):
        mod1.add_constraint(tech_coeff[i].dot(x_vars.transpose())<= rhs_coeff[i],ctname='constraint_number_{}'.format(i+1))


    ## Objective function
    objective=cost_coeff.dot(x_vars.transpose())
    mod1.maximize(objective)

    ## Solving the model
    mod1.solve()
    mod1.print_solution()


    I want to get the RHS (numeric value) of each constraint.
    I request to the community please help me in this regard.

    Thanks,
    Samiullah







  • 2.  RE: Getting RHS of a constraint

    Posted Tue February 28, 2023 03:41 AM
    Edited by Hugues Juille Tue February 28, 2023 04:20 AM

    Hello Mohammad,

    In your example, the RHS of constraints are defined by a constant array so the solution below is rather cumbersome, but it works in the general case...

    The simplest way is to create one additional decision variable for each constraint for which you want to get the RHS value, and add the corresponding equality constraint to the model.

    In your example, this would look like as follows:

    rhs_vars = [mod1.continuous_var(name='rhs_{}'.format(i)) for i in range(len(tech_coeff))]
    for i in range(len(tech_coeff)):
    mod1.add_constraint(rhs_vars[i] == rhs_coeff[i])
    mod1.add_constraint(tech_coeff[i].dot(x_vars.transpose()) <= rhs_vars[i], ctname='constraint_number_{}'.format(i+1))

    I hope this helps.

    Best regards



    ------------------------------
    Hugues Juille
    ------------------------------



  • 3.  RE: Getting RHS of a constraint

    Posted Wed March 01, 2023 02:23 AM

    Dear Hugues Juille,

    Thanks for your reply.



    ------------------------------
    MOHAMMAD SAMIULLAH
    ------------------------------