简体   繁体   中英

How to use initial solution in pulp (python)

Community,

I have the next problem I want to do a improvement heuristic (Fix and Optimize) and I need to use the initial solution to start, my problem is MIP having binary and continuous variables. I asked him how I would do to use the previous solution as an initial and how I leave some of those variables unfix for your optimization.

Ps: I use --> prob.solve(CPLEX_PY(timeLimit = '3600', epgap = 0.001))

Thanks.

You said you were using PuLP to solve it. You can actually just tell PuLP to use current values of the variables as a warmStart by adding the corresponding argument.

So, for the line you put you would do as follows:

prob.solve(CPLEX_PY(timeLimit = 3600, gapRel = 0.001, warmStart=True))

This assumes that the variables have a value already (because you already solved a model with them or because you set it out yourself.

For more information on warm-starting as well as an example you can go to the docs: https://coin-or.github.io/pulp/guides/how_to_mip_start.html

More information on what you can pass to each solver (including CPLEX_PY ): https://coin-or.github.io/pulp/technical/solvers.html

with docplex python API you can do this :

from docplex.mp.model import 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*460 + nbbus30*360)

mdl.get_cplex().MIP_starts.read("c:/file.mst")

sol=mdl.solve(log_output=True)

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

Full example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooreadMST.py

Or you can use APIs

from docplex.mp.model import 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)

warmstart=mdl.new_solution()
warmstart.add_var_value(nbbus40,8)
warmstart.add_var_value(nbbus30,0)
mdl.add_mip_start(warmstart)


sol=mdl.solve(log_output=True)

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

in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoowarmstartapi.py

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM