Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Changing copy of models

    Posted Fri July 12, 2019 06:02 PM

    Originally posted by: Allexandre


    Hello,

    I have a model (let's call it s0) that grows fast. I solve it with a certain gap. Then, I made a copy (s1 = s0, with attribution, I'm programming in C++) of this model and fix some values for sets of variables, and solve *s1*. My problems are:

    1. All modification that I do over s1 is also done over s0;
    2. If I undo the alterations in s1 or s0, it still continues valid for any copy of them.

    How can I proceed?

    Really thanks,

    Allexandre.


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Changing copy of models

    Posted Fri July 12, 2019 06:24 PM

    It sounds like you want create a deep copy of s1 as described in this thread.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Changing copy of models

    Posted Sat July 13, 2019 08:58 AM

    Originally posted by: Allexandre


    Thanks for the Help RyanKesh!

    So, Does it means that I must not create a deep copy?

    Sorry, but I'm not used with these computer terms, Laughing

    Thanks!


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Changing copy of models

    Posted Mon July 15, 2019 02:16 AM

    It means you must create a deep copy. As you can read in the documentation, classes like IloModel are only just handles to the actual model object. If you do something like

    IloModel model1 = IloModel(env);
    IloModel model2 = model1;

    Then you create only one model with two references to it. You can imagine the IloModel class to be defined someting like this:

    class IloModel {
       IloModelI *modelI;
    };

    The "real" model object is the IloModelI (note the 'I' at the end of the name) instance to which the IloModel instance points. If you just do model2 = model1 then this will just copy the modelI pointer field, not the model. So you will have model1->modelI == model2->modelI, i.e., model1 and model2 point to the same object. That is why any modification done through model1 is also visible in model2.

    What you need is a deep copy, i.e., create a new model that is a copy of the old one.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Changing copy of models

    Posted Mon July 15, 2019 10:04 AM

    Originally posted by: Allexandre


    Hello Daniel,

    Thank you for the clarifications.

    How could I do a deep copy? To clarify, I have a struct with all information about my model/solution in the following form,

    
    typedef 
    struct {
        IloNum ub;          
        IloNum lb;          
        IloNum gap;
        IloTimer *crono;
        IloCplex cplx;
        IloModel model;
    
        IloNumVarArray x;
        IloNumArray x_;
        IloNumVarArray y;
        IloNumArray y_;
        IloNumVarArray z;
        IloNumArray z_;
    
        IloRangeArray constraints;
        IloObjective objective;
    } 
    cpx;
    

    There is some function or best way to do this deep copy? Do I have to copy item by item of my struct cpx? Also, I would like to delete or free the memory used by the models that not bring any improvement after the modifications.

    Really thanks, best regards.

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Changing copy of models

    Posted Wed July 17, 2019 10:12 AM

    In case you missed it, in the link I provided above the following technique is used to create a deep copy of the model:

    IloModel model2(env);
    for (IloModel::Iterator it(model1); it.ok(); ++it)
       model2.add(*it);
    

    For the other objects in your struct, you can use similar techniques. You should consult the C++ reference manual for the details on each of those.

    To delete or free an object, you will typically call the end() method. Please see the C++ examples that are included when you install CPLEX to see how this is done.


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Changing copy of models

    Posted Wed July 17, 2019 12:24 PM

    Originally posted by: Allexandre


    Hi Ryan,

    I tried this IloModel deep copy, it's ok. Now I have to copy my IloObjective, IloRangeArray and a series of IloNumVarArray. The assignment "a=b" for them also means a shallow copy? If yes, how should I do the deep one? The other elements (IloCplex and IloTimer)? How do I proceed?

    Thanks,


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Changing copy of models

    Posted Thu July 25, 2019 05:57 AM

    As you told me in a private message, you basically have a method

    cpx c = init(data);

    that takes some data and initializes all fields in the cpx instance. The best way to get the same problem twice, you can just call that function twice with the same data:

    cpx c1 = init(data);
    cpx c2 = init(data);

    After that, c1 and c2 will contain equivalent models, but modifications made to c1 will not affect c2 and vice versa.

    About deletion: I suggest to add an 'IloEnv env' field to the cpx type. Then create a new IloEnv in each call to init() and store that environment in the returned cpx instance. To free all the modeling objects for a cpx instance you can then just call the end() method of that instance's IloEnv field.


    #CPLEXOptimizers
    #DecisionOptimization