Topic
  • 2 replies
  • Latest Post - ‏2018-09-17T12:15:35Z by AndyHam
AndyHam
AndyHam
52 Posts

Pinned topic OPL Syntax Question

‏2018-09-16T20:02:17Z |

tuple t_Job {
  int p1; int p2; int qty;   
};
{t_Job} Job={
  <1 1 2>, 
  <1 4 1>,
  <1 7 3>,
  <2 2 1>,
  <2 3 2>,
  <2 5 4>,
  <2 6 2>
}; 

range p2lst = 1..7;

 

Now, I am trying to extract p2 and qty from the tuple and give to array as shown below.

int i_Qty[p2 in p2lst] = [<j.qty> | j in Job: j.p2==p2];

The syntax is not right, but I could not make it right. 

What I want is to derive the following array from the tuple.

i_Qty[1]=2
i_Qty[2]=1
i_Qty[3]=2
i_Qty[4]=1
i_Qty[5]=4
i_Qty[6]=2
i_Qty[7]=3

Would you please fix my syntax? Any help will be appreciated.
int i_Qty[p2 in p2lst] = [<j.qty> | j in Job: j.p2==p2];

Thanks,
Andy

  • AlexFleischer
    AlexFleischer
    274 Posts

    Re: OPL Syntax Question

    ‏2018-09-17T06:52:05Z  

    Hi

    tuple t_Job {
      int p1; key int p2; int qty;   
    };
    {t_Job} Job={
      <1, 1, 2>,
      <1, 4, 1>,
      <1, 7, 3>,
      <2, 2, 1>,
      <2, 3, 2>,
      <2 ,5 ,4>,
      <2 ,6, 2>
    };

    range p2lst = 1..7;

     


    int i_Qty[p2 in p2lst] = first({j.qty | j in Job: j.p2==p2});

    execute
    {
    writeln(i_Qty);
    }

    gives

     

    [2 1 2 1 4 2 3]

    regards

     

    https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/

  • AndyHam
    AndyHam
    52 Posts

    Re: OPL Syntax Question

    ‏2018-09-17T12:15:35Z  

    Hi

    tuple t_Job {
      int p1; key int p2; int qty;   
    };
    {t_Job} Job={
      <1, 1, 2>,
      <1, 4, 1>,
      <1, 7, 3>,
      <2, 2, 1>,
      <2, 3, 2>,
      <2 ,5 ,4>,
      <2 ,6, 2>
    };

    range p2lst = 1..7;

     


    int i_Qty[p2 in p2lst] = first({j.qty | j in Job: j.p2==p2});

    execute
    {
    writeln(i_Qty);
    }

    gives

     

    [2 1 2 1 4 2 3]

    regards

     

    https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/

    It works beautifully! Thanks!