Decision Optimization

Decision Optimization

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

 View Only
  • 1.  IloCP to IloCPEngine in IlcConstraintI (CP Optimizer 12.22)

    Posted Wed April 16, 2025 05:19 PM
    Edited by Hossein H Thu April 17, 2025 08:22 AM

    Hi,

    I am running a code in CP Optimizer 12.22 that requires using IlcConstraint and ILOCPCONSTRAINTWRAPPER3 as follows:

    //----------------------------------------------------------------------------------------------------------------------------------
    class IlcCheckWhenBoundI : public IlcConstraintI {

    protected:
        IlcIntVarArray _x;
        IlcInt _a;
        IlcFloatVar _z;
    public:
        IlcCheckWhenBoundI(IloCPEngine s, IlcIntVarArray x, IlcInt a, IlcFloatVar z): IlcConstraintI(s), _x(x), _a(a), _z(z) {}
        ~IlcCheckWhenBoundI() {}
        virtual void post();
        virtual void propagate() {}
        void varDemon(IlcIntVar v);
    };
     
    ILCCTDEMON1(RealizeVarBound, IlcCheckWhenBoundI, varDemon, IlcIntVar, v);
     
    void IlcCheckWhenBoundI::post () {
        _x[_a].whenValue(RealizeVarBound(getCPEngine(), this, _x[_a]));
    }
     
    void IlcCheckWhenBoundI::varDemon (IlcIntVar v) {
        if (_x[_a].isFixed()==1){
            _z.setMax(64000000);
        }
    }
     
     
    IlcConstraint IlcCheckWhenBound(IloCPEngine s, IlcIntVarArray x, IlcInt a, IlcFloatVar z) {
        return new (s.getHeap()) IlcCheckWhenBoundI(s, x, a, z);
    }
     
    ILOCPCONSTRAINTWRAPPER3(IloCheckWhenBound, cp, IloIntVarArray, _v, IlcInt, _a, IloNumVar, _z) {
        cout << "here" << endl;
        use(cp, _v);
        use(cp, _z);
        return IlcCheckWhenBound(cp, cp.getIntVarArray(_v), _a, cp.getFloatVar(_z));
    }

    //----------------------------------------------------------------------------------------------------------------------------------

    This code was working well for CP Optimizer 12.6 but it does not work in CP Optimizer 12.22, as it requires replacing IloCP with IloCPEngine and some other modifications. I revised the above code as follows:

     //----------------------------------------------------------------------------------------------------------------------------------
    class IlcCheckWhenBoundI : public IlcConstraintI {
    protected:
        IlcIntVarArray _x;
        IlcInt _a;
        IlcFloatVar _z;
    public:
        IlcCheckWhenBoundI(IloCPEngine s, IlcIntVarArray x, IlcInt a, IlcFloatVar z): IlcConstraintI(s), _x(x), _a(a), _z(z) {}
        ~IlcCheckWhenBoundI() {}
        virtual void post();
        virtual void propagate() {}
        void varDemon(IlcIntVar v);
    };
     
    ILCCTDEMON1(RealizeVarBound, IlcCheckWhenBoundI, varDemon, IlcIntVar, v);
     
    void IlcCheckWhenBoundI::post () {
        _x[_a].whenValue(RealizeVarBound(getCPEngine(), this, _x[_a]));
    }
     
    void IlcCheckWhenBoundI::varDemon (IlcIntVar v) {
        if (_x[_a].isFixed()==1){
            _z.setMax(64000000);
        }
    }
     
     
    IlcConstraint IlcCheckWhenBound(IloCPEngine s, IlcIntVarArray x, IlcInt a, IlcFloatVar z) {
        return new (s.getHeap()) IlcCheckWhenBoundI(s, x, a, z);
    }
     
    ILOCPCONSTRAINTWRAPPER3(IloCheckWhenBound, cp, IloIntVarArray, _v, IlcInt, _a, IloNumVar, _z) {
        use(cp, _v);
        use(cp, _z);
        return IlcCheckWhenBound(cp, cp.getIntVarArray(_v), _a, cp.getFloatVar(_z));
    }

    //----------------------------------------------------------------------------------------------------------------------------------

    This code debugs but unfortunately runs into an exception error (Exception thrown at 0x00007FF65AE7187C in CP Project1.exe: 0xC0000005: Access violation reading location 0x0000000000000010.) in line "use(cp, _v);". Would you please tell me how to fix the original code or the last code to make it work with CP Optimizer 12.22? I have attached .h files of the original and the revised codes in the case you feel more comfortable checking them. Thanks.



    ------------------------------
    Hossein H
    ------------------------------

    Attachment(s)

    zip
    old file.zip   604 B 1 version
    zip
    new file.zip   610 B 1 version


  • 2.  RE: IloCP to IloCPEngine in IlcConstraintI (CP Optimizer 12.22)

    Posted Thu April 17, 2025 09:00 AM

    I assume your new release is CP Optimizer 22.1.2. Using this release, there is no problem when running this code. This is how I used it :

      IloEnv env; 
      IloModel model(env);
    
      IloIntVarArray x(env, 5, -10, 10);
      IloNumVar z(env, -20, 20); 
      IloIntVar zi(env, -200, 200);
    
      model.add(zi == z * 10);
      model.add(IloCheckWhenBound(env, x, 3, z));
    
      IloCP cp(model);
      cp.solve();
    
      env.end();

    and I replaced _z.setMax(64000000); by _z.setMax(0);
    Can you post a (small) code that one can run and that shows the problem ? 
    Thanks.

    Philippe




  • 3.  RE: IloCP to IloCPEngine in IlcConstraintI (CP Optimizer 12.22)

    Posted 29 days ago
      |   view attached

    Dear Philippe,

    Thanks very much for your reply. I have attached my code as a zip file. I would be very thankful if you could run it and tell me what the problem is. In the attached zip file, in line 95 of file "cplex part.cpp", you need to change ARG1 to the address of the folder in your PC where you put the attached data file  "Naple_Instance_Week_55.data".

    Thanks again for your time.



    ------------------------------
    Hossein H
    ------------------------------

    Attachment(s)

    zip
    CP Project1.zip   13 KB 1 version


  • 4.  RE: IloCP to IloCPEngine in IlcConstraintI (CP Optimizer 12.22)

    Posted 28 days ago

    I think the problem comes from this code :

    	//Definition of new variables
    	IloIntVarArray Number_of_Used_ORs_On_Day(*env, Number_Days);
    	for (int d = 0; d < Number_Days; d++) {
    		Number_of_Used_ORs_On_Day[d] = IloIntVar(*env, 0, RoomMatrix[d]);
    		All_Regular_Int_Variables.add(Number_of_Used_ORs_On_Day);
    		//Number_of_Used_ORs_On_Day[d] = IloIntVar(env, 0, 300);
    	}

    I should probably be 

    All_Regular_Int_Variables.add(Number_of_Used_ORs_On_Day[d]);

    In the original code the whole array is added and it contains indexes that have not been set. Since this array appears in your custom constraints, use(x) will be called on null pointer elements and crash.




  • 5.  RE: IloCP to IloCPEngine in IlcConstraintI (CP Optimizer 12.22)

    Posted 28 days ago

    Great. Thanks very much, Philippe. My code is working now. I appreciate your time.



    ------------------------------
    Hossein H
    ------------------------------