mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-21 03:21:34 +02:00
updated simple factory
This commit is contained in:
@ -46,7 +46,6 @@ class BaseFactory(object):
|
|||||||
x, y = np.argwhere(agent_slice == 1)[0]
|
x, y = np.argwhere(agent_slice == 1)[0]
|
||||||
collisions_vec = self.state[:, x, y].copy() # otherwise you overwrite the grid/state
|
collisions_vec = self.state[:, x, y].copy() # otherwise you overwrite the grid/state
|
||||||
collisions_vec[i+1] = 0 # no self-collisions
|
collisions_vec[i+1] = 0 # no self-collisions
|
||||||
#collision_vecs.append(collisions_vec)
|
|
||||||
collision_vecs[i] += collisions_vec
|
collision_vecs[i] += collisions_vec
|
||||||
reward, info = self.step_core(np.array(collision_vecs), actions, r)
|
reward, info = self.step_core(np.array(collision_vecs), actions, r)
|
||||||
r += reward
|
r += reward
|
||||||
|
@ -8,18 +8,21 @@ class SimpleFactory(BaseFactory):
|
|||||||
super(SimpleFactory, self).__init__(*args, **kwargs)
|
super(SimpleFactory, self).__init__(*args, **kwargs)
|
||||||
self.slice_strings.update({self.state.shape[0]-1: 'dirt'})
|
self.slice_strings.update({self.state.shape[0]-1: 'dirt'})
|
||||||
|
|
||||||
|
def spawn_dirt(self):
|
||||||
|
free_for_dirt = self.free_cells()
|
||||||
|
for x, y in free_for_dirt[:self.max_dirt]: # randomly distribute dirt across the grid
|
||||||
|
self.state[-1, x, y] = 1
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
super().reset()
|
super().reset()
|
||||||
dirt_slice = np.zeros((1, *self.state.shape[1:]))
|
dirt_slice = np.zeros((1, *self.state.shape[1:]))
|
||||||
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
|
self.state = np.concatenate((self.state, dirt_slice)) # dirt is now the last slice
|
||||||
free_for_dirt = self.free_cells()
|
self.spawn_dirt()
|
||||||
for x, y in free_for_dirt[:self.max_dirt]:
|
|
||||||
self.state[-1, x, y] = 1
|
|
||||||
|
|
||||||
def step_core(self, collisions_vecs, actions, r):
|
def step_core(self, collisions_vecs, actions, r):
|
||||||
for agent_i, cols in enumerate(collisions_vecs):
|
for agent_i, cols in enumerate(collisions_vecs):
|
||||||
cols = np.argwhere(cols != 0).flatten()
|
cols = np.argwhere(cols != 0).flatten()
|
||||||
print(f'Agent #{agent_i} has collisions with '
|
print(f't = {self.steps}\tAgent {agent_i} has collisions with '
|
||||||
f'{[self.slice_strings[entity] for entity in cols]}')
|
f'{[self.slice_strings[entity] for entity in cols]}')
|
||||||
return 0, {}
|
return 0, {}
|
||||||
|
|
||||||
@ -28,6 +31,6 @@ class SimpleFactory(BaseFactory):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import random
|
import random
|
||||||
factory = SimpleFactory(n_agents=1, max_dirt=8)
|
factory = SimpleFactory(n_agents=1, max_dirt=8)
|
||||||
random_actions = [random.randint(0,8) for _ in range(200)]
|
random_actions = [random.randint(0, 8) for _ in range(200)]
|
||||||
for action in random_actions:
|
for action in random_actions:
|
||||||
state, r, done, _ = factory.step(action)
|
state, r, done, _ = factory.step(action)
|
Reference in New Issue
Block a user