简体   繁体   English

MiniZinc 约束:变量集 = 关联域集

[英]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.我对 MiniZinc 非常陌生,并尝试 model 我觉得应该是一个简单的约束但是我真的很难理解语法和查看各种文档让我更加困惑。

Basically I want to constrain specific sets of variables to specific sets of their associated domains, which in my case is just 0..1.基本上我想将特定的变量集约束到它们相关域的特定集,在我的例子中只是 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) (然后我只想使用solve satisfy;简单地将变量分配给 0..1 以便满足所有约束)

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.正如@sascha 所说,当有很多选择时,使用表约束可能是最好的选择。 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.如果有许多(小)(布尔)表约束,那么对于某些求解器来说,直接使用 Boolean 子句可能更有效。 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];

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

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