如何在MiniZinc中求和集合的元素



我想创建一个将活动划分为集合的分区。每个活动都有一个允许其发生的时间段。我定义了可变数组调度和相关约束:

int: nb_act = 6; % number of activities
set of int: window_1 = {1,2,3}; % time slots (sets) in which activity 1 (for instance) can occur
array[1..nb_act] of var 0..nb_act: day_cost; % Total cost of the activities scheduled on each day
array[1..nb_act] of var set of ACTIVITY: Schedule; % Partitioning of the activities in 6 sets.
% Each set represents a time slot and there is no min or max on the number of activities that can occur in a time slot (set).
% Extremes are all activities on 1 day, or each day having 1 activity exactly.
var 0..nb_act: day_cost_1;
constraint partition_set([Schedule[i] | i in ACTIVITY], ACTIVITY);
constraint Schedule[1] subset window_1; % The time slot of activity 1 is is in window 1
constraint 
day_cost_1 = sum(Schedule[1]); % Error because Schedule[1] is a set, I suppose.

我现在想总结每天的活动(稍后这些将是他们的成本(。但是,内置函数"sum"只允许对数组求和。有变通办法吗?

查看要编译和测试的完整模型会有所帮助。

表达式sum(Schedule[1])不正确。sum()确实需要一个数组作为参数。

可以采用以下方式:

constraint
day_cost_1 = sum([s * (s in Schedule[1]) | s in ACTIVITY]);

最新更新