Search code examples
setunionmultiplicationcplex

Cartesian multiplication of two sets where the member of each set is a set


enter link description hereI have two expressions that I want to get a member-by-member union of. But the problem is where the member of each expression is a set.

I have a graph from which I obtained two expressions where the first two terms include all odd subsets of odd nodes (it has 128 members) and the second term includes all even subsets of even nodes (it has 7 members). I want to define a new expression called Sub_odd_even which contains the union of all the previous two expressions. for example: A=[{1,2,3},{4}] B=[{6}] AB = [{1,2,3,6},{4,6}]

I tried the following codes, but none of them were correct. cplex software itself errors them

` int num_odd_even1 = card(Sub_even_even); int num_odd_even2 = card(Sub_odd_odd); int num_odd_even = odd_even1 * odd_even2

{int} Sub_odd_even = {i * j | i in Sub_odd_odd, j in Sub_even_even};
{int} Sub_odd_even = {foreach (i in Sub_odd_odd, j in Sub_even_even) i * j};
{int} Sub_odd_evev[oe in num_odd_even] = {item(Sub_odd, i-1) | i in 1..subr and j in 1..subre};

`


Solution

  • I shared an example in https://github.com/AlexFleischerParis/howtowithopl/blob/master/cartesianproduct2.mod

    // Cartesian product for a given number of sets
    
    tuple Tset {
            {string} members;
    }
    
    {Tset} setOfTuples = {<{"1","2"}>,<{"4","5","6"}>,<{"8","9"}>};
    
    // Here we have 3 dimensions
    
    {Tset} result=union (k in item(setOfTuples,2).members,
         j in  item(setOfTuples,1).members,
         i in item(setOfTuples,0).members)
    {<{i,j,k}>};
    
    execute
    {
      writeln(result);
    }
    
    /*
    
    which gives
    
    {<{"1" "4" "8"}> <{"2" "4" "8"}> <{"1" "5" "8"}> <{"2" "5" "8"}>
         <{"1" "6" "8"}> <{"2" "6" "8"}> <{"1" "4" "9"}>
         <{"2" "4" "9"}> <{"1" "5" "9"}> <{"2" "5" "9"}>
         <{"1" "6" "9"}> <{"2" "6" "9"}>}
         
         */
    

    and then for your second question about how to do the union:

    tuple t
    {
      {int} s1;
      {int} s2;
    }
    
    {t} s={<{1},{5,6}>};
    
    {int} res[i in s]=i.s1 union i.s2;
    
    execute
    {
      writeln(res);
    }
    

    which gives

    [{1 5 6}]