简体   繁体   English

"如何在 Python 中导出 docplex 解决方案?"

[英]How to export docplex solutions in Python?

I use docplex.model.print_solution()<\/code> to get solutions in the console.我使用docplex.model.print_solution()<\/code>在控制台中获取解决方案。 How can I export all the solutions to a file?如何将所有解决方案导出到文件?

Thanks谢谢

"

You can use python files.您可以使用 python 文件。

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来自https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoowritesolutioninafile.py

The export method will create a detailed json file:导出方法会创建一个详细的 json 文件:

mdl.solution.export("solution.json")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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