Topic
8 replies Latest Post - ‏2012-12-13T13:30:38Z by SystemAdmin
SystemAdmin
SystemAdmin
1883 Posts
ACCEPTED ANSWER

Pinned topic Using allocation dynamic in OPL Studio

‏2012-11-27T12:44:28Z |
Dear all,

I want to use an allocation dynamic in my problem with OPL but i can't find any example or information that this is possible with OPL Studio. Anyone has an idea of how to do it or any example? Indeed my decison variable have 3 dimensions, and i want to create the 3rd dimension based on the 2nd dimenson.

Thank you in advance for yours kindly help,

Son
Updated on 2012-12-13T13:30:38Z at 2012-12-13T13:30:38Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    1883 Posts
    ACCEPTED ANSWER

    Re: Using allocation dynamic in OPL Studio

    ‏2012-12-11T13:34:21Z  in response to SystemAdmin
    Do you mean that each dimension of your array has a different size? If so, you can use tuples for this purpose. Here is an example: say that you want to have an array of months, each of which has is itself an array of days. Since some months have a different number of days, the second dimension of your array will have a size depending on the first one: the "April" array will have 30 elements, while the "July" one 31. So instead of writing:

    
    range Days = 1..31; range Months = 1..12;   dvar 
    
    int x[m in M, d in D];
    


    which will allow you to have an undesired 31th day for some months, you can do this:

    
    
    
    int days_count = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; range Months = 1..12;   tuple MonthDay
    { 
    
    int month; 
    
    int day; 
    }   
    {MonthDay
    } MD = 
    {<m,d> : m in M, d in 1..days_count[m]
    } dvar 
    
    int x[md in MD];
    


    MD is a set of tuples <month, day>, where each month has its number of days defined in the constant array days_count. Defining an array over this set, you emulated a jagged array having dimensions of different size by spanning it across a single dimension which is the set of tuples.

    This trick works only if the sizes of the dimensions of the array are constant values. I don't think you can do anything if they are variable, because what you would want to do would be writing a model which has a variable number of variables. I'm not sure this can be done, but I'd wait for someone from IBM to confirm.

    Regards,
    Stefano
  • SystemAdmin
    SystemAdmin
    1883 Posts
    ACCEPTED ANSWER

    Re: Using allocation dynamic in OPL Studio

    ‏2012-12-11T14:21:10Z  in response to SystemAdmin
    Thank you for your answer. It's not i want to do though, the size of the array is variable, it depends on the dimension before it but i know its exact number. So i want to know how to create the variable xa in Ab in Bc in ?? where ?? is variable and it depends on b.

    Thank you.

    Son
    • SystemAdmin
      SystemAdmin
      1883 Posts
      ACCEPTED ANSWER

      Re: Using allocation dynamic in OPL Studio

      ‏2012-12-11T15:48:18Z  in response to SystemAdmin
      Try to show a concrete example. For what is my understanding, if you know the exact number of the size of a dimension of the array, you can do it like I said. In my example the size depended on another array (+days_count+, not the M that was defining the previous dimension), but still it could have depended on the value of m in M (the generic variable in the range defining the first dimension) with no problems.

      In your case, it would be something like

      range A = ...;
      range B = ...;
       
      tuple Triple{
              int field_a;
              int field_b;
              int field_c;
      }
       
      {Triple} ABC = {<a,b,c> : a in A, b in B, c in $range_expression_depending_on_b$ // any expression like -3 .. b+2^b + 5 works fine here}
       
      dvar int x[abc in ABC];
      


      Regards,
      Stefano
      Updated on 2014-03-26T05:52:06Z at 2014-03-26T05:52:06Z by iron-man
      • SystemAdmin
        SystemAdmin
        1883 Posts
        ACCEPTED ANSWER

        Re: Using allocation dynamic in OPL Studio

        ‏2012-12-12T09:05:55Z  in response to SystemAdmin
        Dear,

        It's nice to see your answer, it really helps to allocate the array with variable dimension. But it's not really what i want to do. In my case it is more complicated because i know its dependence but it cant be expressed by any expression. Here is my concrete example.
        tuple Triplet{
                string a;
                string b;
                string c;
        };
        {Triplet}   Triple = ...;
        dvar boolean y[l in V][v in V][i in Triple];
        


        Here is what i do for the moment, but it created too much variable that i dont need. In my case it is an arborescence to destination l, v is a node in the network, the set triple i declare here is too big. The exact size of Triple depends on the node V, and it is a set that i can count if i know v but there is no expression for it. So my question here is how to create the variable approriately. Can i parcours each array and allocate the sub-array dynamically like in C++?

        Thank you and best regards,

        Son
        Updated on 2014-03-26T05:52:02Z at 2014-03-26T05:52:02Z by iron-man
        • SystemAdmin
          SystemAdmin
          1883 Posts
          ACCEPTED ANSWER

          Re: Using allocation dynamic in OPL Studio

          ‏2012-12-12T17:27:49Z  in response to SystemAdmin
          If the dependence can't be expressed by any expression (because for instance), I think you can still do the following:

          
          range A = ...; range B = ...; 
          
          int C[b in B]; 
          // the array C is just declared but not instanced   tuple Triple
          { 
          
          int field_a; 
          
          int field_b; 
          
          int field_c; 
          } 
          {Triple
          } ABC = 
          {<a,b,c> : a in A, b in B, c in 1..C[b]
          } dvar 
          
          int x[abc in ABC];     execute COMPUTE_C 
          { 
          // in the execute preprocessing block you can use the OPL scripting language to fill the C array using complex operators and commands commonly available in programming languages like if-else, while, for and user defined functions   
          }
          


          In this way, the values of the ranges for the third dimension C are computed, any way you like, in the execute block like you would do in Java or C# for instance.

          More details on the OPL Scripting language are here: http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp?topic=%2Filog.odms.ide.help%2FOPL_Studio%2Fopllanguser%2Ftopics%2Fopl_languser_intro_scripting.html

          Is this what you meant?

          Regards,
          Stefano
          • SystemAdmin
            SystemAdmin
            1883 Posts
            ACCEPTED ANSWER

            Re: Using allocation dynamic in OPL Studio

            ‏2012-12-13T10:24:15Z  in response to SystemAdmin
            Dear,
            I think this is what i need, thanks alot. I will try this. About the code it should be
            {Triple} ABC = {<a,b,c> : a in A, b in B, c in C[b]}
            

            right?

            Best regards,

            Son
            Updated on 2014-03-26T05:51:57Z at 2014-03-26T05:51:57Z by iron-man
            • SystemAdmin
              SystemAdmin
              1883 Posts
              ACCEPTED ANSWER

              Re: Using allocation dynamic in OPL Studio

              ‏2012-12-13T12:50:16Z  in response to SystemAdmin
              I think the code should be:

              {Triple} ABC = {<a,b,c> | a in A, b in B, c in 1..C[b]}
              


              Two remarks:

              1) | should be used in place of : (sets syntax slightly differs from aggregators one)
              2) C[b] is defined as an integer (it contains information about the size of your range which depends on b), but in requires a range, so it should be c in 1..C[b] instead of c in C[b]

              Regards,
              Stefano
              Updated on 2014-03-26T05:51:53Z at 2014-03-26T05:51:53Z by iron-man
              • SystemAdmin
                SystemAdmin
                1883 Posts
                ACCEPTED ANSWER

                Re: Using allocation dynamic in OPL Studio

                ‏2012-12-13T13:30:38Z  in response to SystemAdmin
                yeah, you're right. In your example it should be that way. I will have to declare in my model C[b] as a range.

                Regards,

                Son