I use docplex.model.print_solution()
to get solutions in the console. How can I export all the solutions to a file?
Thanks
I use docplex.model.print_solution()
to get solutions in the console. How can I export all the solutions to a file?
Thanks
You can use python files.
Let me use the zoo example.
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)
mdl.solve(log_output=True,)
#display solution
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
#write solution to a file
f= open("c://temp//sol.txt", "w")
for v in mdl.iter_integer_vars():
f.write(str(v)+" = "+str(v.solution_value)+'\n')
f.close()
"""
which gives
nbBus40 = 6.0
nbBus30 = 2.0
in the display and in the file
"""
from https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoowritesolutioninafile.py
The export method will create a detailed json file:
mdl.solution.export("solution.json")
If you want to store only the results you see in docplex.model.print_solution()
, you can use:
with open("solution.txt", "w") as solfile:
solfile.write(mdl.solution.to_string())