-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhpc_run_RBC.py
More file actions
165 lines (137 loc) · 5.64 KB
/
Copy pathhpc_run_RBC.py
File metadata and controls
165 lines (137 loc) · 5.64 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
from agents.rbc_agent import BasicRBCAgent
import sys
import numpy as np
import time
from agents.orderenforcingwrapper import OrderEnforcingAgent
from citylearn.citylearn import CityLearnEnv
def action_space_to_dict(aspace):
"""Only for box space"""
return {
"high": aspace.high,
"low": aspace.low,
"shape": aspace.shape,
"dtype": str(aspace.dtype),
}
def env_reset(env):
observations = env.reset()
action_space = env.action_space
observation_space = env.observation_space
building_info = env.get_building_information()
building_info = list(building_info.values())
action_space_dicts = [action_space_to_dict(asp) for asp in action_space]
observation_space_dicts = [action_space_to_dict(osp) for osp in observation_space]
obs_dict = {
"action_space": action_space_dicts,
"observation_space": observation_space_dicts,
"building_info": building_info,
"observation": observations,
}
return obs_dict
def evaluate_rbc(agent_used, total_steps=9000, phase_num=1, grid_include=True):
print("Starting local evaluation")
schema_path = f"./data/citylearn_challenge_2022_phase_{phase_num}/schema.json"
env = CityLearnEnv(schema=schema_path)
agent = OrderEnforcingAgent(agent_used)
obs_dict = env_reset(env)
agent_time_elapsed = 0
step_start = time.perf_counter()
actions = agent.register_reset(obs_dict)
agent_time_elapsed += time.perf_counter() - step_start
episodes_completed = 0
num_steps = 0
interrupted = False
episode_metrics = []
try:
while True:
### This is only a reference script provided to allow you
### to do local evaluation. The evaluator **DOES NOT**
### use this script for orchestrating the evaluations.
observations, _, done, _ = env.step(actions)
if done or (num_steps + 1) == total_steps:
# Log run
filename = f"debug_logs/run_logs.csv"
episodes_completed += 1
metrics_t = env.evaluate()
metrics = {
"price_cost": metrics_t[0],
"emmision_cost": metrics_t[1],
"grid_cost": metrics_t[2],
}
if np.any(np.isnan(metrics_t)):
raise ValueError(
"Episode metrics are nan, please contant organizers"
)
episode_metrics.append(metrics)
print(
f"Episode complete: {episodes_completed} | Latest episode metrics: {metrics}",
)
obs_dict = env_reset(env)
step_start = time.perf_counter()
actions = agent.register_reset(obs_dict)
agent_time_elapsed += time.perf_counter() - step_start
else:
step_start = time.perf_counter()
actions = agent.compute_action(observations)
agent_time_elapsed += time.perf_counter() - step_start
num_steps += 1
if num_steps % 100 == 0:
# filename = f"debug_logs/run_logs_{episodes_completed}.csv"
# log_usefull(env, filename)
print(f"Num Steps: {num_steps}, Num episodes: {episodes_completed}")
if episodes_completed >= 1:
break
except KeyboardInterrupt:
print("========================= Stopping Evaluation =========================")
interrupted = True
if not interrupted:
print("=========================Completed=========================")
if len(episode_metrics) > 0:
print(
"Average Price Cost:", np.mean([e["price_cost"] for e in episode_metrics])
)
print(
"Average Emmision Cost:",
np.mean([e["emmision_cost"] for e in episode_metrics]),
)
print("Average Grid Cost:", np.mean([e["grid_cost"] for e in episode_metrics]))
if grid_include == True:
total_cost = np.mean(
[
e["price_cost"] + e["emmision_cost"] + e["grid_cost"]
for e in episode_metrics
]
)
print("Average Total Cost:", total_cost / 3)
tc = total_cost / 3
else:
total_cost = np.mean([e["price_cost"] + e["emmision_cost"] for e in episode_metrics])
print("Average Total Cost:", total_cost / 2)
tc = total_cost / 2
apc = np.mean([e["price_cost"] for e in episode_metrics])
aec = np.mean([e["emmision_cost"] for e in episode_metrics])
agc = np.mean([e["grid_cost"] for e in episode_metrics])
print(f"Total time taken by agent: {agent_time_elapsed}s")
return tc, apc, aec, agc, agent_time_elapsed
def hpc_evaluate(phase_num):
phase_num = int(phase_num)
grid_cost_bool = True
total_steps = 9000
if phase_num == 3:
n_buildings = 7
else:
n_buildings = 5
agent_used = BasicRBCAgent()
tc, apc, aec, agc, agent_time_elapsed = evaluate_rbc(agent_used, total_steps=total_steps, phase_num=phase_num, grid_include=grid_cost_bool)
if grid_cost_bool:
print("Grid cost included")
file = open(f"opt_and_forecast_RBC_phase{phase_num}.csv", "a+")
else:
print("Grid cost NOT included")
file = open(f"opt_and_forecast_RBC_nogridscore_phase{phase_num}.csv", "a+")
file.write(f"\n{phase_num},{tc},{apc},{aec},{agc},{agent_time_elapsed}")
file.close()
if __name__ == "__main__":
phase_num = 3
print('CONFIGURATION: phase number')
print(phase_num)
hpc_evaluate(phase_num)