mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-11-02 21:47:25 +01:00
Results resolved. Small ADjustments
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from .actions import BtryCharge
|
||||
from .actions import Charge
|
||||
from .entitites import ChargePod, Battery
|
||||
from .groups import ChargePods, Batteries
|
||||
from .rules import DoneAtBatteryDischarge, BatteryDecharge
|
||||
|
||||
@@ -8,10 +8,10 @@ from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.utils import helpers as h
|
||||
|
||||
|
||||
class BtryCharge(Action):
|
||||
class Charge(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(b.ACTION_CHARGE)
|
||||
super().__init__(b.ACTION_CHARGE, b.REWARD_CHARGE_VALID, b.Reward_CHARGE_FAIL)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if charge_pod := h.get_first(state[b.CHARGE_PODS].by_pos(entity.pos)):
|
||||
@@ -24,5 +24,4 @@ class BtryCharge(Action):
|
||||
valid = c.NOT_VALID
|
||||
state.print(f'{entity.name} failed to charged batteries at {entity.pos}.')
|
||||
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid,
|
||||
reward=b.REWARD_CHARGE_VALID if valid else b.Reward_CHARGE_FAIL)
|
||||
return self.get_result(valid, entity)
|
||||
|
||||
@@ -11,7 +11,7 @@ from marl_factory_grid.environment import constants as c
|
||||
class CleanUp(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(d.CLEAN_UP)
|
||||
super().__init__(d.CLEAN_UP, r.CLEAN_UP_VALID, r.CLEAN_UP_FAIL)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if dirt := next((x for x in state.entities.pos_dict[entity.pos] if "dirt" in x.name.lower()), None):
|
||||
@@ -24,13 +24,10 @@ class CleanUp(Action):
|
||||
valid = c.VALID
|
||||
print_str = f'{entity.name} did just clean up some dirt at {entity.pos}.'
|
||||
state.print(print_str)
|
||||
reward = r.CLEAN_UP_VALID
|
||||
identifier = d.CLEAN_UP
|
||||
|
||||
else:
|
||||
valid = c.NOT_VALID
|
||||
print_str = f'{entity.name} just tried to clean up some dirt at {entity.pos}, but failed.'
|
||||
state.print(print_str)
|
||||
reward = r.CLEAN_UP_FAIL
|
||||
identifier = d.CLEAN_UP_FAIL
|
||||
|
||||
return ActionResult(identifier=identifier, validity=valid, reward=reward, entity=entity)
|
||||
return self.get_result(valid, entity)
|
||||
|
||||
@@ -11,7 +11,7 @@ from marl_factory_grid.environment import constants as c
|
||||
class DestAction(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(d.DESTINATION)
|
||||
super().__init__(d.DESTINATION, d.REWARD_WAIT_VALID, d.REWARD_WAIT_FAIL)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if destination := state[d.DESTINATION].by_pos(entity.pos):
|
||||
@@ -20,5 +20,4 @@ class DestAction(Action):
|
||||
else:
|
||||
valid = c.NOT_VALID
|
||||
state.print(f'{entity.name} just tried to do_wait_action do_wait_action at {entity.pos} but failed')
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid,
|
||||
reward=d.REWARD_WAIT_VALID if valid else d.REWARD_WAIT_FAIL)
|
||||
return self.get_result(valid, entity)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from typing import Union
|
||||
|
||||
from marl_factory_grid.environment.actions import Action
|
||||
from marl_factory_grid.modules.doors.entitites import Door
|
||||
from marl_factory_grid.modules.doors import constants as d, rewards as r
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.utils.results import ActionResult
|
||||
@@ -8,21 +9,23 @@ from marl_factory_grid.utils.results import ActionResult
|
||||
|
||||
class DoorUse(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(d.ACTION_DOOR_USE)
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(d.ACTION_DOOR_USE, r.USE_DOOR_VALID, r.USE_DOOR_FAIL, **kwargs)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
# Check if agent really is standing on a door:
|
||||
e = state.entities.get_entities_near_pos(entity.pos)
|
||||
try:
|
||||
# Only one door opens TODO introduce loop
|
||||
door = next(x for x in e if x.name.startswith(d.DOOR))
|
||||
valid = door.use()
|
||||
state.print(f'{entity.name} just used a {door.name} at {door.pos}')
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=r.USE_DOOR_VALID)
|
||||
entities_close = state.entities.get_entities_near_pos(entity.pos)
|
||||
|
||||
except StopIteration:
|
||||
# When he doesn't...
|
||||
valid = False
|
||||
for door in [e for e in entities_close if isinstance(e, Door)]:
|
||||
try:
|
||||
# Will always be true, when there is at least a single door.
|
||||
valid = door.use()
|
||||
state.print(f'{entity.name} just used a {door.name} at {door.pos}')
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
if not valid:
|
||||
# When he doesn't stand necxxt to a door tell me.
|
||||
state.print(f'{entity.name} just tried to use a door at {entity.pos}, but there is none.')
|
||||
return ActionResult(entity=entity, identifier=self._identifier,
|
||||
validity=c.NOT_VALID, reward=r.USE_DOOR_FAIL)
|
||||
return self.get_result(valid, entity)
|
||||
|
||||
@@ -9,8 +9,14 @@ from marl_factory_grid.environment import constants as c
|
||||
|
||||
class ItemAction(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(i.ITEM_ACTION)
|
||||
def __init__(self, failed_dropoff_reward: float | None = None, valid_dropoff_reward: float | None = None, **kwargs):
|
||||
super().__init__(i.ITEM_ACTION, r.PICK_UP_FAIL, r.PICK_UP_VALID, **kwargs)
|
||||
self.failed_drop_off_reward = failed_dropoff_reward if failed_dropoff_reward is not None else r.DROP_OFF_FAIL
|
||||
self.valid_drop_off_reward = valid_dropoff_reward if valid_dropoff_reward is not None else r.DROP_OFF_FAIL
|
||||
|
||||
def get_dropoff_result(self, validity, entity):
|
||||
reward = self.valid_drop_off_reward if validity else self.failed_drop_off_reward
|
||||
return ActionResult(self.__name__, validity, reward=reward, entity=entity)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
inventory = state[i.INVENTORY].by_entity(entity)
|
||||
@@ -23,16 +29,15 @@ class ItemAction(Action):
|
||||
state.print(f'{entity.name} just dropped of an item at {drop_off.pos}.')
|
||||
else:
|
||||
state.print(f'{entity.name} just tried to drop off at {entity.pos}, but failed.')
|
||||
reward = r.DROP_OFF_VALID if valid else r.DROP_OFF_FAIL
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=reward)
|
||||
return self.get_dropoff_result(valid, entity)
|
||||
|
||||
elif items := state[i.ITEM].by_pos(entity.pos):
|
||||
item = items[0]
|
||||
item.change_parent_collection(inventory)
|
||||
item.set_pos(c.VALUE_NO_POS)
|
||||
state.print(f'{entity.name} just picked up an item at {entity.pos}')
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=c.VALID, reward=r.PICK_UP_VALID)
|
||||
return self.get_result(c.VALID, entity)
|
||||
|
||||
else:
|
||||
state.print(f'{entity.name} just tried to pick up an item at {entity.pos}, but failed.')
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID, reward=r.PICK_UP_FAIL)
|
||||
return self.get_result(c.NOT_VALID, entity)
|
||||
|
||||
@@ -12,15 +12,12 @@ from marl_factory_grid.utils import helpers as h
|
||||
class MachineAction(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(m.MACHINE_ACTION)
|
||||
super().__init__(m.MACHINE_ACTION, m.MAINTAIN_VALID, m.MAINTAIN_FAIL)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if machine := h.get_first(state[m.MACHINES].by_pos(entity.pos)):
|
||||
if valid := machine.maintain():
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=marl_factory_grid.modules.machines.constants.MAINTAIN_VALID)
|
||||
else:
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=marl_factory_grid.modules.machines.constants.MAINTAIN_FAIL)
|
||||
valid = machine.maintain()
|
||||
return self.get_result(valid, entity)
|
||||
|
||||
else:
|
||||
return ActionResult(entity=entity, identifier=self._identifier,
|
||||
validity=c.NOT_VALID, reward=marl_factory_grid.modules.machines.constants.MAINTAIN_FAIL
|
||||
)
|
||||
return self.get_result(c.NOT_VALID, entity)
|
||||
|
||||
Reference in New Issue
Block a user