简体   繁体   中英

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. I'm new to Minizinc so I'm having a tough time working with the decision variable with an array of sets. Teams must be of size n too.

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";

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. 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.

In this particular case, you might want to consider using integer variables. 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. In this case the card (Cardinality) set constraint could be replace with a global_cardinality_low_up integer constraint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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