-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
75 lines (63 loc) · 2.65 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
class Game():
def __init__(self, starts, goals, agents, layout):
self.init_state = {
'POSITIONS': starts,
'GOALS': goals,
'AGENTS': dict(zip(list(map(lambda x: x.name, agents)),
agents))
}
self.reach_goal = np.zeros(len(agents))
self.agents = agents
for agent in self.agents:
agent.register(starts[agent.name], goals[agent.name])
self.layout = layout
def pos_profile(self, state):
profile = []
for i, agent in enumerate(self.agents):
profile.append(state['POSITIONS'][agent.name])
return profile
def is_end(self):
# print(self.reach_goal)
return np.all(self.reach_goal == np.ones(len(self.reach_goal)))
def run(self):
state = self.init_state
histry = [self.pos_profile(state)]
while True:
pred_profile = self.pos_profile(state)
print(pred_profile)
succ_profile = []
observations = dict()
for agent in self.agents:
observations[agent.name] = agent.observe(state)
for agent in self.agents:
obs = observations[agent.name]
agent.build_closure(state)
action = agent.get_action(obs)
print(f'{agent.name}: {obs} -> {action}')
succ_profile.append(agent.move(state['POSITIONS'][agent.name],
action))
for i in range(len(succ_profile)):
for j in range(i + 1, len(succ_profile)):
if succ_profile[i] == succ_profile[j]:
print('Vertex conflict: '
'{pred_profile[i]}-{pred_profile[j]}!')
break
elif succ_profile[i] == pred_profile[j] and \
succ_profile[j] == pred_profile[i]:
print('Edge conflict: '
'{pred_profile[i]}-{pred_profile[j]}!')
break
if succ_profile in histry:
print(f'Loop back to {succ_profile}!')
break
histry.append(succ_profile)
for i, agent in enumerate(self.agents):
state['POSITIONS'][agent.name] = succ_profile[i]
self.reach_goal[i] = int(state['POSITIONS'][agent.name]
== state['GOALS'][agent.name])
print('\t|\n\tV')
if self.is_end():
print('Goals reached!\n')
break
return histry