refactored main plus small changes

This commit is contained in:
steffen-illium
2021-05-27 15:31:45 +02:00
parent 827c2b9ac2
commit 8768d9b75f
5 changed files with 141 additions and 13 deletions

View File

@@ -15,8 +15,8 @@ class Entity:
class Renderer:
BG_COLOR = (178, 190, 195)#(99, 110, 114)
WHITE = (223, 230, 233)#(200, 200, 200)
BG_COLOR = (178, 190, 195) # (99, 110, 114)
WHITE = (223, 230, 233) # (200, 200, 200)
AGENT_VIEW_COLOR = (9, 132, 227)
def __init__(self, grid_w=16, grid_h=16, cell_size=40, fps=4, grid_lines=True, view_radius=2):

View File

@@ -17,10 +17,12 @@ DIRT_INDEX = -1
@dataclass
class DirtProperties:
clean_amount = 10
max_spawn_ratio = 0.1
gain_amount = 0.1
spawn_frequency = 5
clean_amount = 2 # How much does the robot clean with one action.
max_spawn_ratio = 0.2 # On max how much tiles does the dirt spawn in percent.
gain_amount = 0.5 # How much dirt does spawn per tile
spawn_frequency = 5 # Spawn Frequency in Steps
max_local_amount = 1 # Max dirt amount per tile.
max_global_amount = 20 # Max dirt amount in the whole environment.
class SimpleFactory(BaseFactory):
@@ -64,13 +66,15 @@ class SimpleFactory(BaseFactory):
self.renderer.render(OrderedDict(dirt=dirt, wall=walls, **agents))
def spawn_dirt(self) -> None:
if not self.state[DIRT_INDEX].sum() > self.max_dirt or not np.argwhere(self.state[DIRT_INDEX] != h.IS_FREE_CELL).shape[0] > 10:
if not np.argwhere(self.state[DIRT_INDEX] != h.IS_FREE_CELL).shape[0] > self._dirt_properties.max_global_amount:
free_for_dirt = self.free_cells(excluded_slices=DIRT_INDEX)
# randomly distribute dirt across the grid
n_dirt_tiles = int(random.uniform(0, self._dirt_properties.max_spawn_ratio) * len(free_for_dirt))
for x, y in free_for_dirt[:n_dirt_tiles]:
self.state[DIRT_INDEX, x, y] += self._dirt_properties.gain_amount
new_value = self.state[DIRT_INDEX, x, y] + self._dirt_properties.gain_amount
self.state[DIRT_INDEX, x, y] = max(new_value, self._dirt_properties.max_local_amount)
else:
pass
@@ -130,19 +134,20 @@ class SimpleFactory(BaseFactory):
f'{[self.slice_strings[entity] for entity in cols if entity != self.string_slices["dirt"]]}')
if self._is_clean_up_action(agent_state.action):
if agent_state.action_valid:
reward += 0.9
reward += 1
self.print(f'Agent {agent_state.i} did just clean up some dirt at {agent_state.pos}.')
self.monitor.set('dirt_cleaned', 1)
else:
reward -= 1
self.print(f'Agent {agent_state.i} just tried to clean up some dirt '
f'at {agent_state.pos}, but was unsucsessfull.')
self.monitor.set('failed_cleanup_attempt', 1)
reward -= 0.01
elif self._is_moving_action(agent_state.action):
if agent_state.action_valid:
reward -= 0.2
reward -= 0.01
else:
reward -= 0.1
reward -= 0.5
for entity in cols:
if entity != self.string_slices["dirt"]:

View File

@@ -64,6 +64,9 @@ def check_agent_move(state, dim, action):
or y_new >= agent_slice.shape[0]
)
# Check for collision with level walls
valid = valid and not state[LEVEL_IDX][x_new, y_new]
return (x, y), (x_new, y_new), valid

View File

@@ -3,6 +3,23 @@ import seaborn as sns
from matplotlib import pyplot as plt
PALETTE = 10 * (
"#377eb8",
"#4daf4a",
"#984ea3",
"#e41a1c",
"#ff7f00",
"#a65628",
"#f781bf",
"#888888",
"#a6cee3",
"#b2df8a",
"#cab2d6",
"#fb9a99",
"#fdbf6f",
)
def plot(filepath, ext='png', tag='monitor', **kwargs):
plt.rcParams.update(kwargs)
@@ -18,7 +35,7 @@ def prepare_plot(filepath, results_df, ext='png', tag=''):
_ = sns.lineplot(data=results_df, ci='sd', x='step')
# %%
sns.set_theme(palette='husl', style='whitegrid')
sns.set_theme(palette=PALETTE, style='whitegrid')
font_size = 16
tex_fonts = {
# Use LaTeX to write all text