Decision Optimization

Decision Optimization

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

 View Only
  • 1.  How to modify the coefficients of constraints already added in Ilomodel

    Posted Tue February 14, 2012 01:05 PM

    Originally posted by: SystemAdmin


    Hi, how can I modify the coefficients of a constraint (extractable) in a model (Ilomodel). The Ilomodel::iterator returns an object of class IloExtractable, which doesn't have any routine to change the coefficients of the constraint (or may be there is one and I am not able to find it!).

    Code to give details-
    Step 1) Adding a constraint:

    IloRangeArray c1(env); c1.add(IloRange(env, -IloInfinity, -1.0));
    for(int I=0;I < NumVar; I++) c1[0].setLinearCoef(Var[I],-1);
    model.add(c1); c1.end();

    Step 2) Now how to modify the coefficients of the constraint, just added, by using the object, model?

    Thanks in advance!
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Tue February 14, 2012 06:24 PM

    Originally posted by: SystemAdmin


    Repeating here the answer I posted on OR-Exchange:

    I don't know why you're using an IloRangeArray for a single range, but that really does not matter.

    IloModel::add returns the handle of the added object; store it somewhere, after casting it back to IloRange or IloRangeArray as appropriate (I think the return value is IloExtractable). Do not invoke end() on the stored value. (I'm not sure it's safe to end() c1, either. It is safe to reinitialize c1 later.)

    If you know what modifications you want to do, just use IloRange::setLinearCoefficient on each constraint to be modified.

    Iterating is only necessary if you do not know which variables appear in the constraint, and need to scan the constraint to find them.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Fri February 22, 2013 05:34 PM

    Originally posted by: chrysg


    Hi,

    I am posting in this thread to reinvigorate the discussion, and also to ask a relevant but more specific question. Here is what I have (C++ API):

    IloEnv env;
    IloModel mod(env);
    // Here the model is populated via the add() method
    


    Let us now assume that, at some later point in the code, I have no information anymore about how the model was created. That is, I do not anymore have access to the IloExtractable objects I used, nor have I kept any handles when the add() method was called.

    All I really have is the model object itself, and I can only identify variables and constraints through their names (as those were previously set by the setName() method).

    Is there a way to modify the model in such a case (change coefficients and RHS) ?

    Thank you very much,

    chrysg
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Fri February 22, 2013 05:36 PM

    Originally posted by: chrysg


    Hi,

    I am posting in this thread to reinvigorate the discussion, and also to ask a relevant but more specific question. Here is what I have (C++ API):

    IloEnv env;
    IloModel mod(env);
    // Here the model is populated via the add() method
    


    Let us now assume that, at some later point in the code, I have no information anymore about how the model was created. That is, I do not anymore have access to the IloExtractable objects I used, nor have I kept any handles when the add() method was called. (This is because the object of type IloModel was passed to me from another program that created it).

    All I really have is the model object itself, and I can only identify variables and constraints through their names (as those were previously set by the setName() method) (the assumption here is that I know the names, although these were not passed to me explicitly).

    Is there a way to modify the model in such a case (change coefficients and RHS) ?

    Thank you very much,

    chrysg
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Sat February 23, 2013 01:54 PM

    Originally posted by: SystemAdmin


    > chrysg wrote:
    >
    > Is there a way to modify the model in such a case (change coefficients and RHS) ?

    Yes. The key is the IloModel::Iterator class, which lets you pull extractable objects out of the model. Each object is initially IloExtractable, so you probably need to use the IloExtractble::is... methods to figure out what you've got. Then you can use IloExtractableVisitor or the IloExtractable::get... methods to proceed from there.

    I have some model introspection code posted elsewhere, but unfortunately it's for the Java API, which is a fair bit different when it comes to parsing a model that was produced externally.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Sat February 23, 2013 04:04 PM

    Originally posted by: chrysg


    Paul,

    Thank you very much for the response....I have gotten this far:

    For a current IloExtractable object "extr":

    1) When the object is a variable, I can do
    extr.asVariable().setBounds(lo,hi);
    
    and this works fine, i.e., the variable in the modified model now has new bounds.

    2) When the object is a constraint, however, when I do
    extr.asConstraint()...
    
    I get back a handler to an object of type IloConstraint, for which no direct manipulation methods exist, i.e., I cannot do setBounds() or setLinearCoef()....these are typically methods for IloRange objects (all my constraints are supposed to be of IloRange type).

    So, my next question is:

    How do I go from an IloConstraint to an IloRange ? (direct typecasting does not seem to work).

    Thanks,

    chrysg
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Sun February 24, 2013 07:30 AM

    Originally posted by: SystemAdmin


    The problem is that IloRange and IloConstraint are only handle classes. That is why type casting does not work with them. What you need to cast is the implementation class. Here is how this is done
    if (extr.isConstraint()) {
       IloRangeI *impl = dynamic_cast<IloRangeI *>(extr.asConstraint().getImpl());
       if (impl) {
         // The constraint was indeed an instance of IloRange.
         IloRange range(impl);
         // Now manipulate 'range' as desired.
         // ...
       }
       else {
         // The constraint was not a range. If that is unexpected then error out.
       }
    }
    

    Note that some compilers require special flags to get dynamic_cast working correctly. If you cannot figure out those flags or if you are absolute sure that all your constraints are ranges then you could also use a C-style cast.
    If you did not do that already you may want to search this Forum for how to iterate over a model. I have posted example code for this several times, I think.
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: How to modify the coefficients of constraints already added in Ilomodel

    Posted Mon February 25, 2013 02:10 PM

    Originally posted by: chrysg


    Daniel,

    thank you so much for the response !!!
    I get it now and was indeed able to manipulate the model according to my needs.

    The thread "How can I get decision variables information from model?" was also very helpful.

    Thanks again.

    chrysg
    #CPLEXOptimizers
    #DecisionOptimization