IBM ILOG Solver User's Manual > Extending the Library > Writing a Constraint: Allocating Frequencies > Writing a constraint: Two examples > A new class of constraint using iterators |
A new class of constraint using iterators |
INDEX
![]() |
Let's consider the constraint i E if and only if i + 1
E on a set E of integers i. In other words, this set of integers must not contain two consecutive values.
The following rules serve as a model of the semantics of this constraint.
The constraint must be propagated when new required elements are added to the set.
To keep this example to the point, you won't define the negation of the constraint. That is, you will not define isViolated
, makeOpposite
or metaPost
, as you would have to do if you were creating a metaconstraint. With that in mind, here's the definition of the corresponding class of constraint.
class IlcNoConsecConstraint : public IlcConstraintI { // set E doesn't contain two consecutive values. IlcIntSetVar _E; public: IlcNoConsecConstraint(IloSolver solver, IlcIntSetVar E) :IlcConstraintI(solver), _E(E){} virtual void post (); virtual void propagate (); void propagateDomain(); }; IlcBool IlcNoConsecConstraint::isViolated() { for(IlcIntSetIterator iter(_E.getRequiredSet()); iter.ok(); ++it) { IlcInt val = *iter; if (_E.isRequired(val +1)) return IlcTrue; } return IlcFalse; } ILCCTDEMON(NoConsecConstraintDomainDemon, IlcNoConsecConstraintI, propagateDomain); } void IlcNoConsecConstraint::post(){ _E.whenDomain(NoConsecConstraintDomainDemon(getSolver(), this)); } void IlcNoConsecConstraint::propagate() { for(IlcIntSetIterator iter(_E.getRequiredSet()); iter.ok(); ++iter){ IlcInt val = *iter; _E.removePossible(val-1); _E.removePossible(val+1); } } void IlcNoConsecConstraintI::propagateDomain() { for(IlcIntDeltaRequiredIterator iter(_E); iter.ok(); ++iter){ IlcInt val = *iter; _E.removePossible(val-1); _E.removePossible(val+1); } } IlcConstraint noConsecutiveValues(IlcIntSetVar E){ IloSolver solver = E.getSolver(); return IlcConstraint(new (solver.getHeap()) IlcNoConsecConstraint(solver, E)); }
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |