Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  can not remove a constraint from the model

    Posted 04/17/15 05:04 AM

    Originally posted by: albaska


    Hello,

    I have a question! I have the following code :

    model.add(x_l[i][j]  == val);
    IloCplex heur_lp_cplex(model);
    int nrows = heur_lp_cplex.getNrows();
    int ncols = heur_lp_cplex.getNcols();
    cout << "nrows and ncols before are = " << nrows << " , " << ncols << endl;
    // solve the lp model
    cout<<"#######################start"<<endl;
    if ( !heur_lp_cplex.solve() ) {
    env.error() << "Failed to optimize LP." << endl;
    throw (-1);
    }
    model.remove(x_l[i][j] == val);
    nrows =  heur_lp_cplex.getNrows();
    ncols = heur_lp_cplex.getNcols();
    cout << "nrows and ncols aftere are = " << nrows << " , " << ncols << endl;
     

    I added a constrain and then after solving the model I removed it, but when I ask to write down the number of constraints before and after removing that 

    constraint, it returns the same number, am I missing something in here?

    another question, when we pass a model to a function, is this a pass by value or by reference? I passed a model to function(not by reference), and then I added a constraint and solve it, but I do not want this constraint to be added to my model, it seems that CPLEX pass the model by reference, because after I came back to the main function, the changes are still valid!! does anyone have any idea about this issues? thank you

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: can not remove a constraint from the model

    Posted 04/17/15 06:00 AM

    Your code to remove the constraint is wrong. You wrote

    model.remove(x_l[i][j] == val);

    That will first create a new constraint instance (x_l[i][j] == val) and then remove that instance from the model. Since that instance was never added to the model, this is a noop. To add a constraint and remove it later, do something like this:

    IloConstraint c = x_l[i][j] == val;
    model.add(c);
    ...
    model.remove(c);

    About pass by value/reference: almost all classes in Concert are handle classes, they just wrap a pointer. So even if you pass those classes by value, you will still be effectively passing the implementation class by reference. See also here.

    In the case you described you could either create a deep copy of the model or do something like this:

    void function(IloModel model) {
       IloModel subModel(model.getEnv());
       subModel.add(model);
       subModel.add(additional_constraint);
    }

    This makes 'model' part of 'subModel', so 'subModel' is 'model' plus 'additional_constraint' and 'model' is not modified.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: can not remove a constraint from the model

    Posted 04/17/15 02:40 PM

    Originally posted by: albaska


    that was very helpful, thank you


    #CPLEXOptimizers
    #DecisionOptimization