英文:
CPO Save Log/Intermediate solutions
问题
抱歉,我只能回答你要求的翻译部分:
"我正在使用CPO Optimizer的Python API。是否有办法保存日志/中间突出显示的解决方案和时间戳,以便构建优化过程的演变图表?"
"先行致谢!"
"到目前为止,我还没有找到解决方法,但复制粘贴日志似乎不是一个可行的方式..."
英文:
I am using the python API of CPO Optimizer. Is there any way to save the log/intermediate highlighted solutions and timestamps to build a chart on the evolution of the optimization?
Thanks in advance!
I haven't found a workaround so far, but copy-pasting the log seems not a feasible way...
答案1
得分: 0
最佳方法是使用来自CPO的回调。然后,当您获得新解决方案时,您可以在回调中执行您想要的任何操作。
英文:
Best approach is to use a callback from CPO. Then you can do whatever you want from that callback when you get a new solution.
答案2
得分: 0
Here is the translated content:
如果您不想依赖回调,您还可以使用CPO解决方案Iterator。
在https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocpoenumerate.py中进行小的更改
导入时间
从docplex.cp.model导入CpoModel
mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,6,name='nbBus40')
nbbus30 = mdl.integer_var(0,6,name='nbBus30')
cost= mdl.integer_var(0,1000000,name='cost')
mdl.add(cost==nbbus40500 + nbbus30400)
mdl.add(cost<=4000)
mdl.add(nbbus4040 + nbbus3030 >= 300)
siter = mdl.start_search(SearchType='DepthFirst', TimeLimit=100)
for msol in siter:
print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats")
print("cost = ",msol[cost])
print("time = ",time.time())
print("\n")
这将会得到
3 buses 40 seats
6 buses 30 seats
cost = 3900
time = 1684850008.5023165
-
3 0.39s 1 4 = nbBus40
4 buses 40 seats
5 buses 30 seats
cost = 4000
time = 1684850008.6289783
-
4 0.51s 1 4 != nbBus40
6 buses 40 seats
2 buses 30 seats
cost = 3800
time = 1684850008.7602491
! ----------------------------------------------------------------------------
! 搜索完成,找到3个解决方案。
英文:
If you do not want to rely on callbacks you can also use CPO solution Iterator.
Small changes in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocpoenumerate.py
gives
import time
from docplex.cp.model import CpoModel
mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,6,name='nbBus40')
nbbus30 = mdl.integer_var(0,6,name='nbBus30')
cost= mdl.integer_var(0,1000000,name='cost')
mdl.add(cost==nbbus40*500 + nbbus30*400)
mdl.add(cost<=4000)
mdl.add(nbbus40*40 + nbbus30*30 >= 300)
siter = mdl.start_search(SearchType='DepthFirst', TimeLimit=100)
for msol in siter:
print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats")
print("cost = ",msol[cost])
print("time = ",time.time())
print("\n")
which gives
3 buses 40 seats
6 buses 30 seats
cost = 3900
time = 1684850008.5023165
* 3 0.39s 1 4 = nbBus40
4 buses 40 seats
5 buses 30 seats
cost = 4000
time = 1684850008.6289783
* 4 0.51s 1 4 != nbBus40
6 buses 40 seats
2 buses 30 seats
cost = 3800
time = 1684850008.7602491
! ----------------------------------------------------------------------------
! Search completed, 3 solutions found.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论