forked from Farama-Foundation/Minigrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplayer_manual_control.py
executable file
·179 lines (142 loc) · 4.98 KB
/
multiplayer_manual_control.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
from threading import Thread, Lock
import time
import argparse
import numpy as np
import gym
import gym_minigrid
from gym_minigrid.wrappers import *
from gym_minigrid.window import Window
from run.run_minigrid_env import make_create_env
import random
class TwoPlayerEnvController(Thread):
def __init__(self, dx, window, agent_names=['agent_1', 'agent_2']):
assert len(agent_names) == 2, "Two Players Only!"
self.agent_names = agent_names
self.dx = dx
self.curr_action_lock = Lock()
self.action_list = []
self.window = window
self.done = {agent_name: False for agent_name in self.agent_names}
self.reset_curr_action()
def reset_curr_action(self):
self.curr_action = {self.agent_names[0]: MiniGridEnv.Actions.no_op,
self.agent_names[1]: MiniGridEnv.Actions.no_op}
def update_action(self, agent_id, action):
self.curr_action_lock.acquire()
self.curr_action[agent_id] = action
self.curr_action_lock.release()
def run(self):
while not self.window.closed:
self.curr_action_lock.acquire()
curr_action = {agent_id: self.curr_action[agent_id]
for agent_id in self.curr_action if not self.done[agent_id]}
self.reset_curr_action()
self.curr_action_lock.release()
self.action_list.append(curr_action)
self.step(curr_action)
time.sleep(self.dx)
def redraw(self):
img = env.grid.render(
args.tile_size,
[env.agents[agent_id].pos for agent_id in env.agent_ids],
[env.agents[agent_id].dir for agent_id in env.agent_ids],
['red', 'green']
)
self.window.show_img(img)
def reset(self):
if args.seed != -1:
env.seed(args.seed)
env.reset()
self.done = {agent_name: False for agent_name in self.agent_names}
if hasattr(env, 'mission'):
print('Mission: %s' % env.mission)
window.set_caption(env.mission)
self.redraw()
def step(self, action):
assert type(action) is dict
obs, reward_dict, self.done, info = env.step(action)
for agent in reward_dict:
print("Agent {}: {}", agent, reward_dict[agent])
if self.done['__all__']:
print('done!')
self.reset()
else:
self.redraw()
def get_key_handler(self):
def key_handler(event):
if event.key == 'escape':
self.window.close()
return
if event.key == 'backspace':
self.reset()
return
if event.key == 'enter':
self.step(env.actions.done)
return
if event.key == 'left':
key_a = env.actions.left
elif event.key == 'right':
key_a = env.actions.right
elif event.key == 'up':
key_a = env.actions.forward
elif event.key == ' ':
key_a = env.actions.toggle
elif event.key == '/':
key_a = env.actions.pickup
elif event.key == '.':
key_a = env.actions.drop
else:
key_a = None
if event.key == 'a':
key_b = env.actions.left
elif event.key == 'd':
key_b = env.actions.right
elif event.key == 'w':
key_b = env.actions.forward
elif event.key == 'x':
key_b = env.actions.toggle
elif event.key == 'e':
key_b = env.actions.pickup
elif event.key == 'c':
key_b = env.actions.drop
else:
key_b = None
if key_a is not None:
self.update_action(self.agent_names[0], key_a)
elif key_b is not None:
self.update_action(self.agent_names[1], key_b)
return key_handler
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--env",
help="gym environment to load",
default='MiniGrid-MultiRoom-N6-v0'
)
parser.add_argument(
"--seed",
type=int,
help="random seed to generate the environment with",
default=-1
)
parser.add_argument(
"--tile_size",
type=int,
help="size at which to render tiles",
default=32
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
config = {'render' : False, 'anneal_horizon': 1000}
create_env = make_create_env(args.env)
env = create_env(config)
window = Window('gym_minigrid - ' + args.env)
controller = TwoPlayerEnvController(dx=0.1, window=window)
key_handler = controller.get_key_handler()
window.reg_key_handler(key_handler)
controller.reset()
controller.run()
# Blocking event loop
window.show(block=True)