2017-07-17 30 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); 
}