I have built a simple constraint enforcing that the value of a variable must be consistent with a modulo operation. Two consistency levels are available: bounds consistency and domain consistency. In this example, solving the problem with bounds consistency works well with 1 or 2 workers, but with 3 or more workers the propagate()
function loops infinitely since it appears to be unable to remove the maximum value of the domain. My computer has 2 physical cores, so my guess is that using more workers than physical cores is the problem. I am using the latest version of Cplex Optimization Studio. Here is the code:
File main.cc
:
#include <ilcp/cp.h>
#include "IloModulo.h"
int main() {
IloEnv env;
IloModel model(env);
IloIntVar x(env, 1, 100000);
model.add(IloModulo(env, x, 3, BOUNDS_CONSISTENCY));
model.add(IloMinimize(env, x));
IloCP solver(model);
solver.setParameter(IloCP::Workers, 1); // Infinite loop with 3 or more workers
solver.solve();
return 0;
}
File IloModulo.h
:
#ifndef ILOMODULO_H_
#define ILOMODULO_H_
#include <ilcp/cpext.h>
#define BOUNDS_CONSISTENCY 1
#define DOMAIN_CONSISTENCY 2
class IlcModuloI : public IlcConstraintI {
private:
IlcIntVar x_;
IlcInt modulo_;
IlcInt consistency_;
public:
IlcModuloI(IloCPEngine cp,
IlcIntVar x,
IlcInt modulo,
IlcInt consistency);
~IlcModuloI();
virtual void propagate();
virtual void post();
};
// The IloModulo constraint is used with the following function
IloConstraint IloModulo(IloEnv env,
IloIntVar x,
IloInt modulo,
IloInt consistency);
#endif // ILOMODULO_H_
File IloModulo.cc
:
#include "IloModulo.h"
IlcModuloI::IlcModuloI(IloCPEngine solver,
IlcIntVar x,
IlcInt modulo,
IlcInt consistency) :
IlcConstraintI(solver),
modulo_(modulo),
x_(x),
consistency_(consistency) {
;
}
IlcModuloI::~IlcModuloI() {
;
}
void IlcModuloI::propagate() {
switch (consistency_) {
case BOUNDS_CONSISTENCY: {
while ((x_.getMin() % modulo_ != 0) ||
(x_.getMax() % modulo_ != 0)) {
if (x_.getMin() % modulo_ != 0) {
x_.setMin(x_.getMin()+1);
continue;
}
if (x_.getMax() % modulo_ != 0) {
std::cout << "Min/max values: " << x_.getMin() << "/" << x_.getMax() << std::endl;
std::cout << "Decreasing maximum value by 1." << std::endl;
x_.setMax(x_.getMax()-1);
std::cout << "Min/max values: " << x_.getMin() << "/" << x_.getMax() << std::endl;
std::cout << "------------------------------" << std::endl;
continue;
}
}
break;
}
case DOMAIN_CONSISTENCY: {
IlcInt threshold = x_.getMin();
while (threshold <= x_.getMax()) {
if (threshold % modulo_ != 0) {
x_.removeValue(threshold);
}
if (threshold == x_.getMax()) {
break;
}
threshold = x_.getNextHigher(threshold);
}
break;
}
}
}
void IlcModuloI::post() {
switch (consistency_) {
case BOUNDS_CONSISTENCY: {
x_.whenRange(this);
break;
}
case DOMAIN_CONSISTENCY: {
x_.whenDomain(this);
break;
}
}
}
ILOCPCONSTRAINTWRAPPER3(IloModuloWrapper, solver,
IloIntVar, x_,
IloInt, modulo_,
IloInt, consistency_) {
use(solver, x_);
return new (solver.getHeap()) IlcModuloI(solver,
solver.getIntVar(x_),
modulo_,
consistency_);
}
IloConstraint IloModulo(IloEnv env,
IloIntVar x,
IloInt modulo,
IloInt consistency) {
return IloModuloWrapper(env, x, modulo, consistency);
}
Indeed it really seems to be an issue with the propagation of user defined constraints in some very specific conditions. I'm not sure if there is a direct link with the number of workers. The development team of CP Optimizer will investigate this problem and we will let you know.