mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-12-11 01:50:38 +01:00
added changes from code submission branch and coin entity
This commit is contained in:
4
marl_factory_grid/modules/coins/__init__.py
Normal file
4
marl_factory_grid/modules/coins/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .actions import Collect
|
||||
from .entitites import CoinPile
|
||||
from .groups import CoinPiles
|
||||
from .rules import DoneOnAllCoinsCollected
|
||||
36
marl_factory_grid/modules/coins/actions.py
Normal file
36
marl_factory_grid/modules/coins/actions.py
Normal file
@@ -0,0 +1,36 @@
|
||||
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.coins import constants as d
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
|
||||
class Collect(Action):
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Attempts to reduce coin amount on entity's position. Fails if no coin is found at the at agents' position.
|
||||
"""
|
||||
super().__init__(d.COLLECT, d.REWARD_COLLECT_VALID, d.REWARD_COLLECT_FAIL)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if coin_pile := next((x for x in state.entities.pos_dict[entity.pos] if "coin" in x.name.lower()), None):
|
||||
new_coin_pile_amount = coin_pile.amount - state[d.COIN].collect_amount
|
||||
|
||||
if new_coin_pile_amount <= 0:
|
||||
state[d.COIN].delete_env_object(coin_pile)
|
||||
else:
|
||||
coin_pile.set_new_amount(max(new_coin_pile_amount, c.VALUE_FREE_CELL))
|
||||
valid = c.VALID
|
||||
print_str = f'{entity.name} did just collect some coins at {entity.pos}.'
|
||||
state.print(print_str)
|
||||
|
||||
else:
|
||||
valid = c.NOT_VALID
|
||||
print_str = f'{entity.name} just tried to collect some coins at {entity.pos}, but failed.'
|
||||
state.print(print_str)
|
||||
|
||||
return self.get_result(valid, entity)
|
||||
BIN
marl_factory_grid/modules/coins/coinpiles.png
Normal file
BIN
marl_factory_grid/modules/coins/coinpiles.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
11
marl_factory_grid/modules/coins/constants.py
Normal file
11
marl_factory_grid/modules/coins/constants.py
Normal file
@@ -0,0 +1,11 @@
|
||||
COIN = 'CoinPiles'
|
||||
|
||||
COLLECT = 'do_collect_action'
|
||||
|
||||
COLLECT_VALID = 'collect_valid'
|
||||
COLLECT_FAIL = 'collect_fail'
|
||||
COLLECT_ALL = 'all_collected'
|
||||
|
||||
REWARD_COLLECT_VALID: float = 0.5
|
||||
REWARD_COLLECT_FAIL: float = -0.1
|
||||
REWARD_COLLECT_ALL: float = 4.5
|
||||
46
marl_factory_grid/modules/coins/entitites.py
Normal file
46
marl_factory_grid/modules/coins/entitites.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.utils.utility_classes import RenderEntity
|
||||
from marl_factory_grid.modules.coins import constants as d
|
||||
|
||||
|
||||
class CoinPile(Entity):
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
"""
|
||||
Internal Usage
|
||||
"""
|
||||
return self._amount
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return self._amount
|
||||
|
||||
def __init__(self, *args, amount=2, max_local_amount=5, **kwargs):
|
||||
"""
|
||||
Represents a pile of coins at a specific position in the environment that agents can interact with. Agents can
|
||||
clean the dirt pile or, depending on activated rules, interact with it in different ways.
|
||||
|
||||
:param amount: The amount of coins in the pile.
|
||||
:type amount: float
|
||||
|
||||
:param max_local_amount: The maximum amount of dirt allowed in a single pile at one position.
|
||||
:type max_local_amount: float
|
||||
"""
|
||||
super(CoinPile, self).__init__(*args, **kwargs)
|
||||
self._amount = amount
|
||||
self.max_local_amount = max_local_amount
|
||||
|
||||
def set_new_amount(self, amount):
|
||||
"""
|
||||
Internal Usage
|
||||
"""
|
||||
self._amount = min(amount, self.max_local_amount)
|
||||
|
||||
def summarize_state(self):
|
||||
state_dict = super().summarize_state()
|
||||
state_dict.update(amount=float(self.amount))
|
||||
return state_dict
|
||||
|
||||
def render(self):
|
||||
return RenderEntity(d.COIN, self.pos, min(0.15 + self.amount, 1.5), 'scale')
|
||||
108
marl_factory_grid/modules/coins/groups.py
Normal file
108
marl_factory_grid/modules/coins/groups.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import ast
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.environment.groups.collection import Collection
|
||||
from marl_factory_grid.modules.coins.entitites import CoinPile
|
||||
from marl_factory_grid.utils.results import Result
|
||||
from marl_factory_grid.utils import helpers as h
|
||||
|
||||
|
||||
class CoinPiles(Collection):
|
||||
_entity = CoinPile
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_move(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_has_position(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def global_amount(self) -> float:
|
||||
"""
|
||||
Internal Usage
|
||||
"""
|
||||
return sum([dirt.amount for dirt in self])
|
||||
|
||||
def __init__(self, *args, max_local_amount=5, collect_amount=1, max_global_amount: int = 20, coords_or_quantity=10,
|
||||
initial_amount=2, amount_var=0.2, n_var=0.2, **kwargs):
|
||||
"""
|
||||
A Collection of dirt piles that triggers their spawn.
|
||||
|
||||
:param max_local_amount: The maximum amount of coins allowed in a single pile at one position.
|
||||
:type max_local_amount: int
|
||||
|
||||
:param clean_amount: The amount of coins removed by a single collecting action.
|
||||
:type clean_amount: int
|
||||
|
||||
:param max_global_amount: The maximum total amount of coins allowed in the environment.
|
||||
:type max_global_amount: int
|
||||
|
||||
:param coords_or_quantity: Determines whether to use coordinates or quantity when triggering coin pile spawn.
|
||||
:type coords_or_quantity: Union[Tuple[int, int], int]
|
||||
|
||||
:param initial_amount: The initial amount of coin in each newly spawned pile.
|
||||
:type initial_amount: int
|
||||
|
||||
:param amount_var: The variability in the initial amount of coin in each pile.
|
||||
:type amount_var: float
|
||||
|
||||
:param n_var: The variability in the number of new coin piles spawned.
|
||||
:type n_var: float
|
||||
|
||||
"""
|
||||
super(CoinPiles, self).__init__(*args, **kwargs)
|
||||
self.amount_var = amount_var
|
||||
self.n_var = n_var
|
||||
self.collect_amount = collect_amount
|
||||
self.max_global_amount = max_global_amount
|
||||
self.max_local_amount = max_local_amount
|
||||
self.coords_or_quantity = coords_or_quantity
|
||||
self.initial_amount = initial_amount
|
||||
|
||||
def trigger_spawn(self, state, coords_or_quantity=0, amount=0, ignore_blocking=False) -> [Result]:
|
||||
if ignore_blocking:
|
||||
print("##########################################")
|
||||
print("Blocking should not be ignored for this Entity")
|
||||
print("Exiting....")
|
||||
exit()
|
||||
coords_or_quantity = coords_or_quantity if coords_or_quantity else self.coords_or_quantity
|
||||
if isinstance(coords_or_quantity, int):
|
||||
n_new = int(abs(coords_or_quantity + (state.rng.uniform(-self.n_var, self.n_var))))
|
||||
n_new = state.get_n_random_free_positions(n_new)
|
||||
else:
|
||||
coords_or_quantity = ast.literal_eval(coords_or_quantity)
|
||||
if isinstance(coords_or_quantity[0], int):
|
||||
n_new = [coords_or_quantity]
|
||||
else:
|
||||
n_new = [pos for pos in coords_or_quantity]
|
||||
|
||||
amounts = [amount if amount else (self.initial_amount ) # removed rng amount
|
||||
for _ in range(len(n_new))]
|
||||
|
||||
spawn_counter = 0
|
||||
for idx, (pos, a) in enumerate(zip(n_new, amounts)):
|
||||
if not self.global_amount > self.max_global_amount:
|
||||
if coin := self.by_pos(pos):
|
||||
coin = h.get_first(coin)
|
||||
new_value = coin.amount + a
|
||||
coin.set_new_amount(new_value)
|
||||
else:
|
||||
super().spawn([pos], amount=a)
|
||||
spawn_counter += 1
|
||||
else:
|
||||
return Result(identifier=f'{self.name}_spawn', validity=c.NOT_VALID, value=spawn_counter)
|
||||
|
||||
return Result(identifier=f'{self.name}_spawn', validity=c.VALID, value=spawn_counter)
|
||||
|
||||
def __repr__(self):
|
||||
s = super(CoinPiles, self).__repr__()
|
||||
return f'{s[:-1]}, {self.global_amount}]'
|
||||
59
marl_factory_grid/modules/coins/rules.py
Normal file
59
marl_factory_grid/modules/coins/rules.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from marl_factory_grid.modules.coins import constants as d
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
from marl_factory_grid.environment.rules import Rule
|
||||
from marl_factory_grid.utils.helpers import is_move
|
||||
from marl_factory_grid.utils.results import TickResult
|
||||
from marl_factory_grid.utils.results import DoneResult
|
||||
|
||||
|
||||
class DoneOnAllCoinsCollected(Rule):
|
||||
|
||||
def __init__(self, reward: float = d.REWARD_COLLECT_ALL):
|
||||
"""
|
||||
Defines a 'Done'-condition which triggers, when there is no more 'Dirt' in the environment.
|
||||
|
||||
:type reward: float
|
||||
:parameter reward: Given reward when condition triggers.
|
||||
"""
|
||||
super().__init__()
|
||||
self.reward = reward
|
||||
|
||||
def on_check_done(self, state) -> [DoneResult]:
|
||||
if len(state[d.COIN]) == 0 and state.curr_step:
|
||||
return [DoneResult(validity=c.VALID, identifier=self.name, reward=self.reward)]
|
||||
return []
|
||||
|
||||
|
||||
class RespawnCoins(Rule):
|
||||
|
||||
def __init__(self, respawn_freq: int = 15, respawn_n: int = 5, respawn_amount: float = 1.0):
|
||||
"""
|
||||
Defines the spawn pattern of initial and additional 'Dirt'-entities.
|
||||
First chooses positions, then tries to spawn dirt until 'respawn_n' or the maximal global amount is reached.
|
||||
If there is already some, it is topped up to min(max_local_amount, amount).
|
||||
|
||||
:type respawn_freq: int
|
||||
:parameter respawn_freq: In which frequency should this Rule try to spawn new 'Dirt'?
|
||||
:type respawn_n: int
|
||||
:parameter respawn_n: How many respawn positions are considered.
|
||||
:type respawn_amount: float
|
||||
:parameter respawn_amount: Defines how much dirt 'amount' is placed every 'spawn_freq' ticks.
|
||||
"""
|
||||
super().__init__()
|
||||
self.respawn_n = respawn_n
|
||||
self.respawn_amount = respawn_amount
|
||||
self.respawn_freq = respawn_freq
|
||||
self._next_coin_spawn = respawn_freq
|
||||
|
||||
def tick_step(self, state):
|
||||
collection = state[d.COIN]
|
||||
if self._next_coin_spawn < 0:
|
||||
result = [] # No CoinPile Spawn
|
||||
elif not self._next_coin_spawn:
|
||||
result = [collection.trigger_spawn(state, coords_or_quantity=self.respawn_n, amount=self.respawn_amount)]
|
||||
self._next_coin_spawn = self.respawn_freq
|
||||
else:
|
||||
self._next_coin_spawn -= 1
|
||||
result = []
|
||||
return result
|
||||
Reference in New Issue
Block a user