如何在序列中获得相邻的间隔

时间:2017-07-17 03:41:29

标签: sequence intervals cplex cp

我定义了一个间隔的dvar来表示两个城市之间的一条腿,以及一个间隔的dvar序列。右边,我想惩罚以下条件:如果间隔的目的地城市不是它的下一个间隔的出发城市,那么我在一个变量中计数1,例如,将其命名为countVar。我将最小化目标中的countVar。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

你可以使用typeOfNext来做你需要的事情。让我举个小例子。 这样,你可以计算countVar

    using CP;

    range R = 1..6;

    dvar interval tia[i in R] size i;
    dvar sequence seq in all(i in R)tia[i] types all(i in R)i;
    dvar int typeOfNextResult[i in R];

    subject to {
      noOverlap(seq);

      // computing typeOfNextResult
      forall(i in R) typeOfNextResult[i]==typeOfNext(seq,tia[i],-1,-2);
    }

    execute {
      writeln(seq);
      writeln(seq.first() );
      writeln(seq.next(seq.first() ) );
      writeln(seq.last() );

      writeln("loop");
      var s=seq.first();
      for(var i in R)
      {
       writeln(s);
       s=seq.next(s) ;
      }
      writeln(s);

      writeln("typeOfNextResult=",typeOfNextResult);
    }

给出了

{<"tia[1]" 0 0 1 0 1 1>
     <"tia[2]" 1 1 2 1 3 2>
     <"tia[3]" 2 2 3 3 6 3>
     <"tia[4]" 3 3 4 6 10 4>
     <"tia[5]" 4 4 5 10 15 5>
     <"tia[6]" 5 5 6 15 21 6>}
 <1 0 1 1>
 <1 1 3 2>
 <1 15 21 6>
loop
 <1 0 1 1>
 <1 1 3 2>
 <1 3 6 3>
 <1 6 10 4>
 <1 10 15 5>
 <1 15 21 6>
null
typeOfNextResult= [2 3 4 5 6 -1]

问候

答案 1 :(得分:0)

//这是CPLEX计划

using CP;
tuple flightLeg{
key int LegID;
int departurePoint;
int destinationPoint;
int aircraftID;
}
{flightLeg} Legs=...;
tuple aircraft{
key int aircraftID;
int aircraftType;
}
{aircraft} aircraftSet=...;
tuple stop{
    int LegID1;
    int LegID2;
    int stopTime;
}
{stop} stopTimes=...;
dvar interval invFlighttasks[i in Legs][j in aircraftSet] optional  
        size 10;
dvar sequence seqAircrafts[i in aircraftSet] in 
    all(j in Legs) invFlighttasks[j][i] types 
    all(j in Legs) j.LegID;
//minize the number of disconnect (means the destinationPoint of a interval
//is not the departurePoint of it's next interval)
subject to {
    forall (i in aircraftSet)
      noOverlap(seqAircrafts[i],stopTimes);
}