9
9
10
10
sys .path .append (sys .path [0 ] + '/..' )
11
11
from build .inverse_sensor_model import *
12
+ from build .astar import *
12
13
from random import shuffle
13
14
import os
14
15
@@ -29,6 +30,7 @@ def __init__(self, index_map, train, plot):
29
30
self .global_map , self .robot_position = self .map_setup (self .map_dir + '/' + self .map_list [self .li_map ])
30
31
self .op_map = np .ones (self .global_map .shape ) * 127
31
32
self .map_size = np .shape (self .global_map )
33
+ self .finish_percent = 0.985
32
34
self .resolution = 1
33
35
self .sensor_range = 80
34
36
self .old_position = np .zeros ([2 ])
@@ -40,17 +42,17 @@ def __init__(self, index_map, train, plot):
40
42
self .robot_size = 6
41
43
self .local_size = 40
42
44
if self .plot :
43
- self .xPoint = []
44
- self .yPoint = []
45
+ self .xPoint = np .array ([self .robot_position [0 ]])
46
+ self .yPoint = np .array ([self .robot_position [1 ]])
47
+ self .x2frontier = np .empty ([0 ])
48
+ self .y2frontier = np .empty ([0 ])
45
49
46
50
def begin (self ):
47
51
self .op_map = self .inverse_sensor (self .robot_position , self .sensor_range , self .op_map , self .global_map )
48
52
step_map = self .robot_model (self .robot_position , self .robot_size , self .t , self .op_map )
49
53
map_local = self .local_map (self .robot_position , step_map , self .map_size , self .sensor_range + self .local_size )
50
54
if self .plot :
51
- self .xPoint .append (self .robot_position [0 ])
52
- self .yPoint .append (self .robot_position [1 ])
53
- self .gui ()
55
+ self .plot_env ()
54
56
return map_local
55
57
56
58
def step (self , action_index ):
@@ -92,21 +94,21 @@ def step(self, action_index):
92
94
new_location = True
93
95
terminal = True
94
96
if self .plot and self .mode :
95
- self .xPoint .append (self .robot_position [0 ])
96
- self .yPoint .append (self .robot_position [1 ])
97
- self .gui ()
97
+ self .xPoint = ma .append (self . xPoint , self .robot_position [0 ])
98
+ self .yPoint = ma .append (self . yPoint , self .robot_position [1 ])
99
+ self .plot_env ()
98
100
self .robot_position = self .old_position .copy ()
99
101
self .op_map = self .old_op_map .copy ()
100
102
if self .plot and self .mode :
101
- self .xPoint . pop ()
102
- self .yPoint . pop ()
103
+ self .xPoint [ self . xPoint . size - 1 ] = ma . masked
104
+ self .yPoint [ self . yPoint . size - 1 ] = ma . masked
103
105
else :
104
106
if self .plot :
105
- self .xPoint .append (self .robot_position [0 ])
106
- self .yPoint .append (self .robot_position [1 ])
107
- self .gui ()
107
+ self .xPoint = ma .append (self . xPoint , self .robot_position [0 ])
108
+ self .yPoint = ma .append (self . yPoint , self .robot_position [1 ])
109
+ self .plot_env ()
108
110
109
- if np .size (np .where (self .global_map == 255 )) - np .size (np .where (self .op_map == 255 )) < 500 :
111
+ if np .size (np .where (self .op_map == 255 ))/ np .size (np .where (self .global_map == 255 )) > self . finish_percent :
110
112
self .li_map += 1
111
113
if self .li_map == self .map_number :
112
114
self .li_map = 0
@@ -121,20 +123,25 @@ def step(self, action_index):
121
123
def rescuer (self ):
122
124
complete = False
123
125
all_map = False
124
-
126
+ pre_position = self . robot_position . copy ()
125
127
self .robot_position = self .frontier (self .op_map , self .map_size , self .t )
126
128
self .op_map = self .inverse_sensor (self .robot_position , self .sensor_range , self .op_map , self .global_map )
127
129
step_map = self .robot_model (self .robot_position , self .robot_size , self .t , self .op_map )
128
130
map_local = self .local_map (self .robot_position , step_map , self .map_size , self .sensor_range + self .local_size )
129
131
130
132
if self .plot :
131
- self .xPoint .append (ma .masked )
132
- self .yPoint .append (ma .masked )
133
- self .xPoint .append (self .robot_position [0 ])
134
- self .yPoint .append (self .robot_position [1 ])
135
- self .gui ()
136
-
137
- if np .size (np .where (self .global_map == 255 )) - np .size (np .where (self .op_map == 255 )) < 500 :
133
+ path = self .astar_path (self .op_map , pre_position .tolist (), self .robot_position .tolist ())
134
+ self .x2frontier = ma .append (self .x2frontier , ma .masked )
135
+ self .y2frontier = ma .append (self .y2frontier , ma .masked )
136
+ self .x2frontier = ma .append (self .x2frontier , path [1 , :])
137
+ self .y2frontier = ma .append (self .y2frontier , path [0 , :])
138
+ self .xPoint = ma .append (self .xPoint , ma .masked )
139
+ self .yPoint = ma .append (self .yPoint , ma .masked )
140
+ self .xPoint = ma .append (self .xPoint , self .robot_position [0 ])
141
+ self .yPoint = ma .append (self .yPoint , self .robot_position [1 ])
142
+ self .plot_env ()
143
+
144
+ if np .size (np .where (self .op_map == 255 ))/ np .size (np .where (self .global_map == 255 )) > self .finish_percent :
138
145
self .li_map += 1
139
146
if self .li_map == self .map_number :
140
147
self .li_map = 0
@@ -297,11 +304,38 @@ def unique_rows(self, a):
297
304
result = result [~ np .isnan (result ).any (axis = 1 )]
298
305
return result
299
306
300
- def gui (self ):
307
+ def astar_path (self , weights , start , goal , allow_diagonal = True ):
308
+ temp_start = [start [1 ], start [0 ]]
309
+ temp_goal = [goal [1 ], goal [0 ]]
310
+ temp_weight = (weights < 150 ) * 254 + 1
311
+ # For the heuristic to be valid, each move must cost at least 1.
312
+ if temp_weight .min (axis = None ) < 1. :
313
+ raise ValueError ("Minimum cost to move must be 1, but got %f" % (
314
+ temp_weight .min (axis = None )))
315
+ # Ensure start is within bounds.
316
+ if (temp_start [0 ] < 0 or temp_start [0 ] >= temp_weight .shape [0 ] or
317
+ temp_start [1 ] < 0 or temp_start [1 ] >= temp_weight .shape [1 ]):
318
+ raise ValueError ("Start lies outside grid." )
319
+ # Ensure goal is within bounds.
320
+ if (temp_goal [0 ] < 0 or temp_goal [0 ] >= temp_weight .shape [0 ] or
321
+ temp_goal [1 ] < 0 or temp_goal [1 ] >= temp_weight .shape [1 ]):
322
+ raise ValueError ("Goal of lies outside grid." )
323
+
324
+ height , width = temp_weight .shape
325
+ start_idx = np .ravel_multi_index (temp_start , (height , width ))
326
+ goal_idx = np .ravel_multi_index (temp_goal , (height , width ))
327
+
328
+ path = astar (
329
+ temp_weight .flatten (), height , width , start_idx , goal_idx , allow_diagonal ,
330
+ )
331
+ return path
332
+
333
+ def plot_env (self ):
301
334
plt .cla ()
302
335
plt .imshow (self .op_map , cmap = 'gray' )
303
336
plt .axis ((0 , self .map_size [1 ], self .map_size [0 ], 0 ))
304
337
plt .plot (self .xPoint , self .yPoint , 'b' , linewidth = 2 )
338
+ plt .plot (self .x2frontier , self .y2frontier , 'r' , linewidth = 2 )
305
339
plt .plot (self .robot_position [0 ], self .robot_position [1 ], 'mo' , markersize = 8 )
306
340
plt .plot (self .xPoint [0 ], self .yPoint [0 ], 'co' , markersize = 8 )
307
341
plt .pause (0.05 )
0 commit comments