mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-07-05 17:11:35 +02:00
added target agent test and fixed tsp agents
This commit is contained in:
@ -11,10 +11,11 @@ class TSPDirtAgent(TSPBaseAgent):
|
||||
super(TSPDirtAgent, self).__init__(*args, **kwargs)
|
||||
|
||||
def predict(self, *_, **__):
|
||||
if self._env.state[di.DIRT].by_pos(self.state.pos) is not None:
|
||||
dirt_at_position = self._env.state[di.DIRT].by_pos(self.state.pos)
|
||||
if dirt_at_position:
|
||||
# Translate the action_object to an integer to have the same output as any other model
|
||||
action = di.CLEAN_UP
|
||||
elif door := self._door_is_close(self._env):
|
||||
elif door := self._door_is_close(self._env.state):
|
||||
action = self._use_door_or_move(door, di.DIRT)
|
||||
else:
|
||||
action = self._predict_move(di.DIRT)
|
||||
|
@ -18,13 +18,15 @@ class TSPItemAgent(TSPBaseAgent):
|
||||
self.mode = mode
|
||||
|
||||
def predict(self, *_, **__):
|
||||
if self._env.state[i.ITEM].by_pos(self.state.pos) is not None:
|
||||
item_at_position = self._env.state[i.ITEM].by_pos(self.state.pos)
|
||||
dropoff_at_position = self._env.state[i.DROP_OFF].by_pos(self.state.pos)
|
||||
if item_at_position:
|
||||
# Translate the action_object to an integer to have the same output as any other model
|
||||
action = i.ITEM_ACTION
|
||||
elif self._env.state[i.DROP_OFF].by_pos(self.state.pos) is not None:
|
||||
elif dropoff_at_position:
|
||||
# Translate the action_object to an integer to have the same output as any other model
|
||||
action = i.ITEM_ACTION
|
||||
elif door := self._door_is_close(self._env):
|
||||
elif door := self._door_is_close(self._env.state):
|
||||
action = self._use_door_or_move(door, i.DROP_OFF if self.mode == MODE_BRING else i.ITEM)
|
||||
else:
|
||||
action = self._choose()
|
||||
|
@ -20,7 +20,7 @@ class TSPTargetAgent(TSPBaseAgent):
|
||||
return None
|
||||
|
||||
def predict(self, *_, **__):
|
||||
if door := self._door_is_close(self._env):
|
||||
if door := self._door_is_close(self._env.state):
|
||||
action = self._use_door_or_move(door, d.DESTINATION)
|
||||
else:
|
||||
action = self._predict_move(d.DESTINATION)
|
||||
|
@ -38,6 +38,21 @@ Agents:
|
||||
- Inventory
|
||||
- DropOffLocations
|
||||
- Maintainers
|
||||
Target test agent:
|
||||
Actions:
|
||||
- Noop
|
||||
- Charge
|
||||
- DoorUse
|
||||
- Move8
|
||||
Observations:
|
||||
- Combined:
|
||||
- Other
|
||||
- Walls
|
||||
- GlobalPosition
|
||||
- Battery
|
||||
- Destinations
|
||||
- Doors
|
||||
- Maintainers
|
||||
|
||||
Entities:
|
||||
|
||||
@ -97,14 +112,11 @@ Rules:
|
||||
done_at_collisions: false
|
||||
|
||||
# Done Conditions
|
||||
DoneAtDestinationReach:
|
||||
DoneOnAllDirtCleaned:
|
||||
DoneAtBatteryDischarge:
|
||||
DoneAtMaintainerCollision:
|
||||
DoneAtMaxStepsReached:
|
||||
max_steps: 500
|
||||
|
||||
Tests:
|
||||
MaintainerTest: {}
|
||||
DirtAgentTest: {}
|
||||
ItemAgentTest: {}
|
||||
# MaintainerTest: {}
|
||||
# DirtAgentTest: {}
|
||||
# ItemAgentTest: {}
|
||||
TargetAgentTest: {}
|
||||
|
@ -57,9 +57,11 @@ class MaintainerTest(Test):
|
||||
def tick_step(self, state) -> List[TickResult]:
|
||||
for maintainer in state.entities[M.MAINTAINERS]:
|
||||
|
||||
# has valid action result
|
||||
self.assertIsInstance(maintainer.state, ActionResult)
|
||||
# self.assertEqual(maintainer.state.validity, True)
|
||||
# has valid action result (except after maintaining)
|
||||
self.assertIsInstance(maintainer.state, (ActionResult, TickResult))
|
||||
if not any(isinstance(entity, Machine) for entity in
|
||||
state.entities.by_pos(maintainer.pos)) and maintainer._path:
|
||||
self.assertEqual(maintainer.state.validity, True)
|
||||
# print(f"state validity {maintainer.state.validity}")
|
||||
|
||||
# will open doors when standing in front
|
||||
@ -81,7 +83,10 @@ class MaintainerTest(Test):
|
||||
if last_action.identifier == 'DoorUse':
|
||||
if door := next((entity for entity in state.entities.get_entities_near_pos(maintainer.pos) if
|
||||
isinstance(entity, Door)), None):
|
||||
self.assertTrue(door.is_open)
|
||||
agents_near_door = [agent for agent in state.entities.get_entities_near_pos(door.pos) if
|
||||
isinstance(agent, Agent)]
|
||||
if len(agents_near_door) < 2:
|
||||
self.assertTrue(door.is_open)
|
||||
if last_action.identifier == 'MachineAction':
|
||||
if machine := next((entity for entity in state.entities.get_entities_near_pos(maintainer.pos) if
|
||||
isinstance(entity, Machine)), None):
|
||||
@ -115,7 +120,7 @@ class DirtAgentTest(Test):
|
||||
def tick_step(self, state) -> List[TickResult]:
|
||||
for dirtagent in [a for a in state.entities[c.AGENT] if "Clean" in a.identifier]: # isinstance TSPDirtAgent
|
||||
# has valid actionresult
|
||||
self.assertIsInstance(dirtagent.state, ActionResult)
|
||||
self.assertIsInstance(dirtagent.state, (ActionResult, TickResult))
|
||||
# self.assertEqual(agent.state.validity, True)
|
||||
# print(f"state validity {maintainer.state.validity}")
|
||||
|
||||
@ -129,7 +134,10 @@ class DirtAgentTest(Test):
|
||||
if last_action.identifier == 'DoorUse':
|
||||
if door := next((entity for entity in state.entities.get_entities_near_pos(dirtagent.pos) if
|
||||
isinstance(entity, Door)), None):
|
||||
self.assertTrue(door.is_open) # TODO catch if someone closes a door in same moment
|
||||
agents_near_door = [agent for agent in state.entities.get_entities_near_pos(door.pos) if
|
||||
isinstance(agent, Agent)]
|
||||
if len(agents_near_door) < 2:
|
||||
self.assertTrue(door.is_open) # TODO fix
|
||||
if last_action.identifier == 'Clean':
|
||||
if dirt := next((entity for entity in state.entities.get_entities_near_pos(dirtagent.pos) if
|
||||
isinstance(entity, DirtPile)), None):
|
||||
@ -180,20 +188,22 @@ class ItemAgentTest(Test):
|
||||
if last_action.identifier == 'DoorUse':
|
||||
if door := next((entity for entity in state.entities.get_entities_near_pos(itemagent.pos) if
|
||||
isinstance(entity, Door)), None):
|
||||
self.assertTrue(door.is_open)
|
||||
agents_near_door = [agent for agent in state.entities.get_entities_near_pos(door.pos) if
|
||||
isinstance(agent, Agent)]
|
||||
if len(agents_near_door) < 2:
|
||||
self.assertTrue(door.is_open)
|
||||
if last_action.identifier == 'ItemAction':
|
||||
|
||||
print(last_action.valid_drop_off_reward) # kann man das nehmen für dropoff vs pickup?
|
||||
# valid pickup?
|
||||
|
||||
# If it was a pick-up action
|
||||
nearby_items = [e for e in state.entities.get_entities_near_pos(itemagent.pos) if
|
||||
isinstance(e, Item)]
|
||||
self.assertNotIn(Item, nearby_items)
|
||||
|
||||
# If the agent has the item in its inventory
|
||||
self.assertTrue(itemagent.bound_entity)
|
||||
# self.assertTrue(itemagent.bound_entity)
|
||||
|
||||
# valid drop off
|
||||
# If it was a drop-off action
|
||||
nearby_drop_offs = [e for e in state.entities.get_entities_near_pos(itemagent.pos) if
|
||||
isinstance(e, DropOffLocation)]
|
||||
@ -211,3 +221,51 @@ class ItemAgentTest(Test):
|
||||
temp_state = itemagent._status
|
||||
self.temp_state_dict[itemagent.identifier] = temp_state
|
||||
return []
|
||||
|
||||
|
||||
class TargetAgentTest(Test):
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Tests whether the target agent will perform the correct actions and whether the actions register correctly in the
|
||||
environment.
|
||||
"""
|
||||
super().__init__()
|
||||
self.temp_state_dict = {}
|
||||
pass
|
||||
|
||||
def on_init(self, state, lvl_map):
|
||||
return []
|
||||
|
||||
def on_reset(self):
|
||||
return []
|
||||
|
||||
def tick_step(self, state) -> List[TickResult]:
|
||||
for targetagent in [a for a in state.entities[c.AGENT] if "Target" in a.identifier]:
|
||||
# has valid action result
|
||||
self.assertIsInstance(targetagent.state, (ActionResult, TickResult))
|
||||
# self.assertEqual(agent.state.validity, True)
|
||||
# print(f"state validity {targetagent.state.validity}")
|
||||
|
||||
return []
|
||||
|
||||
def tick_post_step(self, state) -> List[TickResult]:
|
||||
# do agents' actions have correct effects on environment i.e. doors open, targets are destinations
|
||||
for targetagent in [a for a in state.entities[c.AGENT] if "Target" in a.identifier]:
|
||||
if self.temp_state_dict != {}:
|
||||
last_action = self.temp_state_dict[targetagent.identifier]
|
||||
if last_action.identifier == 'DoorUse':
|
||||
if door := next((entity for entity in state.entities.get_entities_near_pos(targetagent.pos) if
|
||||
isinstance(entity, Door)), None):
|
||||
agents_near_door = [agent for agent in state.entities.get_entities_near_pos(door.pos) if
|
||||
isinstance(agent, Agent)]
|
||||
if len(agents_near_door) < 2:
|
||||
self.assertTrue(door.is_open)
|
||||
|
||||
return []
|
||||
|
||||
def on_check_done(self, state) -> List[DoneResult]:
|
||||
for targetagent in [a for a in state.entities[c.AGENT] if "Target" in a.identifier]:
|
||||
temp_state = targetagent._status
|
||||
self.temp_state_dict[targetagent.identifier] = temp_state
|
||||
return []
|
||||
|
Reference in New Issue
Block a user