/
evaluate.py
72 lines (57 loc) · 2.52 KB
/
evaluate.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
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
import os
import sys
import json
import numpy as np
def evaluate_submission_score_as_codalab():
input_dir = sys.argv[1]
output_dir = sys.argv[2]
print('input_dir: {}'.format(input_dir))
print('output_dir: {}'.format(output_dir))
submit_dir = os.path.join(input_dir)
if not os.path.isdir(submit_dir):
print("%s doesn't exist" % submit_dir)
if os.path.isdir(submit_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
score = 0
duration = 0
n_gen = 5
n_load = 11
n_lines = 20
input_dir = os.path.join(input_dir, "saved_experiment")
if os.path.exists(input_dir):
for el in os.listdir(input_dir):
if not os.path.isdir(os.path.join(input_dir, el)):
continue
if not os.path.exists(os.path.join(input_dir, el, "episode_meta.json")):
continue
if not os.path.exists(os.path.join(input_dir, el, "episode_times.json")):
continue
with open(os.path.join(input_dir, el, "episode_meta.json"), "r") as f:
meta = json.load(f)
with open(os.path.join(input_dir, el, "episode_times.json"), "r") as f:
timings = json.load(f)
tmp_sc = float(meta["cumulative_reward"]) * (int(meta["nb_timestep_played"]) == int(meta["chronics_max_timestep"]))
print("Score for scenario {}: {}".format(el, tmp_sc))
score += tmp_sc
duration += float(timings["BaseAgent"]["total"])
else:
print("Your submission is not valid.")
score = -1
duration = 99999
raise RuntimeError("Nothing found at {} where the data should be located".format(input_dir))
output_filename = os.path.join(output_dir, 'scores.txt')
with open(output_filename, 'w') as f:
f.write("score: {:.6f}\n".format(score))
f.write("duration: {:0.6f}\n".format(duration))
f.close()
if __name__ == "__main__":
evaluate_submission_score_as_codalab()
print("Your submission is valid, you may proceed with the next steps")