-
Notifications
You must be signed in to change notification settings - Fork 36
/
overcooked_environment.py
472 lines (390 loc) · 18.7 KB
/
overcooked_environment.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Recipe planning
from recipe_planner.stripsworld import STRIPSWorld
import recipe_planner.utils as recipe
from recipe_planner.recipe import *
# Delegation planning
from delegation_planner.bayesian_delegator import BayesianDelegator
# Navigation planning
import navigation_planner.utils as nav_utils
# Other core modules
from utils.interact import interact
from utils.world import World
from utils.core import *
from utils.agent import SimAgent
from misc.game.gameimage import GameImage
from utils.agent import COLORS
import copy
import networkx as nx
import numpy as np
from itertools import combinations, permutations, product
from collections import namedtuple
import gym
from gym import error, spaces, utils
from gym.utils import seeding
CollisionRepr = namedtuple("CollisionRepr", "time agent_names agent_locations")
class OvercookedEnvironment(gym.Env):
"""Environment object for Overcooked."""
def __init__(self, arglist):
self.arglist = arglist
self.t = 0
self.set_filename()
# For visualizing episode.
self.rep = []
# For tracking data during an episode.
self.collisions = []
self.termination_info = ""
self.successful = False
def get_repr(self):
return self.world.get_repr() + tuple([agent.get_repr() for agent in self.sim_agents])
def __str__(self):
# Print the world and agents.
_display = list(map(lambda x: ''.join(map(lambda y: y + ' ', x)), self.rep))
return '\n'.join(_display)
def __eq__(self, other):
return self.get_repr() == other.get_repr()
def __copy__(self):
new_env = OvercookedEnvironment(self.arglist)
new_env.__dict__ = self.__dict__.copy()
new_env.world = copy.copy(self.world)
new_env.sim_agents = [copy.copy(a) for a in self.sim_agents]
new_env.distances = self.distances
# Make sure new objects and new agents' holdings have the right pointers.
for a in new_env.sim_agents:
if a.holding is not None:
a.holding = new_env.world.get_object_at(
location=a.location,
desired_obj=None,
find_held_objects=True)
return new_env
def set_filename(self):
self.filename = "{}_agents{}_seed{}".format(self.arglist.level,\
self.arglist.num_agents, self.arglist.seed)
model = ""
if self.arglist.model1 is not None:
model += "_model1-{}".format(self.arglist.model1)
if self.arglist.model2 is not None:
model += "_model2-{}".format(self.arglist.model2)
if self.arglist.model3 is not None:
model += "_model3-{}".format(self.arglist.model3)
if self.arglist.model4 is not None:
model += "_model4-{}".format(self.arglist.model4)
self.filename += model
def load_level(self, level, num_agents):
x = 0
y = 0
with open('utils/levels/{}.txt'.format(level), 'r') as file:
# Mark the phases of reading.
phase = 1
for line in file:
line = line.strip('\n')
if line == '':
phase += 1
# Phase 1: Read in kitchen map.
elif phase == 1:
for x, rep in enumerate(line):
# Object, i.e. Tomato, Lettuce, Onion, or Plate.
if rep in 'tlop':
counter = Counter(location=(x, y))
obj = Object(
location=(x, y),
contents=RepToClass[rep]())
counter.acquire(obj=obj)
self.world.insert(obj=counter)
self.world.insert(obj=obj)
# GridSquare, i.e. Floor, Counter, Cutboard, Delivery.
elif rep in RepToClass:
newobj = RepToClass[rep]((x, y))
self.world.objects.setdefault(newobj.name, []).append(newobj)
else:
# Empty. Set a Floor tile.
f = Floor(location=(x, y))
self.world.objects.setdefault('Floor', []).append(f)
y += 1
# Phase 2: Read in recipe list.
elif phase == 2:
self.recipes.append(globals()[line]())
# Phase 3: Read in agent locations (up to num_agents).
elif phase == 3:
if len(self.sim_agents) < num_agents:
loc = line.split(' ')
sim_agent = SimAgent(
name='agent-'+str(len(self.sim_agents)+1),
id_color=COLORS[len(self.sim_agents)],
location=(int(loc[0]), int(loc[1])))
self.sim_agents.append(sim_agent)
self.distances = {}
self.world.width = x+1
self.world.height = y
self.world.perimeter = 2*(self.world.width + self.world.height)
def reset(self):
self.world = World(arglist=self.arglist)
self.recipes = []
self.sim_agents = []
self.agent_actions = {}
self.t = 0
# For visualizing episode.
self.rep = []
# For tracking data during an episode.
self.collisions = []
self.termination_info = ""
self.successful = False
# Load world & distances.
self.load_level(
level=self.arglist.level,
num_agents=self.arglist.num_agents)
self.all_subtasks = self.run_recipes()
self.world.make_loc_to_gridsquare()
self.world.make_reachability_graph()
self.cache_distances()
self.obs_tm1 = copy.copy(self)
if self.arglist.record or self.arglist.with_image_obs:
self.game = GameImage(
filename=self.filename,
world=self.world,
sim_agents=self.sim_agents,
record=self.arglist.record)
self.game.on_init()
if self.arglist.record:
self.game.save_image_obs(self.t)
return copy.copy(self)
def close(self):
return
def step(self, action_dict):
# Track internal environment info.
self.t += 1
print("===============================")
print("[environment.step] @ TIMESTEP {}".format(self.t))
print("===============================")
# Get actions.
for sim_agent in self.sim_agents:
sim_agent.action = action_dict[sim_agent.name]
# Check collisions.
self.check_collisions()
self.obs_tm1 = copy.copy(self)
# Execute.
self.execute_navigation()
# Visualize.
self.display()
self.print_agents()
if self.arglist.record:
self.game.save_image_obs(self.t)
# Get a plan-representation observation.
new_obs = copy.copy(self)
# Get an image observation
image_obs = self.game.get_image_obs()
done = self.done()
reward = self.reward()
info = {"t": self.t, "obs": new_obs,
"image_obs": image_obs,
"done": done, "termination_info": self.termination_info}
return new_obs, reward, done, info
def done(self):
# Done if the episode maxes out
if self.t >= self.arglist.max_num_timesteps and self.arglist.max_num_timesteps:
self.termination_info = "Terminating because passed {} timesteps".format(
self.arglist.max_num_timesteps)
self.successful = False
return True
assert any([isinstance(subtask, recipe.Deliver) for subtask in self.all_subtasks]), "no delivery subtask"
# Done if subtask is completed.
for subtask in self.all_subtasks:
# Double check all goal_objs are at Delivery.
if isinstance(subtask, recipe.Deliver):
_, goal_obj = nav_utils.get_subtask_obj(subtask)
delivery_loc = list(filter(lambda o: o.name=='Delivery', self.world.get_object_list()))[0].location
goal_obj_locs = self.world.get_all_object_locs(obj=goal_obj)
if not any([gol == delivery_loc for gol in goal_obj_locs]):
self.termination_info = ""
self.successful = False
return False
self.termination_info = "Terminating because all deliveries were completed"
self.successful = True
return True
def reward(self):
return 1 if self.successful else 0
def print_agents(self):
for sim_agent in self.sim_agents:
sim_agent.print_status()
def display(self):
self.update_display()
print(str(self))
def update_display(self):
self.rep = self.world.update_display()
for agent in self.sim_agents:
x, y = agent.location
self.rep[y][x] = str(agent)
def get_agent_names(self):
return [agent.name for agent in self.sim_agents]
def run_recipes(self):
"""Returns different permutations of completing recipes."""
self.sw = STRIPSWorld(world=self.world, recipes=self.recipes)
# [path for recipe 1, path for recipe 2, ...] where each path is a list of actions
subtasks = self.sw.get_subtasks(max_path_length=self.arglist.max_num_subtasks)
all_subtasks = [subtask for path in subtasks for subtask in path]
print('Subtasks:', all_subtasks, '\n')
return all_subtasks
def get_AB_locs_given_objs(self, subtask, subtask_agent_names, start_obj, goal_obj, subtask_action_obj):
"""Returns list of locations relevant for subtask's Merge operator.
See Merge operator formalism in our paper, under Fig. 11:
https://arxiv.org/pdf/2003.11778.pdf"""
# For Merge operator on Chop subtasks, we look at objects that can be
# chopped and the cutting board objects.
if isinstance(subtask, recipe.Chop):
# A: Object that can be chopped.
A_locs = self.world.get_object_locs(obj=start_obj, is_held=False) + list(map(lambda a: a.location,\
list(filter(lambda a: a.name in subtask_agent_names and a.holding == start_obj, self.sim_agents))))
# B: Cutboard objects.
B_locs = self.world.get_all_object_locs(obj=subtask_action_obj)
# For Merge operator on Deliver subtasks, we look at objects that can be
# delivered and the Delivery object.
elif isinstance(subtask, recipe.Deliver):
# B: Delivery objects.
B_locs = self.world.get_all_object_locs(obj=subtask_action_obj)
# A: Object that can be delivered.
A_locs = self.world.get_object_locs(
obj=start_obj, is_held=False) + list(
map(lambda a: a.location, list(
filter(lambda a: a.name in subtask_agent_names and a.holding == start_obj, self.sim_agents))))
A_locs = list(filter(lambda a: a not in B_locs, A_locs))
# For Merge operator on Merge subtasks, we look at objects that can be
# combined together. These objects are all ingredient objects (e.g. Tomato, Lettuce).
elif isinstance(subtask, recipe.Merge):
A_locs = self.world.get_object_locs(
obj=start_obj[0], is_held=False) + list(
map(lambda a: a.location, list(
filter(lambda a: a.name in subtask_agent_names and a.holding == start_obj[0], self.sim_agents))))
B_locs = self.world.get_object_locs(
obj=start_obj[1], is_held=False) + list(
map(lambda a: a.location, list(
filter(lambda a: a.name in subtask_agent_names and a.holding == start_obj[1], self.sim_agents))))
else:
return [], []
return A_locs, B_locs
def get_lower_bound_for_subtask_given_objs(
self, subtask, subtask_agent_names, start_obj, goal_obj, subtask_action_obj):
"""Return the lower bound distance (shortest path) under this subtask between objects."""
assert len(subtask_agent_names) <= 2, 'passed in {} agents but can only do 1 or 2'.format(len(agents))
# Calculate extra holding penalty if the object is irrelevant.
holding_penalty = 0.0
for agent in self.sim_agents:
if agent.name in subtask_agent_names:
# Check for whether the agent is holding something.
if agent.holding is not None:
if isinstance(subtask, recipe.Merge):
continue
else:
if agent.holding != start_obj and agent.holding != goal_obj:
# Add one "distance"-unit cost
holding_penalty += 1.0
# Account for two-agents where we DON'T want to overpenalize.
holding_penalty = min(holding_penalty, 1)
# Get current agent locations.
agent_locs = [agent.location for agent in list(filter(lambda a: a.name in subtask_agent_names, self.sim_agents))]
A_locs, B_locs = self.get_AB_locs_given_objs(
subtask=subtask,
subtask_agent_names=subtask_agent_names,
start_obj=start_obj,
goal_obj=goal_obj,
subtask_action_obj=subtask_action_obj)
# Add together distance and holding_penalty.
return self.world.get_lower_bound_between(
subtask=subtask,
agent_locs=tuple(agent_locs),
A_locs=tuple(A_locs),
B_locs=tuple(B_locs)) + holding_penalty
def is_collision(self, agent1_loc, agent2_loc, agent1_action, agent2_action):
"""Returns whether agents are colliding.
Collisions happens if agent collide amongst themselves or with world objects."""
# Tracks whether agents can execute their action.
execute = [True, True]
# Collision between agents and world objects.
agent1_next_loc = tuple(np.asarray(agent1_loc) + np.asarray(agent1_action))
if self.world.get_gridsquare_at(location=agent1_next_loc).collidable:
# Revert back because agent collided.
agent1_next_loc = agent1_loc
agent2_next_loc = tuple(np.asarray(agent2_loc) + np.asarray(agent2_action))
if self.world.get_gridsquare_at(location=agent2_next_loc).collidable:
# Revert back because agent collided.
agent2_next_loc = agent2_loc
# Inter-agent collision.
if agent1_next_loc == agent2_next_loc:
if agent1_next_loc == agent1_loc and agent1_action != (0, 0):
execute[1] = False
elif agent2_next_loc == agent2_loc and agent2_action != (0, 0):
execute[0] = False
else:
execute[0] = False
execute[1] = False
# Prevent agents from swapping places.
elif ((agent1_loc == agent2_next_loc) and
(agent2_loc == agent1_next_loc)):
execute[0] = False
execute[1] = False
return execute
def check_collisions(self):
"""Checks for collisions and corrects agents' executable actions.
Collisions can either happen amongst agents or between agents and world objects."""
execute = [True for _ in self.sim_agents]
# Check each pairwise collision between agents.
for i, j in combinations(range(len(self.sim_agents)), 2):
agent_i, agent_j = self.sim_agents[i], self.sim_agents[j]
exec_ = self.is_collision(
agent1_loc=agent_i.location,
agent2_loc=agent_j.location,
agent1_action=agent_i.action,
agent2_action=agent_j.action)
# Update exec array and set path to do nothing.
if not exec_[0]:
execute[i] = False
if not exec_[1]:
execute[j] = False
# Track collisions.
if not all(exec_):
collision = CollisionRepr(
time=self.t,
agent_names=[agent_i.name, agent_j.name],
agent_locations=[agent_i.location, agent_j.location])
self.collisions.append(collision)
print('\nexecute array is:', execute)
# Update agents' actions if collision was detected.
for i, agent in enumerate(self.sim_agents):
if not execute[i]:
agent.action = (0, 0)
print("{} has action {}".format(color(agent.name, agent.color), agent.action))
def execute_navigation(self):
for agent in self.sim_agents:
interact(agent=agent, world=self.world)
self.agent_actions[agent.name] = agent.action
def cache_distances(self):
"""Saving distances between world objects."""
counter_grid_names = [name for name in self.world.objects if "Supply" in name or "Counter" in name or "Delivery" in name or "Cut" in name]
# Getting all source objects.
source_objs = copy.copy(self.world.objects["Floor"])
for name in counter_grid_names:
source_objs += copy.copy(self.world.objects[name])
# Getting all destination objects.
dest_objs = source_objs
# From every source (Counter and Floor objects),
# calculate distance to other nodes.
for source in source_objs:
self.distances[source.location] = {}
# Source to source distance is 0.
self.distances[source.location][source.location] = 0
for destination in dest_objs:
# Possible edges to approach source and destination.
source_edges = [(0, 0)] if not source.collidable else World.NAV_ACTIONS
destination_edges = [(0, 0)] if not destination.collidable else World.NAV_ACTIONS
# Maintain shortest distance.
shortest_dist = np.inf
for source_edge, dest_edge in product(source_edges, destination_edges):
try:
dist = nx.shortest_path_length(self.world.reachability_graph, (source.location,source_edge), (destination.location, dest_edge))
# Update shortest distance.
if dist < shortest_dist:
shortest_dist = dist
except:
continue
# Cache distance floor -> counter.
self.distances[source.location][destination.location] = shortest_dist
# Save all distances under world as well.
self.world.distances = self.distances