-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy.py
313 lines (296 loc) · 14.2 KB
/
greedy.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env pyhon3
from itertools import count
import random
import time
from random import choice
from cloud_foundation import *
from console_IO import *
import numpy as np
from next_server import get_server_type
class Greedy():
def __init__(self, server_list: List[Server], daily_requests: List[DailyRequest], svr_type_list: List[ServerType]) -> None:
self.server_list = server_list
self.daily_requests = daily_requests
self.svr_type_list = svr_type_list
# self.left_remain_core = np.array([ x.left_remain_core for x in self.server_list ])
# self.right_remain_core = np.array([ x.right_remain_core for x in self.server_list ])
# self.left_remain_mem = np.array([ x.left_remain_mem for x in self.server_list ])
# self.right_remain_mem = np.array([ x.right_remain_mem for x in self.server_list ])
self.svr_in_use: List[Server] = []
self.svr_not_use_idx: Set[int] = set(range(len(server_list)))
def run(self):
for rq in self.daily_requests:
node_double, node_single, mv_del = self.vm_type_classify(rq)
for idx in node_double:
core_rank = self.res_core_rank(node_double, rq)[::-1]
pass
@staticmethod
def vm_type_classify(rq: DailyRequest) -> Tuple[List[int], List[int], List[int]]:
node_double: List[int] = [] # only store idx in rq
node_single: List[int] = []
vm_del: List[int] = []
for idx, ope, vm in zip(count(), rq.operation_list, rq.vm_list):
if ope:
if vm.double:
node_double.append(idx)
else:
node_single.append(idx)
else:
vm_del.append(idx)
return node_double, node_single, vm_del
def server_classify(self):
pass
@staticmethod
def res_core_rank(node_idx: List[int], rq: DailyRequest) -> List[int]:
core_list = np.array([ rq.vm_list[idx].node_core for idx in node_idx ])
sorted_idx = node_idx[core_list.argsort()].tolist()
return sorted_idx
@staticmethod
def res_mem_rank(node_idx: List[int], rq: DailyRequest) -> List[int]:
mem_list = np.array([ rq.vm_list[idx].node_mem for idx in node_idx ])
sorted_idx = node_idx[mem_list.argsort()].tolist()
return sorted_idx
@staticmethod
def satisfy_require(vm_core: int, vm_mem: int, svr_core: int, svr_mem: int) -> bool:
return svr_core >= vm_core and svr_mem >= vm_mem
def normal_greedy(self):
# start_time = time.time()
for iter, rq in enumerate(self.daily_requests):
# iter_start_time = time.time()
output = OutputCommand()
vm_double_idxs, vm_single_idxs, vm_del_idxs = self.vm_type_classify(rq)
# double occupy vm
for vm_idx in vm_double_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
found = False
for svr in self.svr_in_use:
if svr.can_put_vm_left(vm) and svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 0)
output.add_unorder_vm_dispatch(svr, '', vm_idx)
found = True
break
if not found:
found = False
for svr_idx in list(self.svr_not_use_idx):
svr = self.server_list[svr_idx]
if svr.can_put_vm_left(vm):
self.svr_not_use_idx.remove(svr_idx)
self.svr_in_use.append(svr)
# svr.activate_server(output)
svr.add_vm_by_instance(vm, 0)
output.add_unorder_vm_dispatch(svr, '', vm_idx, new_svr=True)
found = True
break
if not found: raise ValueError
for vm_idx in vm_single_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
found = False
for svr in self.svr_in_use:
if svr.can_put_vm_left(vm):
svr.add_vm_by_instance(vm, -1)
output.add_unorder_vm_dispatch(svr, 'A', vm_idx)
found = True
break
if svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 1)
output.add_unorder_vm_dispatch(svr, 'B', vm_idx)
found = True
break
if not found:
found = False
for svr_idx in list(self.svr_not_use_idx):
svr = self.server_list[svr_idx]
if svr.can_put_vm_right(vm):
self.svr_not_use_idx.remove(svr_idx)
self.svr_in_use.append(svr)
# svr.activate_server(output)
svr.add_vm_by_instance(vm, 1) # add vm to right side
output.add_unorder_vm_dispatch(svr, 'B', vm_idx, new_svr=True)
found = True
break
if not found: raise ValueError
for vm_idx in vm_del_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
vm.del_vm()
output.print()
# iter_end_time = time.time()
# print(f'iter time: {iter_end_time - iter_start_time}')
# end_time = time.time()
# print(f'all time: {end_time - start_time}')
def random_greedy(self):
# start_time = time.time()
for iter, rq in enumerate(self.daily_requests):
# iter_start_time = time.time()
output = OutputCommand()
vm_double_idxs, vm_single_idxs, vm_del_idxs = self.vm_type_classify(rq)
# double occupy vm
for vm_idx in vm_double_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
found = False
for svr in self.svr_in_use:
if svr.can_put_vm_left(vm) and svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 0)
output.add_unorder_vm_dispatch(svr, '', vm_idx)
found = True
break
if not found:
found = False
while True:
# for svr_idx in self.svr_not_use_idx:
svr_idx = random.choice(list(self.svr_not_use_idx))
svr = self.server_list[svr_idx]
svr = Server(svr._type)
if svr.can_put_vm_left(vm):
self.svr_in_use.append(svr)
# svr.activate_server(output)
svr.add_vm_by_instance(vm, 0)
output.add_unorder_vm_dispatch(svr, '', vm_idx, new_svr=True)
found = True
break
if not found: raise ValueError
for vm_idx in vm_single_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
found = False
for svr in self.svr_in_use:
if svr.can_put_vm_left(vm):
svr.add_vm_by_instance(vm, -1)
output.add_unorder_vm_dispatch(svr, 'A', vm_idx)
found = True
break
if svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 1)
output.add_unorder_vm_dispatch(svr, 'B', vm_idx)
found = True
break
if not found:
found = False
while True:
# for svr_idx in self.svr_not_use_idx:
svr_idx = random.choice(list(self.svr_not_use_idx))
svr = self.server_list[svr_idx]
svr = Server(svr._type)
if svr.can_put_vm_right(vm):
self.svr_in_use.append(svr)
# svr.activate_server(output)
svr.add_vm_by_instance(vm, 1) # add vm to right side
output.add_unorder_vm_dispatch(svr, 'B', vm_idx, new_svr=True)
found = True
break
if not found: raise ValueError
for vm_idx in vm_del_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
vm.del_vm()
output.print()
# iter_end_time = time.time()
# print(f'iter time: {iter_end_time - iter_start_time}')
# end_time = time.time()
# print(f'all time: {end_time - start_time}')
def smart_greedy(self):
# start_time = time.time()
for iter, rq in enumerate(self.daily_requests):
# iter_start_time = time.time()
output = OutputCommand()
vm_double_idxs, vm_single_idxs, vm_del_idxs = self.vm_type_classify(rq)
# double occupy vm
for vm_idx in vm_double_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
found = False
for svr in self.svr_in_use:
if svr.can_put_vm_left(vm) and svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 0)
output.add_unorder_vm_dispatch(svr, '', vm_idx)
found = True
break
if not found:
svr_type = get_server_type(vm, self.svr_type_list, len(self.daily_requests) - iter)
svr = Server(svr_type)
self.svr_in_use.append(svr)
# svr.activate_server(output)
svr.add_vm_by_instance(vm, 0)
output.add_unorder_vm_dispatch(svr, '', vm_idx, new_svr=True)
for vm_idx in vm_single_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
found = False
for svr in self.svr_in_use:
if svr.can_put_vm_left(vm):
svr.add_vm_by_instance(vm, -1)
output.add_unorder_vm_dispatch(svr, 'A', vm_idx)
found = True
break
if svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 1)
output.add_unorder_vm_dispatch(svr, 'B', vm_idx)
found = True
break
if not found:
svr_type = get_server_type(vm, self.svr_type_list, len(self.daily_requests) - iter)
svr = Server(svr_type)
self.svr_in_use.append(svr)
# svr.activate_server(output)
svr.add_vm_by_instance(vm, 1) # add vm to right side
output.add_unorder_vm_dispatch(svr, 'B', vm_idx, new_svr=True)
for vm_idx in vm_del_idxs:
vm = VM.get_vm_by_id(rq.id_list[vm_idx])
vm.del_vm()
output.print()
# iter_end_time = time.time()
# print(f'iter time: {iter_end_time - iter_start_time}')
# end_time = time.time()
# print(f'all time: {end_time - start_time}')
pass
def order_greedy(self):
start_time = time.time()
for iter, rq in enumerate(self.daily_requests):
# iter_start_time = time.time()
output = OutputCommand()
vm_double_idxs, vm_single_idxs, vm_del_idxs = self.vm_type_classify(rq)
for vm_idx, vm in enumerate(rq.vm_list):
if vm_idx in vm_del_idxs:
vm.del_vm()
continue
found = False
for svr in self.svr_in_use:
if vm.double:
if svr.can_put_vm_left(vm) and svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 0)
# output.add_unorder_vm_dispatch(svr.id, '', vm_idx)
output.add_new_vm_dispatch(svr.id, '')
found = True
break
else:
if svr.can_put_vm_left(vm):
svr.add_vm_by_instance(vm, -1)
# output.add_unorder_vm_dispatch(svr.id, 'A', vm_idx)
output.add_new_vm_dispatch(svr.id, 'A')
found = True
break
if svr.can_put_vm_right(vm):
svr.add_vm_by_instance(vm, 1)
# output.add_unorder_vm_dispatch(svr.id, 'B', vm_idx)
output.add_new_vm_dispatch(svr.id, 'B')
found = True
break
pass
if not found:
for svr_idx in list(self.svr_not_use_idx):
svr = self.server_list[svr_idx]
if svr.can_put_vm_left(vm):
self.svr_not_use_idx.remove(svr_idx)
self.svr_in_use.append(svr)
svr.activate_server(output)
if vm.double:
svr.add_vm_by_instance(vm, 0)
# output.add_unorder_vm_dispatch(svr.id, '', vm_idx)
output.add_new_vm_dispatch(svr.id, '')
else:
svr.add_vm_by_instance(vm, 1) # add vm to right side
# output.add_unorder_vm_dispatch(svr.id, 'B', vm_idx)
output.add_new_vm_dispatch(svr.id, 'B')
found = True
break
if not found: raise ValueError
output.print()
# iter_end_time = time.time()
# print(f'iter time: {iter_end_time - iter_start_time}')
end_time = time.time()
# print(f'all time: {end_time - start_time}')