I've been to trying to read a .sol
file in cplex python API before solving the problem but couldn't find any command to do so. There are analogous commands for this operation in c++, c, and Java.
1 Answer
First, let us see how to write a .mst file
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)
sol=mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
sol.export_as_mst(path="C:/", basename="file.mst")
generates file.mst
<?xml version = "1.0" standalone="yes"?>
<?xml-stylesheet href="https://www.ilog.com/products/cplex/xmlv1.0/solution.xsl" type="text/xsl"?>
<!-- This file has been generated by DOcplex version 2.4.61 -->
<CPLEXSolution version="1.0">
<header
problemName="buses"
objectiveValue="3480"
/>
<variables>
<variable name="nbBus40" index="0" value="6"/>
<variable name="nbBus30" index="1" value="2"/>
</variables>
</CPLEXSolution>
and then to warm start from there:
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)
which will display
1 of 1 MIP starts provided solutions.
MIP start 'm1' defined initial solution with objective 3480.0000.
from warmstart here
.sol
files look likexml
. Then, if no other way around it, you can use packages in python to read thosexml
files. Although, I personally just iterate over thesolution_value
of the variables that I want rather than relying on.sol
. Just a thought in case you can do the same. $\endgroup$