forked from semitable/robotic-warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warehouse.py
712 lines (592 loc) · 24.4 KB
/
warehouse.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import logging
from collections import defaultdict, OrderedDict
import gym
from gym import spaces
from rware.utils import MultiAgentActionSpace, MultiAgentObservationSpace
from enum import Enum
import numpy as np
from typing import List, Tuple, Optional, Dict
import networkx as nx
_AXIS_Z = 0
_AXIS_Y = 1
_AXIS_X = 2
_COLLISION_LAYERS = 2
_LAYER_AGENTS = 0
_LAYER_SHELFS = 1
class _VectorWriter:
def __init__(self, size: int):
self.vector = np.zeros(size, dtype=np.float32)
self.idx = 0
def write(self, data):
data_size = len(data)
self.vector[self.idx : self.idx + data_size] = data
self.idx += data_size
def skip(self, bits):
self.idx += bits
class Action(Enum):
NOOP = 0
FORWARD = 1
LEFT = 2
RIGHT = 3
TOGGLE_LOAD = 4
class Direction(Enum):
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
class RewardType(Enum):
GLOBAL = 0
INDIVIDUAL = 1
TWO_STAGE = 2
class Entity:
def __init__(self, id_: int, x: int, y: int):
self.id = id_
self.prev_x = None
self.prev_y = None
self.x = x
self.y = y
class Agent(Entity):
counter = 0
def __init__(self, x: int, y: int, dir_: Direction, msg_bits: int):
Agent.counter += 1
super().__init__(Agent.counter, x, y)
self.dir = dir_
self.message = np.zeros(msg_bits)
self.req_action: Optional[Action] = None
self.carrying_shelf: Optional[Shelf] = None
self.canceled_action = None
self.has_delivered = False
@property
def collision_layers(self):
if self.loaded:
return (_LAYER_AGENTS, _LAYER_SHELFS)
else:
return (_LAYER_AGENTS,)
def req_location(self, grid_size) -> Tuple[int, int]:
if self.req_action != Action.FORWARD:
return self.x, self.y
elif self.dir == Direction.UP:
return self.x, max(0, self.y - 1)
elif self.dir == Direction.DOWN:
return self.x, min(grid_size[0] - 1, self.y + 1)
elif self.dir == Direction.LEFT:
return max(0, self.x - 1), self.y
elif self.dir == Direction.RIGHT:
return min(grid_size[1] - 1, self.x + 1), self.y
raise ValueError(
f"Direction is {self.dir}. Should be one of {[v for v in Direction]}"
)
def req_direction(self) -> Direction:
wraplist = [Direction.UP, Direction.RIGHT, Direction.DOWN, Direction.LEFT]
if self.req_action == Action.RIGHT:
return wraplist[(wraplist.index(self.dir) + 1) % len(wraplist)]
elif self.req_action == Action.LEFT:
return wraplist[(wraplist.index(self.dir) - 1) % len(wraplist)]
else:
return self.dir
class Shelf(Entity):
counter = 0
def __init__(self, x, y):
Shelf.counter += 1
super().__init__(Shelf.counter, x, y)
@property
def collision_layers(self):
return (_LAYER_SHELFS,)
class Warehouse(gym.Env):
metadata = {"render.modes": ["human", "rgb_array"]}
def __init__(
self,
shelf_columns: int,
column_height: int,
shelf_rows: int,
n_agents: int,
msg_bits: int,
sensor_range: int,
request_queue_size: int,
max_inactivity_steps: Optional[int],
max_steps: Optional[int],
reward_type: RewardType,
fast_obs=True,
layout: str = None,
):
"""The robotic warehouse environment
Creates a grid world where multiple agents (robots)
are supposed to collect shelfs, bring them to a goal
and then return them.
.. note:
The grid looks like this:
shelf
columns
vv
----------
-XX-XX-XX- ^
-XX-XX-XX- Column Height
-XX-XX-XX- v
----------
-XX----XX- <\
-XX----XX- <- Shelf Rows
-XX----XX- </
----------
----GG----
G: is the goal positions where agents are rewarded if
they bring the correct shelfs.
The final grid size will be
height: (column_height + 1) * shelf_rows + 2
width: (2 + 1) * shelf_columns + 1
The bottom-middle column will be removed to allow for
robot queuing next to the goal locations
:param shelf_columns: Number of columns in the warehouse
:type shelf_columns: int
:param column_height: Column height in the warehouse
:type column_height: int
:param shelf_rows: Number of columns in the warehouse
:type shelf_rows: int
:param n_agents: Number of spawned and controlled agents
:type n_agents: int
:param msg_bits: Number of communication bits for each agent
:type msg_bits: int
:param sensor_range: Range of each agents observation
:type sensor_range: int
:param request_queue_size: How many shelfs are simultaneously requested
:type request_queue_size: int
:param max_inactivity: Number of steps without a delivered shelf until environment finishes
:type max_inactivity: Optional[int]
:param reward_type: Specifies if agents are rewarded individually or globally
:type reward_type: RewardType
:param layout: A string for a custom warehouse layout. X are shelve locations, dots are corridors, and g are the goal locations. Ignores shelf_columns, shelf_height and shelf_rows when used.
:type layout: str
"""
self.goals: List[Tuple[int, int]] = []
if not layout:
self._make_layout_from_params(shelf_columns, shelf_rows, column_height)
else:
self._make_layout_from_str(layout)
self.n_agents = n_agents
self.msg_bits = msg_bits
self.sensor_range = sensor_range
self.max_inactivity_steps: Optional[int] = max_inactivity_steps
self.reward_type = reward_type
self.reward_range = (0, 1)
self._cur_inactive_steps = None
self._cur_steps = 0
self.max_steps = max_steps
sa_action_space = [len(Action), *msg_bits * (2,)]
if len(sa_action_space) == 1:
sa_action_space = spaces.Discrete(sa_action_space[0])
else:
sa_action_space = spaces.MultiDiscrete(sa_action_space)
self.action_space = spaces.Tuple(tuple(n_agents * [sa_action_space]))
self.request_queue_size = request_queue_size
self.request_queue = []
self.agents: List[Agent] = []
self._obs_bits_for_self = 4 + len(Direction)
self._obs_bits_per_agent = 1 + len(Direction) + self.msg_bits
self._obs_bits_per_shelf = 2
self._obs_bits_for_requests = 2
self._obs_sensor_locations = (1 + 2 * self.sensor_range) ** 2
self._obs_length = (
self._obs_bits_for_self
+ self._obs_sensor_locations * self._obs_bits_per_agent
+ self._obs_sensor_locations * self._obs_bits_per_shelf
)
# default values:
self.fast_obs = None
self.observation_space = None
self._use_slow_obs()
# for performance reasons we
# can flatten the obs vector
if fast_obs:
self._use_fast_obs()
self.renderer = None
def _make_layout_from_params(self, shelf_columns, shelf_rows, column_height):
assert shelf_columns % 2 == 1, "Only odd number of shelf columns is supported"
self.grid_size = (
(column_height + 1) * shelf_rows + 2,
(2 + 1) * shelf_columns + 1,
)
self.column_height = column_height
self.grid = np.zeros((_COLLISION_LAYERS, *self.grid_size), dtype=np.int32)
self.goals = [
(self.grid_size[1] // 2 - 1, self.grid_size[0] - 1),
(self.grid_size[1] // 2, self.grid_size[0] - 1),
]
self.highways = np.zeros(self.grid_size, dtype=np.int32)
highway_func = lambda x, y: (
(x % 3 == 0) # vertical highways
or (y % (self.column_height + 1) == 0) # horizontal highways
or (y == self.grid_size[0] - 1) # delivery row
or ( # remove a box for queuing
(y > self.grid_size[0] - (self.column_height + 3))
and ((x == self.grid_size[1] // 2 - 1) or (x == self.grid_size[1] // 2))
)
)
for x in range(self.grid_size[1]):
for y in range(self.grid_size[0]):
self.highways[y, x] = highway_func(x, y)
def _make_layout_from_str(self, layout):
layout = layout.strip()
layout = layout.replace(" ", "")
grid_height = layout.count("\n") + 1
lines = layout.split("\n")
grid_width = len(lines[0])
for line in lines:
assert len(line) == grid_width, "Layout must be rectangular"
self.grid_size = (grid_height, grid_width)
self.grid = np.zeros((_COLLISION_LAYERS, *self.grid_size), dtype=np.int32)
self.highways = np.zeros(self.grid_size, dtype=np.int32)
for y, line in enumerate(lines):
for x, char in enumerate(line):
assert char.lower() in "gx."
if char.lower() == "g":
self.goals.append((x, y))
self.highways[y, x] = 1
elif char.lower() == ".":
self.highways[y, x] = 1
assert len(self.goals) >= 1, "At least one goal is required"
def _use_slow_obs(self):
self.fast_obs = False
self.observation_space = spaces.Tuple(
tuple(
[
spaces.Dict(
OrderedDict(
{
"self": spaces.Dict(
OrderedDict(
{
"location": spaces.Box(
low=0,
high=np.array(
[
self.grid_size[1],
self.grid_size[0],
]
),
dtype=int,
),
"carrying_shelf": spaces.MultiBinary(1),
"direction": spaces.Discrete(4),
"on_highway": spaces.MultiBinary(1),
}
)
),
"sensors": spaces.Tuple(
self._obs_sensor_locations
* (
spaces.Dict(
OrderedDict(
{
"has_agent": spaces.MultiBinary(1),
"direction": spaces.Discrete(4),
"local_message": spaces.MultiBinary(
self.msg_bits
),
"has_shelf": spaces.MultiBinary(1),
"shelf_requested": spaces.MultiBinary(
1
),
}
)
),
)
),
}
)
)
for _ in range(self.n_agents)
]
)
)
def _use_fast_obs(self):
if self.fast_obs:
return
self.fast_obs = True
ma_spaces = []
for sa_obs in self.observation_space:
flatdim = spaces.flatdim(sa_obs)
ma_spaces += [
spaces.Box(
low=-float("inf"),
high=float("inf"),
shape=(flatdim,),
dtype=np.float32,
)
]
self.observation_space = spaces.Tuple(tuple(ma_spaces))
def _is_highway(self, x: int, y: int) -> bool:
return self.highways[y, x]
def _make_obs(self, agent):
y_scale, x_scale = self.grid_size[0] - 1, self.grid_size[1] - 1
min_x = agent.x - self.sensor_range
max_x = agent.x + self.sensor_range + 1
min_y = agent.y - self.sensor_range
max_y = agent.y + self.sensor_range + 1
# sensors
if (
(min_x < 0)
or (min_y < 0)
or (max_x > self.grid_size[1])
or (max_y > self.grid_size[0])
):
padded_agents = np.pad(
self.grid[_LAYER_AGENTS], self.sensor_range, mode="constant"
)
padded_shelfs = np.pad(
self.grid[_LAYER_SHELFS], self.sensor_range, mode="constant"
)
# + self.sensor_range due to padding
min_x += self.sensor_range
max_x += self.sensor_range
min_y += self.sensor_range
max_y += self.sensor_range
else:
padded_agents = self.grid[_LAYER_AGENTS]
padded_shelfs = self.grid[_LAYER_SHELFS]
agents = padded_agents[min_y:max_y, min_x:max_x].reshape(-1)
shelfs = padded_shelfs[min_y:max_y, min_x:max_x].reshape(-1)
if self.fast_obs:
obs = _VectorWriter(self.observation_space[agent.id - 1].shape[0])
obs.write([agent.x, agent.y, int(agent.carrying_shelf is not None)])
direction = np.zeros(4)
direction[agent.dir.value] = 1.0
obs.write(direction)
obs.write([int(self._is_highway(agent.x, agent.y))])
for i, (id_agent, id_shelf) in enumerate(zip(agents, shelfs)):
if id_agent == 0:
obs.skip(1)
obs.write([1.0])
obs.skip(3 + self.msg_bits)
else:
obs.write([1.0])
direction = np.zeros(4)
direction[self.agents[id_agent - 1].dir.value] = 1.0
obs.write(direction)
if self.msg_bits > 0:
obs.write(self.agents[id_agent - 1].message)
if id_shelf == 0:
obs.skip(2)
else:
obs.write(
[1.0, int(self.shelfs[id_shelf - 1] in self.request_queue)]
)
return obs.vector
# --- self data
obs = {}
obs["self"] = {
"location": np.array([agent.x, agent.y]),
"carrying_shelf": [int(agent.carrying_shelf is not None)],
"direction": agent.dir.value,
"on_highway": [int(self._is_highway(agent.x, agent.y))],
}
# --- sensor data
obs["sensors"] = tuple({} for _ in range(self._obs_sensor_locations))
# find neighboring agents
for i, id_ in enumerate(agents):
if id_ == 0:
obs["sensors"][i]["has_agent"] = [0]
obs["sensors"][i]["direction"] = 0
obs["sensors"][i]["local_message"] = self.msg_bits * [0]
else:
obs["sensors"][i]["has_agent"] = [1]
obs["sensors"][i]["direction"] = self.agents[id_ - 1].dir.value
obs["sensors"][i]["local_message"] = self.agents[id_ - 1].message
# find neighboring shelfs:
for i, id_ in enumerate(shelfs):
if id_ == 0:
obs["sensors"][i]["has_shelf"] = [0]
obs["sensors"][i]["shelf_requested"] = [0]
else:
obs["sensors"][i]["has_shelf"] = [1]
obs["sensors"][i]["shelf_requested"] = [
int(self.shelfs[id_ - 1] in self.request_queue)
]
return obs
def _recalc_grid(self):
self.grid[:] = 0
for s in self.shelfs:
self.grid[_LAYER_SHELFS, s.y, s.x] = s.id
for a in self.agents:
self.grid[_LAYER_AGENTS, a.y, a.x] = a.id
def reset(self):
Shelf.counter = 0
Agent.counter = 0
self._cur_inactive_steps = 0
self._cur_steps = 0
# n_xshelf = (self.grid_size[1] - 1) // 3
# n_yshelf = (self.grid_size[0] - 2) // 9
# make the shelfs
self.shelfs = [
Shelf(x, y)
for y, x in zip(
np.indices(self.grid_size)[0].reshape(-1),
np.indices(self.grid_size)[1].reshape(-1),
)
if not self._is_highway(x, y)
]
# spawn agents at random locations
agent_locs = np.random.choice(
np.arange(self.grid_size[0] * self.grid_size[1]),
size=self.n_agents,
replace=False,
)
agent_locs = np.unravel_index(agent_locs, self.grid_size)
# and direction
agent_dirs = np.random.choice([d for d in Direction], size=self.n_agents)
self.agents = [
Agent(x, y, dir_, self.msg_bits)
for y, x, dir_ in zip(*agent_locs, agent_dirs)
]
self._recalc_grid()
self.request_queue = list(
np.random.choice(self.shelfs, size=self.request_queue_size, replace=False)
)
return tuple([self._make_obs(agent) for agent in self.agents])
# for s in self.shelfs:
# self.grid[0, s.y, s.x] = 1
# print(self.grid[0])
def step(
self, actions: List[Action]
) -> Tuple[List[np.ndarray], List[float], List[bool], Dict]:
assert len(actions) == len(self.agents)
for agent, action in zip(self.agents, actions):
if self.msg_bits > 0:
agent.req_action = Action(action[0])
agent.message[:] = action[1:]
else:
agent.req_action = Action(action)
# # stationary agents will certainly stay where they are
# stationary_agents = [agent for agent in self.agents if agent.action != Action.FORWARD]
# # forward agents will move only if they avoid collisions
# forward_agents = [agent for agent in self.agents if agent.action == Action.FORWARD]
commited_agents = set()
G = nx.DiGraph()
for agent in self.agents:
start = agent.x, agent.y
target = agent.req_location(self.grid_size)
if (
agent.carrying_shelf
and start != target
and self.grid[_LAYER_SHELFS, target[1], target[0]]
and not (
self.grid[_LAYER_AGENTS, target[1], target[0]]
and self.agents[
self.grid[_LAYER_AGENTS, target[1], target[0]] - 1
].carrying_shelf
)
):
# there's a standing shelf at the target location
# our agent is carrying a shelf so there's no way
# this movement can succeed. Cancel it.
agent.req_action = Action.NOOP
G.add_edge(start, start)
else:
G.add_edge(start, target)
wcomps = [G.subgraph(c).copy() for c in nx.weakly_connected_components(G)]
for comp in wcomps:
try:
# if we find a cycle in this component we have to
# commit all nodes in that cycle, and nothing else
cycle = nx.algorithms.find_cycle(comp)
if len(cycle) == 2:
# we have a situation like this: [A] <-> [B]
# which is physically impossible. so skip
continue
for edge in cycle:
start_node = edge[0]
agent_id = self.grid[_LAYER_AGENTS, start_node[1], start_node[0]]
if agent_id > 0:
commited_agents.add(agent_id)
except nx.NetworkXNoCycle:
longest_path = nx.algorithms.dag_longest_path(comp)
for x, y in longest_path:
agent_id = self.grid[_LAYER_AGENTS, y, x]
if agent_id:
commited_agents.add(agent_id)
commited_agents = set([self.agents[id_ - 1] for id_ in commited_agents])
failed_agents = set(self.agents) - commited_agents
for agent in failed_agents:
assert agent.req_action == Action.FORWARD
agent.req_action = Action.NOOP
rewards = np.zeros(self.n_agents)
for agent in self.agents:
agent.prev_x, agent.prev_y = agent.x, agent.y
if agent.req_action == Action.FORWARD:
agent.x, agent.y = agent.req_location(self.grid_size)
if agent.carrying_shelf:
agent.carrying_shelf.x, agent.carrying_shelf.y = agent.x, agent.y
elif agent.req_action in [Action.LEFT, Action.RIGHT]:
agent.dir = agent.req_direction()
elif agent.req_action == Action.TOGGLE_LOAD and not agent.carrying_shelf:
shelf_id = self.grid[_LAYER_SHELFS, agent.y, agent.x]
if shelf_id:
agent.carrying_shelf = self.shelfs[shelf_id - 1]
elif agent.req_action == Action.TOGGLE_LOAD and agent.carrying_shelf:
if not self._is_highway(agent.x, agent.y):
agent.carrying_shelf = None
if agent.has_delivered and self.reward_type == RewardType.TWO_STAGE:
rewards[agent.id - 1] += 0.5
agent.has_delivered = False
self._recalc_grid()
shelf_delivered = False
for x, y in self.goals:
shelf_id = self.grid[_LAYER_SHELFS, y, x]
if not shelf_id:
continue
shelf = self.shelfs[shelf_id - 1]
if shelf not in self.request_queue:
continue
# a shelf was successfully delived.
shelf_delivered = True
# remove from queue and replace it
new_request = np.random.choice(
list(set(self.shelfs) - set(self.request_queue))
)
self.request_queue[self.request_queue.index(shelf)] = new_request
# also reward the agents
if self.reward_type == RewardType.GLOBAL:
rewards += 1
elif self.reward_type == RewardType.INDIVIDUAL:
agent_id = self.grid[_LAYER_AGENTS, y, x]
rewards[agent_id - 1] += 1
elif self.reward_type == RewardType.TWO_STAGE:
agent_id = self.grid[_LAYER_AGENTS, y, x]
self.agents[agent_id - 1].has_delivered = True
rewards[agent_id - 1] += 0.5
if shelf_delivered:
self._cur_inactive_steps = 0
else:
self._cur_inactive_steps += 1
self._cur_steps += 1
if (
self.max_inactivity_steps
and self._cur_inactive_steps >= self.max_inactivity_steps
) or (self.max_steps and self._cur_steps >= self.max_steps):
dones = self.n_agents * [True]
else:
dones = self.n_agents * [False]
new_obs = tuple([self._make_obs(agent) for agent in self.agents])
info = {}
return new_obs, list(rewards), dones, info
def render(self, mode="human"):
if not self.renderer:
from rware.rendering import Viewer
self.renderer = Viewer(self.grid_size)
return self.renderer.render(self, return_rgb_array=mode == "rgb_array")
def close(self):
if self.renderer:
self.renderer.close()
def seed(self, seed=None):
...
if __name__ == "__main__":
env = Warehouse(9, 8, 3, 10, 3, 1, 5, None, None, RewardType.GLOBAL)
env.reset()
import time
from tqdm import tqdm
time.sleep(2)
# env.render()
# env.step(18 * [Action.LOAD] + 2 * [Action.NOOP])
for _ in tqdm(range(1000000)):
# time.sleep(2)
# env.render()
actions = env.action_space.sample()
env.step(actions)