Waldo
Waldo

Reputation: 13

How to sum elements of a set in MiniZinc

I want to create a partitioning of activities into sets. Each activity has a time slot in which it is allowed to occur. I defined the variable array Schedule and associated constraints:

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.

I now want to sum the activities on each day (later these will be their cost). However the build-in function 'sum' only allows for arrays to be summed. Is there a workaround?

Upvotes: 1

Views: 990

Answers (1)

Axel Kemper
Axel Kemper

Reputation: 11322

It would help to see a complete model to compile and test.

The expression sum(Schedule[1]) is incorrect. sum() does expect an array as parameter.

The following could be intended:

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

Upvotes: 2

Related Questions