簡體   English   中英

Minizinc:根據陣列位置設置枚舉選項集,而不是所有陣列位置的選項集

[英]Minizinc: set enumerated set of options based on array position, as opposed to set of options for all array positions

我熟悉這樣的方法

set of int: RANGE = 1..5;
enum OPTS = {A,B,C};
array[RANGE] of var OPTS = result;

但是當我想為RANGE每個位置指定OPTS呢? 看起來像的東西

[{A,B,C},{A,B},{A,B,C,D},{B,D},{A}]

然后生成result[n]以便它選擇n處的可用選項之一。

我已嘗試使用以下模型,但檢測到模型不一致。

set of int: RANGE = 1..5;
enum OPTS = {A,B,C,D};
array[RANGE] of set of OPTS: t = [{A,B,C},{A,B},{A,B,C,D},{B,D},{A}];
array[RANGE] of var OPTS: result;
constraint forall(i in RANGE)(
  forall(j in t[i])(
    result[i] = j
  )
);

output [show(result)]

可能的結果是[B,A,B,D,A] - 最后一個位置result[5]不能是A以外A任何其他內容。

我嘗試使用 Minizinc 手冊,但我無法解讀如何在規范中使用示例,例如https://www.minizinc.org/doc-2.4.3/en/spec.html#set-operations ,它以其他知識為前提(例如,如何解釋語法),我可能會花費無數個小時試圖在我的水平上尋找解釋,因為搜索這些東西似乎真的沒有成果。

謝謝!

如果我理解正確,你就可以使用set of OPTS構建的陣列OPTS ,這里所謂的t (你不能把它的output ,因為它是一個保留字)。 請注意,當您提出問題時,它不是var而是常量數組,因此在構造數組時不需要var關鍵字。

set of int: RANGE = 1..5;
enum OPTS = {A,B,C,D};
array[RANGE] of set of OPTS: t = [{A,B,C},{A,B},{A,B,C,D},{B,D},{A}];

更新

這是更新問題的模型。 這里的技巧是t[i]定義了result[i]的有效域,您可以簡單地使用in來實現這一點。

set of int: RANGE = 1..5;
enum OPTS = {A,B,C,D};
array[RANGE] of set of OPTS: t = [{A,B,C},{A,B},{A,B,C,D},{B,D},{A}];
array[RANGE] of var OPTS: result;

constraint 
  forall(i in RANGE) (
     % ensure that result[i] only takes the values in t[i]
     result[i] in t[i]
  )
;

output [show(result)]

這個問題有48種解決方案,例如:

[A, A, A, B, A]
----------
[B, A, A, B, A]
----------
[C, A, A, B, A]
----------
[A, B, A, B, A]
----------
[B, B, A, B, A]
----------
[C, B, A, B, A]
----------
[A, A, B, B, A]
----------
...

暫無
暫無

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

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