Skip to content

Commit cbc72ea

Browse files
committed
finish A* search
1 parent 6ab3fda commit cbc72ea

File tree

7 files changed

+204
-18
lines changed

7 files changed

+204
-18
lines changed

.pydevproject

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
4+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
5+
</pydev_project>

Assign2/jon/assign2/A_star_search.py

+68-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,80 @@
22
Created on Feb 16, 2014
33
44
@author: Jon
5-
'''
6-
from jon.assign2.prover9 import Prover9
7-
85
96
7+
pseudocode of A* search
8+
A*-GRAPH-SEARCH(current,goals,allowed)
9+
input: current, current position of the agent
10+
goals, a list of goal squares
11+
allowed, a list of save squares to go to
12+
13+
'''
14+
from prover9 import Prover9
1015

16+
def a_star_batch(current,goals,allowed):
17+
'''
18+
@param current: current position of the agent
19+
@param goals: a list of goal squares
20+
@param allowed: a list of save squares to go to
21+
@return: the nearest goal square and a plan to get to the goal square with shortest path
22+
'''
23+
nearest_goal = (float("inf"),None)
24+
for goal in goals:
25+
distance,plan = a_star(current,goal,allowed)
26+
if distance < nearest_goal[0]:
27+
nearest_goal = goal
28+
shortest_path = (distance,plan)
29+
return (nearest_goal, shortest_path)
1130

31+
def a_star(current,goal,allowed):
32+
'''
33+
@param current: current position of the agent
34+
@param goal: goal square of the path
35+
@param allowed: a list of save squares to go to
36+
@return: a plan to get to the goal square
37+
'''
38+
nodes = {}
39+
for square in allowed:
40+
nodes[square] = node(mdistance(square,goal));
41+
nodes[current].g = 0
42+
open = []
43+
plan = []
44+
f = nodes[current].g + nodes[current].h
45+
heappush(open,(f, current))
46+
while len(open) > 0:
47+
p = heappop(open)
48+
neighbors = allowed_neighbors(p,allowed)
49+
for square in neighbors:
50+
g = nodes[p].g + 1
51+
h = mdistance(square,goal)
52+
f_new = g + h
53+
if square == goal:
54+
nodes[goal].parent = p
55+
plan.append(goal)
56+
node_in_path = nodes[goal]
57+
while node_in_path.parent != None:
58+
plan.append(node_in_path.parent)
59+
node_in_path = nodes[node_in_path.parent]
60+
return (f_new, plan)
61+
f_old = nodes[square].g + nodes[square].h
62+
if f_new < f_old:
63+
nodes[square].g = g
64+
nodes[square].parent = p
65+
if open.count((f_old,square)) > 0:
66+
open.remove((f_old,square))
67+
heappush(open,(f_new, square))
68+
return(-1,plan)
1269

1370

71+
class node(object):
72+
def __init__(self,h=float("inf"),g=float("inf"),parent=None):
73+
'''
74+
75+
'''
76+
self.g = g
77+
self.h = h
78+
self.parent = parent
1479

15-
if __name__ == '__main__':
1680

1781

Assign2/jon/assign2/Utilities.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Created on Feb 14, 2014
3+
4+
@author: Jon
5+
'''
6+
7+
def allowed_neighbors(current, allowed):
8+
'''
9+
Generate neighbor square of current position from a list of allowed square
10+
@param current: a tuple containing the coordinates of the current position of the agent
11+
@param allowed: a tuple list of safe squares
12+
@return: a tuple list of allowed neighbor square of the current position
13+
'''
14+
neighbors = []
15+
currentx = current[0]
16+
currenty = current[1]
17+
for i in range(-1,3):
18+
for j in range(-1,3):
19+
temp = (currentx + i, currenty + j)
20+
if allowed.count(temp) > 0:
21+
neighbors.append(temp)
22+
neighbors.remove(current)
23+
return neighbors
24+
25+
def mdistance(square, goal):
26+
"""
27+
@return: Manhattan distance between given square and the goal
28+
"""
29+
return abs(goal[0] - square[0]) + abs(goal[1] - square[1])
30+
31+
if __name__ == '__main__':
32+
current = (1, 1)
33+
allowed = [(1, 1), (1, 2)]
34+
neighbors = allowed_neighbors(current, allowed)
35+
print(neighbors)
36+
print(current)

Assign2/jon/assign2/hybrid_search.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
Created on Feb 16, 2014
3+
4+
@author: Jon
5+
6+
7+
pseudocode of a hybrid agent program for the wumpus world.
8+
It uses a propositional knowledge base to infer the state
9+
of the world, and a combination of problem-solving search
10+
and domain-specific code to decide what actions to take.
11+
12+
function HYBRID-WUMPUS-AGENT(percept) returns an action
13+
inputs: percept, a list, [stench,breeze,glitter,bump,scream]
14+
persistent: KB, a knowledge base, initially the atemporal "wumpus physics"
15+
t, a counter, initially 0, indicating time
16+
plan, an action sequence, initially empty
17+
18+
TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
19+
TELL the KB the temporal "physics" sentences for time t
20+
safe <- {[x,y] : ASK(KB, OK_xy^t)=true}
21+
22+
if ASK(KB, Glitter^t) = true then
23+
plan <- [Grab] + PLAN-ROUTE(current,{[1,1]},safe)+[Climb]
24+
if plan is empty then
25+
unvisited <- {[x,y] : ASK(KB,L_x,y^t') = false for all t'<=t}
26+
plan <- PLAN-ROUTE(current,unvisited^safe,safe)
27+
if plan is empty and ASK(KB,HaveArrow^t) = true then
28+
possible_wumpus <- {[s,y] : ASK(KB,-W_x,y)=false}
29+
plan <- PLAN-SHOT(current,possible_wumpus,safe)
30+
if plan is empty then // no choice but to take a risk
31+
not_unsafe <- {[x,y] : ASK(KB,-OK_x,y^t)=false}
32+
plan <- PLAN-ROUTE(current,unvisited^not_unsafe,safe)
33+
if plan is empty then
34+
plan <- PLAN-ROUTE(current,{[1,1]},safe) + [Climb]
35+
action <- POP(plan)
36+
TELL(KB,MAKE-ACTION-SENTENCE(action,t))
37+
t <- t + 1
38+
return action
39+
40+
function PLAN-ROUTE(current,goals,allowed) returns an action sequence
41+
input: current, the agent's current position
42+
goals, a set of squares; try to plan a route to one of them
43+
allowed, a set of squares that can form part of the route
44+
45+
problem <- ROUTE-PROBLEM(current,goals,allowed)
46+
return A*-GRAPH-SEARCH(problem)
47+
48+
'''
49+
from prover9 import Prover9
50+
51+
52+
53+
54+
55+
56+
57+
58+
if __name__ == '__main__':
59+
pass
60+

Assign2/jon/assign2/mainmodule.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ def buildmap(filename):
6767
fptr.close()
6868
return (wumpusmap, agent_location, goal)
6969

70-
def mdistance(square, goal):
71-
"""
72-
Return Manhattan distance between given square and the goal
73-
"""
74-
return abs(goal[0] - square[0]) + abs(goal[1] - square[1])
70+
7571

7672
def initassumption():
7773
s = 'all x all y all a all b adjacent((x, y), (a,b)) <->'

Assign2/jon/assign2/test.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
44
@author: Jon
55
'''
6-
from jon.assign2.prover9 import Prover9
6+
from prover9 import Prover9
7+
8+
9+
10+
class test1(object):
11+
def __init__(self,a=0):
12+
self.a = a
13+
14+
def setA(self,a):
15+
self.a = a
16+
717

818
if __name__ == '__main__':
919
# filename = 'F:\\Program Files (x86)\\Prover9-Mace4\\bin-win32\\prover9.exe'
@@ -34,7 +44,22 @@
3444
# print(dict[0])
3545
# dict[0].append('one')
3646
# print(dict[0][0])
37-
for i in range(1, 10):
38-
if i == 4:
39-
break
40-
print(i)
47+
# for i in range(1, 10):
48+
# if i == 4:
49+
# break
50+
# print(i)
51+
# a = [(1,2), (2,1)]
52+
# print(a[0][1])
53+
# b = test1(1)
54+
# print(b.a)
55+
# b.a = 10
56+
# print(b.a)
57+
# b.setA(100)
58+
# print(b.a)
59+
a = []
60+
for i in range(1,10):
61+
a.append(i)
62+
for i in range(1,10):
63+
print(a.pop())
64+
65+

Assign2/jon/assign2/wumpusmap.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,29 @@ def shoot(self, x, y, tx, ty):
9292
self.get_content(_x, y).remove('W')
9393
for nx, ny in self.neighbor(_x, y):
9494
self.get_content(nx, ny).remove('S')
95-
return 'Hit'
95+
return 'Scream'
9696
elif x < tx:
9797
for _x in range(x+1, self._width):
9898
if 'W' in self.get_content(_x, y):
9999
self.get_content(_x, y).remove('W')
100100
for nx, ny in self.neighbor(_x, y):
101101
self.get_content(nx, ny).remove('S')
102-
return 'Hit'
102+
return 'Scream'
103103
if y != ty:
104104
if y > ty:
105105
for _y in range(0, y):
106106
if 'W' in self.get_content(x, _y):
107107
self.get_content(x, _y).remove('W')
108108
for nx, ny in self.neighbor(x, _y):
109109
self.get_content(nx, ny).remove('S')
110-
return 'Hit'
110+
return 'Scream'
111111
elif y < ty:
112112
for _y in range(y+1, self._height):
113113
if 'W' in self.get_content(x, _y):
114114
self.get_content(x, _y).remove('W')
115115
for nx, ny in self.neighbor(x, _y):
116116
self.get_content(nx, ny).remove('S')
117-
return 'Hit'
117+
return 'Scream'
118118
return 'Miss'
119119

120120

0 commit comments

Comments
 (0)