Search code examples
pyomo

Use solver results for initialization of the next step of solving in pyomo


I am new to pyomo and I am solving a problem in which step of solving some sets length increase.Therefore, the variables and constraints length will be increased. E.g. in the first step of solving model.p = pyo.Set(initialize = [1,2,3]). In the next step of solving model.p will be [1,2,3,4]; so, variables and constraints that depend on model.p will cahnge as well.

I want to use solver results in each step of solving for initilaizing the next step. This is the solving part:

model = pyo.ConcreteModel() opt = SolverFactory('cplex')

while True:

results = opt.solve(model, tee=True)
pyo.assert_optimal_termination(results)

if pyo.value(model.r_positive) == 1:
    AddCons(0)
else:
    AddCons(1)

if pyo.value(model.phi) == 0.0: 
   break

AddCons is the function that decides to increase one of the Sets length as well as variables and constraints.

Do you have any recommendation how to use solver results for initialization of the next step of solving?

THAnks in advance

I searched a lot but didn't find anything useful.


Solution

  • let me change a bit the zoo and buses example:

    import pyomo.environ as pyo
    from pyomo.opt import SolverFactory
    
    opt = pyo.SolverFactory('cplex')
    
    model = pyo.ConcreteModel()
    
    model.nbBus = pyo.Var([40,30], domain=pyo.PositiveIntegers)
    
    model.OBJ = pyo.Objective(expr = 500*model.nbBus[40] + 400*model.nbBus[30])
    
    model.Constraint1 = pyo.Constraint(expr = 40*model.nbBus[40] + 30*model.nbBus[30] >= 300)
    
    
    model.nbBus[40]=8
    model.nbBus[30]=1
    
    results = opt.solve(model,warmstart=True)
    
    print("nbBus40=",model.nbBus[40].value)
    print("nbBus30=",model.nbBus[30].value)
    

    gives

    nbBus40= 6.0
    nbBus30= 2.0