3
$\begingroup$

I came across the following functionality that is offered by CPLEX for modifying and re-optimizing a model based on previous computations: https://perso.ensta-paris.fr/~diam/ro/online/cplex/cplex1271/CPLEX/GettingStarted/topics/tutorials/Python/modify_reopt.html

I could not find any examples of how to use this functionality with the Python API. Does CPLEX support this "under the hood" or I need to do something more advanced to enable it? Can someone point me to a resource or give an example?

$\endgroup$

2 Answers 2

2
$\begingroup$

You can do incremental changes

https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooincremental.py

from docplex.mp.model import Model

# original 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)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

#now 350 kids instead of 300

print()
print("now 350 kids instead of 300")    
    
mdl.get_constraint_by_name("kids").rhs=350;
mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

# no more than 4 buses 40 seats

print()
print("no more than 4 buses 40 seats")


mdl.get_var_by_name("nbBus40").ub=4
mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

#change the objective so that cost for 40 seats is 450
#and remove the limit on the number of buses 40 seats

print()
print("change the objective so that cost for 40 seats is 450")
print("and remove the limit on the number of buses 40 seats  ")  
    
mdl.get_var_by_name("nbBus40").ub=1000
mdl.set_objective("min",nbbus40*450 + nbbus30*400);
mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

which gives

which gives
nbBus40  =  6.0
nbBus30  =  2.0
now 350 kids instead of 300
nbBus40  =  8.0
nbBus30  =  1.0
no more than 4 buses 40 seats
nbBus40  =  2.0
nbBus30  =  9.0
change the objective so that cost for 40 seats is 450
and remove the limit on the number of buses 40 seats  
nbBus40  =  8.0
nbBus30  =  1.0
$\endgroup$
1
  • $\begingroup$ Thank you for the answer, @Alex! I have sent you a LinkedIn invite as I wanted to ask you something more specific $\endgroup$
    – Pia MiA
    Dec 12, 2022 at 19:38
1
$\begingroup$

Now with the CPLEX Matrix API you can do the same kind of changes of course.

Let me start with the example from

https://medium.com/@alexfleischer_84755/optimization-simply-do-more-with-less-zoo-buses-and-kids-part2-python-java-c-cc04558e49b5

And suppose that in buses 40 seats, suddenly we allow 70 kids.

# Import packages.
import cplex
my_prob = cplex.Cplex()
my_obj = [500, 400]
my_ub = [cplex.infinity, cplex.infinity]
my_lb = [0.0, 0.0]
my_ctype = "II"
my_colnames = ["nbBus40", "nbBus30"]
my_rhs = [300]
my_rownames = ["nbKids"]
my_sense = "G"
my_prob.objective.set_sense(my_prob.objective.sense.minimize)
my_prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
names=my_colnames)
rows = [[["nbBus40", "nbBus30"], [40,30]]]
my_prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
rhs=my_rhs, names=my_rownames)
my_prob.solve()
print("cost  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()
    
sol = my_prob.solution.get_values()
for j in range(numcols):
    print(my_colnames[j],"  = ", sol[j])

#now let us do some change

print("now the 40 seats buses can bring 70 kids to the zoo")

my_prob.linear_constraints.set_coefficients(0,0,70)

my_prob.solve()
print("cost  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()
    
sol = my_prob.solution.get_values()
for j in range(numcols):
    print(my_colnames[j],"  = ", sol[j])

works fine and gives

cost  =  3800.0
nbBus40   =  6.0
nbBus30   =  2.0
now the 40 seats buses can bring 70 kids to the zoo

MIP start 'm1' defined initial solution with objective 3800.0000.

cost  =  2400.0
nbBus40   =  4.0
nbBus30   =  1.0
$\endgroup$

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.