How to initiate the interval variable bounds in docplex (python)?

£可爱£侵袭症+ 提交于 2020-02-06 07:43:10

问题


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);
} 

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

Here is what i have done:

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()

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,


回答1:


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()

works much better



来源:https://stackoverflow.com/questions/60071915/how-to-initiate-the-interval-variable-bounds-in-docplex-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!