3

I'd like to check which is the number between 1 to 5 that not occurs in the array group and put this number (or numbers) in an other array.

g=2;

set of int: GROUPS = 1..g; 

groups = [{1, 3}, {2,5}];

p=5;

set of int: PEOPLE = 1..p;  

I tried in this way but it doesn't work.

int: peopleInGroup= (g*g);
set of int: INGROUP = 1..peopleInGroup;
array [INGROUP] of var int: inGroup;  

int: peopleNotGroup= c-(g*g);
set of int: NOTGROUP = 1..peopleNotGroup;
array [NOTGROUP] of var int: notGroup;     


constraint forall(i in groups,person in i) (if sum(j in PEOPLE-1)  (i==person then inGroup[i]=person else notGroup[i]=person  endif)); 
P.ru
  • 93
  • 1
  • 13

1 Answers1

1

In case groups is a literal -as in your question-, then you can use set and list comprehensions -described at page 22 of this tutorial- to achieve your goal.

e.g.

set of int: DOM = 1..5;
set of int: population = DOM;
array[1..2] of set of DOM: groups = [{1, 3}, {2, 5}];


% array initialization

array [int] of var DOM: in_array =
               [i | i in DOM where exists(g in groups) (i in g)];
array [int] of var DOM: out_array =
               [i | i in DOM where not exists(g in groups) (i in g)];

% set initialization

var set of DOM: in_set =
               {i | i in DOM where exists(g in groups) (i in g)};
var set of DOM: out_set =
               {i | i in DOM where not exists(g in groups) (i in g)};


solve satisfy;

output [
    "in_set=", show(in_set), "\n",
    "out_set=", show(out_set), "\n",
    "in_array=", show(in_array), "\n",
    "out_array=", show(out_array), "\n"
];

note: var can be dropped from definition of all variables, here I use it only because flatzinc would otherwise not print their content on standard output.

The output is:

~$ mzn2fzn example.mzn ; flatzinc example.fzn
in_array = array1d(1..4, [1, 2, 3, 5]);
in_set = {1, 2, 3, 5};
out_array = array1d(1..1, [4]);
out_set = {4};
----------

Here is the generated intermediate flatzinc model:

array [1..2] of set of int: groups = [{1, 3}, {2, 5}];
array [1..4] of var 1..5: in_array :: output_array([1..4]) = [1, 2, 3, 5];
var set of 1..5: in_set :: output_var = {1, 2, 3, 5};
array [1..1] of var 4..4: out_array :: output_array([1..1]) = [4];
var set of 1..5: out_set :: output_var = 4..4;
solve satisfy;
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40