Topic
  • 10 replies
  • Latest Post - ‏2012-05-15T12:42:07Z by AlexFleischer
SystemAdmin
SystemAdmin
1883 Posts

Pinned topic Collect Variables Value using OPL C++ API IloOplModelSource

‏2012-05-14T13:41:39Z |
Hi,

I build a C++ project from a model and a data file, as it is done in the given example mulprod, using the classes IloOplModelSource and IloOplDataSource.
It is possible to get back the objective value, using the well named function getObjValue(), and to print the variables value, with printSolution(cout), but is it possible to actually collect then to use them in another optimization ? (Of course, I can always print them in a file and read it afterwards, but ... it does not seem very clever and it might compute errors)

I am aware that, as I imported the model from a file, variables do not have an actual C++ (only the string in the .mod) name so I cannot call them, but as it is done in Python ( the get_values() metod), it might exists in C++ - well it should, as the API is more developped in this language...

Question : Is there an efficent way to get the solution values ?

Thanks, Adrian
Updated on 2012-05-15T12:42:07Z at 2012-05-15T12:42:07Z by SystemAdmin
  • AlexFleischer
    AlexFleischer
    1271 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-14T15:50:53Z  
    Hi,

    you can have access to the decision variables after the solve.

    In the example mulprod you mentioned, after

    opl.postProcess();
    


    you may add

    IloNumVarMap inv = opl.getElement("Inv").asNumVarMap();
    cout << "Number of dimensions of Inv" 
    << inv.getNbDim() << endl;
    


    that will give

    Number of dimensions of Inv2

    I hope this helps you

    Regards
    Updated on 2014-03-26T05:56:46Z at 2014-03-26T05:56:46Z by AlexFleischer
  • SystemAdmin
    SystemAdmin
    1883 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T06:39:33Z  
    Hi,

    you can have access to the decision variables after the solve.

    In the example mulprod you mentioned, after

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">opl.postProcess(); </pre>

    you may add

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">IloNumVarMap inv = opl.getElement("Inv").asNumVarMap(); cout << "Number of dimensions of Inv" << inv.getNbDim() << endl; </pre>

    that will give

    Number of dimensions of Inv2

    I hope this helps you

    Regards
    Thanks for you answer.

    I had managed to go so far, but then I'm stuck.
    Long story short, in my OPL model, I have to variables, one matrix MSET1SET2 and the other is a tuple indexed array T<a,b,c>.
    I opl.getElement.asNumVarMap() or asIntVarMap() them, but then which operator or function have I to use to get the values (like M[i][j] or Ta,b,c) ?

    Thanks
    Adrian
  • SystemAdmin
    SystemAdmin
    1883 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T06:40:17Z  
    Hi,

    you can have access to the decision variables after the solve.

    In the example mulprod you mentioned, after

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">opl.postProcess(); </pre>

    you may add

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">IloNumVarMap inv = opl.getElement("Inv").asNumVarMap(); cout << "Number of dimensions of Inv" << inv.getNbDim() << endl; </pre>

    that will give

    Number of dimensions of Inv2

    I hope this helps you

    Regards
    Thanks for you answer.

    I had managed to go so far, but then I'm stuck.
    Long story short, in my OPL model, I have to variables, one matrix MSET1SET2 and the other is a tuple indexed array T<a,b,c>.
    I opl.getElement.asNumVarMap() or asIntVarMap() them, but then which operator or function have I to use to get the values (like M[i][j] or Ta,b,c) ?

    Thanks
    Adrian
  • SystemAdmin
    SystemAdmin
    1883 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T07:24:43Z  
    Thanks for you answer.

    I had managed to go so far, but then I'm stuck.
    Long story short, in my OPL model, I have to variables, one matrix MSET1SET2 and the other is a tuple indexed array T<a,b,c>.
    I opl.getElement.asNumVarMap() or asIntVarMap() them, but then which operator or function have I to use to get the values (like M[i][j] or Ta,b,c) ?

    Thanks
    Adrian
    Indeed,
    I have no problem for the matrix or array type, I just convert them into IloNumArray (and not Var) and I can access the values with MIloInt(i).get(IloInt(j))
    But my real issue is for the tuple indexed array, because I do not know the relation between (a,b,c) and k with T<a,b,c> in OPL and T[k] in IloCplex
    Adrian
  • AlexFleischer
    AlexFleischer
    1271 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T08:39:00Z  
    Indeed,
    I have no problem for the matrix or array type, I just convert them into IloNumArray (and not Var) and I can access the values with MIloInt(i).get(IloInt(j))
    But my real issue is for the tuple indexed array, because I do not know the relation between (a,b,c) and k with T<a,b,c> in OPL and T[k] in IloCplex
    Adrian
    Hi,

    the example iterators.cpp in opl\examples\opl_interfaces\cpp\src could help you.
    The sample3 function is a good example.

    Regards
  • SystemAdmin
    SystemAdmin
    1883 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T09:19:01Z  
    Hi,

    the example iterators.cpp in opl\examples\opl_interfaces\cpp\src could help you.
    The sample3 function is a good example.

    Regards
    Thanks for the tip, it is actually quite interesting,

    But my problem is different.
    In the example, this is the case of a tuple array, string indexed.
    Mine is a boolean array, tuple indexed...
    The issue is that I do not know the relation between the index (wich is an int in a range that do not start at 0 or 1 because ther are other variables declared before I guess) of the array and the associated tuple.

    Hope it's clear enough...
    Thanks,
    Adrian
  • AlexFleischer
    AlexFleischer
    1271 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T11:51:59Z  
    Thanks for the tip, it is actually quite interesting,

    But my problem is different.
    In the example, this is the case of a tuple array, string indexed.
    Mine is a boolean array, tuple indexed...
    The issue is that I do not know the relation between the index (wich is an int in a range that do not start at 0 or 1 because ther are other variables declared before I guess) of the array and the associated tuple.

    Hope it's clear enough...
    Thanks,
    Adrian
    Hi,

    suppose you have

    tuple t
    {
     int x;
    int y;
    }
     
    {t} s={<1,2>,<2,1>};
     
    int z[s]=[10,5];
    


    then to read this you can use

    IloTupleSet s = opl.getElement("s").asTupleSet();
                    IloMapIndexArray indices(env);
                    IloTuple tuple=s.makeFirst();;
                    
                    IloIntMap z = opl.getElement("z").asIntMap();
                    int value=z.get(tuple);
                    cout << value << endl;
                    IloTuple tuple2=s.makeTuple(1);
                    int value2=z.get(tuple2);
                    cout << value2 << endl;
    
    Updated on 2014-03-26T05:56:42Z at 2014-03-26T05:56:42Z by AlexFleischer
  • SystemAdmin
    SystemAdmin
    1883 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T12:07:42Z  
    Hi,

    suppose you have

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">tuple t { int x; int y; } {t} s={<1,2>,<2,1>}; int z[s]=[10,5]; </pre>

    then to read this you can use

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">IloTupleSet s = opl.getElement("s").asTupleSet(); IloMapIndexArray indices(env); IloTuple tuple=s.makeFirst();; IloIntMap z = opl.getElement("z").asIntMap(); int value=z.get(tuple); cout << value << endl; IloTuple tuple2=s.makeTuple(1); int value2=z.get(tuple2); cout << value2 << endl; </pre>
    Perfect!

    Thanks
  • SystemAdmin
    SystemAdmin
    1883 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T12:13:26Z  
    Perfect!

    Thanks
    What is the use fot the following line ?

    IloMapIndexArray indices(env);
  • AlexFleischer
    AlexFleischer
    1271 Posts

    Re: Collect Variables Value using OPL C++ API IloOplModelSource

    ‏2012-05-15T12:42:07Z  
    What is the use fot the following line ?

    IloMapIndexArray indices(env);
    hi,

    IloMapIndexArray indices(env);
    


    is useless.

    regards
    Updated on 2014-03-26T05:56:38Z at 2014-03-26T05:56:38Z by AlexFleischer