CPO Save Log/Intermediate solutions

huangapple go评论55阅读模式
英文:

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=&#39;buses&#39;)

nbbus40 = mdl.integer_var(0,6,name=&#39;nbBus40&#39;)
nbbus30 = mdl.integer_var(0,6,name=&#39;nbBus30&#39;)
cost= mdl.integer_var(0,1000000,name=&#39;cost&#39;)

mdl.add(cost==nbbus40*500 + nbbus30*400)
mdl.add(cost&lt;=4000)
mdl.add(nbbus40*40 + nbbus30*30 &gt;= 300)

siter = mdl.start_search(SearchType=&#39;DepthFirst&#39;,  TimeLimit=100)




for msol in siter:
    print(msol[nbbus40],&quot; buses 40 seats&quot;)
    print(msol[nbbus30],&quot; buses 30 seats&quot;)
    print(&quot;cost = &quot;,msol[cost])
    print(&quot;time = &quot;,time.time())
    print(&quot;\n&quot;)

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.

huangapple
  • 本文由 发表于 2023年5月22日 21:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定