Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to implement powerset?

    Posted 01/06/14 05:52 AM

    Originally posted by: kasm


    Hello everyone,

    I am a new user of OPL and OPL Script and also new in this forum. So excuse me if my post is related to a previous topic.

    I would like to implement a powerset algorithm using OPL but I do not succeed to correctly represent the output type of the function, which should be a set of sets of integerger. I have learned that we can define set of anytype of data but when I tried using set of integer as a type, like for tuple, it does not work.

    Can anyone here help me to handle this problem? do you have any other idea of how to solve it?

     

    BTW, a powerset of a set S is a function which returns all the subsets of the set S. http://en.wikipedia.org/wiki/Power_set

     

    Thank you for your answers


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to implement powerset?

    Posted 01/06/14 01:59 PM

    Originally posted by: fbahr


    Something like

    set SS := 1 .. 2**card(S);
    set POW {k in SS} := {i in S: (k div 2**(ord(i))) mod 2 = 1};

    should work.

    --fbahr


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to implement powerset?

    Posted 01/07/14 02:54 AM

    Hi,

    indeed your way fbahr is more efficient that what I suggested at

    https://www.ibm.com/developerworks/community/forums/html/topic?id=2ca9cc74-f030-46ae-8c32-82156cfa2cd4#a32bb643-ae67-4675-a02b-9206d595d15c

    and can be rewritten in OPL as

    {string} s={"A","B","C","D"};
    range r=1.. ftoi(pow(2,card(s)));
    {string} s2 [k in r] = {i | i in s: ((k div (ftoi(pow(2,(ord(s,i))))) mod 2) == 1)};

    execute
    {
     writeln(s2);
    }

     

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How to implement powerset?

    Posted 10/16/14 03:43 AM

    Originally posted by: heidizh


    Hi, 

    It returns  all the subsets of the set S, but what if I only want the subsets which is more than two elements and less than |S|-1 elements?

    Thank you for your answers.

    Best wishes,

    Heidi


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How to implement powerset?

    Posted 10/16/14 04:30 AM

    Hi,

    {string} s={"A","B","C","D"};
    range r=1.. ftoi(pow(2,card(s)));
    {string} s2 [k in r] = {i | i in s: ((k div (ftoi(pow(2,(ord(s,i))))) mod 2) == 1)};

    execute
    {
     writeln(s2);
    }

     // more than two elements and less than |S|-1 elements
     
     tuple t
     {
     {string} set;
     };
     
     {t} res={<s2[k]> | k in r : 2<=card(s2[k])<=card(s)-1};
     
     execute
     {
     writeln(res);  
     }

    will work.

    Regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How to implement powerset?

    Posted 01/31/15 01:41 PM

    Originally posted by: max__x


    Dear Alex, 

    The code you give works, but can you make a small example and explain how to get all the subsets?  actually, I dont understand the code 

    {string} s2 [k in r] = {i | i in s: ((k div (ftoi(pow(2,(ord(s,i))))) mod 2) == 1)};

    Thank you


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: How to implement powerset?

    Posted 01/31/15 02:14 PM

    Hi,

    k is the index of the set between 1 and 2 power card(s)

    And then for all k, I use div and pow in order to turn k into its binary decomposition which will tell for all k and for all i in s whether i should be in the set or not.

    Regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: How to implement powerset?

    Posted 02/19/19 12:32 PM

    Originally posted by: nouira


    Hello Alexis,

    I need to define a subassembly of a range set N={1,..n} on opl such, Q is a subset or equal to N. it is about a subtour elimination in VRP problem.

    I tried this

    {int} s ={1,2,3,4,5,6};
    range r=1.. ftoi(pow(2,card(s)));
    {int} s2 [k in r] = {i | i in s: ((k div (ftoi(pow(2,(ord(s,i))))) mod 2) == 1)};

    Subtour elimination constraint

     forall (s2, k in Vehicles, d in Day)
       sum (i,j in s)x[d][i][j][k]<=card(s)-1;

    but it doesn't works

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: How to implement powerset?



  • 10.  Re: How to implement powerset?

    Posted 02/20/19 02:45 AM

    nouira, are you sure you got this right? At first glance I think you need 's2' instead of 's' in your constraint?


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: How to implement powerset?

    Posted 02/20/19 05:58 AM

    Originally posted by: nouira


    hi

    when i put card(s2) it mention that function card(int) doesn't exist

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: How to implement powerset?

    Posted 02/20/19 06:12 AM

    That points at another problem in your code:

     forall (s2, k in Vehicles, d in Day)
       sum (i,j in s)x[d][i][j][k]<=card(s)-1;

    I think you don't want s2 to be in Vehicles. What you want instead is some subset S of s2, right? Something like this

     forall (S in s2, k in Vehicles, d in Day)
       sum (i,j in S)x[d][i][j][k]<=card(S)-1;

    Did you look at the link that Alex posted? It has spelled out all the details and should be very simple to copy and paste.


    #CPLEXOptimizers
    #DecisionOptimization