mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-20 11:08:10 +02:00
Refactoring of Collision checking bug fixes
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
import random
|
import random
|
||||||
|
from typing import Tuple, List, Union, Iterable
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -10,6 +11,8 @@ class BaseFactory:
|
|||||||
def __init__(self, level='simple', n_agents=1, max_steps=1e3):
|
def __init__(self, level='simple', n_agents=1, max_steps=1e3):
|
||||||
self.n_agents = n_agents
|
self.n_agents = n_agents
|
||||||
self.max_steps = max_steps
|
self.max_steps = max_steps
|
||||||
|
self.allow_vertical_movement = True
|
||||||
|
self.allow_horizontal_movement = True
|
||||||
self.level = h.one_hot_level(
|
self.level = h.one_hot_level(
|
||||||
h.parse_level(Path(__file__).parent / h.LEVELS_DIR / f'{level}.txt')
|
h.parse_level(Path(__file__).parent / h.LEVELS_DIR / f'{level}.txt')
|
||||||
)
|
)
|
||||||
@ -38,49 +41,66 @@ class BaseFactory:
|
|||||||
actions = [actions] if isinstance(actions, int) else actions
|
actions = [actions] if isinstance(actions, int) else actions
|
||||||
assert isinstance(actions, list), f'"actions has to be in [{int, list}]'
|
assert isinstance(actions, list), f'"actions has to be in [{int, list}]'
|
||||||
self.steps += 1
|
self.steps += 1
|
||||||
|
|
||||||
|
# FixMe: Why do we need this?
|
||||||
r = 0
|
r = 0
|
||||||
|
|
||||||
|
# Move this in a seperate function?
|
||||||
actions = list(enumerate(actions))
|
actions = list(enumerate(actions))
|
||||||
random.shuffle(actions)
|
random.shuffle(actions)
|
||||||
for agent_i, action in actions:
|
for agent_i, action in actions:
|
||||||
if action <= 8:
|
if self._is_moving_action(action):
|
||||||
pos, did_collide = self.move_or_colide(agent_i, action)
|
pos, valid = self.move_or_colide(agent_i, action)
|
||||||
else:
|
else:
|
||||||
pos, did_collide = self.additional_actions(agent_i, action)
|
pos, valid = self.additional_actions(agent_i, action)
|
||||||
actions[agent_i] = (pos, did_collide)
|
actions[agent_i] = (agent_i, action, pos, valid)
|
||||||
|
|
||||||
collision_vecs = np.zeros((self.n_agents, self.state.shape[0])) # n_agents x n_slices
|
collision_vecs = self.check_all_collisions(actions, self.state.shape[0])
|
||||||
for agent_i, action in enumerate(actions):
|
|
||||||
collision_vecs[agent_i] = self.check_collisions(agent_i, *action)
|
|
||||||
|
|
||||||
reward, info = self.step_core(collision_vecs, actions, r)
|
reward, info = self.calculate_reward(collision_vecs, [a[1] for a in actions], r)
|
||||||
r += reward
|
r += reward
|
||||||
if self.steps >= self.max_steps:
|
if self.steps >= self.max_steps:
|
||||||
self.done = True
|
self.done = True
|
||||||
return self.state, r, self.done, info
|
return self.state, r, self.done, info
|
||||||
|
|
||||||
def check_collisions(self, agent_i, pos, valid):
|
def _is_moving_action(self, action):
|
||||||
|
movement_actions = (int(self.allow_vertical_movement) + int(self.allow_horizontal_movement)) * 4
|
||||||
|
if action < movement_actions:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_all_collisions(self, agent_action_pos_valid_tuples: (int, int, (int, int), bool), collisions: int) -> np.ndarray:
|
||||||
|
collision_vecs = np.zeros((len(agent_action_pos_valid_tuples), collisions)) # n_agents x n_slices
|
||||||
|
for agent_i, action, pos, valid in agent_action_pos_valid_tuples:
|
||||||
|
if self._is_moving_action(action):
|
||||||
|
collision_vecs[agent_i] = self.check_collisions(agent_i, pos, valid)
|
||||||
|
return collision_vecs
|
||||||
|
|
||||||
|
def check_collisions(self, agent_i: int, pos: (int, int), valid: bool) -> np.ndarray:
|
||||||
pos_x, pos_y = pos
|
pos_x, pos_y = pos
|
||||||
collisions_vec = self.state[:, pos_x, pos_y].copy() # "vertical fiber" at position of agent i
|
# FixMe: We need to find a way to spare out some dimensions, eg. an info dimension etc... a[?,]
|
||||||
|
collisions_vec = self.state[:, pos_x, pos_y].copy() # "vertical fiber" at position of agent i
|
||||||
collisions_vec[h.AGENT_START_IDX + agent_i] = h.IS_FREE_CELL # no self-collisions
|
collisions_vec[h.AGENT_START_IDX + agent_i] = h.IS_FREE_CELL # no self-collisions
|
||||||
if valid:
|
if valid:
|
||||||
|
# ToDo: Place a function hook here
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
collisions_vec[h.LEVEL_IDX] = h.IS_OCCUPIED_CELL
|
collisions_vec[h.LEVEL_IDX] = h.IS_OCCUPIED_CELL
|
||||||
return collisions_vec
|
return collisions_vec
|
||||||
|
|
||||||
def move(self, agent_i, old_pos, new_pos):
|
def do_move(self, agent_i: int, old_pos: (int, int), new_pos: (int, int)) -> None:
|
||||||
(x, y), (x_new, y_new) = old_pos, new_pos
|
(x, y), (x_new, y_new) = old_pos, new_pos
|
||||||
self.state[agent_i + h.AGENT_START_IDX, x, y] = h.IS_FREE_CELL
|
self.state[agent_i + h.AGENT_START_IDX, x, y] = h.IS_FREE_CELL
|
||||||
self.state[agent_i + h.AGENT_START_IDX, x_new, y_new] = h.IS_OCCUPIED_CELL
|
self.state[agent_i + h.AGENT_START_IDX, x_new, y_new] = h.IS_OCCUPIED_CELL
|
||||||
|
|
||||||
def move_or_colide(self, agent_i, action) -> ((int, int), bool):
|
def move_or_colide(self, agent_i: int, action: int) -> ((int, int), bool):
|
||||||
old_pos, new_pos, valid = h.check_agent_move(state=self.state,
|
old_pos, new_pos, valid = h.check_agent_move(state=self.state,
|
||||||
dim=agent_i + h.AGENT_START_IDX,
|
dim=agent_i + h.AGENT_START_IDX,
|
||||||
action=action)
|
action=action)
|
||||||
if valid:
|
if valid:
|
||||||
# Does not collide width level boundrys
|
# Does not collide width level boundrys
|
||||||
self.move(agent_i, old_pos, new_pos)
|
self.do_move(agent_i, old_pos, new_pos)
|
||||||
return new_pos, valid
|
return new_pos, valid
|
||||||
else:
|
else:
|
||||||
# Agent seems to be trying to collide in this step
|
# Agent seems to be trying to collide in this step
|
||||||
@ -93,7 +113,7 @@ class BaseFactory:
|
|||||||
np.random.shuffle(free_cells)
|
np.random.shuffle(free_cells)
|
||||||
return free_cells
|
return free_cells
|
||||||
|
|
||||||
def step_core(self, collisions_vec, actions, r):
|
def calculate_reward(self, collisions_vec: np.ndarray, actions: Iterable[int], r: int) -> (int, dict):
|
||||||
# Returns: Reward, Info
|
# Returns: Reward, Info
|
||||||
# Set to "raise NotImplementedError"
|
# Set to "raise NotImplementedError"
|
||||||
return 0, {} # What is returned here?
|
return 0, {}
|
||||||
|
@ -19,7 +19,7 @@ class SimpleFactory(BaseFactory):
|
|||||||
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
|
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
|
||||||
self.spawn_dirt()
|
self.spawn_dirt()
|
||||||
|
|
||||||
def step_core(self, collisions_vecs, actions, r):
|
def calculate_reward(self, collisions_vecs, actions, r):
|
||||||
for agent_i, cols in enumerate(collisions_vecs):
|
for agent_i, cols in enumerate(collisions_vecs):
|
||||||
cols = np.argwhere(cols != 0).flatten()
|
cols = np.argwhere(cols != 0).flatten()
|
||||||
print(f't = {self.steps}\tAgent {agent_i} has collisions with '
|
print(f't = {self.steps}\tAgent {agent_i} has collisions with '
|
||||||
|
@ -30,7 +30,7 @@ class GettingDirty(BaseFactory):
|
|||||||
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
|
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
|
||||||
self.spawn_dirt()
|
self.spawn_dirt()
|
||||||
|
|
||||||
def step_core(self, collisions_vecs, actions, r):
|
def calculate_reward(self, collisions_vecs, actions, r):
|
||||||
for agent_i, cols in enumerate(collisions_vecs):
|
for agent_i, cols in enumerate(collisions_vecs):
|
||||||
cols = np.argwhere(cols != 0).flatten()
|
cols = np.argwhere(cols != 0).flatten()
|
||||||
print(f't = {self.steps}\tAgent {agent_i} has collisions with '
|
print(f't = {self.steps}\tAgent {agent_i} has collisions with '
|
||||||
|
Reference in New Issue
Block a user