mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-09-13 22:44:00 +02:00
Machines
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .entitites import Machine
|
||||
from .groups import Machines
|
||||
from .rules import MachineRule
|
||||
|
25
marl_factory_grid/modules/machines/actions.py
Normal file
25
marl_factory_grid/modules/machines/actions.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from typing import Union
|
||||
|
||||
from marl_factory_grid.environment.actions import Action
|
||||
from marl_factory_grid.utils.results import ActionResult
|
||||
|
||||
from marl_factory_grid.modules.machines import constants as m, rewards as r
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
|
||||
class MachineAction(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(m.MACHINE_ACTION)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if machine := state[m.MACHINES].by_pos(entity.pos):
|
||||
if valid := machine.maintain():
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=r.MAINTAIN_VALID)
|
||||
else:
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=r.MAINTAIN_FAIL)
|
||||
else:
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID, reward=r.MAINTAIN_FAIL)
|
||||
|
||||
|
||||
|
@@ -2,6 +2,8 @@
|
||||
MACHINES = 'Machines'
|
||||
MACHINE = 'Machine'
|
||||
|
||||
MACHINE_ACTION = 'Maintain'
|
||||
|
||||
STATE_WORK = 'working'
|
||||
STATE_IDLE = 'idling'
|
||||
STATE_MAINTAIN = 'maintenance'
|
||||
|
@@ -2,27 +2,43 @@ from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.utils.render import RenderEntity
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.utils.results import TickResult
|
||||
from marl_factory_grid.modules.machines import constants as m, rewards as r
|
||||
|
||||
from . import constants as m
|
||||
|
||||
|
||||
class Machine(Entity):
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_move(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_has_position(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return self._encodings[self.state]
|
||||
return self._encodings[self.status]
|
||||
|
||||
def __init__(self, *args, work_interval: int = 10, pause_interval: int = 15, **kwargs):
|
||||
super(Machine, self).__init__(*args, **kwargs)
|
||||
self._intervals = dict({m.STATE_IDLE: pause_interval, m.STATE_WORK: work_interval})
|
||||
self._encodings = dict({m.STATE_IDLE: pause_interval, m.STATE_WORK: work_interval})
|
||||
|
||||
self.state = m.STATE_IDLE
|
||||
self.status = m.STATE_IDLE
|
||||
self.health = 100
|
||||
self._counter = 0
|
||||
self.__delattr__('move')
|
||||
|
||||
def maintain(self):
|
||||
if self.state == m.STATE_WORK:
|
||||
if self.status == m.STATE_WORK:
|
||||
return c.NOT_VALID
|
||||
if self.health <= 98:
|
||||
self.health = 100
|
||||
@@ -31,10 +47,10 @@ class Machine(Entity):
|
||||
return c.NOT_VALID
|
||||
|
||||
def tick(self):
|
||||
if self.state == m.STATE_MAINTAIN and any([c.AGENT in x.name for x in self.tile.guests]):
|
||||
return TickResult(self.name, c.VALID, r.NONE, self)
|
||||
elif self.state == m.STATE_MAINTAIN and not any([c.AGENT in x.name for x in self.tile.guests]):
|
||||
self.state = m.STATE_WORK
|
||||
if self.status == m.STATE_MAINTAIN and any([c.AGENT in x.name for x in self.tile.guests]):
|
||||
return TickResult(identifier=self.name, validity=c.VALID, reward=0, entity=self)
|
||||
elif self.status == m.STATE_MAINTAIN and not any([c.AGENT in x.name for x in self.tile.guests]):
|
||||
self.status = m.STATE_WORK
|
||||
self.reset_counter()
|
||||
return None
|
||||
elif self._counter:
|
||||
@@ -42,12 +58,12 @@ class Machine(Entity):
|
||||
self.health -= 1
|
||||
return None
|
||||
else:
|
||||
self.state = m.STATE_WORK if self.state == m.STATE_IDLE else m.STATE_IDLE
|
||||
self.status = m.STATE_WORK if self.status == m.STATE_IDLE else m.STATE_IDLE
|
||||
self.reset_counter()
|
||||
return None
|
||||
|
||||
def reset_counter(self):
|
||||
self._counter = self._intervals[self.state]
|
||||
self._counter = self._intervals[self.status]
|
||||
|
||||
def render(self):
|
||||
return RenderEntity(m.MACHINE, self.pos)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
from marl_factory_grid.environment.groups.env_objects import EnvObjects
|
||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
||||
from marl_factory_grid.modules.machines.entitites import Machine
|
||||
|
||||
from .entitites import Machine
|
||||
|
||||
|
||||
class Machines(PositionMixin, EnvObjects):
|
||||
|
BIN
marl_factory_grid/modules/machines/machine.png
Normal file
BIN
marl_factory_grid/modules/machines/machine.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.5 KiB |
@@ -12,7 +12,7 @@ class MachineRule(Rule):
|
||||
super(MachineRule, self).__init__()
|
||||
self.n_machines = n_machines
|
||||
|
||||
def on_init(self, state):
|
||||
def on_init(self, state, lvl_map):
|
||||
empty_tiles = state[c.FLOOR].empty_tiles[:self.n_machines]
|
||||
state[m.MACHINES].add_items(Machine(tile) for tile in empty_tiles)
|
||||
|
||||
@@ -27,3 +27,9 @@ class MachineRule(Rule):
|
||||
|
||||
def on_check_done(self, state) -> List[DoneResult]:
|
||||
pass
|
||||
|
||||
|
||||
class DoneOnBreakRule(Rule):
|
||||
|
||||
def on_check_done(self, state) -> List[DoneResult]:
|
||||
pass
|
Reference in New Issue
Block a user