我想检查数组中没有出现的1到5之间的数字,并将这个数字(或数字)放在另一个数组中。
g=2;
set of int: GROUPS = 1..g;
groups = [{1, 3}, {2,5}];
p=5;
set of int: PEOPLE = 1..p;
我试过这种方式,但它没有用。
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));
答案 0 :(得分:1)
如果groups
在您的问题中是文字 -as - ,那么您可以使用set和list comprehensions < em> - 在this tutorial - 的第22页描述,以实现您的目标。
<强> 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"
];
注意: var
可以从所有变量的定义中删除,这里我只使用flatzinc
否则不会在标准输出上打印其内容。
输出结果为:
~$ 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};
----------
以下是生成的中间flatzinc
模型:
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;