Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Comparing elements of string sets in a tuple by item()

  • 1.  Comparing elements of string sets in a tuple by item()

    Posted 12/17/14 10:18 PM

    Originally posted by: ShunjiTanaka


    Hello.

    I found that the following code does not work as I expected.

    tuple TupA {
      int no;
      {int} set;
    };
    tuple TupB {
      int no;
      {string} set;
    };
    
    {TupA} A = { <0, {0}>,   <1, {0}>,   <2, {0}>,   <3, {1}>,   <4, {1}> };
    {TupB} B = { <0, {"0"}>, <1, {"0"}>, <2, {"0"}>, <3, {"1"}>, <4, {"1"}> };
    
    tuple Tup {
      int a;
      int b;
    };
    
    // It works.
    {Tup} x = {<a.no, b.no> | ordered a, b in A : item(a.set, 0) == item(b.set, 0)};
    // It does not work.
    {Tup} y = {<a.no, b.no> | ordered a, b in B : item(a.set, 0) == item(b.set, 0)};
    
    // A simple workaround is to introduce another tuple.
    tuple TupC {
      int a_no;
      int b_no;
      string a_string;
      string b_string;
    };
    
    {TupC} C = {<a.no, b.no, item(a.set, 0), item(b.set, 0)> | ordered a, b in B};
    {Tup} z = {<u.a_no, u.b_no> | u in C : u.a_string == u.b_string};
    
    execute {
      writeln(x);
      writeln(y);
      writeln(z);
    }
    

    The three sets, x, y, and z should be the same, but y is {<0 1> <0 2> <3 4>} instead of {<0 1> <0 2> <1 2> <3 4>}.

    This happens when elements of string sets in a tuple are compared by item().  I would like to know if it is a specification of OPL or not.

    I tested the code by:

    • platforms: Linux x86-64 and Windows 7 x86-64
    • versions: 12.6.0 and 12.6.1
    • executables: oplide and oplrun

    Regards,

    Shunji


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: Comparing elements of string sets in a tuple by item()

    Posted 12/19/14 02:14 AM

    Hi,

    indeed there seems to be an issue.

    I would write:

    {Tup} y = {<a.no, b.no> | ordered a, b in B : 1==card(a.set inter b.set) };
     

    instead of your workaround.

    You may notice that if you rewrite:

    // It works.
    {Tup} x = {<a.no, b.no> | ordered a, b in A : first(a.set) == first(b.set)};
    // It does not work.
    {Tup} y = {<a.no, b.no> | ordered a, b in B : first(a.set) == first(b.set)};

    then x works and y gives a concert error

    regards


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: Comparing elements of string sets in a tuple by item()

    Posted 12/19/14 05:35 AM

    Originally posted by: ShunjiTanaka


    Thank you for your reply.

    I understand it is an issue.  I'll wait for it fixed.

    Your workaround is perfect when the number of elements in the set is always one.  However, as you know, we need not use a set in such a case. ;-)

    The following workaround will be more memory efficient than my first one:

    tuple TupD {
      int no;
      string s;
    };
    
    {TupD} D = {<a.no, first(a.set)> | a in B};
    {Tup} u = {<a.no, b.no> | ordered a, b in D : a.s == b.s};
    

    then x works and y gives a concert error

    Yes.  I first tried first(), but I gave it up due to the error.  I had completely forgotten it till your reply.

     

    Regards,

    Shunji


    #DecisionOptimization
    #OPLusingCPOptimizer