mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-07-11 23:42:40 +02:00
Movement printing and Results, state, state reset.
This commit is contained in:
@ -79,7 +79,7 @@ Rules:
|
|||||||
done_at_collisions: false
|
done_at_collisions: false
|
||||||
|
|
||||||
# Done Conditions
|
# Done Conditions
|
||||||
DoneAtDestinationReachAny:
|
DoneAtDestinationReach:
|
||||||
DoneOnAllDirtCleaned:
|
DoneOnAllDirtCleaned:
|
||||||
DoneAtBatteryDischarge:
|
DoneAtBatteryDischarge:
|
||||||
DoneAtMaintainerCollision:
|
DoneAtMaintainerCollision:
|
||||||
|
@ -51,13 +51,18 @@ class Move(Action, abc.ABC):
|
|||||||
def do(self, entity, state):
|
def do(self, entity, state):
|
||||||
new_pos = self._calc_new_pos(entity.pos)
|
new_pos = self._calc_new_pos(entity.pos)
|
||||||
if state.check_move_validity(entity, new_pos):
|
if state.check_move_validity(entity, new_pos):
|
||||||
# noinspection PyUnresolvedReferences
|
valid = entity.move(new_pos, state)
|
||||||
move_really_was_valid = entity.move(new_pos, state)
|
|
||||||
return self.get_result(move_really_was_valid, entity)
|
else:
|
||||||
else: # There is no place to go, propably collision
|
# There is no place to go, propably collision
|
||||||
# This is currently handeld by the WatchCollisions rule, so that it can be switched on and off by conf.yml
|
# This is currently handeld by the WatchCollisions rule, so that it can be switched on and off by conf.yml
|
||||||
# return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID, reward=r.COLLISION)
|
# return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID, reward=r.COLLISION)
|
||||||
return self.get_result(c.NOT_VALID, entity)
|
valid = c.NOT_VALID
|
||||||
|
if valid:
|
||||||
|
state.print(f'{entity.name} just moved to {entity.pos}.')
|
||||||
|
else:
|
||||||
|
state.print(f'{entity.name} just tried to move to {new_pos} but either failed or hat a Collision.')
|
||||||
|
return self.get_result(valid, entity)
|
||||||
|
|
||||||
def _calc_new_pos(self, pos):
|
def _calc_new_pos(self, pos):
|
||||||
x_diff, y_diff = MOVEMAP[self._identifier]
|
x_diff, y_diff = MOVEMAP[self._identifier]
|
||||||
|
@ -43,9 +43,6 @@ class Agent(Entity):
|
|||||||
def var_is_blocking_pos(self):
|
def var_is_blocking_pos(self):
|
||||||
return self._is_blocking_pos
|
return self._is_blocking_pos
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
return self._state or ActionResult(entity=self, identifier=c.NOOP, validity=c.VALID)
|
|
||||||
|
|
||||||
def __init__(self, actions: List[Action], observations: List[str], *args, is_blocking_pos=False, **kwargs):
|
def __init__(self, actions: List[Action], observations: List[str], *args, is_blocking_pos=False, **kwargs):
|
||||||
super(Agent, self).__init__(*args, **kwargs)
|
super(Agent, self).__init__(*args, **kwargs)
|
||||||
@ -53,21 +50,16 @@ class Agent(Entity):
|
|||||||
self.step_result = dict()
|
self.step_result = dict()
|
||||||
self._actions = actions
|
self._actions = actions
|
||||||
self._observations = observations
|
self._observations = observations
|
||||||
self._state: Union[Result, None] = None
|
self._status: Union[Result, None] = None
|
||||||
self._is_blocking_pos = is_blocking_pos
|
self._is_blocking_pos = is_blocking_pos
|
||||||
|
|
||||||
# noinspection PyAttributeOutsideInit
|
|
||||||
def clear_temp_state(self):
|
|
||||||
self._state = None
|
|
||||||
return self
|
|
||||||
|
|
||||||
def summarize_state(self):
|
def summarize_state(self):
|
||||||
state_dict = super().summarize_state()
|
state_dict = super().summarize_state()
|
||||||
state_dict.update(valid=bool(self.state.validity), action=str(self.state.identifier))
|
state_dict.update(valid=bool(self.state.validity), action=str(self.state.identifier))
|
||||||
return state_dict
|
return state_dict
|
||||||
|
|
||||||
def set_state(self, action_result):
|
def set_state(self, action_result):
|
||||||
self._state = action_result
|
self._status = action_result
|
||||||
|
|
||||||
def paralyze(self, reason):
|
def paralyze(self, reason):
|
||||||
self._paralyzed.add(reason)
|
self._paralyzed.add(reason)
|
||||||
|
@ -90,7 +90,14 @@ class Entity(Object, abc.ABC):
|
|||||||
self.set_pos(next_pos)
|
self.set_pos(next_pos)
|
||||||
for observer in self.observers:
|
for observer in self.observers:
|
||||||
observer.notify_add_entity(self)
|
observer.notify_add_entity(self)
|
||||||
|
# Aftermath Collision Check
|
||||||
|
if len([x for x in state.entities.by_pos(next_pos) if x.var_can_collide]):
|
||||||
|
# The entity did move, but there was something to collide with...
|
||||||
|
# Is then reported as a non-valid move, which did work.
|
||||||
|
valid = False
|
||||||
|
|
||||||
return valid
|
return valid
|
||||||
|
# Bad naming... Was the same was the same pos, not moving....
|
||||||
return not_same_pos
|
return not_same_pos
|
||||||
|
|
||||||
def __init__(self, pos, bind_to=None, **kwargs):
|
def __init__(self, pos, bind_to=None, **kwargs):
|
||||||
|
@ -40,6 +40,7 @@ class Object:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def __init__(self, str_ident: Union[str, None] = None, **kwargs):
|
def __init__(self, str_ident: Union[str, None] = None, **kwargs):
|
||||||
|
self._status = None
|
||||||
self._bound_entity = None
|
self._bound_entity = None
|
||||||
self._observers = set()
|
self._observers = set()
|
||||||
self._str_ident = str_ident
|
self._str_ident = str_ident
|
||||||
@ -84,6 +85,10 @@ class Object:
|
|||||||
def summarize_state(self):
|
def summarize_state(self):
|
||||||
return dict()
|
return dict()
|
||||||
|
|
||||||
|
def clear_temp_state(self):
|
||||||
|
self._status = None
|
||||||
|
return self
|
||||||
|
|
||||||
def bind_to(self, entity):
|
def bind_to(self, entity):
|
||||||
# noinspection PyAttributeOutsideInit
|
# noinspection PyAttributeOutsideInit
|
||||||
self._bound_entity = entity
|
self._bound_entity = entity
|
||||||
|
@ -132,7 +132,8 @@ class WatchCollisions(Rule):
|
|||||||
for i, guest in enumerate(guests):
|
for i, guest in enumerate(guests):
|
||||||
try:
|
try:
|
||||||
guest.set_state(TickResult(identifier=c.COLLISION, reward=self.reward,
|
guest.set_state(TickResult(identifier=c.COLLISION, reward=self.reward,
|
||||||
validity=c.NOT_VALID, entity=self))
|
validity=c.NOT_VALID, entity=guest)
|
||||||
|
)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
results.append(TickResult(entity=guest, identifier=c.COLLISION,
|
results.append(TickResult(entity=guest, identifier=c.COLLISION,
|
||||||
|
@ -19,5 +19,5 @@ class DestAction(Action):
|
|||||||
state.print(f'{entity.name} just waited at {entity.pos}')
|
state.print(f'{entity.name} just waited at {entity.pos}')
|
||||||
else:
|
else:
|
||||||
valid = c.NOT_VALID
|
valid = c.NOT_VALID
|
||||||
state.print(f'{entity.name} just tried to do_wait_action do_wait_action at {entity.pos} but failed')
|
state.print(f'{entity.name} just tried to "do_wait_action" at {entity.pos} but failed')
|
||||||
return self.get_result(valid, entity)
|
return self.get_result(valid, entity)
|
||||||
|
@ -70,6 +70,10 @@ class Inventory(IsBoundMixin, Collection):
|
|||||||
def set_collection(self, collection):
|
def set_collection(self, collection):
|
||||||
self._collection = collection
|
self._collection = collection
|
||||||
|
|
||||||
|
def clear_temp_state(self):
|
||||||
|
# Entites need this, but inventories have no state....
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Inventories(Objects):
|
class Inventories(Objects):
|
||||||
_entity = Inventory
|
_entity = Inventory
|
||||||
|
@ -26,16 +26,22 @@ class Maintainer(Entity):
|
|||||||
self._last_serviced = 'None'
|
self._last_serviced = 'None'
|
||||||
|
|
||||||
def tick(self, state):
|
def tick(self, state):
|
||||||
|
self.clear_temp_state
|
||||||
if found_objective := h.get_first(state[self.objective].by_pos(self.pos)):
|
if found_objective := h.get_first(state[self.objective].by_pos(self.pos)):
|
||||||
if found_objective.name != self._last_serviced:
|
if found_objective.name != self._last_serviced:
|
||||||
self.action.do(self, state)
|
self.action.do(self, state)
|
||||||
self._last_serviced = found_objective.name
|
self._last_serviced = found_objective.name
|
||||||
else:
|
else:
|
||||||
action = self.get_move_action(state)
|
action = self.get_move_action(state)
|
||||||
return action.do(self, state)
|
result = action.do(self, state)
|
||||||
else:
|
else:
|
||||||
action = self.get_move_action(state)
|
action = self.get_move_action(state)
|
||||||
return action.do(self, state)
|
result = action.do(self, state)
|
||||||
|
self.set_state(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def set_state(self, action_result):
|
||||||
|
self._status = action_result
|
||||||
|
|
||||||
def get_move_action(self, state) -> Action:
|
def get_move_action(self, state) -> Action:
|
||||||
if self._path is None or not len(self._path):
|
if self._path is None or not len(self._path):
|
||||||
|
@ -152,6 +152,9 @@ class Gamestate(object):
|
|||||||
results = list()
|
results = list()
|
||||||
self.curr_step += 1
|
self.curr_step += 1
|
||||||
|
|
||||||
|
for entity in self.entities.iter_entities():
|
||||||
|
entity.clear_temp_state()
|
||||||
|
|
||||||
# Main Agent Step
|
# Main Agent Step
|
||||||
results.extend(self.rules.tick_pre_step_all(self))
|
results.extend(self.rules.tick_pre_step_all(self))
|
||||||
|
|
||||||
@ -210,6 +213,7 @@ class Gamestate(object):
|
|||||||
"""
|
"""
|
||||||
Whether it is safe to move to the target positions and moving entity does not introduce a blocking attribute,
|
Whether it is safe to move to the target positions and moving entity does not introduce a blocking attribute,
|
||||||
when position is allready occupied.
|
when position is allready occupied.
|
||||||
|
!!! Will still report true even though, there could be an enity, which var_can_collide == true !!!
|
||||||
|
|
||||||
:param moving_entity: Entity
|
:param moving_entity: Entity
|
||||||
:param target_position: pos
|
:param target_position: pos
|
||||||
|
@ -29,7 +29,7 @@ if __name__ == '__main__':
|
|||||||
ce.save_all(run_path / 'all_out.yaml')
|
ce.save_all(run_path / 'all_out.yaml')
|
||||||
|
|
||||||
# Path to config File
|
# Path to config File
|
||||||
path = Path('marl_factory_grid/configs/eight_puzzle.yaml')
|
path = Path('marl_factory_grid/configs/default_config.yaml')
|
||||||
|
|
||||||
# Env Init
|
# Env Init
|
||||||
factory = Factory(path)
|
factory = Factory(path)
|
||||||
|
Reference in New Issue
Block a user