Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Adding and removing variable conversions

    Posted 11/22/18 03:34 PM

    Originally posted by: TOYSED


    Hello,

     

    I am using C++ and having difficulty about adding and removing variable conversions to my model since

    my variable number is rapidly changing.

     

    That is I start with x variables float variables. 

    I change them to bool with 

    IloConversion conversion = IloConversion(env, X, ILOBOOL);

    Then I remove this conversion. But in the next iteration I have x+y variable. Thus, if I use the same conversion 

    I only convert x variable to bool.

    How can I update my models in this case? Do I need to define a new conversion for each new variable?

     

    Thank you...


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Adding and removing variable conversions

    Posted 11/23/18 01:44 AM

    You have to register a conversion for any variable you want to convert (otherwise CPLEX could not know that you want this variable converted).

    Note that IloConversion is a subclass of IloExtractable. So you can store multiple conversions in an IloExtractableArray and then use IloModel::add(IloExtractableArray) and IloModel::remove(IloExtractableArray) do add and remove all conversions in one shot. That may simplify your code a little.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Adding and removing variable conversions

    Posted 11/24/18 11:04 PM

    Originally posted by: TOYSED


    Hello Daniel,

     

    Thanks for the reply. I do understand that I need an array structure but I have some more questions.

    First, if I need to define an IloExtractableArray, then how should I assign conversions to this array? Could you give me a small example...

    Second, I am generating columns at each iteration and do not know how many additional columns I will have at each iteration.

    So, is this a problem when defining IloExtractableArray for the first time. I guess I need a flexible structure like a vector in C++.

     

    Thanks,

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Adding and removing variable conversions

    Posted 11/25/18 07:38 AM

    IloExtractableArray is extendible just like std::vector(). You may want to take a look at the reference documentation here. From that I hope it is clear how to use such an array: just use IloExtractableArray::add() to append a new element to the array.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Adding and removing variable conversions

    Posted 11/26/18 11:57 AM

    Originally posted by: TOYSED


    Hello,

     

    I am trying to do it. But I could not achieve so far.

    I have a set of variables X in the x vector.

    After adding contraint and variable to the model I am defining a Bool Array (which I understand is a default Extractable Array)

    IloBoolVarArray ARRAY(env, x.size());

    IntegerMaster.add(ARRAY);

    In this case I add x more variables to the model.

    However I want to define an Array which is the bool equivalent of the original variables.

     

    I also used

     

    IloExtractableArray ARRAY(env, x.size(),X);

    IntegerMaster.add(ARRAY);

     

    But I got the error

    Error    25    error C2664: 'IloExtractableArray::IloExtractableArray(const IloEnv,IloInt,const IloExtractable)' : cannot convert parameter 3 from 'IloNumVarArray' to 'const IloExtractable'  

     

    What should I do? Do I need to define an IloBoolVarArray (which is a default Extractable Array what I understand from the documents) or a new Array using IloExtratableArray and convert it to Bool and/ Float regularly?

     

     


     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Adding and removing variable conversions

    Posted 11/26/18 01:15 PM

    I think what you have to do is this (assuming that X is your array of boolean variables):

    IloExtractableArray convertX(env);
    for (IloInt i = 0; i < X.getSize(); ++i)
      convertX.add(IloConversion(env, X[i], ILOFLOAT));

    After that, model.add(convertX) will relax all variables in X and model.remove(convertX) will unrelax them. If you create a new variable then just also append a new conversion to convertX.

    Is that what you wanted to do?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Adding and removing variable conversions

    Posted 11/26/18 01:39 PM

    Originally posted by: TOYSED


    Let me summarize my algorithm again.

    I have x variables at the beginning.

    At each iteration I could add one or more variables.

    Before running the algorithm I have to define a
    IloExtractableArray convertX(env);

    while(condition is satisfied){

    convert X to float.

    Run the algorithm.

    Add new variables to the X

    convert all X to bool

    }

    that is I need to update X regularly and at each step I need a coversion from bool to float and float to bool

    Thus what you offer is true I guess. But how I will update convertX?

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Adding and removing variable conversions

    Posted 11/26/18 02:27 PM

    Originally posted by: TOYSED


    I tried the following;

    IloExtractableArray convertX(env);
    for (IloInt i = 0; i < X.getSize(); ++i)
      convertX.add(IloConversion(env, X[i], ILOBOOL));

    But I guess it does not work because I didn't see the integratiy constarints in the lp file.


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Adding and removing variable conversions

    Posted 11/27/18 03:11 AM

    I am confused now: You said your X array is an IloBoolVarArray. So initially all the variables should have type bool. Then why do you want to have a conversion that converts to ILOBOOL?. As far as I understand, what you want is a conversion to ILOFLOAT?

    About extending convertX after adding a new variable: like I said, just add a new conversion for the new variable:

    convertX(IloConversion(env, new_variable, ILOFLOAT));

    Maybe you can show us parts of your code and tell us where things go wrong. Then it may be more clear what your problem is.


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Adding and removing variable conversions

    Posted 11/27/18 11:28 AM

    Originally posted by: TOYSED


    Hello, 

     

    The flow is as follows;   

     

        IloNumVarArray X(env,0,1, ILOFLOAT);

        IloNumVarArray AX(env,0,1, ILOFLOAT);

     

           //Convert LP MODEL TO IP MODEL

                    IloModelIntegerMaster(env);

           IntegerMaster.add(Mmodel);

          

     

           IloExtractableArrayconvertX(env);

           IloExtractableArrayconvertAX(env);

     

           for(IloInti = 0; i < X.getSize(); i++)

           convertX.add(IloConversion(env, X[i], ILOBOOL));

     

           for(IloInti = 0; i < AX.getSize(); i++)

           convertAX.add(IloConversion(env, AX[i], ILOBOOL));

          

           IloCplexcplex1(IntegerMaster);  

           IntegerMaster.add(convertX);

           IntegerMaster.add(convertAX);

     

    cplex1.solve();

     

     

          

           while(B > C){

                                       

                       

                              //remove conversion to obtain all float variables

                               IntegerMaster.remove(convertAX);

                               IntegerMaster.remove(convertX);

     

                                                  

                               //add new variables

                                                                                                                    convertX.add(IloConversion(env, X, ILOBOOL));  //it does not accept like convertX(IloConversion(env, new_variable, ILOFLOAT))

                                                               

                               convertAX.add(IloConversion(env, AX, ILOBOOL));

                                                               

     

                               IntegerMaster.add(convertX);

                               IntegerMaster.add(convertAX);

     

                               //solve with bool variables

                               cplex1.solve();

    B--;

                                     

                                     

    } //end while

     

    When I run like this, it gives Multiple Conversion Exception.


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Adding and removing variable conversions

    Posted 11/27/18 11:36 AM

    This is wrong:

    convertX.add(IloConversion(env, X, ILOBOOL));

    (and similarly for convertAX). You are adding conversions for X again. So you will have multiple conversions for each variable in X in convertX, hence the MultipleConversionException. You should only add a conversion for the new variables. For all other variables you already have the conversion in convertX!

    Like maybe so:

    IloInt const oldSize = X.getSize();
    // Create new variables and append them to X
    ...
    for (IloInt i = oldSize; i < X.getSize(); ++i)
      convertX.add(IloConversion(env, X[i], ILOBOOL));

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: Adding and removing variable conversions

    Posted 11/28/18 11:20 AM

    Originally posted by: TOYSED


    Thank you so much Daniel,

    It finally worked..:)


    #CPLEXOptimizers
    #DecisionOptimization