-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSYSC4906AIAssignment1W2025.py
More file actions
433 lines (373 loc) · 12.6 KB
/
SYSC4906AIAssignment1W2025.py
File metadata and controls
433 lines (373 loc) · 12.6 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import math
import random
import matplotlib.pyplot as plt
# Use ggplot style for plotting
plt.style.use("ggplot")
##############################
# GLOBAL PARAMETERS
##############################
alpha = 0.5 # weighting for cost = alpha*delay + (1-alpha)*energy
NUM_STEPS = 120
##############################
# 1. DEFINE 20 TASKS
##############################
PREDEFINED_20 = [
{
"Tlocal": 5.0, "Elocal": 6.0,
"Tcomm": 1.0, "Tedge": 1.0,
"Ecomm": 1.0
},
{
"Tlocal": 6.0, "Elocal": 5.5,
"Tcomm": 1.2, "Tedge": 1.0,
"Ecomm": 1.0
},
{
"Tlocal": 5.0, "Elocal": 6.0,
"Tcomm": 0.8, "Tedge": 1.0,
"Ecomm": 1.2
},
{
"Tlocal": 5.5, "Elocal": 5.0,
"Tcomm": 1.0, "Tedge": 0.8,
"Ecomm": 1.0
},
{
"Tlocal": 6.0, "Elocal": 7.0,
"Tcomm": 1.0, "Tedge": 1.0,
"Ecomm": 1.0
},
{
"Tlocal": 10, "Elocal": 1.1,
"Tcomm": 0.1, "Tedge": 0.9,
"Ecomm": 11.1
},
{
"Tlocal": 11.8, "Elocal": 1.2,
"Tcomm": 0.5, "Tedge": 0.5,
"Ecomm": 11.3
},
{
"Tlocal": 11.5, "Elocal": 2.0,
"Tcomm": 1.0, "Tedge": 1.0,
"Ecomm": 13.0
},
{
"Tlocal": 11.2, "Elocal": 1.5,
"Tcomm": 1.0, "Tedge": 1.0,
"Ecomm": 13.4
},
{
"Tlocal": 11.6, "Elocal": 2.0,
"Tcomm": 1.1, "Tedge": 1.0,
"Ecomm": 13.1
},
{
"Tlocal": 1.0, "Elocal": 10.5,
"Tcomm": 11.5, "Tedge": 0.5,
"Ecomm": 1.0
},
{
"Tlocal": 1.0, "Elocal": 10.2,
"Tcomm": 11.0, "Tedge": 0.1,
"Ecomm": 1.0
},
{
"Tlocal": 1.5, "Elocal": 10.0,
"Tcomm": 10.5, "Tedge": 1.0,
"Ecomm": 1.5
},
{
"Tlocal": 1.5, "Elocal": 11.0,
"Tcomm": 11.0, "Tedge": 0.5,
"Ecomm": 1.2
},
{
"Tlocal": 1.3, "Elocal": 9.3,
"Tcomm": 11.2, "Tedge": 0.1,
"Ecomm": 1.4
},
{
"Tlocal": 2.0, "Elocal": 2.0,
"Tcomm": 2.2, "Tedge": 9.0,
"Ecomm": 9.5
},
{
"Tlocal": 2.2, "Elocal": 2.5,
"Tcomm": 2.2, "Tedge": 9.0,
"Ecomm": 9.8
},
{
"Tlocal": 2.0, "Elocal": 2.5,
"Tcomm": 2.0, "Tedge": 9.0,
"Ecomm": 9.0
},
{
"Tlocal": 2.5, "Elocal": 2.5,
"Tcomm": 2.8, "Tedge": 9.0,
"Ecomm": 9.4
},
{
"Tlocal": 2.1, "Elocal": 2.3,
"Tcomm": 2.2, "Tedge": 9.0,
"Ecomm": 9.3
}
]
##############################
# 2. Weighted Cost for local/offload
##############################
def cost_local(task):
return alpha * task["Tlocal"] + (1 - alpha) * task["Elocal"]
def cost_offload(task):
return alpha * (task["Tcomm"] + task["Tedge"]) + (1 - alpha) * task["Ecomm"]
##############################
# 3. 4-step feasibility for local/offload
##############################
def valid_move_p1(last_moves, new_move):
"""
- No more than 2 'local' in any 4 consecutive
- No more than 3 'offload' in any 4 consecutive
"""
window = (last_moves[-3:] + [new_move])
if window.count("local") > 2:
return False
if window.count("offload") > 3:
return False
return True
##############################
# 4. Generate the 120-step scenario
##############################
def generate_scenario_120():
"""
Creates a list of 120 tasks, each a random pick from PREDEFINED_20.
"""
tasks = []
for _ in range(NUM_STEPS):
task_dict = random.choice(PREDEFINED_20)
tasks.append(task_dict)
return tasks
##############################
# 5. Minimax & Random approaches
##############################
def get_smart_move(c_loc, c_off, factor):
"""
Returns the ideal move based on the factor.
"""
beta = 2 # High performance
adjusted_local_cost = c_loc + beta*factor
if (adjusted_local_cost < c_off):
return "local", c_loc
else:
return "offload", c_off
def run_minimax(tasks):
last4moves = ["none", "none", "none", "none"] # Format: [oldest_decision, second_oldest_decision, thid_oldest_decision, most_recent_decision]
moves = []
total_cost = 0.0
for tdict in tasks:
print("The last four moves were: ", last4moves)
c_loc = cost_local(tdict)
c_off = cost_offload(tdict)
# Represents the chosen move and it's associated cost
chosen, step_cost = "none", 0.0
# Get the available moves
feasible = []
if valid_move_p1(last4moves, "local"):
feasible.append("local")
if valid_move_p1(last4moves, "offload"):
feasible.append("offload")
print("The following moves are feasible", feasible)
if (len(feasible) == 0):
# Scenario a: There are no valid moves.
print("There are no valid moves :(")
elif (len(feasible) == 1):
# Scenario b: There is only one valid move. Includes scenarios s4, s5, s6, s8
if (feasible[0] == "local"):
print("There was only 1 option. Performing task locally.")
chosen, step_cost = "local", c_loc
else:
print("There was only 1 option. Offloading task to the edge server.")
chosen, step_cost = "offload", c_off
else:
# Scenario c: Both options are available
# Foreach possible state in the last 4 moves, create a different factor...
# The factor represents an amount to add to the local cost when it comes to comparing local vs offload
# A positive value means based on the current state, local would be a bad next move
# A negative means that based on the current state, local would be a good next move
# Zero means no preference - Choose optimally
if last4moves == ["local", "local", "offload", "offload"]:
factor = -1
print("Scenario: local, local, offload, offload - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
elif last4moves == ["local", "offload", "local", "offload"]:
factor = 1
print("Scenario: local, offload, local, offload - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
elif last4moves == ["local", "offload", "offload", "local"]:
factor = 2
print("Scenario: local, offload, offload, local - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
elif last4moves == ["offload", "local", "offload", "offload"]:
factor = -1
print("Scenario: offload, local, offload, offload - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
elif last4moves == ["offload", "offload", "local", "offload"]:
factor = 1
print("Scenario: offload, offload, local, offload - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
elif last4moves == ["offload", "offload", "offload", "local"]:
factor = 0
print("Scenario: offload, offload, offload, local - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
elif "none" in last4moves:
factor = 0
print("Scenario: This is one of the first four moves. Pick optimally - factor =", factor)
chosen, step_cost = get_smart_move(c_loc, c_off, factor)
else:
print("ERROR - Undefined state. Last 4 moves: ", last4moves)
exit(1)
print("The chosen move was", chosen, "\n")
moves.append(chosen)
total_cost += step_cost
last4moves.pop(0) # Remove the oldest move
last4moves.append(chosen)
no_moves = 0;
for move in moves:
if move == "none":
no_moves += 1
print("DEBUG: The number of moves wasted is", no_moves)
# Returns 2 values: moves, total_cost
return moves, total_cost
def run_random_player(tasks):
"""
For each task, pick local/offload at random if feasible,
else default to local with cost=0 if no feasible moves.
Return (moves, totalCost).
"""
last3moves = []
moves = []
total_cost = 0.0
for tdict in tasks:
c_loc = cost_local(tdict)
c_off = cost_offload(tdict)
feasible = []
if valid_move_p1(last3moves, "local"):
feasible.append(("local", c_loc))
if valid_move_p1(last3moves, "offload"):
feasible.append(("offload", c_off))
if not feasible:
chosen = "local"
step_cost = 0.0
else:
chosen, step_cost = random.choice(feasible)
moves.append(chosen)
last3moves.append(chosen)
total_cost += step_cost
return moves, total_cost
##############################
# 6. Stepwise evaluation
##############################
def evaluate_stepwise(tasks, moves):
"""
Return stepwise cumulative delay, energy, cost arrays for plotting.
"""
delay_arr = []
energy_arr = []
cost_arr = []
cum_delay = 0.0
cum_energy = 0.0
cum_cost = 0.0
for i, tdict in enumerate(tasks):
if moves[i] == "local":
step_delay = tdict["Tlocal"]
step_energy = tdict["Elocal"]
else: # offload
step_delay = tdict["Tcomm"] + tdict["Tedge"]
step_energy = tdict["Ecomm"]
cum_delay += step_delay
cum_energy += step_energy
step_cost = alpha * step_delay + (1 - alpha) * step_energy
cum_cost += step_cost
delay_arr.append(cum_delay)
energy_arr.append(cum_energy)
cost_arr.append(cum_cost)
return delay_arr, energy_arr, cost_arr
##############################
# 7. Plot
##############################
def plot_results(dG, eG, cG, dR, eR, cR):
steps = range(1, NUM_STEPS + 1)
# 1) Delay
plt.figure(figsize=(8, 5))
plt.plot(steps, dG, label="Minimax")
plt.plot(steps, dR, label="Random")
plt.title("Cumulative Delay (120 steps)")
plt.xlabel("Step")
plt.ylabel("Delay")
plt.legend()
plt.show()
# 2) Energy
plt.figure(figsize=(8, 5))
plt.plot(steps, eG, label="Minimax")
plt.plot(steps, eR, label="Random")
plt.title("Cumulative Energy (120 steps)")
plt.xlabel("Step")
plt.ylabel("Energy")
plt.legend()
plt.show()
# 3) Cost
plt.figure(figsize=(8, 5))
plt.plot(steps, cG, label="Minimax")
plt.plot(steps, cR, label="Random")
plt.title(f"Cumulative Cost (alpha={alpha})")
plt.xlabel("Step")
plt.ylabel("Cost")
plt.legend()
plt.show()
##############################
# 8. Print table
##############################
def print_3row_table(tasks, movesG, movesR):
"""
Row 0 => The index of each task in PREDEFINED_20
Row 1 => Minimax's local/off decisions
Row 2 => Random's local/off decisions
"""
print("\nTABLE (3 rows => tasks, minimax, random):\n")
# tasks row
print("Tasks: ", end="")
for tdict in tasks:
idx = PREDEFINED_20.index(tdict)
print(f"T{idx:2}", end=" ")
print()
# Minimax row
print("Minimax: ", end="")
for mv in movesG:
print(f"{mv:6}", end=" ")
print()
# Random row
print("Random: ", end="")
for mv in movesR:
print(f"{mv:6}", end=" ")
print("\n")
##############################
# MAIN
##############################
def main():
random.seed(42)
# 1) Generate scenario of 120 tasks
tasks_120 = generate_scenario_120()
# 2) Minimax approach
movesMinimax, costMinimax = run_minimax(tasks_120)
print(f"Minimax final cost: {costMinimax:.2f}")
# 3) Random approach
movesRandom, costRandom = run_random_player(tasks_120)
print(f"Random final cost: {costRandom:.2f}")
# 4) Evaluate stepwise
dG, eG, cG = evaluate_stepwise(tasks_120, movesMinimax)
dR, eR, cR = evaluate_stepwise(tasks_120, movesRandom)
# 5) Print table
print_3row_table(tasks_120, movesMinimax, movesRandom)
# 6) Plot
plot_results(dG, eG, cG, dR, eR, cR)
if __name__ == "__main__":
main()