Decision Optimization

Decision Optimization

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

 View Only
  • 1.  How to index a set without iterating over it

    Posted Tue April 13, 2010 04:53 PM

    Originally posted by: DouglasC


    Hello

    I have a set declared as follows.

    {string} team_names =...;

    The data is then read from a spreadsheet. All this works okay, as I can see the data if I set a breakpoint.

    I want to display this data once I have set the decision variables (Fixs[i][j][k]). To do this I have the following execute code.
    
    execute 
    { 
    
    for(k in Days) 
    { 
    
    for(i in nRows_and_nCols) 
    { 
    
    for(j in nRows_and_nCols) 
    { 
    
    if(Fixs[i][j][k] == 1) 
    { writeln(team_names[i], 
    ",", team_names[j], 
    ",", distance[i][j]); 
    // THIS DOES NOT WORK 
    } 
    // if(Fixs) 
    } 
    // for j 
    } 
    // for i 
    } 
    // for k 
    } 
    // execute
    

    I want to iterate over the decision variables and, if set, then display the i'th and j'th elements of team_names, but I can't see how to do it.

    I have tried to create an index to num_teams, but can't get it to work (either an error at complie or run time or the values come out as "Undefined").

    In my naive mind I just want to create a couple of counters (say var i_cnt = 0; j_cnt =0;), increment them (e.g. i_cnt++) as I loop through i,j,k and then use them as an index to team_names when necessary.
    I realise (believe anyway) that this is a real newbie questiosn but it has been driving me nuts for hours.

    Any help appreciated.

    Doug
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to index a set without iterating over it

    Posted Wed April 14, 2010 08:34 AM

    Originally posted by: SystemAdmin


    Doug,

    the problem is that team_names is decared as a set of strings,
    whereas by team_names[i] you access an array of strings.
    One way to overcome this is to copy the set of strings
    to an array, like

    string arr_team_names[nRows_and_nCols]

    Hope this helps

    Regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to index a set without iterating over it

    Posted Wed April 14, 2010 01:59 PM

    Originally posted by: DouglasC


    Many thanks for replying, and sorry for a follow up newbie question.

    How do I copy a set to a string as, in my mind, I still need to iterate over the set - which is my original problem?

    Doug
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to index a set without iterating over it

    Posted Wed April 14, 2010 02:17 PM

    Originally posted by: DouglasC


    Many thanks for replying, and sorry for a follow up newbie question.

    How do I copy a set to a string an array as, in my mind, I still need to iterate over the set - which is my original problem?

    Doug
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: How to index a set without iterating over it

    Posted Thu April 15, 2010 01:33 AM

    Originally posted by: SystemAdmin


    Doug,

    got your point.
    (1) To fill the suggested array you will have to iterate over the set.
    (2) What you are looking for is Opl.item(). See the code snippet
    
    
    {string
    } Teams = 
    {
    "A",
    "B",
    "C"
    }; execute
    { 
    
    for (var i = 0; i < Teams.size; i++) 
    { writeln(i, 
    " --> ", Opl.item(Teams, i)); 
    } 
    }
    


    (3) I would recomment to use something different. An OPL-array is not an array in the classical sense (like C/Fortran) but in Java/C++-notation
    a map. The keys for this map might be integers, but as well strings ans even tuples. This applies as well to multidimensional arrays and to arrays of decision variables. Therefore, I would recomment to index your arrays directly by the items you have in mind (e.g. the teams). The code would be something like
    
    
    {string
    } Teams = 
    {
    "A",
    "B",
    "C"
    }; range Days =1..2;   
    
    int someData[Teams][Teams][Days]; dvar int+ someData[Teams][Teams][Days];   execute
    { 
    
    for(k in Days) 
    { 
    
    for(i in Teams) 
    { 
    
    for(j in Teams) 
    { writeln(i, 
    ",", j, 
    ",", someData[i][j][k]); 
    } 
    // for j 
    } 
    // for i 
    } 
    // for k 
    }   minimize 0; subject to
    {   sum(d in Days, i in Teams,j in Teams) x[i,j,d] <= 100; 
    }
    

    From my point of view this is a more compact formulation that is somewhat
    "self-documenting" ....

    Regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: How to index a set without iterating over it

    Posted Sat April 17, 2010 11:10 AM

    Originally posted by: DouglasC


    Thank you Norbert

    I am playing around with both of your suggestions (for the experience, if nothing else).

    However, when I use Opl.item I get an error message which says

    Scripting runtime error: Element "Opl" does not exist in the OPL model

    I have tried searching for Opl.item (and many other things) but I cannot find anything.

    Can anybody help?

    Doug
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: How to index a set without iterating over it



  • 8.  Re: How to index a set without iterating over it

    Posted Sun April 18, 2010 04:04 AM

    Originally posted by: DouglasC


    Many, many thanks
    #DecisionOptimization
    #OPLusingCPLEXOptimizer