rewards and monitors

This commit is contained in:
steffen-illium 2021-05-20 16:05:53 +02:00
parent 962f914ff9
commit 348c4bfecb
2 changed files with 51 additions and 5 deletions

View File

@ -82,10 +82,6 @@ class SimpleFactory(BaseFactory):
return pos, cleanup_was_sucessfull return pos, cleanup_was_sucessfull
def step(self, actions): def step(self, actions):
# TODO: For debugging only!!!! Remove at times.....
if self.state[h.LEVEL_IDX][self.agent_i_position(0)] == h.IS_OCCUPIED_CELL:
print(f'fAgent placed on wall!!!!, step is :{self.steps}')
raise Exception('Agent placed on wall!!!!')
_, r, done, info = super(SimpleFactory, self).step(actions) _, r, done, info = super(SimpleFactory, self).step(actions)
if not self.next_dirt_spawn: if not self.next_dirt_spawn:
self.spawn_dirt() self.spawn_dirt()

View File

@ -48,7 +48,7 @@ class FactoryMonitor:
except IndexError: except IndexError:
return None return None
df = df.fillna(method='ffill') df = df.fillna(method='ffill')
return return df
def reset(self): def reset(self):
raise RuntimeError("DO NOT DO THIS! Always initalize a new Monitor per Env-Run.") raise RuntimeError("DO NOT DO THIS! Always initalize a new Monitor per Env-Run.")
@ -56,6 +56,8 @@ class FactoryMonitor:
class MonitorCallback(BaseCallback): class MonitorCallback(BaseCallback):
ext = 'png'
def __init__(self, env, filepath=Path('debug_out/monitor.pick')): def __init__(self, env, filepath=Path('debug_out/monitor.pick')):
super(MonitorCallback, self).__init__() super(MonitorCallback, self).__init__()
self.filepath = Path(filepath) self.filepath = Path(filepath)
@ -89,6 +91,7 @@ class MonitorCallback(BaseCallback):
# self.out_file.unlink(missing_ok=True) # self.out_file.unlink(missing_ok=True)
with self.filepath.open('wb') as f: with self.filepath.open('wb') as f:
pickle.dump(self.monitor_as_df_list, f, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(self.monitor_as_df_list, f, protocol=pickle.HIGHEST_PROTOCOL)
self.prepare_plot()
self.closed = True self.closed = True
def _on_step(self) -> bool: def _on_step(self) -> bool:
@ -96,3 +99,50 @@ class MonitorCallback(BaseCallback):
self._monitor_list.append(self.env.monitor) self._monitor_list.append(self.env.monitor)
else: else:
pass pass
def plot(self, **kwargs):
from matplotlib import pyplot as plt
plt.rcParams.update(kwargs)
plt.tight_layout()
figure = plt.gcf()
plt.show()
figure.savefig(str(self.filepath.parent / f'{self.filepath.stem}_monitor_measures.{self.ext}'), format=self.ext)
def prepare_plot(self):
# %% Imports
import pandas as pd
import seaborn as sns
# %% Load MonitorList from Disk
with self.filepath.open('rb') as f:
monitor_list = pickle.load(f)
result = pd.concat(monitor_list, sort=False)
# result.tail()
# %%
lineplot = sns.lineplot(data=result)
lineplot.title.title = f'Lineplot Summary of {len(monitor_list)} Episodes'
# %%
sns.set_theme(palette='husl', style='whitegrid')
font_size = 16
tex_fonts = {
# Use LaTeX to write all text
"text.usetex": True,
"font.family": "serif",
# Use 10pt font in plots, to match 10pt font in document
"axes.labelsize": font_size,
"font.size": font_size,
# Make the legend/label fonts a little smaller
"legend.fontsize": font_size - 2,
"xtick.labelsize": font_size - 2,
"ytick.labelsize": font_size - 2
}
try:
self.plot(**tex_fonts)
except FileNotFoundError:
tex_fonts['text.usetex'] = False
self.plot(**tex_fonts)