small bugfixes and getting dirty

This commit is contained in:
steffen-illium 2021-05-12 14:52:30 +02:00
parent b16f316f08
commit 14741aa5a5
2 changed files with 16 additions and 10 deletions

View File

@ -110,7 +110,7 @@ class BaseFactory:
return old_pos, valid
def agent_i_position(self, agent_i):
return np.argwhere(self.state[h.AGENT_START_IDX+agent_i] == h.IS_OCCUPIED_CELL)
return tuple(np.argwhere(self.state[h.AGENT_START_IDX+agent_i] == h.IS_OCCUPIED_CELL).flatten())
@property
def free_cells(self) -> np.ndarray:

View File

@ -13,17 +13,17 @@ class GettingDirty(BaseFactory):
@property
def _clean_up_action(self):
return self.movement_actions + 1
return self.movement_actions + 1 - 1
def __init__(self, *args, dirt_properties:DirtProperties, **kwargs):
super(GettingDirty, self).__init__(*args, **kwargs)
self._dirt_properties = dirt_properties
super(GettingDirty, self).__init__(*args, **kwargs)
self.slice_strings.update({self.state.shape[0]-1: 'dirt'})
def spawn_dirt(self) -> None:
free_for_dirt = self.free_cells
# randomly distribute dirt across the grid
n_dirt_tiles = self._dirt_properties.max_spawn_ratio * len(free_for_dirt)
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
@ -38,11 +38,16 @@ class GettingDirty(BaseFactory):
self.state[DIRT_INDEX][pos] = max(new_dirt_amount, h.IS_FREE_CELL)
return pos, cleanup_was_sucessfull
def additional_actions(self, agent_i, action) -> ((int, int), bool):
if not action == self._is_moving_action(action):
if action != self._is_moving_action(action):
if action == self._clean_up_action:
self.clean_up()
agent_i_pos = self.agent_i_position(agent_i)
_, valid = self.clean_up(agent_i_pos)
if valid:
print(f'Agent {agent_i} did just clean up some dirt at {agent_i_pos}.')
else:
print(f'Agent {agent_i} just tried to clean up some dirt at {agent_i_pos}, but was unsucsessfull.')
return agent_i_pos, valid
else:
raise RuntimeError('This should not happen!!!')
@ -53,17 +58,18 @@ class GettingDirty(BaseFactory):
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
self.spawn_dirt()
def calculate_reward(self, collisions_vec: np.ndarray, actions: Iterable[int], r: int) -> (int, dict):
def calculate_reward(self, collisions_vecs: np.ndarray, actions: Iterable[int], r: int) -> (int, dict):
for agent_i, cols in enumerate(collisions_vecs):
cols = np.argwhere(cols != 0).flatten()
print(f't = {self.steps}\tAgent {agent_i} has collisions with '
f'{[self.slice_strings[entity] for entity in cols]}')
f'{[self.slice_strings[entity] for entity in cols if entity != self.state.shape[0]]}')
return 0, {}
if __name__ == '__main__':
import random
factory = GettingDirty(n_agents=1, max_dirt=8)
dirt_properties = DirtProperties()
factory = GettingDirty(n_agents=1, dirt_properties=dirt_properties)
random_actions = [random.randint(0, 8) for _ in range(200)]
for action in random_actions:
state, r, done, _ = factory.step(action)