forked from antoine-delaunay/DRL_SmartGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalyzeGym.py
168 lines (129 loc) · 4.53 KB
/
AnalyzeGym.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
import numpy as np
import matplotlib.pyplot as plt
import copy
from EnvGym import Env, ACTIONS, BATTERY_CAPACITY
"""
Strategies supported :
- RL agent
- Nothing
- Random
- Trade
- RandomBattery # random charge/discharge
- SmartBattery
- SmartBattery2
"""
STRATEGIES = [
"Random",
"Nothing",
"Trade",
"RandomBattery",
"SmartBattery",
"SmartBattery2",
"Model",
]
NB_ACTION = len(ACTIONS)
def strategyAction(strategy, state, model=None):
"""
Determines the action of the given strategy on the state.
Parameters:
strategy (str): strategy to use
state (State): current State of the environment
Returns:
action (str): action of the strategy given the state.
"""
if strategy == "Model":
if model is None:
print("No model given in the function strategyAction")
return ACTIONS.index("nothing")
return model.predict(state.toArray(), deterministic=True)[0]
if strategy == "Random":
return np.random.randint(NB_ACTION)
if strategy == "Trade":
return ACTIONS.index("trade")
if strategy == "Nothing":
return ACTIONS.index("nothing")
if strategy == "RandomBattery":
return np.random.choice([ACTIONS.index("charge"), ACTIONS.index("discharge")])
if strategy == "SmartBattery":
if state.panelProd > state.consumption:
return ACTIONS.index("charge")
else:
return ACTIONS.index("discharge")
if strategy == "SmartBattery2":
if state.panelProd > state.consumption and state.battery < BATTERY_CAPACITY * 0.9999:
return ACTIONS.index("charge")
elif state.panelProd > state.consumption:
return ACTIONS.index("trade")
else:
return ACTIONS.index("discharge")
def test(env: Env, model=None):
"""
Displays figures to compare the result of the DQN algorithm to other predefined strategies.
Parameters:
env (Env): environment on which the strategies are tested
model: the model returned by the DQN algorithm.
If this parameter is set to None, then only the predefined strategies are tested.
This is useful to check the environment.
"""
env.reset()
initState = copy.deepcopy(env.currentState)
conso, prod, price = [], [], []
for i in range(env.epLength):
env.step(0)
conso.append(env.currentState.consumption)
prod.append(env.currentState.panelProd)
price.append(env.currentState.price)
actions, cost = {}, {}
battery, charge, discharge, generate, trade = {}, {}, {}, {}, {}
strategies_list = STRATEGIES[:]
if model is None:
strategies_list.remove("Model")
for strategy in strategies_list:
actions[strategy], cost[strategy] = [], []
(battery[strategy], charge[strategy], discharge[strategy], trade[strategy],) = (
[],
[],
[],
[],
)
env.currentState = copy.deepcopy(initState)
for i in range(env.epLength):
action = strategyAction(strategy, env.currentState, model)
_, _, _, info = env.step(action)
step_cost = info["cost"]
cost[strategy].append(step_cost)
actions[strategy].append(action)
battery[strategy].append(env.currentState.battery)
charge[strategy].append(env.currentState.charge)
discharge[strategy].append(env.currentState.discharge)
trade[strategy].append(env.currentState.trade)
for strategy in strategies_list:
print(f"{strategy:<20} cost: {np.sum(cost[strategy])}")
## Issues with Qt (xcb)
# fig, axs = plt.subplots(len(strategies_list))
# for i, s in enumerate(strategies_list):
# axs[i].plot(trade[s])
# axs[i].plot(battery[s])
# axs[i].legend(["Trade", "Battery"])
# axs[i].title.set_text(s)
# plt.figure(1)
# fig, axs = plt.subplots(len(strategies_list))
# for i, s in enumerate(strategies_list):
# axs[i].plot(actions[s])
# axs[i].legend(["Actions"])
# axs[i].title.set_text(s)
# plt.figure(2)
# fig, axs = plt.subplots(2)
# axs[0].plot(conso)
# axs[0].plot(prod)
# axs[1].plot(price)
# axs[0].legend(["Consumption", "Production"])
# axs[1].title.set_text("Price")
# plt.figure(3)
# fig, ax = plt.subplots()
# for s in strategies_list:
# ax.plot(np.cumsum(cost[s]))
# ax.legend(strategies_list)
# ax.title.set_text("Cost")
# plt.figure(4)
# plt.show()