简体   繁体   English

Minizinc:根据阵列位置设置枚举选项集,而不是所有阵列位置的选项集

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

I am familiar with such approaches as我熟悉这样的方法

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

but what about when I would like to specify the OPTS for each position in RANGE ?但是当我想为RANGE每个位置指定OPTS呢? Something that looks like看起来像的东西

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

then generate result[n] so that it picks one of the available options at n .然后生成result[n]以便它选择n处的可用选项之一。

I've tried with the following model but a model inconsistency is detected.我已尝试使用以下模型,但检测到模型不一致。

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)]

with a possible result being [B,A,B,D,A] - the last position result[5] cannot be anything other than A .可能的结果是[B,A,B,D,A] - 最后一个位置result[5]不能是A以外A任何其他内容。

I try to use the Minizinc manuals but I cannot decipher how to use examples within the specifications eg https://www.minizinc.org/doc-2.4.3/en/spec.html#set-operations , which presuppose other knowledge (eg how to interpret the syntax) that I could spend countless hours trying to hunt down an explanation for on my level, since searching this stuff seems to be really unfruitful.我尝试使用 Minizinc 手册,但我无法解读如何在规范中使用示例,例如https://www.minizinc.org/doc-2.4.3/en/spec.html#set-operations ,它以其他知识为前提(例如,如何解释语法),我可能会花费无数个小时试图在我的水平上寻找解释,因为搜索这些东西似乎真的没有成果。

Thanks!谢谢!

If I understand you correctly you can use set of OPTS to construct the array of OPTS , here called t (you cannot call it output since it's a reserved word).如果我理解正确,你就可以使用set of OPTS构建的阵列OPTS ,这里所谓的t (你不能把它的output ,因为它是一个保留字)。 Note that as you presented the problem, it's not a var but a constant array so you don't need the var keyword when you construct the array.请注意,当您提出问题时,它不是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}];

Update更新

Here's a model for the updated question.这是更新问题的模型。 The trick here is that t[i] defines the valid domain of result[i] , and you can simply use in for this.这里的技巧是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)]

There are 48 solutions to this problem, such as:这个问题有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