From 26318a7b4868a50f62ab78cda1a4796cad451e2f Mon Sep 17 00:00:00 2001 From: Chanumask Date: Thu, 16 Nov 2023 19:45:25 +0100 Subject: [PATCH] maintainer test --- marl_factory_grid/environment/tests.py | 83 +++++++++++++++++-- .../modules/destinations/rules.py | 3 +- .../modules/maintenance/rules.py | 7 +- marl_factory_grid/testing/test_config.yaml | 11 +-- 4 files changed, 84 insertions(+), 20 deletions(-) diff --git a/marl_factory_grid/environment/tests.py b/marl_factory_grid/environment/tests.py index b6252af..e180500 100644 --- a/marl_factory_grid/environment/tests.py +++ b/marl_factory_grid/environment/tests.py @@ -1,17 +1,20 @@ -import abc from typing import List +import unittest -from marl_factory_grid.utils.results import TickResult, DoneResult +from marl_factory_grid.modules import Door, Machine, Maintainer +from marl_factory_grid.utils.results import TickResult, DoneResult, ActionResult +import marl_factory_grid.modules.maintenance.constants as M +from marl_factory_grid.environment import constants as c -class Test(abc.ABC): +class Test(unittest.TestCase): @property def name(self): return self.__class__.__name__ def __init__(self): - pass + super().__init__() def __repr__(self): return f'{self.name}' @@ -35,9 +38,77 @@ class Test(abc.ABC): return [] -class FirstTest(Test): +class MaintainerTest(Test): def __init__(self): - print("firstTest") super().__init__() pass + + def tick_step(self, state) -> List[TickResult]: + for maintainer in state.entities[M.MAINTAINERS]: + + # has valid actionresult + self.assertIsInstance(maintainer.state, ActionResult) + self.assertEqual(maintainer.state.validity, True) + + # will open doors when standing in front + if maintainer._closed_door_in_path(state): + self.assertEqual(maintainer.get_move_action(state).name, 'use_door') + + if maintainer._path: + # can move + print(maintainer.move(maintainer._path[1], state)) # 0 immer false ausser schritt 1, 1 meistens true nicht immer + # self.assertTrue(maintainer.move(maintainer._path[1], state)) + + else: + # finds valid targets oder hier? + route = maintainer.calculate_route(maintainer._last[-1], state.flootile_graph) + if entities_at_target_location := [entity for entity in state.entities.by_pos(route[-1])]: + self.assertTrue(any(isinstance(e, Machine) for e in entities_at_target_location)) + + return [] + + def tick_post_step(self, state) -> List[TickResult]: + for maintainer in state.entities[M.MAINTAINERS]: + if maintainer._path: + # if action was door use: was door opened successfully? + if maintainer._closed_door_in_path(state): + # print(maintainer.get_move_action(state)) + door = next( + (entity for entity in state.entities.by_pos(maintainer._path[0]) if isinstance(entity, Door)), + None) + self.assertEqual(door.is_open, True) + + return [] + + +class DirtAgentTest(Test): + + def __init__(self): + super().__init__() + pass + + def on_init(self, state, lvl_map): + # dirtagent richtig gespawnt? + return [] + + def on_reset(self): + return [] + + def tick_step(self, state) -> List[TickResult]: + # check observation correct? + # can open doors + # can find way + # can move + # clean action success? action result valid + return [] + + def tick_post_step(self, state) -> List[TickResult]: + # action success? + # collisions? if yes, reported? + return [] + + def on_check_done(self, state) -> List[DoneResult]: + return [] + +# class ItemAgentTest(Test): diff --git a/marl_factory_grid/modules/destinations/rules.py b/marl_factory_grid/modules/destinations/rules.py index 89a1ad8..b299550 100644 --- a/marl_factory_grid/modules/destinations/rules.py +++ b/marl_factory_grid/modules/destinations/rules.py @@ -9,7 +9,6 @@ from marl_factory_grid.environment import constants as c from marl_factory_grid.modules.destinations import constants as d from marl_factory_grid.modules.destinations.entitites import Destination -from marl_factory_grid.utils.states import Gamestate ANY = 'any' @@ -118,7 +117,7 @@ class SpawnDestinationsPerAgent(Rule): per_agent_d = {agent_name: [ast.literal_eval(x) for x in value]} self.per_agent_positions.update(**per_agent_d) - def on_reset(self, state: Gamestate): + def on_reset(self, state): for (agent_name, coords_or_quantity) in self.per_agent_positions.items(): agent = h.get_first(state[c.AGENT], lambda x: agent_name in x.name) assert agent diff --git a/marl_factory_grid/modules/maintenance/rules.py b/marl_factory_grid/modules/maintenance/rules.py index bb5d70c..6538394 100644 --- a/marl_factory_grid/modules/maintenance/rules.py +++ b/marl_factory_grid/modules/maintenance/rules.py @@ -12,10 +12,11 @@ class MoveMaintainers(Rule): super().__init__() def tick_step(self, state) -> List[TickResult]: + move_results = [] for maintainer in state[M.MAINTAINERS]: - maintainer.tick(state) - # Todo: Return a Result Object. - return [] + result = maintainer.tick(state) + move_results.append(result) + return move_results class DoneAtMaintainerCollision(Rule): diff --git a/marl_factory_grid/testing/test_config.yaml b/marl_factory_grid/testing/test_config.yaml index 29d36f2..1266e8a 100644 --- a/marl_factory_grid/testing/test_config.yaml +++ b/marl_factory_grid/testing/test_config.yaml @@ -22,13 +22,6 @@ Agents: - Inventory - DropOffLocations - Maintainers - # This is special for agents, as each one is different and can act as an adversary e.g. - Positions: - - (16, 7) - - (16, 6) - - (16, 3) - - (16, 4) - - (16, 5) Entities: Batteries: initial_charge: 0.8 @@ -86,7 +79,7 @@ Rules: done_at_collisions: false # Done Conditions - DoneAtDestinationReachAny: + DoneAtDestinationReach: DoneOnAllDirtCleaned: DoneAtBatteryDischarge: DoneAtMaintainerCollision: @@ -94,4 +87,4 @@ Rules: max_steps: 500 Tests: - FirstTest: {} \ No newline at end of file + MaintainerTest: {} \ No newline at end of file