gpt4 book ai didi

constraints - 检查数组中是否存在元素#minizinc

转载 作者:行者123 更新时间:2023-12-05 06:37:55 25 4
gpt4 key购买 nike

我想检查 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));

最佳答案

如果 groups 是一个文字 -如您的问题-,那么您可以使用 set 和 list comprehensions -描述于 this tutorial 的第 22 页- 实现您的目标。

例如

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;

关于constraints - 检查数组中是否存在元素#minizinc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com