-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnalysis.py
309 lines (259 loc) · 13.5 KB
/
Analysis.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
"""
Author: Ruslan Kain
Email: 20rhk@queensu.ca
"""
import numpy as np
import matplotlib.pyplot as plt
from statistics import mean
from Viterbi import Viterbi
from itertools import product #, permutations
from math import sqrt
from operator import add
from mpl_toolkits.mplot3d import Axes3D
from kneed import KneeLocator
from tqdm import tqdm
class Analysis:
def Print_Schedule(self, Schedule, Print, Energy=1, Time_Limits=0):
"""Plot out Sensing Schedules"""
Binary_Schedule=[]
sense_count=0
for i in Schedule:
if i == 'Sense':
Binary_Schedule.append(Energy)
sense_count+=1
else:
Binary_Schedule.append(0)
if Print == True and isinstance(Time_Limits, float):
plt.figure(num=None, figsize=(5.2, 1.2), dpi=80, facecolor='w', edgecolor='k')
Graph=plt.plot(Binary_Schedule)
plt.axvline(x=Time_Limits,color ='y')
plt.yticks(np.arange(2), ("Don't\nSense", "Sense"))
plt.xlabel('Time')
plt.show(Graph)
elif Print == True and isinstance(Time_Limits, list):
plt.figure(num=None, figsize=(5.2, 1.2), dpi=80, facecolor='w', edgecolor='k')
Graph=plt.plot(Binary_Schedule)
for Time_Limit in Time_Limits:
plt.axvline(x=Time_Limit,color ='y')
plt.yticks(np.arange(2), ("Don't\nSense", "Sense"))
plt.xlabel('Time')
plt.show(Graph)
else:
return Binary_Schedule
def Analyze(self, Times, Time_Limits, Schedule, Modified, En=1, Scale=10):
"""Obtains Energy and Delay values for the given sensing schedules"""
Schedule = self.Print_Schedule(self, Schedule, False, En)
Energy=[0]*len(Times)
Delay=[0]*len(Times)
if Modified == False:
n=int(Time_Limits/Scale)
for k in range(len(Times)):
time = int(Times[k]/Scale)
if time<=0:
time=1
if time<n:
for j in range(time,n):
if Schedule[j] == En:
break
for i in range(j):
Energy[k]+=Schedule[i]
Delay[k]=j-time
else:
Energy[k]=0
for i in range(n):
Energy[k]=Schedule[i]+Energy[k]
Energy[k]+=time-n
Delay[k]=0
Mean_Energy=mean(Energy)
Mean_Delay=mean(Delay)
# print("Energy = ", Mean_Energy)
# print("Delay = ", Mean_Delay)
return Mean_Energy, Mean_Delay
else:
Time_Limits= [int(t/Scale) for t in Time_Limits]
for k in range(len(Times)):
n=int(min(Time_Limits, key=lambda x:abs(x-Times[k]/Scale)))
time = int(Times[k]/Scale)
if time<=0:
time=1
if time<n:
for j in range(time,n):
if Schedule[j] == En:
break
for i in range(j):
Energy[k]+=Schedule[i]
Delay[k]=j-time
else:
Energy[k]=0
for i in range(n):
Energy[k]=Schedule[i]+Energy[k]
Energy[k]+=time-n
Delay[k]=0
Mean_Energy=mean(Energy)
Mean_Delay=mean(Delay)
# print("Energy = ", Mean_Energy)
# print("Delay = ", Mean_Delay)
return Mean_Energy, Mean_Delay
def pareto_frontier(self, Xs, Ys, maxX = False, maxY = False):
"""Fucntion 1 which obtains pareto frontier point"""
# Sort the list in either ascending or descending order of X
myList = sorted([[Xs[i], Ys[i]] for i in range(len(Xs))], reverse=maxX)
# Start the Pareto frontier with the first value in the sorted list
p_front = [myList[0]]
# Loop through the sorted list
for pair in myList[1:]:
if maxY:
if pair[1] >= p_front[-1][1]: # Look for higher values of Y…
p_front.append(pair) # … and add them to the Pareto frontier
else:
if pair[1] <= p_front[-1][1]: # Look for lower values of Y…
p_front.append(pair) # … and add them to the Pareto frontier
# Turn resulting pairs back into a list of Xs and Ys
p_frontX = [pair[0] for pair in p_front]
p_frontY = [pair[1] for pair in p_front]
# kneedle = KneeLocator(Xs, Ys, curve='convex', S=1.0, direction='decreasing')
# Knee_X=kneedle.knee
# if Knee_X is not None:
# Knee_Y=Ys[Xs.index(kneedle.knee)]
# else:
# Knee_X = 0
# Knee_Y = 0
return p_frontX, p_frontY
def Schedule_AB_printer(self, Modified, Surv_Prob_Type, Model, combo, E, D, Schedule, Scale):
"""Plots schedules for graphs at key alpha beta combinations"""
if combo[1] > 0.8 - combo[0] and combo[1] < 1.3 - combo[0]:
print('alpha = ', combo[0], 'beta = ', combo[1], 'E = ', round(E,3), 'D = ', round(D,3))
if Modified == True:
if Surv_Prob_Type == 'Distribution':
Last_Model = max(Model)/Scale
self.Print_Schedule(self,Schedule, True, 1, Last_Model)
else:
Models = [x/Scale for x in Model]
self.Print_Schedule(self, Schedule, True, 1, Models)
else:
self.Print_Schedule(self, Schedule, True, 1, Model/Scale)
def Plot_3d(self, Comb, Energy, Delay, N_Energy, N_Delay, P_O_index):
"""3d plots for alpha-beta vs Energy, Delay, and Objective function value """
alphions, betanions, boundary_alpha, boundary_beta, Boundary_Objective_function = [], [], [], [], []
for c in Comb:
alphions.append(c[0])
betanions.append(c[1])
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_trisurf(alphions, betanions, Energy, cmap='viridis', linewidth=0.5, edgecolor='none');
ax.set_xlabel('Alpha')
ax.set_ylabel('Beta')
ax.set_zlabel('Energy')
plt.show()
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_trisurf(alphions, betanions, Delay, cmap='viridis', linewidth=0.5, edgecolor='none');
ax.set_xlabel('Alpha')
ax.set_ylabel('Beta')
ax.set_zlabel('Delay')
plt.show()
Objective_function= list(map(add, N_Energy, N_Delay))
# for c in Comb:
# if round(c[0],1) == 1 - round(c[1],1) and round(c[0],1) != 0:
# boundary_alpha.append(c[0])
# boundary_beta.append(c[1])
# Boundary_Objective_function.append(N_Energy[Comb.index(c)]+N_Delay[Comb.index(c)])
# if round(c[0],1) == 0.1:
# boundary_alpha.append(0.1)
# boundary_beta.append(c[1])
# Boundary_Objective_function.append(N_Energy[Comb.index(c)]+N_Delay[Comb.index(c)])
fig = plt.figure()
ax = plt.axes(projection='3d')
# ax.plot3D(boundary_alpha,boundary_beta, Boundary_Objective_function, color = 'black', linewidth=4)
ax.scatter(Comb[P_O_index][0], Comb[P_O_index][1], Objective_function[P_O_index], color = 'brown', linewidth=4);
ax.plot_trisurf(alphions, betanions, Objective_function, cmap='viridis', linewidth=0.8, edgecolor='none', alpha=0.95);
ax.set_xlabel('Alpha', fontname="Arial", fontsize=12)
ax.set_ylabel('Beta', fontname="Arial", fontsize=12)
ax.set_zlabel('Objective Function', fontname="Arial", fontsize=12)
plt.show()
def Solver(self, Resolution, range_low, range_high, Model, Times, Slack, Modified, MAX_T, Pareto, energy, accuracy, Scale=10, Surv_Prob_Type='Exponential', Dist=[]):
"""Iterats over all possible combinations of alfa and beta between given lower and upper bound based on resolution (step size)"""
Schedule, Energy, Delay, Comb= [], [], [], []
alfas = np.arange(round(range_low+0.1,5),round(range_high,5), Resolution)
betas = np.arange(round(range_low,5),round(range_high,5), Resolution)
for Combos in product(alfas, betas):
# Condition to stay within the boundary to reduce computations
if Combos[0] + Combos[1] < 1.1:
Comb.append((Combos[0],Combos[1]))
MAX_Time = MAX_T[0]
if MAX_T[1] == False:
MAX_Time = 1
for combination_index in tqdm(range(len(Comb))):
combo=list(Comb[combination_index])
VITER = Viterbi(round(combo[0],5),round(combo[1],5), energy, accuracy)
if Modified == True:
if Surv_Prob_Type != 'Distribution':
for q in range(len(Model)):
if q !=0:
Limit = Model[q]-Model[q-1]
Schedule += VITER.Forward(Limit, Slack, Scale, MAX_Time, Surv_Prob_Type)
elif q ==0:
Schedule += VITER.Forward(Model[0], Slack, Scale, MAX_Time, Surv_Prob_Type)
else:
Last_Limit = max(Model)
Schedule = VITER.Forward(Last_Limit, Slack, Scale, MAX_Time, Surv_Prob_Type, Dist)
else:
Schedule = VITER.Forward(Model, Slack, Scale, MAX_Time, Surv_Prob_Type, Dist)
E, D = self.Analyze(self,Times, Model, Schedule, Modified, energy, Scale)
# self.Schedule_AB_printer(self, Modified, Surv_Prob_Type, Model, combo, E, D, Schedule, Scale)
del Schedule[:]
Energy.append(E)
Delay.append(D)
if Pareto == True:
if max(Delay) !=0:
Normalized_Delay = [x/max(Delay) for x in Delay]
Normalized_Energy = [x/max(Energy) for x in Energy]
else:
if max(Delay) !=0:
Normalized_Delay = [x/MAX_T[0] for x in Delay]
Normalized_Energy = [x/(MAX_T[2]*MAX_T[0]) for x in Energy]
Pareto_D, Pareto_E= self.pareto_frontier(self, Normalized_Delay, Normalized_Energy)
Pareto_Opt_Delay, Pareto_Opt_Energy, Pareto_Opt_index = self.Get_Pareto(self, Normalized_Delay, Normalized_Energy)
alfa = Comb[Pareto_Opt_index][0]
beta = Comb[Pareto_Opt_index][1]
self.Plot_3d(self, Comb, Energy, Delay, Normalized_Energy, Normalized_Delay, Pareto_Opt_index)
if Pareto == False:
if max(Pareto_D) !=0:
Pareto_D = [x/max(Pareto_D) for x in Pareto_D]
Pareto_E = [x/max(Pareto_E) for x in Pareto_E]
return Pareto_D, Pareto_E, alfa, beta
def Compare(self, Delay1, Energy1, Delay2, Energy2):
"""Compares combinations of energy and delay values"""
Distances1 = []
Distances2 = []
for i in range(len(Delay1)):
Distances1.append(sqrt(Delay1[i]**2 + Energy1[i]**2))
for j in range(len(Delay2)):
Distances2.append(sqrt(Delay2[j]**2 + Energy2[j]**2))
if min(Distances1)<min(Distances2):
return Delay1[Distances1.index(min(Distances1))], Energy1[Distances1.index(min(Distances1))], Delay2[Distances2.index(min(Distances2))], Energy2[Distances2.index(min(Distances2))], "Group 1"
elif min(Distances2)<min(Distances1):
return Delay2[Distances2.index(min(Distances2))], Energy2[Distances2.index(min(Distances2))], Delay1[Distances1.index(min(Distances1))], Energy1[Distances1.index(min(Distances1))], "Group 2"
else:
return Delay1[Distances1.index(min(Distances1))], Energy1[Distances1.index(min(Distances1))], Delay2[Distances2.index(min(Distances2))], Energy2[Distances2.index(min(Distances2))], "Group 1&2 Same"
def Get_Pareto(self, Delay, Energy):
"""Fucntion 1 which obtains pareto frontier point"""
if len(Energy) < 2:
Distances = []
for i in range(len(Delay)):
Distances.append(sqrt(Delay[i]**2 + Energy[i]**2))
D = Delay[Distances.index(min(Distances))]
En = Energy[Distances.index(min(Distances))]
else:
kneedle = KneeLocator(Energy, Delay, curve='convex', direction='decreasing')
En = kneedle.knee
if En is None:
Distances = []
for i in range(len(Delay)):
Distances.append(sqrt(Delay[i]**2 + Energy[i]**2))
D = Delay[Distances.index(min(Distances))]
En = Energy[Distances.index(min(Distances))]
else:
D = Delay[Energy.index(En)]
Objective_index = np.argmin(list( map(add, Energy, Delay)))
return D, En, Objective_index