mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-05-23 07:16:44 +02:00
small error fixes reset sanity and self sanity almost lost
This commit is contained in:
parent
9b289591ba
commit
b6ab6ab652
@ -22,13 +22,6 @@ Agents:
|
|||||||
- Inventory
|
- Inventory
|
||||||
- DropOffLocations
|
- DropOffLocations
|
||||||
- Maintainers
|
- Maintainers
|
||||||
# This is special for agents, as each one is differten and can act as an adversary e.g.
|
|
||||||
Positions:
|
|
||||||
- (16, 7)
|
|
||||||
- (16, 6)
|
|
||||||
- (16, 3)
|
|
||||||
- (16, 4)
|
|
||||||
- (16, 5)
|
|
||||||
Entities:
|
Entities:
|
||||||
Batteries:
|
Batteries:
|
||||||
initial_charge: 0.8
|
initial_charge: 0.8
|
||||||
|
@ -91,20 +91,26 @@ class Factory(gym.Env):
|
|||||||
return self.state.entities[item]
|
return self.state.entities[item]
|
||||||
|
|
||||||
def reset(self) -> (dict, dict):
|
def reset(self) -> (dict, dict):
|
||||||
|
|
||||||
|
# Reset information the state holds
|
||||||
|
self.state.reset()
|
||||||
|
|
||||||
|
# Reset Information the GlobalEntity collection holds.
|
||||||
self.state.entities.reset()
|
self.state.entities.reset()
|
||||||
|
|
||||||
# All is set up, trigger entity spawn with variable pos
|
# All is set up, trigger entity spawn with variable pos
|
||||||
self.state.rules.do_all_reset(self.state)
|
self.state.rules.do_all_reset(self.state)
|
||||||
|
|
||||||
# Build initial observations for all agents
|
# Build initial observations for all agents
|
||||||
return self.obs_builder.refresh_and_build_for_all(self.state)
|
self.obs_builder.reset(self.state)
|
||||||
|
return self.obs_builder.build_for_all(self.state)
|
||||||
|
|
||||||
def manual_step_init(self) -> List[Result]:
|
def manual_step_init(self) -> List[Result]:
|
||||||
self.state.curr_step += 1
|
self.state.curr_step += 1
|
||||||
|
|
||||||
# Main Agent Step
|
# Main Agent Step
|
||||||
pre_step_result = self.state.rules.tick_pre_step_all(self)
|
pre_step_result = self.state.rules.tick_pre_step_all(self)
|
||||||
self.obs_builder.reset_struc_obs_block(self.state)
|
self.obs_builder.reset(self.state)
|
||||||
return pre_step_result
|
return pre_step_result
|
||||||
|
|
||||||
def manual_get_named_agent_obs(self, agent_name: str) -> (List[str], np.ndarray):
|
def manual_get_named_agent_obs(self, agent_name: str) -> (List[str], np.ndarray):
|
||||||
@ -158,7 +164,7 @@ class Factory(gym.Env):
|
|||||||
|
|
||||||
info.update(step_reward=sum(reward), step=self.state.curr_step)
|
info.update(step_reward=sum(reward), step=self.state.curr_step)
|
||||||
|
|
||||||
obs = self.obs_builder.refresh_and_build_for_all(self.state)
|
obs = self.obs_builder.build_for_all(self.state)
|
||||||
return None, [x for x in obs.values()], reward, done, info
|
return None, [x for x in obs.values()], reward, done, info
|
||||||
|
|
||||||
def summarize_step_results(self, tick_results: list, done_check_results: list) -> (int, dict, bool):
|
def summarize_step_results(self, tick_results: list, done_check_results: list) -> (int, dict, bool):
|
||||||
|
@ -106,7 +106,7 @@ class SpawnDestinationsPerAgent(Rule):
|
|||||||
super(Rule, self).__init__()
|
super(Rule, self).__init__()
|
||||||
self.per_agent_positions = {key: [ast.literal_eval(x) for x in val] for key, val in coords_or_quantity.items()}
|
self.per_agent_positions = {key: [ast.literal_eval(x) for x in val] for key, val in coords_or_quantity.items()}
|
||||||
|
|
||||||
def on_reset(self, state, lvl_map):
|
def on_reset(self, state):
|
||||||
for (agent_name, position_list) in self.per_agent_positions.items():
|
for (agent_name, position_list) in self.per_agent_positions.items():
|
||||||
agent = h.get_first(state[c.AGENT], lambda x: agent_name in x.name)
|
agent = h.get_first(state[c.AGENT], lambda x: agent_name in x.name)
|
||||||
assert agent
|
assert agent
|
||||||
|
@ -24,11 +24,7 @@ class OBSBuilder(object):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
def __init__(self, level_shape: np.size, state: Gamestate, pomdp_r: int):
|
def __init__(self, level_shape: np.size, state: Gamestate, pomdp_r: int):
|
||||||
self._curr_env_step = None
|
|
||||||
self.all_obs = dict()
|
self.all_obs = dict()
|
||||||
self.light_blockers = defaultdict(lambda: False)
|
|
||||||
self.positional = defaultdict(lambda: False)
|
|
||||||
self.non_positional = defaultdict(lambda: False)
|
|
||||||
self.ray_caster = dict()
|
self.ray_caster = dict()
|
||||||
|
|
||||||
self.level_shape = level_shape
|
self.level_shape = level_shape
|
||||||
@ -37,13 +33,15 @@ class OBSBuilder(object):
|
|||||||
self.size = np.prod(self.obs_shape)
|
self.size = np.prod(self.obs_shape)
|
||||||
|
|
||||||
self.obs_layers = dict()
|
self.obs_layers = dict()
|
||||||
|
|
||||||
self.reset_struc_obs_block(state)
|
|
||||||
self.curr_lightmaps = dict()
|
self.curr_lightmaps = dict()
|
||||||
|
|
||||||
self._floortiles = defaultdict(list, {pos: [Floor(*pos)] for pos in state.entities.floorlist})
|
self._floortiles = defaultdict(list, {pos: [Floor(*pos)] for pos in state.entities.floorlist})
|
||||||
|
|
||||||
def reset_struc_obs_block(self, state):
|
self.reset(state)
|
||||||
self._curr_env_step = state.curr_step
|
|
||||||
|
def reset(self, state):
|
||||||
|
# Reset temporary information
|
||||||
|
self.curr_lightmaps = dict()
|
||||||
# Construct an empty obs (array) for possible placeholders
|
# Construct an empty obs (array) for possible placeholders
|
||||||
self.all_obs[c.PLACEHOLDER] = np.full(self.obs_shape, 0, dtype=float)
|
self.all_obs[c.PLACEHOLDER] = np.full(self.obs_shape, 0, dtype=float)
|
||||||
# Fill the all_obs-dict with all available entities
|
# Fill the all_obs-dict with all available entities
|
||||||
@ -52,7 +50,8 @@ class OBSBuilder(object):
|
|||||||
|
|
||||||
def observation_space(self, state):
|
def observation_space(self, state):
|
||||||
from gymnasium.spaces import Tuple, Box
|
from gymnasium.spaces import Tuple, Box
|
||||||
obsn = self.refresh_and_build_for_all(state)
|
self.reset(state)
|
||||||
|
obsn = self.build_for_all(state)
|
||||||
if len(state[c.AGENT]) == 1:
|
if len(state[c.AGENT]) == 1:
|
||||||
space = Box(low=0, high=1, shape=next(x for x in obsn.values()).shape, dtype=np.float32)
|
space = Box(low=0, high=1, shape=next(x for x in obsn.values()).shape, dtype=np.float32)
|
||||||
else:
|
else:
|
||||||
@ -60,14 +59,13 @@ class OBSBuilder(object):
|
|||||||
return space
|
return space
|
||||||
|
|
||||||
def named_observation_space(self, state):
|
def named_observation_space(self, state):
|
||||||
return self.refresh_and_build_for_all(state)
|
self.reset(state)
|
||||||
|
return self.build_for_all(state)
|
||||||
|
|
||||||
def refresh_and_build_for_all(self, state) -> (dict, dict):
|
def build_for_all(self, state) -> (dict, dict):
|
||||||
self.reset_struc_obs_block(state)
|
|
||||||
return {agent.name: self.build_for_agent(agent, state)[0] for agent in state[c.AGENT]}
|
return {agent.name: self.build_for_agent(agent, state)[0] for agent in state[c.AGENT]}
|
||||||
|
|
||||||
def refresh_and_build_named_for_all(self, state) -> Dict[str, Dict[str, np.ndarray]]:
|
def build_named_for_all(self, state) -> Dict[str, Dict[str, np.ndarray]]:
|
||||||
self.reset_struc_obs_block(state)
|
|
||||||
named_obs_dict = {}
|
named_obs_dict = {}
|
||||||
for agent in state[c.AGENT]:
|
for agent in state[c.AGENT]:
|
||||||
obs, names = self.build_for_agent(agent, state)
|
obs, names = self.build_for_agent(agent, state)
|
||||||
@ -85,9 +83,6 @@ class OBSBuilder(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def build_for_agent(self, agent, state) -> (List[str], np.ndarray):
|
def build_for_agent(self, agent, state) -> (List[str], np.ndarray):
|
||||||
assert self._curr_env_step == state.curr_step, (
|
|
||||||
"The observation objekt has not been reset this state! Call 'reset_struc_obs_block(state)'"
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
agent_want_obs = self.obs_layers[agent.name]
|
agent_want_obs = self.obs_layers[agent.name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -166,7 +161,8 @@ class OBSBuilder(object):
|
|||||||
raise ValueError(f'Max(obs.size) for {e.name}: {obs[idx].size}, but was: {len(v)}.')
|
raise ValueError(f'Max(obs.size) for {e.name}: {obs[idx].size}, but was: {len(v)}.')
|
||||||
if self.pomdp_r:
|
if self.pomdp_r:
|
||||||
try:
|
try:
|
||||||
light_map = np.zeros(self.obs_shape)
|
light_map = self.curr_lightmaps.get(agent.name, np.zeros(self.obs_shape))
|
||||||
|
light_map[:] = 0.0
|
||||||
visible_floor = self.ray_caster[agent.name].visible_entities(self._floortiles, reset_cache=False)
|
visible_floor = self.ray_caster[agent.name].visible_entities(self._floortiles, reset_cache=False)
|
||||||
|
|
||||||
for f in set(visible_floor):
|
for f in set(visible_floor):
|
||||||
|
@ -49,7 +49,7 @@ def prepare_plt(df, hue, style, hue_order):
|
|||||||
plt.close('all')
|
plt.close('all')
|
||||||
sns.set(rc={'text.usetex': False}, style='whitegrid')
|
sns.set(rc={'text.usetex': False}, style='whitegrid')
|
||||||
lineplot = sns.lineplot(data=df, x='Episode', y='Score', hue=hue, style=style,
|
lineplot = sns.lineplot(data=df, x='Episode', y='Score', hue=hue, style=style,
|
||||||
ci=95, palette=PALETTE, hue_order=hue_order, )
|
errorbar=('ci', 95), palette=PALETTE, hue_order=hue_order, )
|
||||||
plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
|
plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
|
||||||
plt.tight_layout()
|
plt.tight_layout()
|
||||||
# lineplot.set_title(f'{sorted(list(df["Measurement"].unique()))}')
|
# lineplot.set_title(f'{sorted(list(df["Measurement"].unique()))}')
|
||||||
|
@ -86,6 +86,10 @@ class Gamestate(object):
|
|||||||
self.rules = StepRules(*rules)
|
self.rules = StepRules(*rules)
|
||||||
self._floortile_graph = None
|
self._floortile_graph = None
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.curr_step = 0
|
||||||
|
self.curr_actions = None
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
return self.entities[item]
|
return self.entities[item]
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ from marl_factory_grid.utils.tools import ConfigExplainer
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Render at each step?
|
# Render at each step?
|
||||||
render = False
|
render = True
|
||||||
# Reveal all possible Modules (Entities, Rules, Agents[Actions, Observations], etc.)
|
# Reveal all possible Modules (Entities, Rules, Agents[Actions, Observations], etc.)
|
||||||
explain_config = False
|
explain_config = False
|
||||||
# Collect statistics?
|
# Collect statistics?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user