I am using Cplex python API for my optimization problem. I want to run many instances of the problem and each time I want to create a new model with a different set of variables. The problem is that I am getting a warning that says I have used some variables before. For that, I want to reset the model each time. Even when I call a function and from that function I create the model, still I get that warning. I there any function in cplex python API to reset the variables and everything in a model?
Some examples in Easy optimization with python
import random
import math
random.seed(1)
from docplex.mp.model import Model
# original model
nbKids=300
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
costBus40=500.0;
costBus30=400.0;
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= nbKids, 'kids')
mdl.minimize(nbbus40*costBus40 + nbbus30*costBus30)
nbSamples=20
nbMaxKidsAbsent=30;
nbKidsLess=[random.randint(0,nbMaxKidsAbsent) for i in range(0,nbSamples)]
nbKidsOptions=[nbKids-nbKidsLess[i] for i in range(0,nbSamples)]
#Monte Carlo optimization
totalCost=0.0;
for i in range(0,nbSamples):
mdl.get_constraint_by_name("kids").rhs=nbKidsOptions[i]
mdl.solve()
cost=mdl.solution.get_objective_value()
totalCost+=cost
print("if we need to bring ",nbKidsOptions[i]," kids to the zoo");
print("cost = ",cost)
print()
averageCost=1/nbSamples*totalCost
print("------------------------------");
print("average cost = ",math.ceil(averageCost));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.