4
4
5
5
6
6
class Node :
7
- def __init__ (self , robot_loc : tuple , butter_loc : list ,h : list , last_move : str = None , parent = None , cost : int = 0 ,
8
- depth : int = 0 ):
7
+ def __init__ (self , robot_loc : tuple , butter_loc : list , h : list , last_move : str = None , parent = None , cost : int = 0 ,
8
+ depth : int = 0 ):
9
9
self .robot_loc = robot_loc
10
10
self .butter_loc = butter_loc
11
- self .h = h
11
+ self .h = h
12
12
self .parent = parent
13
13
self .last_move = last_move
14
14
if parent :
@@ -18,7 +18,8 @@ def __init__(self, robot_loc: tuple, butter_loc: list,h :list, last_move: str =
18
18
self .cost = cost
19
19
self .depth = 0
20
20
21
- heuristic = []
21
+
22
+ heuristic = []
22
23
environment = []
23
24
frontier = []
24
25
explored = []
@@ -32,20 +33,22 @@ def __init__(self, robot_loc: tuple, butter_loc: list,h :list, last_move: str =
32
33
x = 0
33
34
y = 0
34
35
36
+
35
37
def fill_heuristic (env ):
36
- h = []
37
- h1 = []
38
+ h = []
39
+ h1 = []
38
40
for i in range (x ):
39
41
for j in range (y ):
40
42
for k in range (len (goal_location )):
41
43
# e=goal_location[k]-env[i][j]
42
- e = (abs (goal_location [k ][0 ]- i ) + abs (goal_location [k ][1 ]- j ))
44
+ e = (abs (goal_location [k ][0 ] - i ) + abs (goal_location [k ][1 ] - j ))
43
45
h .append (e )
44
46
h1 .append (h .copy ())
45
47
h .clear ()
46
48
heuristic .append (h1 .copy ())
47
49
h1 .clear ()
48
50
51
+
49
52
def read_from_file (file_name : str ):
50
53
global x , y , robot_location
51
54
f = open (file_name , "r" )
@@ -67,11 +70,14 @@ def read_from_file(file_name: str):
67
70
68
71
def write_on_file (file_name ):
69
72
f = open (file_name , "w" )
70
- for i in range (len (path ), 0 , - 1 ):
71
- f .write (path [i - 1 ] + " " )
72
- f .write ("\n " )
73
- f .write (str (final_cost ) + "\n " )
74
- f .write (str (final_depth ) + "\n " )
73
+ if (len (goal_location ) == 0 ):
74
+ for i in range (len (path ), 0 , - 1 ):
75
+ f .write (path [i - 1 ] + " " )
76
+ f .write ("\n " )
77
+ f .write (str (final_cost ) + "\n " )
78
+ f .write (str (final_depth ) + "\n " )
79
+ else :
80
+ f .write ("can’t pass the butter" )
75
81
76
82
77
83
def get_path (node : Node ):
@@ -102,27 +108,25 @@ def check_goal(node: Node):
102
108
return None
103
109
104
110
105
- def find_min (fron ,goal_number ):
106
-
107
- min = fron [0 ].h [goal_number ]+ fron [0 ].cost
108
- min_node = fron [0 ]
111
+ def find_min (fron , goal_number ):
112
+ min = fron [0 ].h [goal_number ] + fron [0 ].cost
113
+ min_node = fron [0 ]
109
114
for i in range (len (fron )):
110
- if (min > fron [i ].h [goal_number ]+ fron [i ].cost ):
111
- min = fron [i ].h [goal_number ]+ fron [i ].cost
112
- min_node = fron [i ]
115
+ if (min > fron [i ].h [goal_number ] + fron [i ].cost ):
116
+ min = fron [i ].h [goal_number ] + fron [i ].cost
117
+ min_node = fron [i ]
113
118
return min_node
114
119
115
120
116
-
117
- def A_star (node : Node ):
121
+ def A_star (node : Node ):
118
122
frontier .append (node )
119
- p = 0
123
+ p = 0
120
124
end = False
121
125
while frontier :
122
- min_node = find_min (frontier ,p )
126
+ min_node = find_min (frontier , p )
123
127
frontier .remove (min_node )
124
128
explored .append (min_node )
125
- new_root = check_goal (min_node )
129
+ new_root = check_goal (min_node )
126
130
if (new_root != None ):
127
131
if (len (goal_location ) > 0 ):
128
132
explored .clear ()
@@ -132,7 +136,7 @@ def A_star(node : Node):
132
136
end = True
133
137
if end :
134
138
break
135
- ch = generate_children (min_node )
139
+ ch = generate_children (min_node )
136
140
for i in range (len (ch )):
137
141
condition = True
138
142
for j in range (len (explored )):
@@ -147,17 +151,6 @@ def A_star(node : Node):
147
151
frontier .append (ch [i ])
148
152
149
153
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
154
def generate_children (node : Node ):
162
155
children = []
163
156
robot_neighbors = []
@@ -179,22 +172,25 @@ def generate_children(node: Node):
179
172
if (up [0 ] >= 0 and up [0 ] < x and up [1 ] >= 0 and up [1 ] < y ):
180
173
if (not (up in forbidden_location )):
181
174
if (not (up in neighbor_butter_location )):
182
- children .append (Node (up , node .butter_loc , heuristic [up [0 ]][up [1 ]], "u" , node , fill_node_cost (up )))
175
+ children .append (Node (up , node .butter_loc , heuristic [up [0 ]][up [1 ]], "u" , node , fill_node_cost (up )))
183
176
184
177
if (down [0 ] >= 0 and down [0 ] < x and down [1 ] >= 0 and down [1 ] < y ):
185
178
if (not (down in forbidden_location )):
186
179
if (not (down in neighbor_butter_location )):
187
- children .append (Node (down , node .butter_loc ,heuristic [down [0 ]][down [1 ]], "d" , node , fill_node_cost (down )))
180
+ children .append (
181
+ Node (down , node .butter_loc , heuristic [down [0 ]][down [1 ]], "d" , node , fill_node_cost (down )))
188
182
189
183
if (left [0 ] >= 0 and left [0 ] < x and left [1 ] >= 0 and left [1 ] < y ):
190
184
if (not (left in forbidden_location )):
191
185
if (not (left in neighbor_butter_location )):
192
- children .append (Node (left , node .butter_loc ,heuristic [left [0 ]][left [1 ]], "l" , node , fill_node_cost (left )))
186
+ children .append (
187
+ Node (left , node .butter_loc , heuristic [left [0 ]][left [1 ]], "l" , node , fill_node_cost (left )))
193
188
194
189
if (right [0 ] >= 0 and right [0 ] < x and right [1 ] >= 0 and right [1 ] < y ):
195
190
if (not (right in forbidden_location )):
196
191
if (not (right in neighbor_butter_location )):
197
- children .append (Node (right , node .butter_loc ,heuristic [right [0 ]][right [1 ]], "r" , node , fill_node_cost (right )))
192
+ children .append (
193
+ Node (right , node .butter_loc , heuristic [right [0 ]][right [1 ]], "r" , node , fill_node_cost (right )))
198
194
199
195
if (len (neighbor_butter_location ) != 0 ):
200
196
for i in neighbor_butter_location :
@@ -215,17 +211,16 @@ def generate_children(node: Node):
215
211
temp = node .butter_loc .copy ()
216
212
temp .remove (i )
217
213
temp .append (b_loc )
218
- children .append (Node (i , temp , heuristic [i [0 ]][i [1 ]], action , node , fill_node_cost (i )))
214
+ children .append (Node (i , temp , heuristic [i [0 ]][i [1 ]], action , node , fill_node_cost (i )))
219
215
return children
220
216
221
217
222
218
file_name = input () + ".txt"
223
219
224
220
read_from_file (file_name )
225
221
226
-
227
222
# print(heuristic)
228
- start = Node (robot_location , butter_location ,heuristic [robot_location [0 ]][robot_location [1 ]],None , None , 0 )
223
+ start = Node (robot_location , butter_location , heuristic [robot_location [0 ]][robot_location [1 ]], None , None , 0 )
229
224
A_star (start )
230
225
# ch=generate_children(start)
231
226
# for i in range(len(ch)):
0 commit comments