aboutsummaryrefslogtreecommitdiffstats
path: root/cmso-optimizer/scripts/minizinc/generic_attributes.mzn
blob: af38df9b7b03d385c78a85da5e7afc4b55c1474f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% element scheduling problem
%% Modified on Mar 08, 2019
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Required (core) parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Number of elements.
int: numElements;

% Number of loaders/engineers.
int: numLoaders;

% Index of last possible time-slot for scheduling These time slots can be
% minutes, hours, or nights. E.g., if scheduling happens at the granularity of
% nights, 1st night is counted as 1, 2nd night counted as 2, and so on.  OTOH,
% if we have 6 hours each night and we are scheduling at the granularity of
% hours, 1st hour of 1st night is 1, 1st hour of 2nd night is 7, and so on.
int: maxTime;

% TRUE, if i-th element does NOT have a conflict on j-th timeslot/night.
array[1..numElements, 1..maxTime] of bool: noConflict;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Optional parameters (defined via policy)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Each attribute is composed of 3 parameters that must be supplied. When an
% attribute is selected, all parameters related to it must be given.

%%%%%%%%%%%%%%%%%%%%
% Timeslots / nights capacities
%%%%%%%%%%%%%%%%%%%%

% Maximum number of elements that can be scheduled on j-th timeslot/night.
array[1..maxTime] of 0..numElements: elementSlotCapacity;

%%%%%%%%%%%%%%%%%%%%
% Loader capacities
%%%%%%%%%%%%%%%%%%%%

% Maximum number of elements that can be assigned to the j-th loader per
% timeslot/night.
array[1..numLoaders, 1..maxTime] of 0..numElements: loaderCapacity;

%%%%%%%%%%%%%%%%%%%%
% Attribute matrix
%%%%%%%%%%%%%%%%%%%%

% Number of attributes for wach node, e.g., hardware, software, oss, market,
% pool, etc.
int: numAttributes;

% Assume that i-th attribute has a range 0..attribute_range[i].
% Assume that 0 indicates NA. E.g, we may have pools and a node
% that does not belong to any pools  will have 'pool attribute = 0
% when we write constraints, we only consider attribute values >= 1.
array[1..numAttributes] of int: attributesRange;

% The attribute matrix that holds values of each attribute for a element.
array[1..numElements, 1..numAttributes] of int: attributes;

% Maximum number of nodes per time-slot that match on a given attribute.
array[1..numAttributes, 1..maxTime] of int: attributeConcurrencyLimit;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% TRUE, if i-th element gets scheduled on j-th night.
array[1..numElements, 1..maxTime] of var bool: ELEMENT2TIMESLOT;

% TRUE, if i-th element gets scheduled to j-th loader.
array[1..numElements, 1..numLoaders] of var bool: ELEMENT2LOADER;

% Indicates the time slot (nigth) in which the elements were scheduled.
array[1..numElements] of var 0..maxTime: SCHEDULED_SLOT =
    [sum(j in 1..maxTime)(j * bool2int(ELEMENT2TIMESLOT[i,j])) | i in 1..numElements];

% Indicates the loader for which the elements were scheduled.
array[1..numElements] of var 0..numLoaders: SCHEDULED_LOADER =
    [sum(l in 1..numLoaders)(l * bool2int(ELEMENT2LOADER[i,l])) | i in 1..numElements];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constraints
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Required (core) constraints
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Schedule only on noConclict nights.
constraint
forall(i in 1..numElements, j in 1..maxTime)(
    ELEMENT2TIMESLOT[i,j] -> noConflict[i,j]
);

% Schedule a element to a loader
constraint
forall(i in 1..numElements)(
    sum(t in 1..maxTime)(bool2int(ELEMENT2TIMESLOT[i,t])) ==
    sum(l in 1..numLoaders)(bool2int(ELEMENT2LOADER[i,l]))
);

% Schedule each element exactly one timeslot/night (or none).
constraint
forall(i in 1..numElements)(
    sum(j in 1..maxTime)(bool2int(ELEMENT2TIMESLOT[i,j])) <= 1
);

% Schedule each element exactly one loader (or none).
constraint
forall(i in 1..numElements)(
    sum(j in 1..numLoaders)(bool2int(ELEMENT2LOADER[i,j])) <= 1
);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Optional constraints (defined via policy)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%These constraints are defined via policy and require sets of parameters also
%defined via policy. They must be both available.

%%%%%%%%%%%%%%%%%%%%
% General capacity constraints
%%%%%%%%%%%%%%%%%%%%

% Satisfy element timeslot/nightly capacity.
constraint
forall(j in 1..maxTime)(
    sum(i in 1..numElements)(bool2int(ELEMENT2TIMESLOT[i,j])) <= elementSlotCapacity[j]
);

% Satisfy loader/timeslot capacity.
constraint
forall(l in 1..numLoaders, t in 1..maxTime)(
    sum(i in 1..numElements)(
        bool2int(ELEMENT2LOADER[i,l] /\ ELEMENT2TIMESLOT[i,t])
    ) <= loaderCapacity[l,t]
);

%%%%%%%%%%%%%%%%%%%%
% Attribute capacity constraints
%%%%%%%%%%%%%%%%%%%%

% For attribute a and timeslot/night t, limits the number of elements having
% the same value for attribute k, scheduled in the same timeslot/night t.
constraint
forall(t in 1..maxTime, a in 1..numAttributes)(
    forall(m in 1..attributesRange[a])(
        sum(i in 1..numElements)(
            bool2int(attributes[i,a] == m /\ ELEMENT2TIMESLOT[i,t])
        ) <= attributeConcurrencyLimit[a,t]
    )
);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Objective function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Computes the number of scheduled elements.
var int: NUM_SCHEDULED = sum(i in 1..numElements)(bool2int(SCHEDULED_SLOT[i] > 0));

% Computes the (average) completion time of all elements. Note that average is
% just a simple division by a constant, and it can be dropped from the model
% for robusteness.
var int: TOTAL_COMPLETION_TIME = sum(i in 1..numElements)(SCHEDULED_SLOT[i]);

% First, maximize the number of scheduled elements (using a heavy weight)
% and then minimize the (average) completion time for all elements.
solve maximize maxTime * numElements * NUM_SCHEDULED - TOTAL_COMPLETION_TIME;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

output
["results:"] ++
["\n  -"] ++
["\n    num_scheduled: " ++ show(NUM_SCHEDULED)] ++
["\n    total_completion_time: " ++ show(TOTAL_COMPLETION_TIME)] ++
["\n    element_slot_loader: |"] ++
[
 "\n      " ++ show(element) ++ "," ++ show(SCHEDULED_SLOT[element]) ++ "," ++
 show(SCHEDULED_LOADER[element])
| element in 1..numElements
]

%output
%["\n - num_scheduled: " ++ show(NUM_SCHEDULED)] ++
%["\n total_completion_time: " ++ show(TOTAL_COMPLETION_TIME)] ++
%["\n elementSlotLoader |"] ++
%[ "\n " ++ show(element) ++ "," ++
%show(SCHEDULED_SLOT[element]) ++ "," ++ show(SCHEDULED_LOADER[element]) | element in
%1..numElements ]