Dear Jason,
You can simulate a typeOfNextNext expression by using a secondary sequence variable that will be used to get the array index of the next interval of the sequence. Using this index, that you can determine the type of a successor/predecessor and follow the chained intervals in the sequence.
using CP;
int n = 10;
range r = 1..n;
range r0 = 0..n;
dvar interval itvs[i in r] size i;
// define a type for each interval (3 possible types)
int typeValue[r0] = [-1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2 ];
// sequence for types
dvar sequence seq
in all(i in r)itvs[i]
types all(i in r) typeValue[i];
// sequence for ordering
dvar sequence order in itvs types all(i in r) i;
dexpr int nextId[i in r0] = (i == 0 ? 0 : typeOfNext(order, itvs[i], 0));
dexpr int prevId[i in r0] = (i == 0 ? 0 : typeOfPrev(order, itvs[i], 0));
// used for printing the solution
dexpr int nextType[i in r] = typeOfNext(seq, itvs[i], -1);
dexpr int prevType[i in r] = typeOfPrev(seq, itvs[i], -1);
subject to {
// ensure the intervals do not overlap
noOverlap(seq);
// ensure the ordering of the two seqeuence is the same
sameSequence(seq, order);
// if an interval has a prev of same type
// then ensure that its next will have a different type
forall(i in r) {
(typeValue[i] != typeValue[nextId[i]])
|| (typeValue[i] != typeValue[nextId[nextId[i]]]);
}
}
execute {
writeln("itvs", itvs);
writeln("seq", seq);
writeln("order", order);
write(" index "); for(i = 1; i <= n; ++i) write("\t" + i); writeln();
write(" nextId "); for(i = 1; i <= n; ++i) write("\t" + nextId[i]); writeln();
write(" prevId "); for(i = 1; i <= n; ++i) write("\t" + prevId[i]); writeln();
write(" nextType "); for(i = 1; i <= n; ++i) write("\t" + nextType[i]); writeln();
write("typeValue "); for(i = 1; i <= n; ++i) write("\t" + typeValue[i]); writeln();
write(" prevType "); for(i = 1; i <= n; ++i) write("\t" + prevType[i]); writeln();
for(i = 1; i <= n; ++i)
if(prevType[i] == typeValue[i] && nextType[i] == typeValue[i])
print("ERROR: 3 consecutive intervals have the same type");
}
I hope this helps.
------------------------------
Renaud Dumeur
------------------------------
Original Message:
Sent: Wed June 01, 2022 04:23 AM
From: Wu Jason
Subject: CPOptimizer sequence patterns
Hi,
I get some problem here, we can get the " next type of interval i " by function typeOfNext(seq,itvs[i])
Is there anyway to make the constraint like:
if( typeOfNext(seq,itvs[i]) != typeOf(itvs[i]) ) => typeOfNextNext(seq,itvs[i]) != typeOf(itvs[i])
By the way, thanks for the sharing!
----------------------------------------------
Jason Wu
----------------------------------------------
------------------------------
Wu Jason
------------------------------
Original Message:
Sent: Sat February 06, 2021 01:48 AM
From: ALEX FLEISCHER
Subject: CPOptimizer sequence patterns
Hi
could
https://github.com/AlexFleischerParis/opltipsandtricks/blob/master/positionofintervalinsequence.mod
from opl tips and tricks help ?
regards
------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]