forked from kushalmehta13/AI-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beamSearch.py
122 lines (102 loc) · 3.72 KB
/
beamSearch.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
import heapq
import math
import random
cost_dict = {'w' : math.inf, 'm' : 100, 'p' : 10, 's' : 30}
class Node():
def __init__(self, parent = None, location = None):
self.parent = parent
self.localtion = location
# cost function
self.f = 0
# Heuristic
self.h = 0
self.g = self.f + self.h
def goalTest(curr, goal):
if curr == goal:
return True
return False
def surrounding_spaces(startNode, mat):
# Check if the agent is near the border of the maze
x = startNode.localtion[0]
y = startNode.localtion[1]
observable = []
# North Neighbor
if x-1 >= 0:
observable.append((x-1, y))
# East Neighbor
if y+1 <= len(mat) - 1:
observable.append((x, y+1))
# South Neighbor
if x+1 <= len(mat) - 1:
observable.append((x+1,y))
# West Neighbor
if y-1 >= 0:
observable.append((x, y-1))
# North east neighbor
if x-1 >= 0 and y+1 <= len(mat) - 1 and mat[x-1][y+1] != 'w':
observable.append((x-1, y+1))
# South East Neighbor
if x+1 <= len(mat) - 1 and y+1 <= len(mat) - 1 and mat[x+1][y+1] != 'w':
observable.append((x+1, y+1))
# South West Neighbor
if x+1 <= len(mat) - 1 and y-1 >= 0 and mat[x][y-1] != 'w':
observable.append((x+1, y-1))
# North West Neighbor
if x-1 >= 0 and y-1 >= 0 and mat[x-1][y-1] != 'w':
observable.append((x-1, y-1))
return observable
def findWalls(ifWall, parent):
if mat[parent.localtion[0]-1][parent.localtion[1]+1] == 'w':
ifWall[(parent.localtion[0]-1, parent.localtion[1]+1)] = True
else:
ifWall[(parent.localtion[0] - 1, parent.localtion[1] + 1)] = False
if mat[parent.localtion[0]-1][parent.localtion[1]-1] == 'w':
ifWall[(parent.localtion[0]-1, parent.localtion[1]-1)] = True
else:
ifWall[(parent.localtion[0] - 1, parent.localtion[1] - 1)] = False
if mat[parent.localtion[0]+1][parent.localtion[1]+1] == 'w':
ifWall[(parent.localtion[0]+1, parent.localtion[1]+1)] = True
else:
ifWall[(parent.localtion[0] + 1, parent.localtion[1] + 1)] = False
if mat[parent.localtion[0]+1][parent.localtion[1]-1] == 'w':
ifWall[(parent.localtion[0]+1, parent.localtion[1]-1)] = True
else:
ifWall[(parent.localtion[0] + 1, parent.localtion[1] - 1)] = False
def findNextMove(observable, mat, parent):
available_moves = {}
ifWall = {}
findWalls(ifWall, parent)
for loc in observable:
if mat[loc[0]][loc[1]] != 'w':
score = 0
if loc[0]==0 or loc[0]==len(mat):
score = -math.inf
if loc[1] == 0 or loc[1] == len(mat):
score = -math.inf
# North
# northOfThis = (loc[0]-1, loc[1])
# southOfThis = (loc[0]+1, loc[1])
# eastOfThis = (loc[0], loc[1]+1)
# westOfThis = (loc[0], loc[1]-1)
available_moves[loc] = score
print(available_moves)
def solve(start, goal, mat):
startNode = Node(None, start)
endNode = Node(None, goal)
curr = startNode
observable = surrounding_spaces(curr, mat)
traversed = []
print(observable)
while (goal not in observable):
observable = surrounding_spaces(curr, mat)
chosenTile = findNextMove(observable, mat, curr)
if __name__ == '__main__':
mat = [['w','w','w','w','w','w','w','p'],
['m','m','s','s','s','p','p','w'],
['s','s','p','s','p','s','m','w'],
['p','w','m','s','s','s','w','w'],
['w','w','m','w','w','m','p','p'],
['w','p','w','w','s','p','w','m'],
['m','s','s','w','w','p','p','p'],
['s','s','p','m','m','p','w','w']]
path = solve((6,7), (6,2), mat)