stable-baselines3/stable_baselines3/common/monitor.py

239 lines
7.8 KiB
Python
Raw Normal View History

__all__ = ["Monitor", "ResultsWriter", "get_monitor_files", "load_results"]
2020-02-03 14:07:35 +00:00
2019-10-10 11:41:54 +00:00
import csv
import json
import os
import time
2020-02-03 14:07:35 +00:00
from glob import glob
from typing import Dict, List, Optional, Tuple, Union
2019-10-10 11:41:54 +00:00
2020-02-03 14:07:35 +00:00
import gym
import numpy as np
import pandas
2019-10-10 11:41:54 +00:00
from stable_baselines3.common.type_aliases import GymObs, GymStepReturn
2019-10-10 11:41:54 +00:00
2020-02-03 14:07:35 +00:00
class Monitor(gym.Wrapper):
2020-05-08 11:09:38 +00:00
"""
A monitor wrapper for Gym environments, it is used to know the episode reward, length, time and other data.
:param env: The environment
:param filename: the location to save a log file, can be None for no log
:param allow_early_resets: allows the reset of the environment before it is done
:param reset_keywords: extra keywords for the reset call,
2020-05-08 11:09:38 +00:00
if extra parameters are needed at reset
:param info_keywords: extra information to log, from the information return of env.step()
2020-05-08 11:09:38 +00:00
"""
2019-10-10 11:41:54 +00:00
EXT = "monitor.csv"
def __init__(
self,
env: gym.Env,
filename: Optional[str] = None,
allow_early_resets: bool = True,
reset_keywords: Tuple[str, ...] = (),
info_keywords: Tuple[str, ...] = (),
):
2020-02-03 14:07:35 +00:00
super(Monitor, self).__init__(env=env)
2019-10-10 11:41:54 +00:00
self.t_start = time.time()
if filename is not None:
self.results_writer = ResultsWriter(
filename,
header={"t_start": self.t_start, "env_id": env.spec and env.spec.id},
extra_keys=reset_keywords + info_keywords,
)
2019-10-10 11:41:54 +00:00
else:
self.results_writer = None
2019-10-10 11:41:54 +00:00
self.reset_keywords = reset_keywords
self.info_keywords = info_keywords
self.allow_early_resets = allow_early_resets
self.rewards = None
self.needs_reset = True
self.episode_returns = []
2019-10-10 11:41:54 +00:00
self.episode_lengths = []
self.episode_times = []
self.total_steps = 0
self.current_reset_info = {} # extra info about the current episode, that was passed in during reset()
def reset(self, **kwargs) -> GymObs:
2019-10-10 11:41:54 +00:00
"""
Calls the Gym environment reset. Can only be called if the environment is over, or if allow_early_resets is True
:param kwargs: Extra keywords saved for the next episode. only if defined by reset_keywords
:return: the first observation of the environment
2019-10-10 11:41:54 +00:00
"""
if not self.allow_early_resets and not self.needs_reset:
raise RuntimeError(
"Tried to reset an environment before done. If you want to allow early resets, "
"wrap your env with Monitor(env, path, allow_early_resets=True)"
)
2019-10-10 11:41:54 +00:00
self.rewards = []
self.needs_reset = False
for key in self.reset_keywords:
value = kwargs.get(key)
if value is None:
raise ValueError(f"Expected you to pass keyword argument {key} into reset")
2019-10-10 11:41:54 +00:00
self.current_reset_info[key] = value
return self.env.reset(**kwargs)
def step(self, action: Union[np.ndarray, int]) -> GymStepReturn:
2019-10-10 11:41:54 +00:00
"""
Step the environment with the given action
:param action: the action
:return: observation, reward, done, information
2019-10-10 11:41:54 +00:00
"""
if self.needs_reset:
raise RuntimeError("Tried to step environment that needs reset")
observation, reward, done, info = self.env.step(action)
self.rewards.append(reward)
if done:
self.needs_reset = True
ep_rew = sum(self.rewards)
ep_len = len(self.rewards)
ep_info = {"r": round(ep_rew, 6), "l": ep_len, "t": round(time.time() - self.t_start, 6)}
2019-10-10 11:41:54 +00:00
for key in self.info_keywords:
ep_info[key] = info[key]
self.episode_returns.append(ep_rew)
self.episode_lengths.append(ep_len)
2019-10-10 11:41:54 +00:00
self.episode_times.append(time.time() - self.t_start)
ep_info.update(self.current_reset_info)
if self.results_writer:
self.results_writer.write_row(ep_info)
info["episode"] = ep_info
2019-10-10 11:41:54 +00:00
self.total_steps += 1
return observation, reward, done, info
def close(self) -> None:
2019-10-10 11:41:54 +00:00
"""
Closes the environment
"""
2020-02-03 14:07:35 +00:00
super(Monitor, self).close()
if self.results_writer is not None:
self.results_writer.close()
2019-10-10 11:41:54 +00:00
2020-02-03 14:07:35 +00:00
def get_total_steps(self) -> int:
2019-10-10 11:41:54 +00:00
"""
Returns the total number of timesteps
:return:
2019-10-10 11:41:54 +00:00
"""
return self.total_steps
2020-02-03 14:07:35 +00:00
def get_episode_rewards(self) -> List[float]:
2019-10-10 11:41:54 +00:00
"""
Returns the rewards of all the episodes
:return:
2019-10-10 11:41:54 +00:00
"""
return self.episode_returns
2019-10-10 11:41:54 +00:00
2020-02-03 14:07:35 +00:00
def get_episode_lengths(self) -> List[int]:
2019-10-10 11:41:54 +00:00
"""
Returns the number of timesteps of all the episodes
:return:
2019-10-10 11:41:54 +00:00
"""
return self.episode_lengths
2020-02-03 14:07:35 +00:00
def get_episode_times(self) -> List[float]:
2019-10-10 11:41:54 +00:00
"""
Returns the runtime in seconds of all the episodes
:return:
2019-10-10 11:41:54 +00:00
"""
return self.episode_times
2020-02-03 14:07:35 +00:00
class LoadMonitorResultsError(Exception):
"""
Raised when loading the monitor log fails.
"""
2020-02-03 14:07:35 +00:00
pass
class ResultsWriter:
"""
A result writer that saves the data from the `Monitor` class
:param filename: the location to save a log file, can be None for no log
:param header: the header dictionary object of the saved csv
:param reset_keywords: the extra information to log, typically is composed of
``reset_keywords`` and ``info_keywords``
"""
def __init__(
self,
filename: str = "",
header: Dict[str, Union[float, str]] = None,
extra_keys: Tuple[str, ...] = (),
):
if header is None:
header = {}
if not filename.endswith(Monitor.EXT):
if os.path.isdir(filename):
filename = os.path.join(filename, Monitor.EXT)
else:
filename = filename + "." + Monitor.EXT
self.file_handler = open(filename, "wt")
self.file_handler.write("#%s\n" % json.dumps(header))
self.logger = csv.DictWriter(self.file_handler, fieldnames=("r", "l", "t") + extra_keys)
self.logger.writeheader()
self.file_handler.flush()
def write_row(self, epinfo: Dict[str, Union[float, int]]) -> None:
"""
Close the file handler
:param epinfo: the information on episodic return, length, and time
"""
if self.logger:
self.logger.writerow(epinfo)
self.file_handler.flush()
def close(self) -> None:
"""
Close the file handler
"""
self.file_handler.close()
2020-02-03 14:07:35 +00:00
def get_monitor_files(path: str) -> List[str]:
"""
get all the monitor files in the given path
:param path: the logging folder
:return: the log files
2020-02-03 14:07:35 +00:00
"""
return glob(os.path.join(path, "*" + Monitor.EXT))
def load_results(path: str) -> pandas.DataFrame:
"""
Load all Monitor logs from a given directory path matching ``*monitor.csv``
2020-02-03 14:07:35 +00:00
:param path: the directory path containing the log file(s)
:return: the logged data
2020-02-03 14:07:35 +00:00
"""
monitor_files = get_monitor_files(path)
if len(monitor_files) == 0:
raise LoadMonitorResultsError(f"No monitor files of the form *{Monitor.EXT} found in {path}")
data_frames, headers = [], []
2020-02-03 14:07:35 +00:00
for file_name in monitor_files:
with open(file_name, "rt") as file_handler:
first_line = file_handler.readline()
assert first_line[0] == "#"
header = json.loads(first_line[1:])
data_frame = pandas.read_csv(file_handler, index_col=None)
headers.append(header)
data_frame["t"] += header["t_start"]
2020-02-03 14:07:35 +00:00
data_frames.append(data_frame)
data_frame = pandas.concat(data_frames)
data_frame.sort_values("t", inplace=True)
2020-02-03 14:07:35 +00:00
data_frame.reset_index(inplace=True)
data_frame["t"] -= min(header["t_start"] for header in headers)
2020-02-03 14:07:35 +00:00
return data_frame