Skip to content

Commit 61fa268

Browse files
committedJul 15, 2021
fixed bugs
1 parent 1e301c0 commit 61fa268

File tree

5 files changed

+96
-890
lines changed

5 files changed

+96
-890
lines changed
 

‎A_star.py

+38-43
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55

66
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):
99
self.robot_loc = robot_loc
1010
self.butter_loc = butter_loc
11-
self.h=h
11+
self.h = h
1212
self.parent = parent
1313
self.last_move = last_move
1414
if parent:
@@ -18,7 +18,8 @@ def __init__(self, robot_loc: tuple, butter_loc: list,h :list, last_move: str =
1818
self.cost = cost
1919
self.depth = 0
2020

21-
heuristic=[]
21+
22+
heuristic = []
2223
environment = []
2324
frontier = []
2425
explored = []
@@ -32,20 +33,22 @@ def __init__(self, robot_loc: tuple, butter_loc: list,h :list, last_move: str =
3233
x = 0
3334
y = 0
3435

36+
3537
def fill_heuristic(env):
36-
h=[]
37-
h1=[]
38+
h = []
39+
h1 = []
3840
for i in range(x):
3941
for j in range(y):
4042
for k in range(len(goal_location)):
4143
# 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))
4345
h.append(e)
4446
h1.append(h.copy())
4547
h.clear()
4648
heuristic.append(h1.copy())
4749
h1.clear()
4850

51+
4952
def read_from_file(file_name: str):
5053
global x, y, robot_location
5154
f = open(file_name, "r")
@@ -67,11 +70,14 @@ def read_from_file(file_name: str):
6770

6871
def write_on_file(file_name):
6972
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")
7581

7682

7783
def get_path(node: Node):
@@ -102,27 +108,25 @@ def check_goal(node: Node):
102108
return None
103109

104110

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]
109114
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]
113118
return min_node
114119

115120

116-
117-
def A_star(node : Node):
121+
def A_star(node: Node):
118122
frontier.append(node)
119-
p=0
123+
p = 0
120124
end = False
121125
while frontier:
122-
min_node=find_min(frontier,p)
126+
min_node = find_min(frontier, p)
123127
frontier.remove(min_node)
124128
explored.append(min_node)
125-
new_root=check_goal(min_node)
129+
new_root = check_goal(min_node)
126130
if (new_root != None):
127131
if (len(goal_location) > 0):
128132
explored.clear()
@@ -132,7 +136,7 @@ def A_star(node : Node):
132136
end = True
133137
if end:
134138
break
135-
ch=generate_children(min_node)
139+
ch = generate_children(min_node)
136140
for i in range(len(ch)):
137141
condition = True
138142
for j in range(len(explored)):
@@ -147,17 +151,6 @@ def A_star(node : Node):
147151
frontier.append(ch[i])
148152

149153

150-
151-
152-
153-
154-
155-
156-
157-
158-
159-
160-
161154
def generate_children(node: Node):
162155
children = []
163156
robot_neighbors = []
@@ -179,22 +172,25 @@ def generate_children(node: Node):
179172
if (up[0] >= 0 and up[0] < x and up[1] >= 0 and up[1] < y):
180173
if (not (up in forbidden_location)):
181174
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)))
183176

184177
if (down[0] >= 0 and down[0] < x and down[1] >= 0 and down[1] < y):
185178
if (not (down in forbidden_location)):
186179
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)))
188182

189183
if (left[0] >= 0 and left[0] < x and left[1] >= 0 and left[1] < y):
190184
if (not (left in forbidden_location)):
191185
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)))
193188

194189
if (right[0] >= 0 and right[0] < x and right[1] >= 0 and right[1] < y):
195190
if (not (right in forbidden_location)):
196191
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)))
198194

199195
if (len(neighbor_butter_location) != 0):
200196
for i in neighbor_butter_location:
@@ -215,17 +211,16 @@ def generate_children(node: Node):
215211
temp = node.butter_loc.copy()
216212
temp.remove(i)
217213
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)))
219215
return children
220216

221217

222218
file_name = input() + ".txt"
223219

224220
read_from_file(file_name)
225221

226-
227222
# 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)
229224
A_star(start)
230225
# ch=generate_children(start)
231226
# for i in range(len(ch)):

‎IDS.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ def read_from_file(file_name: str):
5252

5353
def write_on_file(file_name):
5454
f = open(file_name, "w")
55-
for i in range(len(path), 0, -1):
56-
f.write(path[i - 1] + " ")
57-
f.write("\n")
58-
f.write(str(final_cost) + "\n")
59-
f.write(str(final_depth) + "\n")
55+
if (len(goal_location) == 0):
56+
for i in range(len(path), 0, -1):
57+
f.write(path[i - 1] + " ")
58+
f.write("\n")
59+
f.write(str(final_cost) + "\n")
60+
f.write(str(final_depth) + "\n")
61+
else:
62+
f.write("can’t pass the butter")
6063

6164

6265
def get_path(node: Node):
@@ -201,10 +204,4 @@ def generate_children(node: Node):
201204
read_from_file(file_name)
202205
start = Node(robot_location, butter_location, None, None, 0)
203206
IDS(start)
204-
# write_on_file("result" + file_name[4] + ".txt")
205-
206-
207-
if (len(goal_location) == 0):
208-
print(path)
209-
else:
210-
print("can’t pass the butter")
207+
write_on_file("result" + file_name[4] + ".txt")

0 commit comments

Comments
 (0)
Please sign in to comment.