简体   繁体   中英

MiniZinc Constraint: set of variables = set of associated domains

I'm very new to MiniZinc and trying to model what I feel should be a simple constraint however I'm really struggling to understand syntax & looking at various documentation left me more confused.

Basically I want to constrain specific sets of variables to specific sets of their associated domains, which in my case is just 0..1. Here's a non-working example of what I want to achieve:

set of int: DOMAIN = 0..1;

var DOMAIN: x11;
var DOMAIN: x12;
var DOMAIN: x13;
var DOMAIN: x21;
var DOMAIN: x22;
var DOMAIN: x23;

% obviously these constraint don't work but this is the gist is what i'm going for
constraint [x11, x12, x13] = [1, 0, 1] \/ [1, 0, 0] \/ [0, 1, 0]; 
constraint [x11, x21] = [1,0] \/ [1,1];
% ... etc (ultimately every variable will appear in 2 constraints and this situation
% represents a grid of 1's and 0's)

(Then I just want to use solve satisfy; to simply assign variables to 0..1 such that all constraints are satisfied)

I apologise for the sloppy explanation but as you can tell I'm clearly very new to this. If anyone could help me formulate these constraints I'd greatly appreciate - I presume I've vastly oversimplified the syntax, not sure if table constraints would be involved?

As @sascha said, using a table constraint is probably the best option when there are many selections. Your constraints could be written as:

constraint table([x11, x12, x13], [|
    1,0,1 |
    1,0,0 |
    0,1,0 |
|]);
constraint table([x11, x21], [|
    1,0 |
    1,1 |
|]);

If there are many (small) (Boolean) table constraints, then it might for some solvers be more efficient to use Boolean clauses directly. The syntax for this is actually very similar to your non-working example:

constraint [x11, x12, x13] = [1, 0, 1] \/ [x11, x12, x13] = [1, 0, 0] \/ [x11, x12, x13] = [0, 1, 0]; 
constraint [x11, x21] = [1,0] \/ [x11, x21] = [1,1];

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