简体   繁体   English

尝试使用集合数组对 MiniZinc 设置约束

[英]Trying to set up a constraint on MiniZinc with array of sets

I'm given a question where I'm supposed to create a set of teams, with just a simple constraint where there are two arrays of sets on which two members must be together and which shouldn't.我被问到一个问题,我应该在哪里创建一组团队,只有一个简单的约束,其中有两个 arrays 组,其中两个成员必须在一起,而哪些不应该。 I'm new to Minizinc so I'm having a tough time working with the decision variable with an array of sets.我是 Minizinc 的新手,所以我很难用一组集合来处理决策变量。 Teams must be of size n too.团队的规模也必须为 n。

For example:例如:

GroupsThatMustBePaired = [{1,3},{4,5}]
GroupsThatShouldNot = [{2,3}]

Output = [{1,3},{4,5},{2,6}..etc]

Any help?有什么帮助吗?

Using set variables can be a bit tricky at first, but if you can get back to when you learned about sets in mathematics, then the concepts should be very familiar.一开始使用集合变量可能有点棘手,但如果你能回到你在数学中学习集合的时候,那么这些概念应该非常熟悉。

The following is an example of how you could write you model. It has a few extra constraints to make sure that the "teams" contain everyone, nobody twice, and have a maximum capacity include "all_disjoint.mzn";以下是您如何编写 model 的示例。它有一些额外的约束以确保“团队”包含每个人,两次没有人,并且具有最大容量包括“all_disjoint.mzn”;

set of int: MEMBERS = 1..6;
set of int: GROUPS = 1..3;
array[int] of set of MEMBERS: GroupsThatMustBePaired = [{1,3},{4,5}];
array[int] of set of MEMBERS: GroupsThatShouldNot = [{2,3}];

array[GROUPS] of var set of MEMBERS: teams;

% Team members can only be part of one team
constraint all_disjoint(teams);
% Everyone must be part of a team
constraint array_union(teams) = MEMBERS;
% Maximal number of people per group
constraint forall(g in GROUPS) ( card(teams[g]) <= 2 );

% Eliminate bad groups
constraint forall(g in GROUPS, i in index_set(GroupsThatShouldNot)) (
  not (GroupsThatShouldNot[i] subset teams[g])
);

% Enforce good groups
constraint forall(i in index_set(GroupsThatMustBePaired)) (
  exists(g in GROUPS) (
    GroupsThatMustBePaired[i] subset teams[g]
  )
);

Some notes if you want to change this model: Most solvers do not support set variables directly but translate this model to use boolean variables instead.如果您想更改此 model,请注意以下几点:大多数求解器不直接支持设置变量,而是将此 model 转换为使用 boolean 变量。 This is not necessarily worse, but is something to keep in mind if you feel that the problem could be easier expressed using boolean variables.这不一定更糟,但如果您觉得使用 boolean 变量可以更容易地表达问题,则需要记住这一点。

In this particular case, you might want to consider using integer variables.在这种特殊情况下,您可能需要考虑使用 integer 个变量。 This problem can be seen as an assignment problem, where members are assigned to teams.这个问题可以看作是一个分配问题,其中成员被分配到团队中。 This considers the viewpoint from the team-members instead of the team.这考虑了团队成员而不是团队的观点。 Although this will probably make the subset constraint harder to write, this structure would remove the need for the all_disjoint and the array_union constraints, because we know everyone will be in a team and nobody will be in multiple teams.虽然这可能会使子集约束更难编写,但这种结构将消除对all_disjointarray_union约束的需要,因为我们知道每个人都会在一个团队中,而没有人会在多个团队中。 In this case the card (Cardinality) set constraint could be replace with a global_cardinality_low_up integer constraint.在这种情况下,(基数)集约束可以替换为global_cardinality_low_up integer 约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM