Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Copying a model and creating new environments for sub-problems

    Posted 02/13/15 04:55 PM

    Originally posted by: Maichel_Aguayo


    Hello,

     I want to solve a main model, and then copy it and create two sub-problems (in  different environments IloEnv)  so that new variables and constraints can be added to the sub-problems in addition to the one copied from the original model. Once the main model is copied, it has to be deleted to free memory (it wont be used again). 

    As example consider the main problem; max:  x1 + 2 x2 + 3 x3 + x4, Subject To - x1 + x2 + x3 <= 20 and  x1 - 3 x2 + x3 <= 30, and 0 <= x1 <= 40. 

    This problem is defined in the environment denoted by env_root. Then, this model (variables and constraints) needs to be copied into two different sub-problems (each one in different environment: env_node1 and env_node2) . After copying the main model, it is deleted. Furthermore, new variables and constraints will be added to the sub-problems as indicated:

    1). Sub-problem 1: Add the variable "x5" and the constraint "x1+x3<=40". This sub-problem should look like this:  Maximize x1 + 2 x2 + 3 x3 + 5x5, Subject To - x1 + x2 + x3 - x5 <= 20,  x1 - 3 x2 + x3 - 2 x5 <= 30,  x1+ x3 <= 40, x5 <= 4.. 

    2). Sub-Problem 2:  Add the variable "x6" and the constraint" x1+ x6 <=40". This one should be: Maximize x1 + 2 x2 + 3 x3 + 6x6, Subject To,  - x1 + x2 + x3 - x6 <= 20,  x1 - 3 x2 + x3 -3 x6 <= 30,  x1+ x6 <=40", 0 <= x1 <= 40, and 0 <= x6 <= 6.

    I have the following problems:

    a). By deleting  "env_root"  my program crashes. However, if "env_root" is not deleted, then it runs. How should I define the new environments so that "env_root" can be deleted?

    b). Coefficients to the objective function for the new variables cannot be added. For instance, for sub-problem 1, the coefficient of the new variable "x5" does not appear in the model (see Node1.lp file). The same occurs in Sub-problem 2 ( Node1.lp) . How should it be done? 

    c). Constraints involving the new variables cannot be defined. For instance, "x1+x6 <=30" cannot be added to the sub-problem 2. 

    I am using   CPLEX_Studio 12.5.1 in concert with c++.  Attached are my code, and lp files for the main model and sub-problems. 

    I am implementing a branch-and-price algorithm using Cplex as a LP solver, and this routine will be executed every time I need to branch.  

     Thanks for your answers,

    Maichel


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Copying a model and creating new environments for sub-problems

    Posted 02/14/15 09:27 AM

    I think your program is doomed to crash: after deleting root_env you are still using objects that were allocated on that environment (x and c, or copies of them).

    Where did you find the IloNumVarArray::copy() and IloRangeArray::copy() functions you are using in your code? I could not find them in the reference documentation. Did you pick them up by looking at the header file? If they are not documented that means you should not use them.

    Is it really necessary to create new environments and deep copies of the model? Can't you use this approach:

    // Potentially first delete the objective function from 'model'
    // otherwise you will get a MultipleObjectivesException when you
    // try to add a new objective to subModel1 or subModel2.
    ...
    IloModel subModel1(root_env);
    subModel1.add(model);
    IloModel subModel2(root_env);
    subModel2.add(model);

    This creates subModel1 and subModel2 and both contain the original model. You can then use subModeli.add(...) to add more constraints. This will not affect the original model. To me this looks more memory efficient than creating a deep copy of the model. Unless, of course, you want to handle subModel1 and subModel2 in parallel threads. Then you have no choice but to create deep copies.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Copying a model and creating new environments for sub-problems

    Posted 02/15/15 10:36 PM

    Originally posted by: Maichel_Aguayo


    Daniel, Thanks for your quick answer.  First,  I found IloNumVarArray::copy() and IloRangeArray::copy() functions on the header file, as you mentioned they are not documented so I should not use them.

    With respect to your approach, I need to delete "model"  once it is copied since my original model (not this toy example) is a huge model, and have to create several of them. Without deleting them, my program runs out of memory after a few iterations.  As a matter of fact,  once SubProblem1 is solved potentially two new problems (SubProblem 3 and 4) will be created by copying  SubProblem1, and then SubProblem1 should be deleted as well

    If I follow your approach, and try to delete "model" : 

    // delete "model"  once it is copied and add  SubModel1 and SubModel2 to the queue of problems to be explored

    IloModel subModel1(root_env);

    subModel1.add(model);
    IloModel subModel2(root_env);

    subModel2.add(model);

    model.end(); // end model

    IloCplex Solver1(SubModel1);

    Solver.Solve();

    Then, Submodel1 is empty.  What should I do to delete "model" without affecting SubProblem 1 and 2 ?.

     

    With respect to creating different environments, my idea to define a new environment for each subproblem  so that once it is solved and not utilized any more,  all objects in that environment are deleted. Is there a way to achieve this ?

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Copying a model and creating new environments for sub-problems

    Posted 02/24/15 02:20 AM

    I still do not understand why you need to delete the original model. Consider this scenario:

    IloModel rootModel = ...;
    IloModel sub1(env); sub1.add(rootModel); sub1.add(x >= 1);
    IloModel sub2(env); sub2.add(rootModel); sub2.add(x <= 0);

    At this point you will have two different sub models, sub1 and sub2. They both share the constraints in rootModel but also have additional different constraints. So they are equivalent to rootModel+some extra constraints. Note that subX.add(rootModel) does not create a deep copy of rootModel. It only adds a reference. So in general this should be much more memory efficient than creating deep copies of rootModel. And since add(rootModel) only adds a reference, it is an error to do rootModel.end() since that would result in a dangling reference.


    #CPLEXOptimizers
    #DecisionOptimization