-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
1309 lines (1055 loc) · 56.9 KB
/
agent.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
# setting the environment for each nodes (agent)
# get the log data from apis-emulator for states
@author: Qiong
"""
import datetime
import logging.config
from math import sin, cos, pi
import numpy as np
from gym.utils import seeding
logger = logging.getLogger(__name__)
import config as conf
import requests, json
# import os
# os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
from createScenario import CreateScenario1, CreateScenario2, CreateScenario3, CreateScenario4
from RL_learn import DQNNet
# Data loading
# get log data for states
host = conf.b_host
port = conf.b_port
# URL = "http://0.0.0.0:4390/get/log"
URL = "http://" + host + ":" + str(port) + "/get/log"
# dicts of states for all houses
pvc_charge_power = {}
ups_output_power = {}
p2 = {} # powermeter.p2, Power consumption to the power storage system [W]
rsoc = {}
wg = {} # meter.wg, DC Grid power [W]
wb = {} # meter.wb, Battery Power [W]
ig = {} # meter.ig, DC Grid current [A]
vg = {} # meter.vg, DC Grid voltage [V]
pv_list = []
load_list = []
p2_list = []
class APIS(object):
"""
build APIS agent scenarios
"""
def __init__(self, action_request, action_accept):
# request and accept level: between [0, 1]
# self.action_request_space = np.linspace(0.3, 0.9, 7).tolist() # [0.3~0.9], battery mode rsoc > 30%
# self.action_accept_space = np.linspace(0.3, 0.9, 7).tolist() # [0.3~0.9]
# self.action_request_space = np.around(np.linspace(0.3, 0.9, 7).tolist(), 1)
# self.action_accept_space = np.around(np.linspace(0.3, 0.9, 7).tolist(), 1)
self.action_request = action_request
self.action_accept = action_accept
# self.n_actions = len(self.action_request_space) + len(self.action_accept_space)
def CreateSce1(self, action_request, action_accept):
# Create Scenario for house 1 (E001)
newSce = CreateScenario1(action_request=action_request, action_accept=action_accept)
newSce.write_json()
def CreateSce2(self, action_request, action_accept):
# Create Scenario for house 2 (E002)
newSce = CreateScenario2(action_request=action_request, action_accept=action_accept)
newSce.write_json()
def CreateSce3(self, action_request, action_accept):
# Create Scenario for house 3 (E003)
newSce = CreateScenario3(action_request=action_request, action_accept=action_accept)
newSce.write_json()
def CreateSce4(self, action_request, action_accept):
# Create Scenario for house 4 (E004)
newSce = CreateScenario4(action_request=action_request, action_accept=action_accept)
newSce.write_json()
# if __name__ == "__main__":
# interval = 60 * 60 # every 60 * 60s
# command = createJson()
# run(interval, command)
# House Model (Env), step function (reward)
class House():
"""
maybe need different House classes (env) for different houses
agent:
step functions
reset
"""
def __init__(self, action_request, action_accept):
# self.action_request_space = np.linspace(0.2, 0.9, 8).tolist()
# self.action_accept_space = np.linspace(0.2, 0.9, 8).tolist()
# self.agent = agent
self.agent = APIS(action_request, action_accept)
# list of possible actions
# reward
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def step(self, state, action_request, action_accept):
# Exploration hyperparameters for epsilon greedy strategy
explore_start = 1.0 # exploration probability at start
explore_stop = 0.01 # minimum exploration probability
decay_rate = 0.001 # exponential decay rate for exploration prob
decay_step = 0 # Decay rate for ϵ-greedy policy
# action selection
# ϵ-greedy policy
# action_request = sorted(np.random.choice(action_request_num, 2, replace=False), reverse=True) # 2 values
# action_accept = np.random.choice(action_accept_num, 1, replace=False)
exp_exp_tradeoff = np.random.rand()
explore_probability = explore_stop + (explore_start - explore_stop) * np.exp(
-decay_rate * decay_step
)
if explore_probability > exp_exp_tradeoff:
action_request_num = len(self.action_request_space)
action_accept_num = len(self.action_accept_space)
learning_rate = 0.01
action_request = sorted(np.random.choice(action_request_num, 2, replace=False), reverse=True) # 2 values
action_accept = np.random.randint(action_request[1], action_request[0],
1) # 1 value between 2 request actions
# action_request = np.random.choice() # 2 values
# action_accept = np.random.choice() # 1 value
else:
action_request = np.argmax(DQNNet.model.predict(np.expand_dims(state, axis=0)))
# np.argmax -> np.argsort, get top 3 indices
# action_accept =
# minimize purchase from the powerline
# receiving states: pv , load, p2, rsoc
# powerline_energy = power_flow_to_battery - load ?
# reward = powerline_energy
# reward = p2
# return next_state, reward
def step1(self, action_request, action_accept, house_id):
# TODO: set the step function properly !!
# each house learn separately / take as one => step1, step2, step3, step4 for different houses
# how actions changes the states? => follow the apis itself!
"""
Perform one step in the environment following the action.
actions = np.argsort(-actions_value)[:3] e.g. [7, 5, 2]
@param action_request: [actions[0], actions[2]]
action_accept: [actions[1]]
where actions[0] "4320.0-": "excess",
actions[1] "-2880.0": "short",
actions[2] "3360.0-4320.0": "sufficient",
"2880.0-3360.0": "scarce",
ids: house id, string
@return: (for one house) state_ = (pvc_, load_, p2_, rsoc_, rsoc_ave_), reward, done
where reward is set to p2?
but when the goal is reached (time up), done is True
"""
# for house E001: with the actions (act_req, act_acc):
# self.agent.CreateSce1(self.agent.action_request, self.agent.action_accept)
self.agent.CreateSce1(action_request, action_accept)
# TODO: Then get the state_ with the action lists (with the APIS api itself)
# TODO: Shall we add delay for updating the actions for new Scenarios??
# pvc_e001, load_e001, p2_e001, rsoc_e001, rsoc_ave = self.state
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
# print('the name of the dictionary is ', ids)
# print('the dictionary is ', dict_)
# when ids is "E001" (change to other house ID for other houses)
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
rsoc_list.append(rsoc[ids])
# refresh every 60 seconds
# print("\n")
# time.sleep(60) # <-- wait for time pass and renew the actions?
# actions! --> change states
# action_request: [actions[0], actions[2]], action_accept: [actions[1]]
# States pvc_charge_power[ids], for house E001
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
state_ = np.concatenate([all_e001_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E002":
state_ = np.concatenate([all_e002_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E003":
state_ = np.concatenate([all_e003_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E004":
state_ = np.concatenate([all_e004_, np.array([rsoc_ave_])], axis=-1)
else:
print("wrong house id, input again")
# reward: different p2 for each house? / average p2 for all?
# reward = - p2_e001_
reward = - np.sum([p2_e001_, p2_e002_, p2_e003_, p2_e004_])
# TODO: terminal condition: done
# done = time.sleep(60) # time, e.g., one hour(time.sleep(60*60)) or given #EPI
# done: for one day; pesudo code: (hour, day)
# while not gl.sema:
# done = False
# time.sleep(1)
# done = True
# break
# done =
# maybe could not use functions in this way (day, hour has to be within one file)
# input data only has pv and load value, p2,rsoc will be updated within the apis module
# if hour < 24: # 24 hours each day, 24 data points each day
# hour += 1
# state_ = np.concatenate([all_house_id_ + hour, :], rsoc_ave_ )
# else:
# done = True
# day += 1
# hour = 0
# if day < len(all_data) / 24: # all_data: total length of data -> offline??
# state_ = np.concatenate([all_house_id_, :], rsoc_ave_)
# else:
# break
return np.array(state_, dtype=np.float32), reward, {} # done
def step2(self, action_request, action_accept, house_id):
# step function for house 2 (used in main2.py)
# each house learn separately / take as one => step1, step2, step3, step4 for different houses
# how actions changes the states? => follow the apis itself!
"""
Perform one step in the environment following the action.
actions = np.argsort(-actions_value)[:3] e.g. [7, 5, 2]
@param action_request: [actions[0], actions[2]]
action_accept: [actions[1]]
where actions[0] "4320.0-": "excess",
actions[1] "-2880.0": "short",
actions[2] "3360.0-4320.0": "sufficient",
"2880.0-3360.0": "scarce",
ids: house id, string
@return: (for one house) state_ = (pvc_, load_, p2_, rsoc_, rsoc_ave_), reward, done
where reward is set to p2?
but when the goal is reached (time up), done is True
"""
# for house E002: with the actions (act_req, act_acc):
self.agent.CreateSce2(action_request, action_accept)
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
# print('the name of the dictionary is ', ids)
# print('the dictionary is ', dict_)
# when ids is "E001" (change to other house ID for other houses)
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
rsoc_list.append(rsoc[ids])
# States pvc_charge_power[ids], for house E002
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
state_ = np.concatenate([all_e001_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E002":
state_ = np.concatenate([all_e002_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E003":
state_ = np.concatenate([all_e003_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E004":
state_ = np.concatenate([all_e004_, np.array([rsoc_ave_])], axis=-1)
else:
print("wrong house id, input again")
# reward: different p2 for each house? / average p2 for all?
# reward = - p2_e002_
reward = - np.sum([p2_e001_, p2_e002_, p2_e003_, p2_e004_])
# TODO: terminal condition: done
# done = time.sleep(60) # time, e.g., one hour(time.sleep(60*60)) or given #EPI
# done: for one day; pesudo code: (hour, day)
# while not gl.sema:
# done = False
# time.sleep(60)
# done = True
# break
return np.array(state_, dtype=np.float32), reward, {} # done
def step3(self, action_request, action_accept, house_id):
# step function for house 3 (used in main3.py)
# how actions changes the states? => follow the apis itself!
"""
Perform one step in the environment following the action.
actions = np.argsort(-actions_value)[:3] e.g. [7, 5, 2]
@param action_request: [actions[0], actions[2]]
action_accept: [actions[1]]
where actions[0] "4320.0-": "excess",
actions[1] "-2880.0": "short",
actions[2] "3360.0-4320.0": "sufficient",
"2880.0-3360.0": "scarce",
ids: house id, string
@return: (for one house) state_ = (pvc_, load_, p2_, rsoc_, rsoc_ave_), reward, done
where reward is set to p2?
but when the goal is reached (time up), done is True
"""
# for house E003: with the actions (act_req, act_acc):
self.agent.CreateSce3(action_request, action_accept)
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
# print('the name of the dictionary is ', ids)
# print('the dictionary is ', dict_)
# when ids is "E001" (change to other house ID for other houses)
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
rsoc_list.append(rsoc[ids])
# States pvc_charge_power[ids], for house E003
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
state_ = np.concatenate([all_e001_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E002":
state_ = np.concatenate([all_e002_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E003":
state_ = np.concatenate([all_e003_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E004":
state_ = np.concatenate([all_e004_, np.array([rsoc_ave_])], axis=-1)
else:
print("wrong house id, input again")
# reward: different p2 for each house? / average p2 for all?
# reward = - p2_e003_
reward = - np.sum([p2_e001_, p2_e002_, p2_e003_, p2_e004_])
# done = time.sleep(60) # time, e.g., one hour(time.sleep(60*60)) or given #EPI
# done: for one day; pesudo code: (hour, day)
# while not gl.sema:
# done = False
# time.sleep(60)
# done = True
# break
return np.array(state_, dtype=np.float32), reward, {} # done
def step4(self, action_request, action_accept, house_id):
# step function for house 4 (used in main4.py)
# each house learn separately / take as one => step1, step2, step3, step4 for different houses
# how actions changes the states? => follow the apis itself!
"""
Perform one step in the environment following the action.
actions = np.argsort(-actions_value)[:3] e.g. [7, 5, 2]
@param action_request: [actions[0], actions[2]]
action_accept: [actions[1]]
where actions[0] "4320.0-": "excess",
actions[1] "-2880.0": "short",
actions[2] "3360.0-4320.0": "sufficient",
"2880.0-3360.0": "scarce",
ids: house id, string
@return: (for one house) state_ = (pvc_, load_, p2_, rsoc_, rsoc_ave_), reward, done
where reward is set to p2?
but when the goal is reached (time up), done is True
"""
# for house E004: with the actions (act_req, act_acc):
self.agent.CreateSce4(action_request, action_accept)
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
# print('the name of the dictionary is ', ids)
# print('the dictionary is ', dict_)
# when ids is "E004" (change to other house ID for other houses)
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
rsoc_list.append(rsoc[ids])
# States pvc_charge_power[ids], for house E004
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
state_ = np.concatenate([all_e001_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E002":
state_ = np.concatenate([all_e002_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E003":
state_ = np.concatenate([all_e003_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E004":
state_ = np.concatenate([all_e004_, np.array([rsoc_ave_])], axis=-1)
else:
print("wrong house id, input again")
# reward: different p2 for each house? / average p2 for all?
# reward = - p2_e004_
reward = - np.sum([p2_e001_, p2_e002_, p2_e003_, p2_e004_])
# done = time.sleep(60) # time, e.g., one hour(time.sleep(60*60)) or given #EPI
# done: for one day; pesudo code: (hour, day)
# while not gl.sema:
# done = False
# time.sleep(60)
# done = True
# break
return np.array(state_, dtype=np.float32), reward, {} # done
def reset(self, house_id):
"""
reset the states according to standard.json file (../apis-emulator/jsontmp)
all values are the same to each house
super().reset(seed=seed)
"""
# TODO: not set with this standard file (reset shall be based on the last value)
# what is the best way???
# init state
# pvc_charge_power = np.array([0.])
# ups_output_power = np.array([0.])
# p2 = np.array([0.])
# rsoc = np.array([50.])
# # wg = np.array([0])
# # wb = np.array([-4.5])
# rsoc_ave = np.array([50.]) # average rsoc in the same community
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
rsoc_list.append(rsoc[ids])
# States pvc_charge_power[ids], for house E001
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
self.state = np.concatenate([all_e001_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E002":
self.state = np.concatenate([all_e002_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E003":
self.state = np.concatenate([all_e003_, np.array([rsoc_ave_])], axis=-1)
elif house_id == "E004":
self.state = np.concatenate([all_e004_, np.array([rsoc_ave_])], axis=-1)
else:
print("wrong house id, input again")
# self.state = np.array([self.state])
# self.state = np.concatenate([pvc_charge_power, ups_output_power, p2, rsoc, rsoc_ave], axis=-1)
# return np.array(self.state, dtype=np.float32)
return np.array(self.state, dtype=np.float32)
def sin_cos(self, n):
theta = 2 * pi * n
return sin(theta), cos(theta)
def get_cycles_hour(self, time):
# get time info
hour = datetime.datetime.strptime(time, "%Y/%m/%d-%H:%M:%S").hour
# 'hour': sin_cos(d.hour / 24),
return self.sin_cos(hour / 24)
def step1_time(self, action_request, action_accept, house_id):
# use different inputs:
# own house alone ()
# community average (o)
# past history ()
# weather (o) -> weather
# time of the day (o)
# each house learn separately / take as one => step1, step2, step3, step4 for different houses
# how actions changes the states? => follow the apis itself!
"""
Perform one step in the environment following the action.
actions = np.argsort(-actions_value)[:3] e.g. [7, 5, 2]
@param action_request: [actions[0], actions[2]]
action_accept: [actions[1]]
where actions[0] "4320.0-": "excess",
actions[1] "-2880.0": "short",
actions[2] "3360.0-4320.0": "sufficient",
"2880.0-3360.0": "scarce",
ids: house id, string
@return: (for one house) state_ = (pvc_, load_, p2_, rsoc_, rsoc_ave_), reward, done
where reward is set to p2?
but when the goal is reached (time up), done is True
"""
# for house E001: with the actions (act_req, act_acc):
# self.agent.CreateSce1(self.agent.action_request, self.agent.action_accept)
self.agent.CreateSce1(action_request, action_accept)
# TODO: Then get the state_ with the action lists (with the APIS api itself)
# TODO: Shall we add delay for updating the actions for new Scenarios??
# pvc_e001, load_e001, p2_e001, rsoc_e001, rsoc_ave = self.state
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
# time_sin = []
# time_cos = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
# print('the name of the dictionary is ', ids)
# print('the dictionary is ', dict_)
# when ids is "E001" (change to other house ID for other houses)
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
# get time info
# time_cos, time_sin = self.time_of_day(output_data[ids]["emu"]["timestamp"])
hour_sin, hour_cos = self.get_cycles_hour(output_data[ids]["time"])
rsoc_list.append(rsoc[ids])
# refresh every 60 seconds
# print("\n")
# time.sleep(60) # <-- wait for time pass and renew the actions?
# actions! --> change states
# action_request: [actions[0], actions[2]], action_accept: [actions[1]]
# States pvc_charge_power[ids], for house E001
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
state_ = np.concatenate([all_e001_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
elif house_id == "E002":
state_ = np.concatenate([all_e002_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
elif house_id == "E003":
state_ = np.concatenate([all_e003_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
elif house_id == "E004":
state_ = np.concatenate([all_e004_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
else:
print("wrong house id, input again")
# reward: different p2 for each house? / average p2 for all?
# reward = - p2_e001_ # array
reward = - np.sum([p2_e001_, p2_e002_, p2_e003_, p2_e004_]) # sum p2 for all
# print(reward, type(reward))
# TODO: terminal condition: done
# done = time.sleep(60) # time, e.g., one hour(time.sleep(60*60)) or given #EPI
# done: for one day; pesudo code: (hour, day)
# while not gl.sema:
# done = False
# time.sleep(1)
# done = True
# break
# done =
# maybe could not use functions in this way (day, hour has to be within one file)
# input data only has pv and load value, p2,rsoc will be updated within the apis module
# if hour < 24: # 24 hours each day, 24 data points each day
# hour += 1
# state_ = np.concatenate([all_house_id_ + hour, :], rsoc_ave_ )
# else:
# done = True
# day += 1
# hour = 0
# if day < len(all_data) / 24: # all_data: total length of data -> offline??
# state_ = np.concatenate([all_house_id_, :], rsoc_ave_)
# else:
# break
return np.array(state_, dtype=np.float32), reward, {} # done
def step2_time(self, action_request, action_accept, house_id):
# step function for house 2 (used in main2.py)
# each house learn separately / take as one => step1, step2, step3, step4 for different houses
# how actions changes the states? => follow the apis itself!
"""
Perform one step in the environment following the action.
actions = np.argsort(-actions_value)[:3] e.g. [7, 5, 2]
@param action_request: [actions[0], actions[2]]
action_accept: [actions[1]]
where actions[0] "4320.0-": "excess",
actions[1] "-2880.0": "short",
actions[2] "3360.0-4320.0": "sufficient",
"2880.0-3360.0": "scarce",
ids: house id, string
@return: (for one house) state_ = (pvc_, load_, p2_, rsoc_, rsoc_ave_), reward, done
where reward is set to p2?
but when the goal is reached (time up), done is True
"""
# for house E002: with the actions (act_req, act_acc):
self.agent.CreateSce2(action_request, action_accept)
output_data = requests.get(URL).text
output_data = json.loads(output_data) # dict
rsoc_list = []
for ids, dict_ in output_data.items(): # ids: E001, E002, ... house ID
# print('the name of the dictionary is ', ids)
# print('the dictionary is ', dict_)
# when ids is "E001" (change to other house ID for other houses)
pvc_charge_power[ids] = output_data[ids]["emu"]["pvc_charge_power"]
ups_output_power[ids] = output_data[ids]["emu"]["ups_output_power"]
p2[ids] = output_data[ids]["dcdc"]["powermeter"]["p2"]
rsoc[ids] = output_data[ids]["emu"]["rsoc"]
# wg[ids] = output_data[ids]["dcdc"]["meter"]["wg"]
ig[ids] = output_data[ids]["dcdc"]["meter"]["ig"]
# vg[ids] = output_data[ids]["dcdc"]["meter"]["vg"]
# wb[ids] = output_data[ids]["dcdc"]["meter"]["wb"]
rsoc_list.append(rsoc[ids])
hour_sin, hour_cos = self.get_cycles_hour(output_data[ids]["time"])
# States pvc_charge_power[ids], for house E002
if ids == "E001":
pvc_e001_ = np.array([pvc_charge_power["E001"]])
load_e001_ = np.array([ups_output_power["E001"]])
p2_e001_ = np.array([p2["E001"]])
rsoc_e001_ = np.array([rsoc["E001"]])
ig_e001_ = np.array([ig["E001"]])
all_e001_ = np.concatenate([pvc_e001_, load_e001_, p2_e001_, rsoc_e001_, ig_e001_], axis=-1)
if ids == "E002":
pvc_e002_ = np.array([pvc_charge_power["E002"]])
load_e002_ = np.array([ups_output_power["E002"]])
p2_e002_ = np.array([p2["E002"]])
rsoc_e002_ = np.array([rsoc["E002"]])
ig_e002_ = np.array([ig["E002"]])
all_e002_ = np.concatenate([pvc_e002_, load_e002_, p2_e002_, rsoc_e002_, ig_e002_], axis=-1)
if ids == "E003":
pvc_e003_ = np.array([pvc_charge_power["E003"]])
load_e003_ = np.array([ups_output_power["E003"]])
p2_e003_ = np.array([p2["E003"]])
rsoc_e003_ = np.array([rsoc["E003"]])
ig_e003_ = np.array([ig["E003"]])
all_e003_ = np.concatenate([pvc_e003_, load_e003_, p2_e003_, rsoc_e003_, ig_e003_], axis=-1)
if ids == "E004":
pvc_e004_ = np.array([pvc_charge_power["E004"]])
load_e004_ = np.array([ups_output_power["E002"]])
p2_e004_ = np.array([p2["E004"]])
rsoc_e004_ = np.array([rsoc["E004"]])
ig_e004_ = np.array([ig["E004"]])
all_e004_ = np.concatenate([pvc_e004_, load_e004_, p2_e004_, rsoc_e004_, ig_e004_], axis=-1)
# print(rsoc)
# {'E001': 29.98, 'E002': 29.99, 'E003': 29.98, 'E004': 29.99}
rsoc_ave_ = np.mean(rsoc_list) # get average rsoc of this community
# print(rsoc_ave)
if house_id == "E001":
state_ = np.concatenate([all_e001_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
elif house_id == "E002":
state_ = np.concatenate([all_e002_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
elif house_id == "E003":
state_ = np.concatenate([all_e003_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
elif house_id == "E004":
state_ = np.concatenate([all_e004_, np.array([rsoc_ave_, hour_sin, hour_cos])], axis=-1)
else:
print("wrong house id, input again")
# reward: different p2 for each house? / average p2 for all?
# reward = - p2_e002_
reward = - np.sum([p2_e001_, p2_e002_, p2_e003_, p2_e004_])
# print(reward)
# TODO: terminal condition: done
# done = time.sleep(60) # time, e.g., one hour(time.sleep(60*60)) or given #EPI
# done: for one day; pesudo code: (hour, day)
# while not gl.sema:
# done = False
# time.sleep(60)
# done = True
# break
return np.array(state_, dtype=np.float32), reward, {} # done