2

how can I do sensitivity analysis in docplex (python)? Suppose we have this model:

 Max z= 3*x+2*y;
  st:
      2*x+y<=8;
      x+2*y<=6;

I use docplex in python for solve the model:

from docplex.mp.model import Model
tm = Model(name="MyModel")
x = tm.continuous_var()
y = tm.continuous_var()
tm.add_constraint(2*x+y <= 8)
tm.add_constraint(x+2*y <= 6)
expr = 3*x+2*y
tm.maximize(expr)
result = tm.solve()

How can determine the ranges of the right-hand-side constants for the constraints for which the current basis remains optimal?

AComputer
  • 519
  • 2
  • 10
  • 21

1 Answers1

3

you could use the cplex python object:

from docplex.mp.model import Model
tm = Model(name="MyModel")
x = tm.continuous_var()
y = tm.continuous_var()
tm.add_constraint(2*x+y <= 8)
tm.add_constraint(x+2*y <= 6)
expr = 3*x+2*y
tm.maximize(expr)
result = tm.solve()

cpx = tm.get_engine().get_cplex()

print(cpx.solution.sensitivity.lower_bounds())
print(cpx.solution.sensitivity.upper_bounds())
print(cpx.solution.sensitivity.bounds())
print(cpx.solution.sensitivity.objective())
print(cpx.solution.sensitivity.rhs())
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • I tried it but I am getting the following error: CplexSolverError: CPLEX Error 1217: No solution exists. Do you know how to fix it ? – campioni Aug 18 '20 at 08:28
  • Hi, you may get this because your model is not feasible. When you do print(result) what do you get ? If you get None then you have no solution. – Alex Fleischer Aug 18 '20 at 08:50
  • Hi, my model is feasible because I am able to print solutions, and when I checked them, they were correct. Is this possible only with continuous variables? Because all of my variables are integer. – campioni Aug 18 '20 at 08:56
  • Yes. But you could relax your integer model and then get that. See https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxintegrity.py – Alex Fleischer Aug 18 '20 at 09:04
  • Thanks. I would like to know which would be my solution/result in this case, would it be the 'rs' value ? – campioni Aug 18 '20 at 15:24