5

我正在尝试编写一个执行与 相同的操作circuit但忽略数组中的零的谓词,并且我不断收到以下错误:

MiniZinc: type error: initialisation value for 'x_without_0' has invalid type-inst: expected 'array[int] of int', actual 'array[int] of var opt int'

在代码中:

% [0,5,2,0,7,0,3,0] -> true
% [0,5,2,0,4,0,3,0] -> false (no circuit)
% [0,5,2,0,3,0,8,7] -> false (two circuits)
predicate circuit_ignoring_0(array[int] of var int: x) =
  let { 
        array[int] of int: x_without_0 = [x[i] | i in 1..length(x) where x[i] != 0],
        int: lbx = min(x_without_0),
        int: ubx = max(x_without_0),
        int: len = length(x_without_0),
        array[1..len] of var lbx..ubx: order
  } in
  alldifferent(x_without_0) /\
  alldifferent(order) /\

  order[1] = x_without_0[1] /\ 
  forall(i in 2..len) (
     order[i] = x_without_0[order[i-1]]
  )
  /\  % last value is the minimum (symmetry breaking)
  order[ubx] = lbx
;

我正在使用 MiniZinc v2.0.11

编辑

根据 Kobbe 的建议,这是一个可变长度数组的问题,我使用“通常的解决方法”来保持order数组与原始数组的大小相同x,并使用参数 ,nnonzeros来跟踪数组的部分我关心:

set of int: S = index_set(x),
int: u = max(S),
var int: nnonzeros = among(x, S),
array[S] of var 0..u: order
4

1 回答 1

2

这种回答你的问题:

您遇到的问题是您的数组大小取决于var. 这意味着 MiniZinc 无法真正知道应该创建的数组的大小和使用的opt类型。opt如果您不知道如何处理它,我建议您远离该类型。

通常,解决方案是在您的数组不依赖于var. 我的解决方案通常是填充数组,即[2,0,5,0,8] -> [2,2,5,5,8],如果应用程序允许,或者

var int : a;
[i * bool2int(i == a) in 1..5]

如果您对答案中的零表示满意(我想在这种情况下不是)。

此外,alldifferent_except_0您可能会感兴趣,或者至少您可以看看如何alldifferent_except_0用答案中的零来解决问题。

predicate alldifferent_except_0(array [int] of var int: vs) =
forall ( i, j in index_set(vs) where i < j ) ( 
    vs[i]!=0 /\ vs[j]!=0 -> vs[i]!=vs[j] 
)

来自MiniZinc 文档

于 2016-02-17T19:06:04.397 回答