簡體   English   中英

MiniZinc:類型錯誤:期望`int的'array [int],var opt int的實際`array [int]

[英]MiniZinc: type error: expected `array[int] of int', actual `array[int] of var opt int

我試圖寫一個執行與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

編輯

Per 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

這樣回答你的問題:

您遇到的問題是您的數組大小取決於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文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM