-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
120 lines (97 loc) · 3.74 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
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
# Copyright (c) 2023-2024 La Javaness (https://lajavaness.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 L2RPN 2023 LJN Agent, a repository for the winning agent of L2RPN 2023 competition. It is a submodule contribution to the L2RPN Baselines repository.
import os
import warnings
from grid2op.MakeEnv import make
from grid2op.Runner import Runner
from l2rpn_baselines.LJNAgent import LJNAgent
from l2rpn_baselines.utils.save_log_gif import save_log_gif
from lightsim2grid import LightSimBackend
def evaluate(
env,
load_path=".",
logs_path=None,
nb_episode=1,
nb_process=1,
max_steps=-1,
verbose=True,
save_gif=False,
**kwargs
):
"""
Evaluate the agent with a default config based on l2rpn_idf_2023 environment
Parameters
----------
env: :class:`grid2op.Environment.Environment`
The environment on which the baseline will be evaluated.
load_path: ``str``
The path where the model is stored. This is used by the agent when calling "agent.load)
logs_path: ``str``
The path where the agents results will be stored.
nb_episode: ``int``
Number of episodes to run for the assessment of the performance.
By default it's 1.
nb_process: ``int``
Number of process to be used for the assessment of the performance.
Should be an integer greater than 1. By defaults it's 1.
max_steps: ``int``
Maximum number of timestep each episode can last. It should be a positive integer or -1.
-1 means that the entire episode is run (until the chronics is out of data or until a game over).
By default it's -1.
verbose: ``bool``
verbosity of the output
save_gif: ``bool``
Whether or not to save a gif into each episode folder corresponding to the representation of the said episode.
kwargs:
Other key words arguments that you are free to use for either building the agent save it etc.
Returns
-------
``None``
"""
runner_params = env.get_params_for_runner()
runner_params["verbose"] = verbose
# Create the agent - using default parameters suitable for l2rpn_idf_2023
agent = LJNAgent(
env,
env.action_space,
)
# Build the runner
runner = Runner(**env.get_params_for_runner(), agentClass=None, agentInstance=agent)
if logs_path is not None:
os.makedirs(logs_path, exist_ok=True)
results = runner.run(
path_save=logs_path,
nb_episode=nb_episode,
nb_process=nb_process,
max_iter=max_steps,
pbar=verbose,
)
print("Evaluation summary:")
for _, chron_name, cum_reward, nb_time_step, max_ts in results:
msg_tmp = "chronics at: {}".format(chron_name)
msg_tmp += "\ttotal score: {:.6f}".format(cum_reward)
msg_tmp += "\ttime steps: {:.0f}/{:.0f}".format(nb_time_step, max_ts)
print(msg_tmp)
if save_gif:
save_log_gif(logs_path, results)
if __name__ == "__main__":
import grid2op
from l2rpn_baselines.utils import cli_eval
from lightsim2grid.lightSimBackend import LightSimBackend
args_cli = cli_eval().parse_args()
env = grid2op.make("l2rpn_idf_2023", backend=LightSimBackend())
print("--- Starting evaluation on l2rpn_idf_2023 ---")
evaluate(
env,
load_path="./",
nb_episode=args_cli.nb_episode,
nb_process=args_cli.nb_process,
max_steps=args_cli.max_steps,
verbose=args_cli.verbose,
save_gif=args_cli.save_gif,
)