简体   繁体   English

MiniZinc为数组中的每个Var分配不同的域

[英]MiniZinc Assign Different Domain to Each Var in Array

I have an array: array[backpacks] of int: capacity specifying the capacity of each backpack. 我有一个数组: array[backpacks] of int: capacity指定每个背包的容量。

Now I want to create an array of variables which the constraint solver will have to satisfy. 现在,我想创建约束求解器必须满足的变量数组。 I want each variable to take values in domain 1..capacity where capacity corresponds to the one specified in the above array. 我希望每个变量取域1..capacity中的值,其中容量对应于以上数组中指定的容量。

Would something like this work: array[backpacks] of var capacity: bagcaps ? 这样的事情会工作: array[backpacks] of var capacity: bagcaps吗?

Or do I have to do something like: array[backpacks] of var 1..MAX: bagcaps 还是我必须做些类似的事情: array[backpacks] of var 1..MAX: bagcaps

and then add constraints: constraint forall(i in backpacks) bagcaps[i] <= capacity[i] ? 然后添加约束: constraint forall(i in backpacks) bagcaps[i] <= capacity[i]

Thank you. 谢谢。

There is no short cut to restrict the domain of specific element in the array declaration. 没有任何捷径可以限制数组声明中特定元素的范围。 The traditional version is the one you wrote last: 传统版本是您最后写的版本:

constraint forall(i in backpacks) bagcaps[i] <= capacity[i]);

However, you can make this as an predicate (and place it in a separate file which is then imported into the model with include ). 但是,您可以将此作为谓词(并将其放置在一个单独的文件中,然后使用include将其导入到模型中)。 Eg some thing like this: 例如这样的事情:

set of int: backpacks = 1..6;
array[backpacks] of int: capacity = [10,4,3,7,5,3];
array[backpacks] of var 1..max(capacity): bagcaps;

solve satisfy;

predicate restrict_domains(array[int] of var int: x, array[int] of int: d) =
        forall(i in index_set(x)) ( x[i] <= d[i] );

constraint
     % forall(i in backpacks) ( bagcaps[i] <= capacity[i] ) % original
     restrict_domains(bagcaps,capacity)
;

% output [];

Note that you must still use restrict_domains as a constraint. 请注意,您仍然必须使用restrict_domains作为约束。 And I recommend that you always restrict the domain in the declaration as much as possible, ie use the declaration using var 1..max(capacity) instead of var int . 并且我建议您始终尽可能限制声明中的域,即使用声明使用var 1..max(capacity)而不是var int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM