Originally posted by: ol
Hi Fabio,
> If I use "cnt++" in conjunction with "IloIfThen" the counter cnt is incremented
> every time the "if" part is checked
not exactly, the IloIfThen constraint does not see "cnt++". cnt++ is a C++ instuction,
and IloIfThen, from the c++ viewpoint is a function. Thus, cnt is incremented twice when the line 44 is executed: this is simply the c++ parameter evaluation process.
I think the file I sent last time should do what you have in mind: just replace the IloIfThen by the constraint defined in the file, after having filled the arrays a and b as follows:
IloIntVarArray a(env);
IloIntVarArray b(env);
for (int j = 0; j < numBlocks; j++)
for (int i = 0; i < elements.getSize() - 1; i++)
for (int k = i + 1; k < elements.getSize(); k++) {
IloInt delta=k-i;
a.add(elements[k][j]);
b.add(elements[i][j]);
}
model.add(IloMyConstraint(env, a, b, differences, delta));
You aslso need:
- to add a parameter (IlcInt delta) to the constraint
- to modify the propagate and the demon of the constraint by
if (_xa[i].isFixed() && _xa[i].getValue() == 1 && _xb[i].isFixed() && _xb[i].getValue() == 1) {
IlcInt cntVal = _cnt->getValue();
getCP().add(_v1[cntVal] == delta || _v1[cntVal] == size-1 - delta);
//size must be also given in parameter...
//is it really size-1 - delta? I would think size - delta, since delta=k-i ranges from 1 to size-1
_cnt->setValue(getCPEngine(), cntVal + 1);
}
I sent you this code last time because I thought you had a special need. By looking at your file, I am not sure defining a custom constraint like that is not an overkill. It depends on what you want to do with the array differences.
Maybe you can just replace the iloIfThen constraints by computing a 3D array of gaps, the non-zeros will be the differences you want to compute (if we have size - delta and not size-1 - delta).
model.add(
gap[i][j][k]== elements[k][j]*elements[i][j]*(k-i) ||
gap[i][j][k]== elements[k][j]*elements[i][j]*(size-1 -(k-i)));
After that you can for example flatten the gaps array in another array flatgap and use
other constraints or expression like IloCount(flatgap, 0).
#CPOptimizer#DecisionOptimization