MiniZinc:类型错误:预期的`array[int] of int',实际的`array[int] of var opt int

Und*_*lin 5 minizinc

我正在尝试编写一个与 执行相同操作的谓词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
;
Run Code Online (Sandbox Code Playgroud)

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

Kob*_*bbe 3

这样回答你的问题:

您遇到的问题是您的数组大小取决于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]
Run Code Online (Sandbox Code Playgroud)

如果您同意答案中的零(我想在这种情况下不行)。

此外,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] 
)
Run Code Online (Sandbox Code Playgroud)

来自MiniZinc 文档