Originally posted by: rdumeur
Dear FiFila,
Since your variables are floating points and CPO works on integer variable, you will need to scale up their domains by some power of 10. The define float expressions (x/<floating scaling constant>) in your model. Using these expressions, you will be able to express the constraints on your variables.
Here is a simple skeleton code that invokes the CPO solver to check if some floating point values are consistent with a given model:
#include <ilcp/cpext.h>
ILOSTLBEGIN
const IloNum VarScale = 100000.0;
ILCGOAL6(CheckValuesGoal, IloInt, nx,
IloInt, nw,
IloNum*, xvalues,
IloNum*, wvalues,
IlcIntVarArray, xvars,
IlcIntVarArray, wvars)
{
for(IlcInt i = 0; i < nx; ++i)
xvars[i].setValue(IloRound(xvalues[i]*VarScale));
for(IlcInt i = 0; i < nw; ++i)
wvars[i].setValue(IloRound(wvalues[i]*VarScale));
return 0;
}
ILOCPGOALWRAPPER6(CheckValuesGoalWrapper,cp,
IloInt, nx,
IloInt, nw,
IloNum*, xvalues,
IloNum*, wvalues,
IloIntVarArray, xvars,
IloIntVarArray, wvars) {
return CheckValuesGoal(cp, nx, nw, xvalues, wvalues, cp.getIntVarArray(xvars), cp.getIntVarArray(wvars));
}
int main() {
IloEnv env;
IloModel model(env);
// some var range
IloInt xmin = IloCeil(2.1*VarScale);
IloInt xmax = IloFloor(10.3*VarScale);
IloInt nx = 10;
IloInt wmin = IloCeil(0.7*VarScale);
IloInt wmax = IloFloor(17.8*VarScale);
IloInt nw = 20;
IloIntVarArray xvars(env, nx, xmin, xmax);
IloIntVarArray wvars(env, nw, wmin, wmax);
IloNumExprArray x(env, nx);
IloNumExprArray w(env, nw);
for(IloInt i = 0; i < nx; ++i)
x[i] = xvars[i] / VarScale;
for(IloInt i = 0; i < nw; ++i)
w[i] = wvars[i] / VarScale;
// your constraints.
model.add(4*x[0] <= 2*w[2]);
model.add(3*x[2] >= 12*w[4]);
IloCP cp(model);
IloNum* xvalues = 0; // replace with your value arrays
IloNum* wvalues = 0; // ... idem ..
if(cp.solve(CheckValuesGoalWrapper(env, nx, nw, xvalues, wvalues, xvars, wvars)))
cout << "Values are correct" << endl;
cp.end();
env.end();
return 0;
}
The ILOCPGOALWRAPPER macro creates a "concert" goal wrap[per which, when processed by the solver, will produce an "ILC" goal 'CheckValueGoal'.
This goal will simply assign values (xvalues,wvalues) to xvars and wvars variables (scaling them by some factor).
If the values are not consistent with the constraint expressed in the model, a failure with occur and 'cp.solve' will return false.
I hope this helps.
Cheers,
#ConstraintProgramming-General#DecisionOptimization