Search code examples
optimizationcplexdocplex

How to initiate the interval variable bounds in 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);
} 

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,


Solution

  • 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