Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Using item with decision variables

    Posted 05/03/17 02:07 AM

    Originally posted by: TracedWill


    Hi,

     

    In constraint programming, it is common to use the value of decision variables as an index in other variables, especially in assignment problems. I am trying to do this in CPLEX, but I get an error. My code is the following:

     

    using CP;
    
    tuple myTuple { 
        key int foo;
        key int bar;
        int ram;
    }
    {myTuple} myTuples = ...;
    
    range myRange = 1..3;
    
    dvar int myVar[myRange] in myRange;
    
    //minimize ...
    
    subject to{
        forall(i in myRange)
            item(myTuples,<i,myVar[i]>).ram >= myVar[i];
    }
    

    And data:

     myTuples = {
            <1,1,1>,<1,2,1>,<1,3,2>,<2,1,3>,<2,2,1>,<2,3,2>
     };
    

    The problem and data is random, it is only to show how I use the item-command on a tuple with two keys and a property that I want to extract using the value of the decision variable as the second key.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Using item with decision variables

    Posted 05/03/17 03:07 AM

    Hi,

    what you can do is use a decision variable as an index for an array:

    using CP;

     

    tuple myTuple {
        key int foo;
        key int bar;
        int ram;
    }
    {myTuple} myTuples = {
            <1,1,1>,<1,2,1>,<1,3,2>,<2,1,3>,<2,2,1>,<2,3,2>
     };;
     
     range myRange = 1..card(myTuples);

    int ramInAnArray[i in myRange]=item(myTuples,i-1).ram;

     

    dvar int myVar[myRange] in myRange;

    //minimize ...

    subject to{

     

        forall(i in myRange)
            ramInAnArray[myVar[i]]== myVar[i];
    }

     

    works fine

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Using item with decision variables

    Posted 05/03/17 03:28 AM

    Originally posted by: TracedWill


    Thank you.

    Is the "i-1" in "int ramInAnArray[i in myRange]=item(myTuples,i-1).ram;" due to different counting in the range and the items (e.g., if there is an item 0)?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Using item with decision variables

    Posted 05/03/17 03:35 AM

    Hi,

    documentation of item states

    Gives access to the n-th item of a set or to a tuple from a tuple set. When accessing the n-th item, counting starts at 0.

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Using item with decision variables

    Posted 05/03/17 04:16 AM

    Originally posted by: TracedWill


    Ah, I missed that when I read it, thank you.
     

    On a related note... If I have missing data, e.g., if there is some combination of foo and bar that does not exist, is there some sort of if-else statement I can use such that the array is still built but has 0's in the cells corresponding to the missing values? That is, in pseudo-code:

    for(foo in 1..5){
        for(bar in 1..4){
            if(item(myTuples,<foo,bar>) exists) ramArray[foo][bar] = item(myTuples,<foo,bar>).ram; 
            else ramArray[foo][bar] = 0;
        }
    }
    

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Using item with decision variables

    Posted 05/03/17 04:36 AM

    Hi,

    you can write that both in OPL and scripting:

    using CP;

     

    tuple myTuple {
        key int foo;
        key int bar;
        int ram;
    }
    {myTuple} myTuples = {
            <1,1,1>,<1,2,1>,<1,3,2>,<2,1,3>,<2,2,1>,<2,3,2>
     };;
     
     
    range r1to5=1..5;
    range r1to4=1..4;

    int ramArray[r1to5][r1to4];

    int ramArray2[foo in r1to5][bar in r1to4]=
    (<foo,bar> in myTuples)?item(myTuples,<foo,bar>).ram:0;

    execute
    {
    for(foo in r1to5){
        for(bar in r1to4){
            if(myTuples.find(foo,bar)) ramArray[foo][bar] = myTuples.find(foo,bar).ram;
            else ramArray[foo][bar] = 0;
        }
    }
    }

    assert forall(foo in r1to5,bar in r1to4)
        ramArray[foo][bar]==ramArray2[foo][bar];

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Using item with decision variables

    Posted 05/03/17 04:45 AM

    Originally posted by: TracedWill


    You are seriously my favourite human being right now! Everything works!

    Thank you so much!


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Using item with decision variables