-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.py
410 lines (336 loc) · 12.9 KB
/
plot.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
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
import numpy as np
import utility as util
from matplotlib import pyplot as plt
from matplotlib.ticker import (MultipleLocator, FuncFormatter)
def format_func(value, tick_number):
t = int(value//6)
return str(t) + ":00"
if t==2 or t==4 or t==6 or t==8 or t==10:
return str(t) + ":00 am"
if t==0:
return str(t+12) + ":00 am"
if t > 12 and (t-12)%2 == 0:
return str(t-12) + ":00 pm"
return ""
def fig_trans_load_vs_time(result_path, trans, env=None):
result = util.load_dict(result_path)
'''
temp = []
for i in result['base_load']:
temp.append(np.amin(np.array(env['transRating'])/np.array(result['base_load'][i]['trans_load'])))
print(np.amax(np.array(temp)))
'''
x = []
for key in result:
for subKey in result[key]:
x.append(subKey)
break
x = np.array(x)
x = np.sort(x)
fig, ax = plt.subplots()
ax.xaxis.set_major_locator(MultipleLocator(6))
ax.xaxis.set_major_formatter(FuncFormatter(format_func))
ax.xaxis.set_minor_locator(MultipleLocator(1))
#fig.add_subplot(221)
linewidth=1.0
legend = []
y = 100*np.ones(len(x))
legend.append('max')
plt.plot(x,y,'--',linewidth=linewidth)
for key in result:
if key=='llf' or key=='edf' or key=='central' or key=='base_load':
continue
y = []
legend.append(key)
for i in x:
y.append(result[key][i]['n_iter'])
y = np.array(y)
if key=='base_load':
plt.plot(x, y,linewidth=linewidth, alpha=0.3)
else:
plt.plot(x, y,linewidth=linewidth)
plt.legend(legend)
plt.title('# of iterations required for 95% convergence')
plt.xlabel('Time')
plt.ylabel('# of iterations')
plt.xticks(rotation=30)
plt.show()
def fig_trans_load_subplot(result_path, trans_list, env):
result = util.load_dict(result_path)
x = []
for key in result:
for subKey in result[key]:
x.append(subKey)
break
x = np.array(x)
x = np.sort(x)
plt.rcParams.update({'font.size': 24})
fig, ax = plt.subplots(sharex=True, sharey=True)
#ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off')
#ax.yaxis.set_ticks_position('left')
ax.yaxis.set_ticks([])
ax.xaxis.set_ticks([])
ax.set_xlabel('\nTime')
ax.set_ylabel('MVA\n')
#fig.add_subplot(221)
linewidth=1.0
ii = 1
for trans in trans_list:
ax = fig.add_subplot(len(trans_list), 1, ii)
if trans==0:
ax.xaxis.set_major_locator(MultipleLocator(24))
ax.xaxis.set_major_formatter(FuncFormatter(format_func))
ax.xaxis.set_minor_locator(MultipleLocator(12))
else:
ax.get_xaxis().set_visible(False)
legend = []
#legend.append('')
for key in result:
if key=='SGPA' or key=='GPA':
continue
y = []
legend.append(key)
for i in x:
y.append(result[key][i]['trans_load'][(3 * trans + 0)] + result[key][i]['trans_load'][(3 * trans + 1)] + result[key][i]['trans_load'][(3 * trans + 2)])
y = np.array(y)/1000
if key=='Base Load':
plt.plot(x, y,linewidth=linewidth, alpha=0.3)
else:
plt.plot(x, y,linewidth=linewidth)
if ii == 1:
#plt.legend()
#plt.legend(legend, loc='upper left')
plt.legend(legend)
'''
if trans==0:
plt.title('Substation Loading')
else:
plt.title('Transformer (#'+ str(trans)+')'+' Loading')
'''
y = 3*env['transRating'][3*trans]*np.ones(len(x))/1000
plt.plot(x,y,'--',linewidth=linewidth)
#plt.xticks(rotation=30)
#plt.yticks([])
ii += 1
plt.show()
#plt.savefig('trans210.png')
def autolabel(rects, ax, xpos='center', h_offset=0):
"""
Attach a text label above each bar in *rects*, displaying its height.
*xpos* indicates which side to place the text w.r.t. the center of
the bar. It can be one of the following {'center', 'right', 'left'}.
"""
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0, 'right': 1, 'left': -1}
labels = ["{}".format(round(i.get_height(),2)) for i in rects]
#labels = ['' if e=='1.0' else e for e in labels]
#print(labels)
'''
for rect, label in zip(rects, labels):
height = round(rect.get_height(), 2)
ax.text(rect.get_x() + rect.get_width() / 2, height + 10, label,
color='red', fontweight='bold', ha='center', va='top')
'''
for rect in rects:
height = round(rect.get_height(), 2)
height_str = '{}'.format(height)
if height_str=='1.0':
height_str = ''
ax.annotate(height_str,
xy=(rect.get_x() + rect.get_width() / 2, height+h_offset),
xytext=(offset[xpos]*3, 10), # use 3 points offset
textcoords="offset points", # in both directions
ha=ha[xpos], va='center')
return height
def fig_compare(result_path, last_slot, env):
output = {}
result = util.load_dict(result_path)
#men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
#women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
fig, ax = plt.subplots()
for user_type in [0,1]:
output[user_type] = {}
jain_means = []
jain_std = []
soc_means = []
soc_std = []
if user_type==-1:
battery = np.array(env['battery'])
else:
battery = np.array([env['battery'][i] for i in range(0, env['evNumber']) if env['evDriverType'][i]==user_type])
#print(len(battery))
algo_name = []
for key in result:
if key == 'Base Load':
continue
output[user_type][key] = {}
algo_name.append(key)
temp = []
for subKey in result[key]:
#if key=='central':
#print(result[key][subKey]['x'])
if user_type==-1:
value = result[key][subKey]['x']
else:
c = result[key][subKey]['connected']
l = len(c)
value = [result[key][subKey]['x'][i] for i in range(0,l) if env['evDriverType'][c[i]]==user_type]
'''
if 'w' in result[key][subKey]:
w = [result[key][subKey]['w'][i] for i in range(0,l) if env['evDriverType'][c[i]]==user_type]
else:
w = np.ones(len(value)).tolist()
'''
temp.append(util.jain_index(value))
temp = np.array(temp)
output[user_type][key]['jain'] = (np.average(temp), np.std(temp))
jain_means.append(np.average(temp))
jain_std.append(np.std(temp))
if user_type==-1:
remaining_demand = np.array(result[key][last_slot]['remaining_demand'])
else:
remaining_demand = np.array([result[key][last_slot]['remaining_demand'][i] for i in range(0, env['evNumber']) if env['evDriverType'][i]==user_type])
#print(key)
#print(remaining_demand)
temp = (battery - remaining_demand)/battery
count = 0
#print(key)
#print(temp)
for i in range(len(temp)):
if temp[i] >= 0.9:
count+=1
output[user_type][key]['soc'] = (count/len(temp), 0.0)
soc_means.append(count/len(temp))
soc_std.append(0.0)
ind = np.arange(len(jain_means)) # the x locations for the groups
width = 0.3 # the width of the bars
if user_type == 0:
rec = ax.bar(ind - width/2, jain_means, width, yerr=jain_std,
label='Jain Index:Conservative', hatch='/')
h_offset = autolabel(rec, ax)
rec = ax.bar(ind - width/2, soc_means, width, yerr=soc_std,
bottom=jain_means, label='% of EV:Conservative')
autolabel(rec, ax, h_offset=h_offset)
else:
rec = ax.bar(ind + width/2, jain_means, width, yerr=jain_std,
label='Jain Index:Risk Taker', hatch='*')
h_offset = autolabel(rec, ax)
rec = ax.bar(ind + width/2, soc_means, width, yerr=soc_std,
bottom=jain_means, label='% of EV:Risk Taker')
autolabel(rec, ax, h_offset=h_offset)
#autolabel(rects1, ax, "left")
#autolabel(rects2, ax, "right")
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Values')
'''
if user_type==-1:
ax.set_title('Performances for all EVs')
elif user_type==0:
ax.set_title('Performances for Conservative EVs')
elif user_type==1:
ax.set_title('Performances for Risk-Taking EVs')
else:
ax.set_title('Performances for DishonestRisk-Taker EVs')
'''
ax.set_xticks(ind)
#ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.set_xticklabels(algo_name)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.1),
ncol=5, fancybox=True, shadow=True)
#fig.tight_layout()
#plt.show()
print(output)
util.save_dict('data_mix_500_2.txt', output)
#plt.savefig('compare_0_all_risk')
'''
def fig_compare_merge(algo_name, jain, soc):
for key in jain:
ind = np.arange(len(jain[key]['mean'])) # the x locations for the groups
break
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
for key in jain:
rects1 = ax.bar(ind - width/2, jain_means, width, yerr=jain_std,
label='Jain Index', hatch='/')
rects2 = ax.bar(ind + width/2, soc_means, width, yerr=soc_std,
bottom=jain_means, label='% of EV with\n $\geq$90% SoC')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Values')
if user_type==-1:
ax.set_title('Performances for all EVs')
elif user_type==0:
ax.set_title('Performances for Conservative EVs')
elif user_type==1:
ax.set_title('Performances for Risk-Taking EVs')
else:
ax.set_title('Performances for DishonestRisk-Taker EVs')
ax.set_xticks(ind)
#ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.set_xticklabels(algo_name)
ax.legend()
autolabel(rects1, ax, "left")
autolabel(rects2, ax, "right")
fig.tight_layout()
plt.show()
#plt.savefig('compare_0_all_risk')
'''
def fig_soc_vs_time(result_path, usr_type, algo, slot=60):
result = util.load_dict(result_path)
x = []
for key in result:
for subKey in result[key]:
x.append(subKey)
break
x = np.array(x)
x = np.sort(x)
legend = []
for key in result:
if key == algo:
y = [[], [], []]
temp = [[], [], []]
for i in x:
rd = result[key][i]['remaining_demand']
for j in range(0, len(rd)):
temp[usr_type[j]].append(rd[j])
temp_ = np.array(temp)
y[0].append(np.average(temp_[0]))
legend.append('usr_type ' + str(0))
y[1].append(np.average(temp_[1]))
legend.append('usr_type ' + str(1))
y[2].append(np.average(temp_[2]))
legend.append('usr_type ' + str(2))
y = np.array(y) / 3600.0
plt.plot(x, y[0])
plt.plot(x, y[1])
plt.plot(x, y[2])
plt.legend(legend)
plt.title('Remaining Demand: ' + algo)
plt.xlabel('Time Slots (1 slot = ' + str(slot) + ' sec)')
plt.ylabel('kWh')
plt.show()
def fig_conv_ana(result_path):
result = load_dict(result_path)['decentral']
x = []
for key in result['gamma']:
x.append(key)
x = np.array(x)
x = np.sort(x)
linewidth=1.0
y = []
for key in x:
y.append(result['gamma'][key])
#plt.legend([])
plt.plot(x, y)
plt.title('95% Convergence')
plt.xlabel('gamma')
plt.ylabel('iter')
plt.show()
if __name__ == '__main__':
simu_params = util.load_dict('simu_params.txt')
#env = util.load_dict('env/large.txt')
env = util.load_dict(simu_params['env_path'])
#fig_soc_vs_time(simu_params['save_path'], (env['evDriverType']), algo='central')
#fig_trans_load_vs_time(simu_params['save_path'], trans=0, env=env)
fig_trans_load_subplot(simu_params['save_path'], trans_list=[1,0], env=env)
#fig_compare(simu_params['save_path'], 143, env)
#fig_conv_ana('result/meta_large.txt')