Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Error with "Complex Custom Constraint" manual example

  • 1.  Error with "Complex Custom Constraint" manual example

    Posted 05/24/11 01:20 PM

    Originally posted by: sds_rohit


    Hi,

    I am trying to execute the manual example on "Complex Custom Constraint".
    I am getting an error while running the code, which I have attached with this message,
    however I am not getting any compilation error.

    Please suggest what I am doing wrong. How to correct it.

    Thanks,


    #include <ilcp/cp.h>
    #include <vector>
    #include <ilcp/cpext.h>
    using std::vector;

    class DiffConstraintI : public IlcConstraintI {
    IlcIntVar _x, _y;
    public:
    DiffConstraintI(IloCP cp, IlcIntVar x, IlcIntVar y);
    ~DiffConstraintI(){}; // empty destructor

    virtual void propagate ();
    virtual void post();
    };
    DiffConstraintI::DiffConstraintI(IloCP cp,
    IlcIntVar x,
    IlcIntVar y)
    : IlcConstraintI(cp), _x(x), _y(y) {}

    void DiffConstraintI::propagate () {
    if (_x.isFixed()) _y.removeValue(_x.getValue());
    if (_y.isFixed()) _x.removeValue(_y.getValue());
    }

    void DiffConstraintI::post(){
    _x.whenValue(this);
    _y.whenValue(this);
    }

    IlcConstraint DiffConstraint(IlcIntExp x, IlcIntExp y){
    IloCP cp = x.getCP();
    return new (cp.getHeap()) DiffConstraintI(cp, x, y);
    }

    int main(int , const char * []){
    IloEnv env;
    try
    {

    IloIntVar x(env, 0, 10);
    IloIntVar y(env, 0, 50,"y");
    IloModel model(env);
    model.add(x == y + 2);
    IloCP cp(env);
    cp.extract(model);
    IlcIntVar cpx = cp.getIntVar(x);
    IlcIntVar cpy = cp.getIntVar(y);
    cp.add(DiffConstraint(cpx, cpy));
    cp.out() << "The engine variable of " << x << " is " << cp.getIntVar(x) << std::endl;
    cp.out() << "The engine variable of " << y << " is " << cp.getIntVar(y) << std::endl;

    cp.end();

    }
    catch (IloException & ex)
    {
    env.out() << "Caught " << ex << std::endl;
    }
    env.end();
    return 0;
    }
    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Error with "Complex Custom Constraint" manual example

    Posted 05/25/11 05:47 AM

    Originally posted by: SystemAdmin


    You get this error message because your code tries to add a constraint directly to the engine while the model has been extracted. The way to go is to wrap your IlcConstraint into and IloConstraint and add it to the instance of IloModel. It will be extracted to the corresponding IlcConstraint at extraction time.

    Have a look at the section "Using ILOCPCONSTRAINTWRAPPERn to wrap constraints" in the "Writing complex custom constraints" chapter of the CPO Extensions User Manual. You have more details there.

    Regards,

    Philippe
    #CPOptimizer
    #DecisionOptimization