optimizationsettuplescplexilog

Generating all subsets in CPLEX (subtour elimination)


Currently I am trying to model the Dantzig Fulkerson Johnson Subtour elimination constraints in CPLEX.

I want to generate all the possible subsets of customers {"1", "2", "3", "4"} in ILOG Script.

For this, first I created a tuple structure that contains a set of strings where all the customers involved in a subtour can be stored:

tuple subtour {
    {string} customers;
 }

Now, I can create a set of subtour where I can store all the subtours:

{subtour} subtours; 

I create an empty set of strings to help out in the execute-block:

 {string} emptySet;

Now I create my execute-block: (In this first step, I am just trying to fill subtours with the subtours containing just one customer as a first test, e.g. in the first iteration subtours = {<{"1"}>, <{"2"}>,...}

execute FillSubtours {
   for(var i in customers) {
     emptySet.add(i);
     subtours.add(emptySet);
     writeln(subtours);
     emptySet.clear();
   }     
 }

So after this procedure, the goal is to have subtours like this:

 subtours = {<{"1"}>, <{"2"}>, <{"3"}>, <{"4"}>}

Containing all the possible subtours with one element.

Sadly it looks the following:

subtours = {<{"4"}>}

Does anyone know where this went wrong and how I can fix it?


Solution

  • Within how to with OPL you could have a look at powerset

    and then you can rewrite your model into

    {string} customers= {"1", "2", "3", "4"} ;
    
    tuple subtour {
        {string} customers;
     }
     
     
     
     range r=1.. ftoi(pow(2,card(customers)));
     {string} s2 [k in r] = {i | i in customers: 
     ((k div (ftoi(pow(2,(ord(customers,i))))) mod 2) == 1)};
     
     
     {subtour} subtours={<s2[k]> | k in r};
     
     execute
     {
       writeln(subtours);
     }
    

    which gives

    {<{"1"}> <{"2"}> <{"1" "2"}> <{"3"}> <{"1" "3"}> <{"2" "3"}>
         <{"1" "2" "3"}> <{"4"}> <{"1" "4"}> <{"2" "4"}>
         <{"1" "2" "4"}> <{"3" "4"}> <{"1" "3" "4"}> <{"2" "3" "4"}>
         <{"1" "2" "3" "4"}> <{}>}