mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-05-23 15:26:43 +02:00
updated stuff
This commit is contained in:
parent
020288fd55
commit
83d399901b
@ -16,7 +16,7 @@ class Factory(object):
|
|||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self.done = False
|
self.done = False
|
||||||
self.agents = np.zeros((self.n_agents, *self.level.shape))
|
self.agents = np.zeros((self.n_agents, *self.level.shape), dtype=np.int8)
|
||||||
free_cells = np.argwhere(self.level == 0)
|
free_cells = np.argwhere(self.level == 0)
|
||||||
np.random.shuffle(free_cells)
|
np.random.shuffle(free_cells)
|
||||||
for i in range(self.n_agents):
|
for i in range(self.n_agents):
|
||||||
@ -24,17 +24,39 @@ class Factory(object):
|
|||||||
self.agents[i, r, c] = 1
|
self.agents[i, r, c] = 1
|
||||||
free_cells = free_cells[self.n_agents:]
|
free_cells = free_cells[self.n_agents:]
|
||||||
self.state = np.concatenate((self.level[np.newaxis, ...], self.agents), 0)
|
self.state = np.concatenate((self.level[np.newaxis, ...], self.agents), 0)
|
||||||
|
return self.state, 0, self.done, {}
|
||||||
|
|
||||||
def step(self, actions):
|
def step(self, actions):
|
||||||
assert type(actions) in [int, list]
|
assert type(actions) in [int, list]
|
||||||
if type(actions) == int:
|
if type(actions) == int:
|
||||||
actions = [actions]
|
actions = [actions]
|
||||||
|
r = 0
|
||||||
# level, agent 1,..., agent n,
|
# level, agent 1,..., agent n,
|
||||||
for i, a in enumerate(actions):
|
for i, a in enumerate(actions):
|
||||||
old_pos, new_pos, valid = h.check_agent_move(state=self.state, dim=i+1, action=a)
|
old_pos, new_pos, valid = h.check_agent_move(state=self.state, dim=i+1, action=a)
|
||||||
print(old_pos, new_pos, valid)
|
if valid:
|
||||||
|
self.make_move(i, old_pos, new_pos)
|
||||||
|
collision_vecs = []
|
||||||
|
for i in range(self.n_agents): # might as well save the positions (redundant)
|
||||||
|
agent_slice = self.state[i+1]
|
||||||
|
x, y = np.argwhere(agent_slice == 1)[0]
|
||||||
|
collisions_vec = self.state[:, x, y]
|
||||||
|
collisions_vec[i+1] = 0 # no self-collisions
|
||||||
|
collision_vecs.append(collisions_vec)
|
||||||
|
self.handle_collisions(collisions_vec)
|
||||||
|
|
||||||
|
return self.state, r, self.done, {}
|
||||||
|
|
||||||
|
def make_move(self, agent_i, old_pos, new_pos):
|
||||||
|
(x, y), (x_new, y_new) = old_pos, new_pos
|
||||||
|
self.state[agent_i, x, y] = 0
|
||||||
|
self.state[agent_i, x_new, y_new] = 1
|
||||||
|
|
||||||
|
def handle_collisions(self, vecs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
factory = Factory(n_agents=1)
|
factory = Factory(n_agents=1)
|
||||||
factory.step(0)
|
factory.step(0)
|
||||||
|
print(factory.state.shape)
|
@ -16,7 +16,7 @@ def parse_level(path):
|
|||||||
|
|
||||||
def one_hot_level(level, wall_char=WALL):
|
def one_hot_level(level, wall_char=WALL):
|
||||||
grid = np.array(level)
|
grid = np.array(level)
|
||||||
binary_grid = np.zeros(grid.shape)
|
binary_grid = np.zeros(grid.shape, dtype=np.int8)
|
||||||
binary_grid[grid == wall_char] = 1
|
binary_grid[grid == wall_char] = 1
|
||||||
return binary_grid
|
return binary_grid
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user