-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment.py
executable file
·279 lines (242 loc) · 9.21 KB
/
assignment.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
import json
import sys
from docplex.cp.function import CpoSegmentedFunction, CpoStepFunction
from docplex.cp.model import CpoModel
from docplex.cp.parameters import CpoParameters
import docplex.cp.utils_visu as visu
if len(sys.argv) != 2:
print('Filename should be given as first parameter!')
exit(-1)
filename = sys.argv[1]
with open(filename, 'r') as f:
data = json.load(f)
TIMESLOTS = int((24*60)/data['time_resolution'])
NUM_TASKS = len(data['tasks'])
NUM_RESOURCES = data['resources']
NUM_MACHINES = len(data['machines'])
energy_prices = data['energy_prices']
# Create energy function
# It can be evaluated at start and end of intervals
# so the accumulated value is added to the function
energy_sum_array = []
energy_sum_all = 0
assert len(energy_prices) == TIMESLOTS
for i in range(TIMESLOTS):
energy_sum_all += energy_prices[i]
energy_sum_array.append(energy_sum_all)
# Ensure the energy sum is defined to the end
energy_sum_array.append(energy_sum_all)
# This array contains duration i+1 energy sums at the
# ith position and is used for fixed durations
energy_intervals_array = []
for i in range(1, TIMESLOTS):
energy_intervals = CpoSegmentedFunction()
energy_sum = 0
for j in range(i):
energy_sum += energy_prices[j]
energy_intervals.set_value(0, 1, energy_sum)
energy_sum_array.append(energy_sum)
for j in range(i, TIMESLOTS):
energy_sum -= energy_prices[j-i]
energy_sum += energy_prices[j]
energy_intervals.set_value(j-i+1, j-i+2, energy_sum)
energy_intervals_array.append(energy_intervals)
model = CpoModel()
# Function is positive when a machine is on
# This should be also a state function but it cannot be well defined
# with optional intervals as machine intervals
machine_on_off = {m['id']: model.step_at(0, 0) for m in data['machines']}
# State is 1 when a task is running on a machine
tasks_running_on_machines = {
m['id']: model.state_function(name='tasks_on_machines_{}'.format(m['id']))
for m in data['machines']
}
# Denotes the intervals when a machine is ON
on_intervals = {
m['id']: model.interval_var_list(
min(TIMESLOTS, NUM_TASKS),
start=(0, TIMESLOTS),
end=(0, TIMESLOTS),
size=(1, TIMESLOTS),
name='machine_{}'.format(m['id']),
optional=True)
for m in data['machines']
}
# Store the current used capacity of every machine resource
machine_resources = {
m['id']: [model.step_at(0, 0) for i in range(NUM_RESOURCES)]
for m in data['machines']
}
# Phase 1
#
# Cost of tasks is calculated here without concerning ourselves
# about the machine costs.
task_intervals_on_machines = {m['id']: [] for m in data['machines']}
task_intervals = []
for machine in data['machines']:
m_id = machine['id']
# Sequencing of intervals
num_intervals = len(on_intervals[m_id])
for i in range(1, num_intervals):
model.add(model.if_then(
model.presence_of(on_intervals[m_id][i]),
model.presence_of(on_intervals[m_id][i-1])))
model.add(model.end_before_start(
on_intervals[m_id][i-1],
on_intervals[m_id][i],
1))
# Bind with task intervals
for on_i in on_intervals[m_id]:
machine_on_off[m_id] += model.pulse(on_i, 1)
model.add(model.always_constant(
tasks_running_on_machines[m_id],
on_i, True, True))
for task in data['tasks']:
# Master task interval
task_interval = model.interval_var(
size=task['duration'],
start=(task['earliest_start_time'],
task['latest_end_time']-task['duration']),
end=(task['earliest_start_time']+task['duration'],
task['latest_end_time']),
name='task_{}'.format(task['id']))
# Optional task intervals that belong to each machine
task_machine_intervals = model.interval_var_list(
NUM_MACHINES,
name='task_{}_on_'.format(task['id']),
optional=True)
for i in range(NUM_MACHINES):
m_id = data['machines'][i]['id']
# Bind with machine switched on intervals
model.add(model.always_equal(
tasks_running_on_machines[m_id],
task_machine_intervals[i],
1))
model.add(model.always_in(
machine_on_off[m_id],
task_machine_intervals[i],
1, 1))
# Add resource usage by task
for j in range(NUM_RESOURCES):
machine_resources[m_id][j] += model.pulse(
task_machine_intervals[i],
task['resource_usage'][j])
# For visualization
task_intervals_on_machines[m_id].append(
(task, task_machine_intervals[i]))
# Only one interval will be effective
model.add(model.alternative(task_interval, task_machine_intervals))
task_intervals.append((task, task_interval))
# Add power consumption by tasks
cost_tasks = model.sum([
model.start_eval(task_int, energy_intervals_array[task['duration']-1]) *
task['power_consumption'] for task, task_int in task_intervals
])
# Add resource capacity constraints
for machine in data['machines']:
m_id = machine['id']
for j in range(NUM_RESOURCES):
model.add(machine_resources[m_id][j] <=
machine['resource_capacities'][j])
# Add minimize constraint
model.add(model.minimize(cost_tasks))
msol = model.solve(
params=CpoParameters(
TimeLimit=300,
SearchType='IterativeDiving',
LogVerbosity='Terse'
),
trace_log=True)
msol.print_solution()
# Phase 2
#
# This phase starts from a minimal (or relatively minimal,
# depending on the timeout of the first phase) solution
# that will be further minimized with the additional machine
# costs.
# This solution is consistent with the second phase,
# set it as starting point
model.set_starting_point(msol.get_solution())
bounds = msol.get_objective_bounds()
model.remove(cost_tasks)
cost_overall = cost_tasks
for machine in data['machines']:
m_id = machine['id']
# Add machine idle consumption
cost_overall += model.sum([
(model.element(energy_sum_array, model.end_of(on_i)) -
model.element(energy_sum_array, model.start_of(on_i))) *
machine['idle_consumption'] for on_i in on_intervals[m_id]
])
# Add power up/down cost
on_off_cost = machine['power_up_cost'] + machine['power_down_cost']
cost_overall += model.sum([
on_off_cost * model.presence_of(on_interval)
for on_interval in on_intervals[m_id]
])
# We can be sure that the lower bound of
# first phase is a lower bound of this phase
model.add(cost_overall >= bounds[0])
model.add(model.minimize(cost_overall))
msol = model.solve(
params=CpoParameters(
TimeLimit=300,
SearchType='IterativeDiving',
LogVerbosity='Terse'
),
trace_log=True)
msol.print_solution()
# Draw solution
if msol and visu.is_visu_enabled():
for m in data['machines']:
m_id = m['id']
ons = []
cost_sum = 0
energy_costs = CpoStepFunction()
j = 0
# Add machine intervals
for interval in on_intervals[m_id]:
val = msol.get_value(interval)
if val != ():
ons.append((msol.get_var_solution(interval),
j, interval.get_name()))
j += 1
start_value = energy_sum_array[val[0]]
end_value = energy_sum_array[val[1]]
cost_sum += (end_value - start_value) * m['idle_consumption']
cost_sum += m['power_up_cost'] + m['power_down_cost']
# Add segments to cost function
for i in range(val[0], val[1]):
cost_i = energy_prices[i] * m['idle_consumption']
energy_costs.add_value(i, i+1, cost_i)
# Add task intervals
tasks = []
for task, interval in task_intervals_on_machines[m_id]:
val = msol.get_value(interval)
if val != ():
tasks.append((msol.get_var_solution(interval),
1, interval.get_name()))
cost_sum += energy_intervals_array[val[2]-1].get_value(val[0]) * task['power_consumption']
# Add segments to cost function
for i in range(val[0], val[1]):
cost_i = energy_prices[i] * task['power_consumption']
energy_costs.add_value(i, i+1, cost_i)
# Do not show this machine if no task if assigned to it
if tasks and ons:
visu.timeline("Machine " + str(m_id), 0, int(TIMESLOTS))
visu.panel("Tasks")
visu.sequence(name='Machine', intervals=ons)
visu.sequence(name='Tasks', intervals=tasks)
visu.function(name='Cost={}'.format(cost_sum),
segments=energy_costs)
for j in range(NUM_RESOURCES):
visu.panel('resources_{}'.format(j))
res = CpoStepFunction()
for task, interval in task_intervals_on_machines[m_id]:
val = msol.get_value(interval)
if val != ():
res.add_value(val[0], val[1],
task['resource_usage'][j])
visu.function(segments=res, color=j)
visu.show()