3
$\begingroup$

I am working on DoCplex to run a large model with several variables and constraints. I used the following link from sensitivity analysis in python + Docplex to run the sensitivity analysis. In that example, there was a limited number of constraints and variables, which made it easy to interpret, but with a very large instance, the task becomes more tedious.

Therefore, I have a question if I name the set of constraints as mdl.add_constraint(nbbus40==nbbus30, 'samenumberofbuses'), will it be possible to run sensitivity analysis on the values on the right-hand side of a specific set of constraints? Instead of having the values for print(cpx.solution.sensitivity.rhs()) which will give me the values on all constraints, I would only have it for what I am focusing on.

$\endgroup$

1 Answer 1

4
$\begingroup$

You can filter the results you get from sensitivity.rhs :

See small example out of the zoo example again:

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

mdl = Model(name='buses')  
nbbus40 = mdl.integer_var(name='nbBus40')  
nbbus30 = mdl.integer_var(name='nbBus30')  
ctKids=mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
ctKids2=mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 290, 'kids2') 
mdl.minimize(nbbus40*500 + nbbus30*400)   
mdl.solve() 

lp = LinearRelaxer.make_relaxed_model(mdl)
m2 = lp.solve()

cpx = lp.get_engine().get_cplex()



print(cpx.solution.sensitivity.rhs())

rhs=cpx.solution.sensitivity.rhs()

names = cpx.linear_constraints.get_names()
print(names)

#Now we want the result only for constraint "kids"

indexConstraint=names.index("kids")

print("right info for the constraint ",rhs[indexConstraint])

which gives

[(290.0, 1e+20), (-1e+20, 300.0)]
['kids', 'kids2']
right info for the constraint  (290.0, 1e+20)
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.