Originally posted by: davidoff
All right, the error is in the loop from line 12, where we iterate on ypos and adding in ypos in the same time.
A solution is to store all ypos values in an intermediate collection, and then iterate on this collection to increment ypos
int W=30;
{int} widths = { 9, 12};
int minWidth = min(w in widths : w>0) w;
sorted {int} ypos;
{int} tmp;
//given widths w1, w2, ... , wN, , compute any integer combination sum(alpha_i*wi) <= W - minWidth
// a recursion is a natural way : given a new element, it will combine either alone or with all the previous combinations
execute ENUM_YPOS{
function addNew(w){
//1. recursion
writeln(Opl.card(tmp)," elems in tmp before");
tmp.clear();
writeln(Opl.card(tmp)," elems in tmp after");
for(var y0 in ypos)
tmp.add(y0);
for(var y0 in tmp){
var y=w;
while(y+y0<=W-minWidth){
ypos.add(y+y0);
y = y + w;
}
}
//2. add w alone
var y=w;
while(y<=W-minWidth){
ypos.add(y);
y = y + w;
}
}
ypos.add(0);
for(var w in widths)
addNew(w);
}
execute{
writeln("Finding all ypos with widths ",widths," and W=",W);
writeln(ypos);
}
#DecisionOptimization#OPLusingCPOptimizer