0

I am developing a lp model with docplex on python and I need to get solution variables of the relaxed one but do not know how to ask python to give me the relaxed solution variables of the model. I can see them as an output but when I print it, it just turns the unrelaxed values of the variables. So, the question is how can i addressed,

y[i].solution_value    # how can i addressed y[i] as a relaxed solution value of y[i]

Best Regards,

Gas
  • 17,601
  • 4
  • 46
  • 93
alasskaa
  • 1
  • 1

1 Answers1

0

You could rely on LinearRelaxer as shown in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxintegrity.py

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

def make_bus_model():
    mdl = Model(name='buses')
    nbbus40 = mdl.integer_var(name='nbBus40')
    nbbus30 = mdl.integer_var(name='nbBus30')

    mdl.add_constraint(nbbus40 * 40 + nbbus30 * 30 >= 300, 'kids')
    mdl.minimize(nbbus40 * 500 + nbbus30 * 400)
    return mdl

if __name__ == '__main__':
    bm1 = make_bus_model()
    bm1.print_information()
    s1 = bm1.solve(log_output=True)
    s1.display()

    bmr = LinearRelaxer.make_relaxed_model(bm1)
    bmr.print_information()
    rs = bmr.solve(log_output=True)
    rs.display()

    duals = bmr.get_constraint_by_name("kids").dual_value

    print("dual of the 300 kids constraint = ",duals) 

which gives

solution for: buses
objective: 3800
nbBus40 = 6
nbBus30 = 2
Model: lp_buses
 - number of variables: 2
   - binary=0, integer=0, continuous=2
 - number of constraints: 1
   - linear=1
 - parameters: defaults
 - objective: minimize
 - problem type is: LP
Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
CPXPARAM_Read_DataCheck                          1
CPXPARAM_RandomSeed                              201903125
Tried aggregator 1 time.
LP Presolve eliminated 1 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)
solution for: lp_buses
objective: 3750.000
nbBus40 = 7.500
dual of the 300 kids constraint =  12.5
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • thank you for your quick answer, actually i already took a look at this example to understand what is wrong with my codes, but it also did not help. the model is always giving the results of variables for lp model which is unrelaxed. and i need to store relaxed ones, as an output there is no problem actually >>> rs.display() this code is working well – alasskaa Jul 06 '21 at 11:53