Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Modifying IloNumVarArray and IloNumArray

    Posted Wed March 25, 2015 07:44 AM

    Originally posted by: AMurshed


    Hello,

    I am trying to solve a scheduling problem using cplex with C++ API.

    My question is that I would like to modify the size of (IloNumArray & IloNumVarArray) and also the LBound and UBound for IloNumVarArray everytime I solve the model.

    Simply, I would like to do the following:

    ---------------------------------------------

    class CS{
    private:
    IloEnv env;IloModel model; IloCplex cplex; IloNumArray3 r1;

    IloNumArrayVar2 rr1;

    public:
    CS(x1, x2, x3,x4):env(), model(env), cplex(modl), r1(env, x1), rr1(env,x2, x3,x4,ILOINT)
    {

    //input r1;
    }

    void buildModel();
    void changeModel();
    void output();
    };

    CS::buildMod​el(){
    //building model
    }
    CS::changeMod​el(int y1, y2, y3, y4){
    //I would like to clear the values of r1

    //and then change {r1 & rr1} to {r1(env, y1), rr1(env,y2, y3,y4,ILOINT)}
    }
    CS::output(){
    //solve model
    //out

    ---------------------------------------------

     

    What would be the function changeModel() in order to change the parameters of the arrays?

     

    Looking forward to hearing from you.

     

    Best regards,

    Ayman


    #DecisionOptimization
    #MathematicalProgramming-General


  • 2.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Wed March 25, 2015 09:30 AM

    Why not just end() the old instance and assign a new one:

    r1.end();
    r1 = IloNumArray3(env, y1);

    If you also want to delete the variables contained in r1 then use r1.endElements() before r1.end().


    #DecisionOptimization
    #MathematicalProgramming-General


  • 3.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Thu March 26, 2015 05:22 AM

    Originally posted by: AMurshed


    Thanks Daniel,

    How about the variables arrays (IloNumVarArray). Can I delete them and create them again in the same way.

    If yes, then after I redefine the variables arrays, should I rebuild the whole model again or just use (cplex.solve()) command in order to solve the changed model?


    #DecisionOptimization
    #MathematicalProgramming-General


  • 4.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Thu March 26, 2015 06:07 AM

    Originally posted by: AMurshed


    I tried to use the following:

    r11.end();
    r11 = IloNumVarArray(env, y2, y3, y4, ILOINT);
    

    it gave me the following error:

    terminate called after throwing an instance of 'IloAlgorithm::NotExtractedException'
    

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 5.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Thu March 26, 2015 06:19 AM

    You should always include in your post the exact statement that threw the exception and if possible also the backtrace of the exception.

    I guess in your case the exception happens when you attempt to do something like cplex.getValue() for one of the variables in r11? Did you actually add new constraints or objective functions to your model that involve variables from r11? Just changing r11 will not magically create any new elements in your model (there is no way it would know what constraints to create).


    #DecisionOptimization
    #MathematicalProgramming-General


  • 6.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Thu March 26, 2015 06:20 AM

    Just calling cplex.solve() will solve the model that the IloCplex instance has currently extracted. In your case this is almost certainly the old model. You probably want to either create a new model or at least add to the old model new constraints that involve variables from r11. Otherwise the new variables are not related to the model in any way.


    #DecisionOptimization
    #MathematicalProgramming-General


  • 7.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Thu March 26, 2015 07:31 AM

    Originally posted by: AMurshed


    Thanks Daniel for your reply,

    Can you please see the code below and comment on any wrong statments:

    class CS{
    private:
    IloEnv env;IloModel model; IloCplex cplex; IloNumArray3 r1;
    
    IloNumArrayVar2 rr1;
    
    public:
    CS(x1, x2, x3,x4):env(), model(env), cplex(modl), r1(env, x1), rr1(env,x2)
    {
    
    for(IloInt j =0; j<x2;++j)
         rr1[j]= IloNumVarArray(env,x3, 0, x4, ILOINT);
    //input r1;
    }
    
    void buildModel();
    void changeModel();
    void output();
    };
    
    CS::buildModel(){
    //building model
    }
    CS::changeModel(int y1, y2, y3, y4){
    //I would like to clear the values of r1 & rr1
    r1.end();
    r1=IloNumArray3(env, y1);
    //input r1 data
    
    
    rr1.end();
    rr1=IloNumVarArray2(env, y2);
    for(IloInt j =0; j<y2;++j)
                    rr1[j]= IloNumVarArray(env,y3, 0, y4, ILOINT);
    
    
    }
    CS::output(){
    //solve model
    //out
    }
    

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 8.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Fri March 27, 2015 05:49 AM

    Apart from the facts that

    1. this is still not even correct C++ syntax
    2. you are still not making any modifications to the model in function changeModel()

    I don't see any obvious problems. But I also don't see how this code is different from the code that you posted before.

    In general, a question like "this is my code, is it correct" is very hard to answer. You should try to ask more concrete questions, something like "this is my code, this is what it is supposed to do, this is what happens instead, and this is why that is unexpected". Just throwing some code (that in your case does not even compile) at us and asking for a general review seems a little much to ask for.


    #DecisionOptimization
    #MathematicalProgramming-General


  • 9.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Sat March 28, 2015 06:38 AM

    Originally posted by: AMurshed


    Thanks Daniel for your reply,

    My question is that "The following code in function (changeModel) is correct"

    I posted the function code below:

    void CS::changeModel(int y1, y2, y3, y4){
    //I would like to change the array 'r1' & variable array 'rr1'
    
    r1.end();
    r1=IloNumArray3(env, y1);
    //input r1 data
    
    rr1.end();
    rr1=IloNumVarArray2(env, y2);
    for(IloInt j =0; j<y2;++j)
                    rr1[j]= IloNumVarArray(env,y3, 0, y4, ILOINT);
    
    buildModel();
    }
    

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 10.  Re: Modifying IloNumVarArray and IloNumArray

    Posted Fri April 17, 2015 03:28 AM

    Like I already said: this code replaces r1 and rr1 as expected and then calls buildModel(). I think that is what you wanted it to do, so, yes, this code looks correct to me.


    #DecisionOptimization
    #MathematicalProgramming-General