Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only

How to convert an OPL array into a python list ?

  • 1.  How to convert an OPL array into a python list ?

    Posted 07/03/18 10:41 AM

    Hi,

     

    at  https://www.ibm.com/developerworks/community/forums/html/topic?id=1a9f94fb-8ca7-4ac1-aaef-786c44e8ee31&ps=25

    I explained how to convert an OPL tuple set into a python list.

    And since then I got some questions about how to do the same with arrays.

    So let me share an example:


    execute
    {

    // turn an OPL array into a python list
    function getPythonListOfArray(_array)
    {

    var quote="\"";
    var nextline="\\\n";


    var res="[";
    for(var i in _array)
    {
    var value=_array[i];

    if (typeof(value)=="string") res+=quote;
    res+=value;
    if (typeof(value)=="string") res+=quote;
    res+=",";
    //res+=nextline; // if you need one item per line, uncomment this line
    }
    res+="]";
    return res;
    }
    }


    string s[1..3]=["A","B","C"];
    int i[1..10]=[8,7,1,2,3,7,8,1,6,-3];

    execute
    {
    writeln(getPythonListOfArray(s));
    writeln(getPythonListOfArray(i));
    }

     

    gives

    ["A","B","C",]
    [8,7,1,2,3,7,8,1,6,-3,]

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer