Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Creating an array thru scripting

    Posted Mon September 23, 2019 01:05 PM

    Originally posted by: open_ball


    After getting a respond at https://www.ibm.com/developerworks/community/forums/html/topic?id=dcdacecd-180a-4ce2-b70d-a178a9d4c1ac&ps=25, I decided to create an array while reading my data from a CSV file.

     

    I define tuple and fill it. 

            tuple XTuple
            {
            string a;
            string c;
            int t;
            }
            setof(XTuple) Xtup ={<a, c, t> | <c, c2, a> in locset, t in 1..time };
    

    Then, I define an array.

    int arrayX[Xtup];
    

    Now, when I read my data set, I need to identify each <a,c,t> and start filling arrayX accordingly.

     

    execute{
    var f = new IloOplInputFile("X.csv");
    while (!f.eof){
            var str=f.readline();
            var ar = str.split(",");
            if (ar.length==4)  {
                    myX[] = Opl.intValue(ar[3]);
            }
    }     
    f.close();
    }
    

    Could you help me out how I should fill the inside of myX[] ? 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Creating an array thru scripting

    Posted Tue September 24, 2019 03:00 AM


  • 3.  Re: Creating an array thru scripting

    Posted Wed September 25, 2019 11:05 AM

    Originally posted by: open_ball


    Hi,

    That is not what I asked. 

    Suppose in OPL, I have the following tuple

    A 1 

    B 2 

    D 5

    K 9 

     

    When I read data in excel, the rows might be in the following order.

    K 9 8

    D 5 7

    A 1 19

    B 2 4

     

    Now, when I start reading CSV file, the first row is "K 9 8", but I should select  A 1 19 as the first row and pass that into my array as [<A,1>] =19.

    I'd like to do this operation while reading the CSV file. 

     

    Currently, I am using two inner for loop and if statement. In other words;

    for(var i in OPLTuple)

      for(var j in CSVrows)

        if(i.first == j.first && i.second == j.first)

            myArray [ < j.first,  j.first>] = j.third

     

     

    It runs extremely slow due to the size of my problem. Could you tell me if there is an efficient way of doing this operation in OPL?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Creating an array thru scripting

    Posted Wed September 25, 2019 11:18 AM

    Instead of your slow loop you should use the function find in a tuple set

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Creating an array thru scripting

    Posted Wed September 25, 2019 12:11 PM

    Originally posted by: open_ball


    I am trying the following, but obviously I am doing wrong. Could you share a small example with me please?

     

    for(var i in OPLTuple)

       if(CSVrows.contains(i.first, i.second))

            myArray [ <i.first, i.second] =?

    I need to follow the OPL tuple order. On the other hand, CVS file has one column extra. That's why I could not figure out how to deal with it.

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Creating an array thru scripting

    Posted Wed September 25, 2019 04:47 PM

    Hi,

    so let me give you a tiny example with 2 options:

    tuple t2
    {
    string st;
    int i;
    }

    tuple t3
    {
    key string st;
    key int i;
    int j;
    }

    {t2}s2=

     {<"A" ,1 >,
    <"B", 2 >,
    <"D", 5>,
    <"K", 9  >};

    {t3} s3;
    int value[s2];

    execute
    {
    s3.add("K",9, 8);
    s3.add("D", 5, 7);
    s3.add("A", 1 ,19);
    s3.add("B", 2 ,4 );

    for(var l in s3) value[s2.find(l.st,l.i)]=l.j;

    }

    int value2[k in s2]=item(s3,<k.st,k.i>).j;

    execute
    {
    writeln(value);

    writeln(value2);
    }

     

    gives

     

    [19 4 7 8]
     [19 4 7 8]

    regards

     

    Many other how to with OPL at https://www.linkedin.com/pulse/how-opl-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer