mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-21 03:21:34 +02:00
updated stuff
This commit is contained in:
0
environments/factory/__init__.py
Normal file
0
environments/factory/__init__.py
Normal file
38
environments/factory/factory_cleaning.py
Normal file
38
environments/factory/factory_cleaning.py
Normal file
@ -0,0 +1,38 @@
|
||||
import numpy as np
|
||||
from pathlib import Path
|
||||
from environments import helpers as h
|
||||
|
||||
|
||||
class Factory(object):
|
||||
LEVELS_DIR = 'levels'
|
||||
|
||||
def __init__(self, level='simple', n_agents=1, max_steps=1e3):
|
||||
self.n_agents = n_agents
|
||||
self.max_steps = max_steps
|
||||
self.level = h.one_hot_level(
|
||||
h.parse_level(Path(__file__).parent / self.LEVELS_DIR / f'{level}.txt')
|
||||
)#[np.newaxis, ...]
|
||||
self.reset()
|
||||
|
||||
def reset(self):
|
||||
self.done = False
|
||||
self.agents = np.zeros((self.n_agents, *self.level.shape))
|
||||
free_cells = np.argwhere(self.level == 0)
|
||||
np.random.shuffle(free_cells)
|
||||
for i in range(self.n_agents):
|
||||
r, c = free_cells[i]
|
||||
self.agents[i, r, c] = 1
|
||||
free_cells = free_cells[self.n_agents:]
|
||||
self.state = np.concatenate((self.level[np.newaxis, ...], self.agents), 0)
|
||||
|
||||
def step(self, actions):
|
||||
assert type(actions) in [int, list]
|
||||
if type(actions) == int:
|
||||
actions = [actions]
|
||||
# level, agent 1,..., agent n,
|
||||
for i, a in enumerate(actions):
|
||||
h.check_agent_move(state=self.state, dim=i+1, action=a)
|
||||
|
||||
if __name__ == '__main__':
|
||||
factory = Factory(n_agents=1)
|
||||
factory.step(0)
|
0
environments/factory/levels/__init__.py
Normal file
0
environments/factory/levels/__init__.py
Normal file
10
environments/factory/levels/simple.txt
Normal file
10
environments/factory/levels/simple.txt
Normal file
@ -0,0 +1,10 @@
|
||||
----------
|
||||
----------
|
||||
--------#-
|
||||
----------
|
||||
----------
|
||||
----------
|
||||
----#-----
|
||||
----------
|
||||
----------
|
||||
----------
|
@ -1 +0,0 @@
|
||||
import numpy as np
|
57
environments/helpers.py
Normal file
57
environments/helpers.py
Normal file
@ -0,0 +1,57 @@
|
||||
import numpy as np
|
||||
from pathlib import Path
|
||||
|
||||
# Constants
|
||||
WALL = '#'
|
||||
|
||||
|
||||
# Utility functions
|
||||
def parse_level(path):
|
||||
with path.open('r') as lvl:
|
||||
level = list(map(lambda x: list(x.strip()), lvl.readlines()))
|
||||
if len(set([len(line) for line in level])) > 1:
|
||||
raise AssertionError('Every row of the level string must be of equal length.')
|
||||
return level
|
||||
|
||||
|
||||
def one_hot_level(level, wall_char=WALL):
|
||||
grid = np.array(level)
|
||||
binary_grid = np.zeros(grid.shape)
|
||||
binary_grid[grid == wall_char] = 1
|
||||
return binary_grid
|
||||
|
||||
|
||||
def check_agent_move(state, dim, action):
|
||||
agent_slice = state[dim] # horizontal slice from state tensor
|
||||
agent_pos = np.argwhere(agent_slice == 1)
|
||||
if len(agent_pos) > 1:
|
||||
raise AssertionError('Only one agent per slice is allowed.')
|
||||
x, y = agent_pos[0]
|
||||
x_new, y_new = x, y
|
||||
if action == 0: # North
|
||||
x_new -= 1
|
||||
elif action == 1: # East
|
||||
y_new += 1
|
||||
elif action == 2: # South
|
||||
x_new += 1
|
||||
elif action == 3: # West
|
||||
y_new -= 1
|
||||
elif action == 4: # NE
|
||||
x_new -= 1
|
||||
y_new += 1
|
||||
elif action == 5: # SE
|
||||
x_new += 1
|
||||
y_new += 1
|
||||
elif action == 6: # SW
|
||||
x_new += 1
|
||||
y_new -= 1
|
||||
elif action == 7: # NW
|
||||
x_new -= 1
|
||||
y_new -= 1
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
x = parse_level(Path(__file__).parent / 'factory' / 'levels' / 'simple.txt')
|
||||
y = one_hot_level(x)
|
||||
print(np.argwhere(y == 0))
|
Reference in New Issue
Block a user