2017-07-17 90 views
0

我定义了一个代表两个城市之间的一个区间的区间的dvar和区间上的一个dvar序列。现在,我想惩罚下面的情况:如果一个区间的目标城市不是它的下一个区间的出发城市,那么我在一个变量中计数1,例如,将其命名为countVar。我会尽量减少目标的数量。我怎样才能做到这一点?我怎样才能得到一个序列中的相邻区间

回答

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] 

问候

+0

感谢很多对你的帮助。 :)另一个问题:我注意到你使用seq.first()来获取OPL脚本中的第一个区间。我如何获得OPL序列中的第一个区间,而不是OPL脚本。 –

+0

嗨,你可以写“第一(seq,tia [3]);”为了说tia [3]是顺序序列中的第一个区间。 –

+0

首先(seq,tia [3])约束一个区间变量是序列中的第一个。我的意思是如果我想确保第一个区间有一些特殊的属性(例如,第一个区间的旅行必须从一个特定的城市开始),我怎么能得到第一个。 –

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