我试图编写一个谓词来执行与circuit
相同的操作,但忽略数组中的零,我不断收到以下错误:MiniZinc:类型错误:预期`int [int] int,实际`array [int] var var int
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的建议,认为这是具有可变长度数组的问题,我用"the usual workaround"保持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
错误告诉x_without_0是VAR int数组,你把它定义为INT这就是为什么它失败的数组。也许你还想将lbx和ubx定义为var ints? – Emilien
@Emilien不错的想法,但不幸的是,当我将x_without_0的类型更改为var int的'array [int],将lbx/ubx更改为'var int'时,该错误仅变为'expected'var [int] int',实际'var opt int''的数组[int]。 – UnderwaterKremlin
是的,事实上,定义x_without_0的公式使其成为可选var整数的数组。我不是很了解这个[新功能](http://www.minizinc.org/2.0/doc-lib/doc-optiontypes.html)([出版物](http://people.eng.unimelb。 edu.au/pstuckey/papers/mznopt.pdf)) – Emilien