如何在docplex(python)中启动间隔变量界限? [英] How to initiate the interval variable bounds in docplex (python)?

查看:334
本文介绍了如何在docplex(python)中启动间隔变量界限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下OPL代码,需要在docplex(python)中实现,我是该领域的新手...

I have the following OPL code and need to implement it in docplex (python), i'm newbie in that field...

using CP;
dvar interval I1 in 0..20;
dvar interval I2 in 0..20;
dvar interval I3 in 0..20;

dvar int over1;
dvar int start1;
dvar int end1;

dvar interval artificialInterval;

maximize over1;

subject to
{

    over1==overlapLength(I1,I2);

    start1==maxl(startOf(I1),startOf(I2));

    end1==minl(endOf(I1),endOf(I2));

    startOf(artificialInterval)==start1;
    endOf(artificialInterval)==end1;

    over1==overlapLength(I3,artificialInterval);
} 

问题出现在行startOf(artificialInterval)== start1; 和endOf(artificialInterval)== end1;

The problem occurs with the lines startOf(artificialInterval)==start1; and endOf(artificialInterval)==end1;

这是我所做的:

mdl=CpoModel()

   I1=mdl.interval_var(define its parameters)
   I2=mdl.interval_var(define its parameters)
   I3=mdl.interval_var(define its parameters)

   over1=mdl.integer_var()
   start1=mdl.integer_var()
   end1=mdl.integer_var()

   artificialInterval=mdl.interval_var()

   over1=mdl.overlapLength(I1,I2)
   start1=mdl.max(mdl.start_of(I1),mdl.start_of(I2))
   end1=mdl.min(mdl.end_of(I1),mdl.end_of(I2))

   mdl.start_of(artificialInterval) =start1
   mdl.end_of(artificialInterval) =end1

   over1=mdl.overlapLength(I3,artificialInterval)

   obj = mdl.maximize(over1)
   mdl.solve()

与此有关,我有语法错误"SyntaxError:无法分配给函数调用" 因此,我尝试使用set_start(start1),但我认为我也没有很好地使用它...

With this i have the error of syntax "SyntaxError: can't assign to function call" So i have tried to use the set_start(start1) but i think i didn't use it well also...

任何提示都值得赞赏. 谢谢

Any hints are appreciated. Thank you,

推荐答案

from docplex.cp.model import CpoModel 
mdl=CpoModel()

I1=mdl.interval_var(0,10)
I2=mdl.interval_var(0,10)
I3=mdl.interval_var(0,10)

over1=mdl.integer_var()
start1=mdl.integer_var()
end1=mdl.integer_var()

artificialInterval=mdl.interval_var()

mdl.add(over1==mdl.overlap_length(I1,I2))
start1=mdl.max(mdl.start_of(I1),mdl.start_of(I2))
end1=mdl.min(mdl.end_of(I1),mdl.end_of(I2))

mdl.add(mdl.start_of(artificialInterval) ==start1)
mdl.add(mdl.end_of(artificialInterval) ==end1)

mdl.add(over1==mdl.overlap_length(I3,artificialInterval))

obj = mdl.maximize(over1)
mdl.solve()

效果更好

这篇关于如何在docplex(python)中启动间隔变量界限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆