1

新的 MiniZinc 用户在这里......我在理解计数约束的语法时遇到了问题:

predicate exactly(int: n, array[int] of var int: x, int: v)

“需要 x 中的 n 个变量来取值 v。”

我想确保我的 10r x 30c 数组中的每一列至少各有一个 1,2 和 3,其余 7 行等于 0。

如果我将我的数组声明为

array[1..10,1..30] of var 0..3: s;

我怎样才能根据需要准确地使用谓词来填充它?谢谢!

4

1 回答 1

2

好吧,“确切”约束在这里不是很有用,因为您希望至少出现一次 1、2 和 3。最好使用以下count函数:

include "globals.mzn"; 
array[1..10,1..30] of var 0..3: s;
solve satisfy;
constraint
  forall(j in 1..30) (
     forall(c in 1..3) (
        count([s[i,j] | i in 1..10],c) >= 1
     )
  )
;

output [
  if j = 1 then "\n" else " " endif ++
    show(s[i,j])
  | i in 1..10, j in 1..30 
];

您不必对 0 做任何事情,因为域是 0..3,并且所有不是 1、2 或 3 的值都必须为 0。

另一个约束是“at_least”,参见https://www.minizinc.org/2.0/doc-lib/doc-globals-counting.html

如果您还没有阅读MiniZinc教程 ( https://www.minizinc.org/downloads/doc-latest/minizinc-tute.pdf ),我强烈建议您阅读。本教程教您如何思考约束编程,当然还有MiniZinc.

于 2015-07-09T13:45:18.857 回答