Decision Optimization

 View Only
Expand all | Collapse all

Using concatenation to join tuples in a set

  • 1.  Using concatenation to join tuples in a set

    Posted Fri April 17, 2020 04:00 PM

    Originally posted by: tjnelso


    Suppose I have a tupleset like the one shown below.

    tuple def = {string strA; string strB;};
    {def} data = {
        <"cat", "A">
        <"cat", "B">
        <"cat", "C">
        <"cat", "A;B">
        <"cat", "A;C">
        <"dog", "A">
        <"dog", "C">
        <"dog", "A;C">
    };
    

    Is there a way to manipulate it using OPL to generate a new tupleset like the one that follows?

    {def} nData = {
        <"cat", "A;B;C">
        <"dog", "A;C">
    }
    

    I can count the number of tuples in the original set that contain any specific value of strB and use that information to combine some of the rows. If I keep only items that appear once then I can get to this tuple:

    {def} nDataAttempt = {
        <"cat", "A;B">
        <"cat", "A;C">
        <"dog", "A;C">
    }
    

    Is there a way to further combine the tuples in nDataAttempt to get nData? I can't see how because I don't have the desired values of strB already preexisting, but I'm hopeful there is a way to create them by concatenation (either explicit or implicit).


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Using concatenation to join tuples in a set

    Posted Sat April 18, 2020 08:08 AM

    Hi,

    you could do that with scripting but I would recommend to use sets instead of strings and then

    tuple def  {string strA; {string} strB;};
    {def} data = {
        <"cat", {"A"}>,
        <"cat", {"B"}>,
        <"cat", {"C"}>,
        <"cat", {"A","B"}>,
        <"cat", {"A","C"}>,
        <"dog", {"A"}>,
        <"dog", {"C"}>,
        <"dog", {"A","C"}>
    };

    {string} animals={i.strA | i in data};

    {def} result={<i,union(j in data:j.strA==i)j.strB> | i in animals};

    execute
    {
      writeln(result);
    }

    gives

     

    {<"cat" {"A" "B" "C"}> <"dog" {"A" "C"}>}

    regards

     

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


    #DecisionOptimization
    #OPLusingCPLEXOptimizer