Minizinc随机播放列表,学习Minizinc

问题描述

array[1..6] of var 0..1: Path;
include "alldifferent.mzn";
constraint
forall(j in 1..6)(
alldifferent(i in 1..6)(Path[i])
)

我正在尝试将清单改编成minizinc,但我每次都希望获得与众不同的结果,就像使用a for all一样。我该怎么做? 打印此:

Path = array1d(1..6,[5,4,3,2,1,0]);

解决方法

至少有两种方法可以生成随机矩阵,具体取决于您是否要生成所有可能的变量(下面的第一个模型使用决策值),或者是否只想使用“随机”随机矩阵(第二种)使用内置随机数发生器的模型)。 (第三种方法是您编写自己的随机生成器,但这只是练习:-))。

这是一个简单的MiniZinc模型,它生成{0,1}的所有可能的6x6矩阵作为决策变量。

int: n = 6;
array[1..n,1..n] of var 0..1: x;
solve :: int_search(array1d(x),first_fail,indomain_random) satisfy;

constraint
  true
;

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

注意:indomain_random启发式方法以更“类似于随机”的顺序生成解决方案。

还有另一种方法,使用bernoulli(0.5)函数,该函数在创建模型期间随机生成0或1,即不是决策变量:

int: n = 6;
array[1..n,1..n] of int: x = array2d(1..n,1..n,[ bernoulli(0.5) | i,j in 1..n]);

solve satisfy;

constraint
  true
;

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

恰好会生成以下矩阵:

1 1 1 0 0 0
1 0 1 1 0 0
0 0 0 0 1 1
0 1 0 0 1 0
1 1 1 1 0 1
0 1 1 1 1 1

这样做的缺点是您必须手动为随机生成器添加种子,以生成不同的矩阵。 (根据https://www.minizinc.org/doc-2.5.1/en/command_line.html?highlight=random#cmdoption-r)是通过--random-seed i标志(或-r i)完成的,但是在我的MiniZinc版本上目前不起作用。

MiniZinc有很多随机生成器,请点击此处查看更多信息:https://www.minizinc.org/doc-2.5.1/en/lib-stdlib.html?highlight=random#random-number-generator-builtins