-
Notifications
You must be signed in to change notification settings - Fork 1
/
DRLMAPF_A3C_RNN-flatland.py
1317 lines (1147 loc) · 70.1 KB
/
DRLMAPF_A3C_RNN-flatland.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# # Pathfinding via Reinforcement and Imitation Multi-Agent Learning (PRIMAL)
#
# While training is taking place, statistics on agent performance are available from Tensorboard. To launch it use:
#
# `tensorboard --logdir train_primal`
# In[ ]:
#this should be the thing, right?
from __future__ import division
import numpy as np
import random
import tensorflow as tf
import tensorflow.contrib.layers as layers
import matplotlib.pyplot as plt
import threading
import copy
import time
import scipy.signal as signal
import os
import sys
from GroupLock import GroupLock
from StateMaskingObs import StateMaskingObs
from NewAgentInitObs import StateMaskingObs as TrafficLightObs
from expert2 import Solver, Global_H, my_controller
from flatland.envs.rail_generators import complex_rail_generator
from flatland.envs.schedule_generators import complex_schedule_generator
from flatland.envs.rail_env import RailEnv
from flatland.utils.rendertools import RenderTool
from flatland.envs.rail_generators import sparse_rail_generator
from flatland.envs.schedule_generators import sparse_schedule_generator
from flatland.core.grid.grid_utils import distance_on_rail as manhattan_distance
import pickle
from ACNet4 import ACNet
import imageio
from tensorflow.python.client import device_lib
dev_list = device_lib.list_local_devices()
print(dev_list)
# assert len(dev_list) > 1
def Complex_params():
grid_width = np.random.randint(12, 30) # min(int(np.random.uniform(ENVIRONMENT_SIZE[0], ENVIRONMENT_SIZE[1] )),
# int(np.random.uniform(ENVIRONMENT_SIZE[0], ENVIRONMENT_SIZE[1] )))
grid_height = grid_width # min(int(np.random.uniform(ENVIRONMENT_SIZE[0], ENVIRONMENT_SIZE[1])),
# nt(np.random.uniform(ENVIRONMENT_SIZE[0], ENVIRONMENT_SIZE[1] )))
rnd_start_goal = 8 + np.random.randint(0,
3) # int(np.random.uniform(num_workers, num_workers+1+episode_difficulty ))
# int(np.random.uniform( num_workers , min(grid_width,grid_height))),
rnd_extra = np.random.randint(3, 7) # int(np.random.uniform(0 , 1+2*episode_difficulty ))
# int(np.random.uniform( 0 , min(grid_width,grid_height))))
rnd_min_dist = np.random.randint(int(0.2 * min(grid_height, grid_width)), int(
0.75 * min(grid_height, grid_width))) # int(np.random.uniform( episode_difficulty , 4+2*episode_difficulty ))
rnd_max_dist = rnd_min_dist + np.random.randint(5,
15) # int(np.random.uniform(3+episode_difficulty, 6+2*episode_difficulty))
rnd_seed = int(np.random.rand() * 2 * 200)
return grid_width, grid_height, rnd_start_goal, rnd_extra, rnd_min_dist, rnd_max_dist, rnd_seed
def Sparse_params():
tid = np.random.randint(0, 50)
seed = tid * 19997 + 997
random.seed(seed)
nSize = random.randint(0, 5)
width = 20 + nSize * 5
height = 20 + nSize * 5
nr_cities = 2 + nSize // 2 + random.randint(0, 2)
nr_trains = min(nr_cities * 5, 5 + random.randint(0, 5)) # , 10 + random.randint(0, 10))
max_rails_between_cities = 2
max_rails_in_cities = 3 + random.randint(0, nSize)
malfunction_rate = 30 + random.randint(0, 100)
malfunction_min_duration = 3 + random.randint(0, 7)
malfunction_max_duration = 20 + random.randint(0, 80)
return (
seed, width, height,
nr_trains, nr_cities,
max_rails_between_cities, max_rails_in_cities,
malfunction_rate, malfunction_min_duration, malfunction_max_duration
)
def make_gif(images, fname, duration=2, true_image=False,salience=False,salIMGS=None):
imageio.mimwrite(fname,images,subrectangles=True)
print("\nwrote gif")
# Copies one set of variables to another.
# Used to set worker network parameters to those of global network.
def update_target_graph(from_scope,to_scope):
from_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, from_scope)
to_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, to_scope)
op_holder = []
for from_var, to_var in zip(from_vars,to_vars):
op_holder.append(to_var.assign(from_var))
return op_holder
def discount(x, gamma):
return signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1]
def good_discount(x, gamma):
return discount(x, gamma)
# positive = np.clip(x,0,None)
# negative = np.clip(x,None,0)
# return signal.lfilter([1], [1, -gamma], positive[::-1], axis=0)[::-1]+negative
# ## Worker Agent
# In[ ]:
class Worker:
def __init__(self, gameEnv, metaAgentID, workerID, a_size, groupLock):
self.workerID = workerID
self.env = gameEnv
self.metaAgentID = metaAgentID
self.name = "worker_"+str(workerID)
self.agentID = workerID % num_workers
self.groupLock = groupLock
self.nextGIF = episode_count # For GIFs output
# Create the local copy of the network and the tensorflow op to copy global parameters to local network
self.local_AC = ACNet(self.name, a_size, trainer, True, GLOBAL_NET_SCOPE, OBS_SIZE)
self.pull_global = update_target_graph(GLOBAL_NET_SCOPE, self.name)
self.prune_rate = PRUNE_ACTION
if self.workerID == 0:
self.env_renderer = None
self.path_finder = None
def synchronize(self):
# handy thing for keeping track of which to release and acquire
if not hasattr(self,"lock_bool"):
self.lock_bool=False
self.groupLock.release(int(self.lock_bool),self.name)
self.groupLock.acquire(int(not self.lock_bool),self.name)
self.lock_bool = not self.lock_bool
def train(self, rollout, sess, gamma, bootstrap_value, imitation=False):
global episode_count
if imitation:
# we calculate the loss differently for imitation
# if imitation=True the rollout is assumed to have different dimensions:
# [o[0],o[1],optimal_actions]
# rnn_state = self.local_AC.state_init
rollout_obs = np.array(rollout[0])
rollout_obs = np.reshape(rollout_obs, (-1, OBS_SIZE))
rollout_action = np.array([rollout[1]])
rollout_action = np.reshape(rollout_action, (-1, 1))
rollout_action = rollout_action.squeeze()
# rnn_state = self.local_AC.state_init
feed_dict = {global_step: episode_count,
self.local_AC.inputs: rollout_obs,
self.local_AC.optimal_actions: rollout_action,
# self.local_AC.state_in[0]: rnn_state[0],
# self.local_AC.state_in[1]: rnn_state[1]
}
_, i_l, _ = sess.run([self.local_AC.policy, self.local_AC.imitation_loss,
self.local_AC.apply_imitation_grads],
feed_dict=feed_dict)
return i_l
rollout = np.array(rollout)
observations = rollout[:, 0]
observations = np.stack(observations)
observations = np.reshape(observations, (-1, OBS_SIZE))
actions = rollout[:, 1]
rewards = rollout[:, 2]
values = rollout[:, 3]
valids = rollout[:, 4]
train_value = rollout[:, -1]
# train_astar = rollout[:, -1]
# Here we take the rewards and values from the rollout, and use them to
# generate the advantage and discounted returns. (With bootstrapping)
# The advantage function uses "Generalized Advantage Estimation"
self.rewards_plus = np.asarray(rewards.tolist() + [bootstrap_value])
discounted_rewards = discount(self.rewards_plus, gamma)[:-1]
self.value_plus = np.asarray(values.tolist() + [bootstrap_value])
advantages = rewards + gamma * self.value_plus[1:] - self.value_plus[:-1]
advantages = good_discount(advantages, gamma)
num_samples = min(EPISODE_SAMPLES, len(advantages))
sampleInd = np.sort(np.random.choice(advantages.shape[0], size=(num_samples,), replace=False))
# Update the global network using gradients from loss
# Generate network statistics to periodically save
# rnn_state = self.local_AC.state_init
feed_dict = {
global_step: episode_count,
self.local_AC.target_v: np.stack(discounted_rewards),
self.local_AC.inputs: np.stack(observations),
self.local_AC.actions: actions,
self.local_AC.valid_actions: np.stack(valids),
self.local_AC.advantages: advantages,
self.local_AC.train_value: train_value,
# self.local_AC.state_in[0]: rnn_state[0],
# self.local_AC.state_in[1]: rnn_state[1]
}
v_l, p_l, valid_l, e_l, g_n, v_n, _ = sess.run([self.local_AC.value_loss,
self.local_AC.policy_loss,
self.local_AC.valid_loss,
self.local_AC.entropy,
self.local_AC.grad_norms,
self.local_AC.var_norms,
self.local_AC.apply_grads], feed_dict=feed_dict)
return v_l/len(rollout), p_l/len(rollout), valid_l/len(rollout), e_l/len(rollout), g_n, v_n, np.sum(rewards)
def shouldRun(self, coord, episode_count):
if TRAINING:
return (not coord.should_stop())
else:
return (episode_count < NUM_EXPS)
def resetEnv(self):
assert self.agentID == 0
if int(joint_episode_count[self.metaAgentID]) % ENV_CHANGE_FREQUENCY == 0:
random_number = np.random.rand()
if random_number <= SPARSE_POSSIBILITY:
IS_SPARSE[self.metaAgentID] = True
while True:
try:
seed, width, height, nr_trains, nr_cities,\
max_rails_between_cities, max_rails_in_cities, _, _, _ = Sparse_params()
gameEnv = RailEnv(width=width,
height=height,
rail_generator=sparse_rail_generator(
max_num_cities=nr_cities,
max_rails_between_cities=max_rails_between_cities,
max_rails_in_city=max_rails_in_cities,
seed=seed, # Random seed
grid_mode=False # Ordered distribution of nodes
),
schedule_generator=sparse_schedule_generator(),
obs_builder_object=TrafficLightObs(),
number_of_agents=num_agents)
gameEnv.global_reward = 20
gameEnv.step_penalty = -0.3
self.env = gameEnv
obs = self.env.reset(True, True)
joint_env[self.metaAgentID] = copy.deepcopy(self.env)
break
except Exception:
print('bad init')
pass
else:
IS_SPARSE[self.metaAgentID] = False
grid_width , grid_height , rnd_start_goal , rnd_extra , rnd_min_dist , rnd_max_dist , rnd_seed = Complex_params()
gameEnv = RailEnv(width=grid_width, height=grid_height,
rail_generator=complex_rail_generator(
nr_start_goal=rnd_start_goal,nr_extra=rnd_extra,min_dist=rnd_min_dist,max_dist=rnd_max_dist,seed=rnd_seed) ,
schedule_generator=complex_schedule_generator(),
obs_builder_object=TrafficLightObs(),
number_of_agents=num_agents)
gameEnv.global_reward = 20
gameEnv.step_penalty = -0.3
self.env = gameEnv
obs = self.env.reset(True, True)
joint_env[self.metaAgentID] = copy.deepcopy(self.env)
else:
obs = self.env.reset(True, True)
print('init succeed')
return obs
def parse_path(self, obss, actions):
"""
input:
output: rollout:[[obs, action],[...],...]
"""
if not obss or not actions:
return None
agents_rollout = [
[[], []] for i in range(NUM_THREADS)
]
for agentID in range(NUM_THREADS):
for step in range(len(obss)):
if agentID not in actions[step].keys():
# End of episode for that agent, go to next agent
continue
if agentID in obss[step].keys():
agents_rollout[agentID][0].append(obss[step][agentID])
if a_size == 4:
agents_rollout[agentID][1].append(actions[step][agentID] - 1)
else:
agents_rollout[agentID][1].append(actions[step][agentID])
return agents_rollout
def StateClassifier(self, agent_pos, agent_dir):
"""
returns 0 : No decision point
returns 1 : Stopping point (Decision at next cell)
returns 2 : At decision point currently (More than 1 available transition)
returns 3 : MUST STOP point - Agent Ahead
returns 4 : MUST STOP point + Stopping Point
returns None: invalid cell
"""
avb_moves = self.env.rail.get_transitions(*agent_pos, agent_dir)
move2grid = np.array([[[0, -1], [-1, 0], [0, +1]], [[-1, 0], [0, +1], [+1, 0]], [[0, +1], [+1, 0], [0, -1]],
[[+1, 0], [0, -1], [-1, 0]]]) # Obtained from collidingagent code
trans2act = np.array([[2, 3, 0, 1], [1, 2, 3, 0], [0, 1, 2, 3], [3, 0, 1, 2]]) # Maps transition to an action
# next_dir_grid = np.array([-1,0,1]) # Maps action to a change in agent direction
if sum(avb_moves) > 1: # This is definitely a decision junction since more than 1 move possible
return 2
elif sum(avb_moves) == 1:
avbmove = avb_moves.index(1) # Get the available transition to next cell
action = trans2act[agent_dir][avbmove] # Get the corresponding action for that transition
if action == 0:
next_pos = agent_pos + move2grid[(agent_dir + 2) % 4][
1] # This is a dead end, so turn around and move forward
else:
next_pos = agent_pos + move2grid[agent_dir][action - 1]
# next_dir = (agent_dir + (next_dir_grid[action-1]) )%4
sumnextcell = 0 # How many possible transitions at next cell
for i in range(0, 4):
new_avb_moves = self.env.rail.get_transitions(*next_pos, i)
sumnextcell += sum(new_avb_moves)
# Also have to check whether the junction is occupied
Occupied = False
for k in range(len(self.env.agents)):
if self.env.agents[k].position is None:
if self.env.dones[k]:
my_pos = (-3,-3)
else:
my_pos = (-3,-3)
else:
my_pos = self.env.agents[k].position
if my_pos[0] == next_pos[0] and my_pos[1] == next_pos[1]:
Occupied = True
break
if (sumnextcell > 2) and Occupied:
return 4 # The agent is currently at a MUST STOP point
elif (sumnextcell > 2) and (not Occupied): # The agent is at a stopping point
return 1
elif (sumnextcell <= 2) and Occupied:
return 3 # The agent is at a MUST STOP point
else:
return 0 # The agent is at a no decision point
else:
# print("The agent is at an impossible cell") # This happen when checking stopping agents
# print("agent_dir:", agent_dir, " agent_pos:", agent_pos)
return None
def get_collided_agent(self, action, episode_step):
# This whole function is in 5-action style, regardless of a_size
if action == 4:
# If we are stuck because we stopped too long, it's 100% on us
return False, [], False
# (N, E, S, W)
agent_dir = self.env.agents[self.agentID].direction if self.env.agents[self.agentID].direction \
else self.env.agents[self.agentID].initial_direction
agent_pos = self.env.agents[self.agentID].position if self.env.agents[self.agentID].position \
else self.env.agents[self.agentID].initial_position
# move2grid = [[-1, 0], [0, +1], [+1, 0], [0, -1]] # N, E, S, W
move2grid = np.array([[[0, -1], [-1, 0], [0, +1]],
[[-1, 0], [0, +1], [+1, 0]],
[[0, +1], [+1, 0], [0, -1]],
[[+1, 0], [0, -1], [-1, 0]]]) # West: SWN
collision_position = np.asarray(agent_pos) + move2grid[agent_dir][action - 1]
collided_agent = None
for i in range(len(self.env.agents)):
if (self.env.agents[i].position == collision_position).all():
collided_agent = i
if len(shared_nb_va[self.metaAgentID][i]) < len(shared_nb_va[self.metaAgentID][self.agentID]):
# collided_agent was already stuck/done, it's our fault
ep_i, steps_cc = episode_step, []
while ep_i >= 0:
steps_cc.append(ep_i)
if shared_nb_va[self.metaAgentID][self.agentID][ep_i] > 2: # I had a choice then!
break
ep_i -= 1
steps_cc.remove(episode_step)
return True, steps_cc, True
break
if collided_agent is None:
# print('\n({:d}) Hit a ghost (should be rare)!!'.format(int(episode_step)))
# print(agent_pos, agent_dir, list(collision_position))
# for agent in self.env.agents:
# print(agent.position, agent.direction)
# I hit a ghost: I hit an agent by moving into its cell while it was moving out. It's clearly my fault
return True, [], False
ep_i, steps_cc = episode_step, []
while ep_i >= 0:
if shared_nb_va[self.metaAgentID][collided_agent][ep_i] >= 2: # he had a choice then!
if episode_step in steps_cc:
steps_cc.remove(episode_step)
if shared_nb_va[self.metaAgentID][self.agentID][ep_i] >= 2: # I also had a choice at the same time!
return True, steps_cc, True
return False, steps_cc, False # It's all his fault
elif shared_nb_va[self.metaAgentID][self.agentID][ep_i] >= 2: # I had a choice then!
if shared_nb_va[self.metaAgentID][collided_agent][ep_i] >= 2: # He also had a choice at the same time!
if episode_step in steps_cc:
steps_cc.remove(episode_step)
return True, steps_cc, True
if episode_step in steps_cc:
steps_cc.remove(episode_step)
return True, steps_cc, True # It's all my fault
steps_cc.append(ep_i)
ep_i -= 1
# Commented out to check for bugs, should never happen anyway
# return responsible, steps_cc, collisioncourse
print("\nWeird, exiting get_collided_agent() via default exit")
return False, [], False
def _NextValidActions(self):
"""
returns list of valid actions
List[0]= LEFT , List[1] = Straight , List[2] = Right , List[3] = Stop
If at NO decision point, just go forward [0]
If at stopping point, look 1 timestep into future and conclude : returns [0,3](stop,go) or [3](stop)
If at junction, get valid directions to go in. No stopping allowed here
If no available direction at junction : return [3](stop) , This means we're screwed
"""
currentobs = joint_observations[self.metaAgentID][self.agentID][0:OBS_SIZE-ADDITIONAL_INPUT]
traffic_signal = joint_observations[self.metaAgentID][self.agentID][OBS_SIZE-ADDITIONAL_INPUT]
homo_junctions = joint_observations[self.metaAgentID][self.agentID][(OBS_SIZE-3) :OBS_SIZE]
if traffic_signal == -1:
validactions = [3]
return validactions
currentobs = np.reshape(currentobs, (3, -1))
if self.env.agents[self.agentID].position is None:
if self.env.dones[self.agentID]:
actual_dir = self.env.agents[self.agentID].old_direction
actual_pos = self.env.agents[self.agentID].target
else:
actual_dir = self.env.agents[self.agentID].initial_direction
actual_pos = self.env.agents[self.agentID].initial_position
else:
actual_dir = self.env.agents[self.agentID].direction
actual_pos = self.env.agents[self.agentID].position
state = self.StateClassifier(actual_pos, actual_dir)
# currentobs = joint_observations[self.metaAgentID][self.agentID]
if state in [3, 4]: # Must Stop Point
validactions = [3]
return validactions
elif state == 0: # Currently at NO decision point
validactions = [1]
return validactions
elif state == 1: # Currently at stopping point
SolExist = [currentobs[0][0], currentobs[1][0], currentobs[2][0]] # Imagine we are at decision junction
agentsblocking = [currentobs[0][2], currentobs[1][2], currentobs[2][2]]
agentsblockingjunction = [currentobs[0][3], currentobs[1][3], currentobs[2][3]]
agentsdiff = [currentobs[0][4], currentobs[1][4], currentobs[2][4]]
for i in range(0, 3):
# Check if there is any available non-blocked path which leads to a solution
if (SolExist[i] == 1) and (agentsblocking[i] == 0):
validactions = [1, 3] # If there is such a path, then going forward allowed
return validactions
if homo_junctions.count(1) >=2 :
for i in range(0,3) :
if (SolExist[i] == 1) and (agentsblockingjunction[i] == 0) and homo_junctions[i]== 1 :
validactions = [1, 3]
return validactions
validactions = [3] # If there is no such path, only stopping allowed
return validactions
else: # Currently at junction
SolExist = [currentobs[0][0], currentobs[1][0], currentobs[2][0]]
agentsblocking = [currentobs[0][2], currentobs[1][2], currentobs[2][2]]
agentsblockingjunction = [currentobs[0][3], currentobs[1][3], currentobs[2][3]]
agentsdiff = [currentobs[0][4], currentobs[1][4], currentobs[2][4]]
# stoppingoccupied = [currentobs[0][ENTRY_PER_COLUMN - 1],
# currentobs[1][ENTRY_PER_COLUMN - 1],
# currentobs[2][ENTRY_PER_COLUMN - 1]]
validactions = []
for i in range(0, 3):
if (SolExist[i] == 1) and (agentsblocking[i] == 0): # and stoppingoccupied[i]==False:
validactions.append(i)
if validactions:
return validactions
else:
if homo_junctions.count(1) >=2 :
for i in range(0,3) :
if (SolExist[i] == 1) and (agentsblockingjunction[i] == 0) and homo_junctions[i]== 1 :
validactions.append(i)
break
if validactions:
return validactions
# print("Oops we screwed up , we should have stopped at the stopping point")
# validactions = []
# for j in range(0, 3):
# if SolExist[j] == 0 and stoppingoccupied[j] == False:
# validactions.append(j)
# if validactions:
# return validactions
# else:
return [3]
def work(self, max_episode_length, gamma, sess, coord, saver):
global episode_count, swarm_reward, episode_rewards, episode_lengths, \
episode_mean_values, episode_invalid_ops
# global joint_success, MAX_DIFFICULTY
total_steps = 0
with sess.as_default(), sess.graph.as_default():
while self.shouldRun(coord, episode_count):
sess.run(self.pull_global)
# sess.run(self.copy_weights)
episode_buffer, episode_values = [], []
episode_step_count = 0
# Initial state from the environment
if self.agentID == 0:
# print('Meta-agent {}: resetting environment...'.format(self.metaAgentID), end='')
all_obs = self.resetEnv()
# all_obs = self.env.reset(True,True)
if len(all_obs[0]) != NUM_THREADS:
continue
for i in range(num_workers):
joint_observations[self.metaAgentID][i] = all_obs[0][i]
self.synchronize() # synchronize starting time of the threads
if self.env is not joint_env[self.metaAgentID]:
self.env = joint_env[self.metaAgentID]
validActions = self._NextValidActions()
s = joint_observations[self.metaAgentID][self.agentID]
assert len(s) == OBS_SIZE
# rnn_state = self.local_AC.state_init
stopped_counter = 0
valid_stopped_counter = 0
done_tag = False
end_episode = max_episode_length+1
successful_ep = False # only set to true if everyone got to their goal
joint_stuck[self.metaAgentID][self.agentID] = False
joint_done[self.metaAgentID][self.agentID] = False
shared_nb_va[self.metaAgentID][self.agentID] = [len(validActions)]
# number of valid actions at each timestep,
# used for collision course estimation at the end of an episode
if self.agentID == 0:
global demon_probs
demon_probs[self.metaAgentID] = np.random.rand()
self.synchronize() # synchronize starting time of the threads
# reset swarm_reward (for tensorboard)
swarm_reward[self.metaAgentID] = 0
# # Imitation Learning from the expert #
if episode_count < PRIMING_LENGTH or demon_probs[self.metaAgentID] < DEMONSTRATION_PROB:
# for the first PRIMING_LENGTH episodes, or with a certain probability
# don't train on the episode and instead observe a demonstration from M*
if self.workerID == 0 and int(episode_count) % 100 == 0:
saver.save(sess, model_path+'/model-'+str(int(episode_count))+'.cptk')
global rollouts
rollouts[self.metaAgentID] = None
if self.agentID == 0:
heuristic = Global_H(self.env)
self.path_finder = Solver(self.env, heuristic)
masked_obs = []
all_actions = []
masked_actions = []
while self.env.dones["__all__"] is not True:
joint_actions_single_step = my_controller(self.env, self.path_finder)
obs_single_step, _, _, _ = self.env.step(joint_actions_single_step)
all_actions.append(joint_actions_single_step)
for a_id in range(NUM_THREADS):
pos = self.env.agents[a_id].position if self.env.agents[a_id].position \
else self.env.agents[a_id].initial_position
direction = self.env.agents[a_id].direction if \
self.env.agents[a_id].direction is not None \
else self.env.agents[a_id].initial_position
if a_id not in joint_actions_single_step.keys() or joint_actions_single_step[a_id] == 0:
recursive_counter = -1
while recursive_counter > - len(all_actions) - 1:
# assume that agent cannot 'do nothing' for all steps
if a_id in all_actions[recursive_counter].keys():
if all_actions[recursive_counter][a_id] == 4: # check last action
joint_actions_single_step[a_id] = 3 if a_size == 4 else 4
else:
joint_actions_single_step[a_id] = 1 if a_size == 4 else 2
break
else: # recurse for previous action
recursive_counter -= 1
if a_id not in joint_actions_single_step.keys():
# if all steps are 'doing nothing', gives a 'going forward'
joint_actions_single_step[a_id] = 1 if a_size == 4 else 2
if self.StateClassifier(pos, direction) not in [1, 2]:
obs_single_step.pop(a_id)
joint_actions_single_step.pop(a_id)
masked_obs.append(obs_single_step)
masked_actions.append(joint_actions_single_step)
# all_obs and all_actions are dicts!
rollouts[self.metaAgentID] = self.parse_path(masked_obs, masked_actions)
print('env:', self.metaAgentID, 'episode ', episode_count, ' finish IL')
self.synchronize()
if rollouts[self.metaAgentID] is not None and len(rollouts[self.metaAgentID][0]) > 0:
i_l = self.train(rollouts[self.metaAgentID][self.agentID], sess, gamma, None, imitation=True)
if self.agentID == 0:
episode_count += 1
summary = tf.Summary()
summary.value.add(tag='Losses/Imitation loss', simple_value=i_l)
global_summary.add_summary(summary, int(episode_count))
global_summary.flush()
continue
continue
saveGIF = False
if OUTPUT_GIFS and self.workerID == 0 and ((not TRAINING) or (episode_count >= self.nextGIF)):
saveGIF = True
self.nextGIF = int(episode_count) + 128
GIF_episode = int(episode_count)
self.env_renderer = RenderTool(self.env)
self.env_renderer.render_env(show=ON_SCREEN_RENDERING, frames=False, show_observations=False)
episode_frames = [self.env_renderer.get_image()]
print('\nGoing for a GIF episode (next one should be around {:d})'.format(self.nextGIF))
k = 0
t = 0
if self.agentID == 0:
if not IS_SPARSE[self.metaAgentID]:
all_obs, _, _, _ = self.env.step({i: 2 for i in range(NUM_THREADS)})
for i in range(num_workers):
joint_observations[self.metaAgentID][i] = all_obs[i]
s = joint_observations[self.metaAgentID][self.agentID]
validActions = self._NextValidActions()
shared_nb_va[self.metaAgentID][self.agentID] = [len(validActions)]
else:
s = joint_observations[self.metaAgentID][self.agentID]
validActions = self._NextValidActions()
shared_nb_va[self.metaAgentID][self.agentID] = [len(validActions)]
self.synchronize()
episode_inv_count, decision_count, decision_go_straight_count, stopping_count, \
stopping_inv_count, must_stop_inv_count, decision_count_inv_count,initialized,episode_reward = 0, 0, 0, 0, 0, 0, 0, 0,0
while not self.env.dones["__all__"]: # Give me something!
IS_NON_DECISION = False
previous_pos = self.env.agents[self.agentID].position if self.env.agents[self.agentID].position \
else self.env.agents[self.agentID].initial_position
previous_dir = self.env.agents[self.agentID].direction \
if self.env.agents[self.agentID].direction is not None else \
self.env.agents[self.agentID].initial_direction
if episode_step_count <= end_episode:
# state masking
initialization = joint_observations[self.metaAgentID][self.agentID][OBS_SIZE-ADDITIONAL_INPUT+1]
# print(initialization)
state = self.StateClassifier(previous_pos, previous_dir)
if initialization == 1:
joint_actions[self.metaAgentID][self.agentID] = 0
a = 0
# print('initialized' , initialized)
elif initialization == 0 and initialized == 0:
joint_actions[self.metaAgentID][self.agentID] = 2
initialized = 1
a = 2
elif state == 0: # no decision point
a = 1
initialized += 1
joint_actions[self.metaAgentID][self.agentID] = 2 # just go forward
IS_NON_DECISION = True
elif state in [3,4] :
a=3
joint_actions[self.metaAgentID][self.agentID] = 4
IS_NON_DECISION = True
initialized +=1
else: # state == 1 or state == 2
# Take an action using probabilities from policy network output.
if state == 1:
stopping_count += 1
initialized +=1
s_feed = np.reshape(s, (1, OBS_SIZE))
a_dist, v = sess.run([self.local_AC.policy,
self.local_AC.value,
# self.local_AC.state_out
],
feed_dict={self.local_AC.inputs: s_feed,
# self.local_AC.state_in[0]: rnn_state[0],
# self.local_AC.state_in[1]: rnn_state[1]
})
valid_actions = np.zeros(a_size)
valid_actions[validActions] = 1
valid_dist = np.array([a_dist[0, validActions]])
# valid_dist /= np.sum(valid_dist)
if np.sum(valid_dist) > 0:
valid_dist /= np.sum(valid_dist)
else:
valid_dist = np.array([1. / len(valid_dist.ravel())
for _ in range(len(valid_dist.ravel()))])
valid_dist = np.reshape(valid_dist, (1, -1))
train_value = 1.
if TRAINING:
if not self.env.dones[self.agentID]:
if not (np.argmax(a_dist.flatten()) in validActions):
prune_possibility = np.random.rand()
if PRUNE_ACTION and prune_possibility < self.prune_rate:
a = validActions[np.random.choice(range(valid_dist.shape[1]))]
else:
a = np.random.choice(range(a_dist.shape[1]), p=a_dist.ravel())
episode_inv_count += int(not (joint_stuck[self.metaAgentID][self.agentID]
or joint_done[self.metaAgentID][self.agentID]))
if state == 1:
stopping_inv_count += 1
if state in [3, 4]:
must_stop_inv_count += 1
if state == 2:
decision_count_inv_count += 1
train_value = 0.
else:
#a = validActions[np.random.choice(range(valid_dist.shape[1]), p=valid_dist.ravel())]
a= np.argmax(a_dist.flatten())
if joint_stuck[self.metaAgentID][self.agentID] or joint_done[self.metaAgentID][self.agentID]:
a = a_size - 1 # stay there and get punished if stuck...
if state == 1:
if np.random.rand() < np.exp(episode_count*-0.00085):
a = np.random.choice(validActions)
if state == 2:
decision_count += 1
if a == 1:
decision_go_straight_count += 1
if np.random.rand() < np.exp(episode_count*-0.00085):
if 3 not in validActions:
currentobs = joint_observations[self.metaAgentID][self.agentID][0:OBS_SIZE-ADDITIONAL_INPUT]
currentobs = np.reshape(currentobs, (3, -1))
shortest_paths = []
for action in validActions:
shortest_paths.append(float(currentobs[action][1]))
greedyaction = validActions[np.argmin(shortest_paths)]
if np.random.rand() < 0.75:
a = greedyaction
else :
a = np.random.choice(validActions)
train_value = 0.
#if self.agentID ==0:
# print("Random Action taken , Valid Actions =",validActions , 'Action=', a+1)
else:
a = 3 # just stop bc we already achieve the goal,
# note that a is of 4-action style, so a=3
else:
if GREEDY:
a = np.argmax(a_dist.flatten())
else:
a = np.random.choice(range(a_dist.shape[1]), p=a_dist.ravel())
# if a not in validActions or not GREEDY:
# a = validActions[np.random.choice(range(valid_dist.shape[1]), p=valid_dist.ravel())]
# Choose action
if a_size == 4:
joint_actions[self.metaAgentID][self.agentID] = a + 1
else:
joint_actions[self.metaAgentID][self.agentID] = a
self.synchronize() # synchronize threads
# Take single joint step and share new info
if self.agentID == 0:
all_obs, all_rewards, all_done, _ = self.env.step(joint_actions[self.metaAgentID])
for i in range(num_workers):
joint_done[self.metaAgentID][i] = all_done[i]
# If all agents are done (on goal) or stuck (collisions), stop episode early
if any(joint_stuck[self.metaAgentID]):
# done_mask = False # fake true to finish episode early
# base_reward = - self.env.global_reward # and give massive penalty
# todo: we ignore global punishment for stuck env
base_reward = 0
else:
base_reward = 0
if all_done['__all__']:
successful_ep = True
for i in range(num_workers):
joint_observations[self.metaAgentID][i] = all_obs[i]
joint_rewards[self.metaAgentID][i] = base_reward * int(not all_done[i]) + all_rewards[i]
# if someone stucks, all agents are penalized by base_reward
self.synchronize() # synchronize threads
if sum(joint_stuck[self.metaAgentID]) >= STOP_PARAMETER :
done_tag = True
self.synchronize()
if joint_done[self.metaAgentID][self.agentID] and k ==0:
# print('At Target', self.agentID)
k += 1
if episode_step_count <= end_episode:
# Get common observation for all agents after all individual actions have been performed
s1 = joint_observations[self.metaAgentID][self.agentID]
new_pos = self.env.agents[self.agentID].position if self.env.agents[self.agentID].position \
else self.env.agents[self.agentID].initial_position
new_dir = self.env.agents[self.agentID].direction if self.env.agents[self.agentID].direction is not None else \
self.env.agents[self.agentID].initial_direction
r = joint_rewards[self.metaAgentID][self.agentID]
if new_pos == previous_pos and new_dir == previous_dir and \
not joint_stuck[self.metaAgentID][self.agentID]\
and not joint_done[self.metaAgentID][self.agentID] and initialized >1:
if a == a_size - 1:
stopped_counter += 1
if len(validActions) ==1:
valid_stopped_counter += 1
if a != a_size - 1 or stopped_counter >= FUSE_LAYERS :
joint_stuck[self.metaAgentID][self.agentID] = True
if joint_stuck[self.metaAgentID][self.agentID] and t == 0:
# print('Stuck', self.agentID)
t += 1
responsible, steps_cc, _ = self.get_collided_agent(
joint_actions[self.metaAgentID][self.agentID], episode_step_count)
if (stopped_counter >= FUSE_LAYERS and valid_stopped_counter< FUSE_LAYERS-1 and responsible==False ) :
responsible = True
if responsible:
r = r - self.env.global_reward//2 \
+ (max_episode_length - episode_step_count) * self.env.step_penalty
else:
if max(joint_observations[self.metaAgentID][self.agentID][0],
joint_observations[self.metaAgentID][self.agentID][0+ENTRY_PER_COLUMN],
joint_observations[self.metaAgentID][self.agentID][0 + ENTRY_PER_COLUMN * 2]) == 0:
# no optimal path to the goal
r = r + (max_episode_length - episode_step_count) * self.env.step_penalty
else:
# possible to reach goal
r = r +(1)*(max_episode_length - episode_step_count) * self.env.step_penalty
# for ep_i in steps_cc:
# episode_buffer[ep_i][5] = 1
#- max(joint_observations[self.metaAgentID][self.agentID][1],
# joint_observations[self.metaAgentID][self.agentID][1+9],
# joint_observations[self.metaAgentID][self.agentID][1 + 9 * 2]))
else:
stopped_counter = 0
valid_stopped_counter = 0
if saveGIF and self.workerID == 0:
self.env_renderer.render_env(show=False, frames=False, show_observations=False)
episode_frames.append(self.env_renderer.get_image())
episode_step_count += 1
if episode_step_count <= end_episode:
if joint_stuck[self.metaAgentID][self.agentID] or joint_done[self.metaAgentID][self.agentID]:
end_episode = min(end_episode, episode_step_count)
if joint_done[self.metaAgentID][self.agentID] and not self.env.dones["__all__"]:
r = r + self.env.global_reward
if episode_step_count <= end_episode and (not IS_NON_DECISION or episode_step_count == end_episode) and initialized>1:
episode_buffer.append([s, a, r, v[0, 0], valid_actions, train_value])
episode_values.append(v[0, 0])
if episode_step_count != end_episode:
validActions = self._NextValidActions()
# only keep track of number of valid actions until we get stuck/done
shared_nb_va[self.metaAgentID][self.agentID].append(len(validActions))
s = s1
assert len(s) == OBS_SIZE
total_steps += 1
# If the episode hasn't ended, but the experience buffer is full, then we
# make an update step using that experience rollout.
if TRAINING and ((int(episode_step_count) % EXPERIENCE_BUFFER_SIZE == 0 and end_episode== max_episode_length+1) or self.env.dones["__all__"]
or episode_step_count == end_episode or done_tag==True ) and len(episode_buffer) > 0:
# todo: nuke assertion
# assert(len(episode_buffer) == end_episode or end_episode == max_episode_length+1)
# Since we don't know what the true final return is, we "bootstrap"
# from our current value estimation.
if len(episode_buffer) >= EXPERIENCE_BUFFER_SIZE:
training_buffer = episode_buffer[-EXPERIENCE_BUFFER_SIZE:]
else:
training_buffer = episode_buffer[:]
if self.env.dones["__all__"]:
s1Value = 0
else:
s_feed = np.array(s)
s_feed = np.reshape(s_feed, (1, OBS_SIZE))
s1Value = sess.run(self.local_AC.value,
feed_dict={self.local_AC.inputs: s_feed,
# self.local_AC.state_in[0]: rnn_state[0],
# self.local_AC.state_in[1]: rnn_state[1]
})[0, 0]
v_l, p_l, valid_l, e_l, g_n, v_n, episode_reward = self.train(
training_buffer, sess, gamma, s1Value, imitation=False)
self.synchronize() # synchronize threads
if episode_step_count >= max_episode_length or \
(not self.env.dones["__all__"] and
sum(joint_stuck[self.metaAgentID])+sum(joint_done[self.metaAgentID]) == NUM_THREADS)\
or done_tag is True:
if self.agentID == 0:
print('env:', self.metaAgentID, self.env.height, self.env.width, ' episode:', episode_count,
'done:',
sum(joint_done[self.metaAgentID]), ' stuck:',
sum(joint_stuck[self.metaAgentID]), '/', NUM_THREADS,
'at ', episode_step_count, 'th step')
break
if self.env.dones["__all__"]:
if self.agentID == 0:
joint_all_success_count[self.metaAgentID] += 1
print('env:', self.metaAgentID, 'episode ', episode_count, ' finish at ', episode_step_count, 'th step')
break
# # Curriculum update
# if self.agentID == 0:
# global_mutex.acquire()
# if successful_ep and episode_difficulty == MAX_DIFFICULTY: # we successfully finished the latest episode
# joint_success += 1
# if joint_success >= SUCCESS_NEEDED:
# joint_success = 0
# MAX_DIFFICULTY += 1
# print("\n\n\t\tIncreasing Difficult Level to: {:d}\n".format(int(MAX_DIFFICULTY)))
# else:
# joint_success = 0
# global_mutex.release()
if self.env.dones[self.agentID]:
joint_success_count[self.metaAgentID] += 1
actual_episode_lengths[self.metaAgentID].append(episode_step_count)
episode_lengths[self.metaAgentID].append(episode_step_count if self.env.dones["__all__"] else max_episode_length)
episode_mean_values[self.metaAgentID].append(np.nanmean(episode_values))
if PRUNE_ACTION:
effective_count = decision_count + stopping_count
else:
effective_count = episode_step_count
if effective_count != 0:
episode_invalid_ops[self.metaAgentID].append(float(effective_count - episode_inv_count) / effective_count)
episode_invalid_ops_on_decision[self.metaAgentID].append(float(effective_count
- decision_count_inv_count)/effective_count)
episode_invalid_ops_on_stopping[self.metaAgentID].append(float(effective_count
- stopping_inv_count) / effective_count)
episode_invalid_ops_on_muststop[self.metaAgentID].append(float(effective_count
- must_stop_inv_count) / effective_count)
else:
episode_invalid_ops_on_decision[self.metaAgentID].append(1)
episode_invalid_ops_on_stopping[self.metaAgentID].append(1)
episode_invalid_ops_on_muststop[self.metaAgentID].append(1)
if decision_count != 0:
episode_steer_rate[self.metaAgentID].append(float(decision_count
- decision_go_straight_count) / decision_count)
else:
episode_steer_rate[self.metaAgentID].append(0)
# Periodically save gifs of episodes, model parameters, and summary statistics.
if int(episode_count) % EXPERIENCE_BUFFER_SIZE == 0 and printQ:
print(' ', end='\r')
print('({}) Episode terminated ({},{})'.format(int(episode_count), self.agentID, episode_reward), end='\r')