簡體   English   中英

"如何在 Python 中導出 docplex 解決方案?"

[英]How to export docplex solutions in Python?

我使用docplex.model.print_solution()<\/code>在控制台中獲取解決方案。 如何將所有解決方案導出到文件?

謝謝

您可以使用 python 文件。

讓我用動物園的例子

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

"""

來自https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoowritesolutioninafile.py

導出方法會創建一個詳細的 json 文件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM