-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
147 lines (115 loc) · 4.14 KB
/
search.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import matplotlib.pyplot as plt
import numpy as np
import time
import matplotlib.animation as animation
from utils import *
from grid import *
from node import *
def bfSearch(problem, dest):
current = Node(problem)
pathCost = 0
if current.state == dest:
return current
frontier = Queue()
frontier.push(current)
reached = [current.state] # set to check if point has been reached before.
while not frontier.isEmpty():
node = frontier.pop()
nodes = expand(node, False)
for n in nodes:
print(n.state)
s = n.getPoint()
if s == dest:
return n
reached_count = reached.count(s)
if reached_count == 0:
reached.append(s)
frontier.push(n)
return -1
def dfSearch(problem, dest):
current = Node(problem)
frontier = PriorityQueue()
frontier.push(current)
reached = [current.state]
while not frontier.isEmpty():
node = frontier.pop()
if node.state == dest:
def expand(node, informed): #expand order - up, right, down, left (clockwise)
"""
general idea:
for BFS and DFS, check if point would cause path to go into an enclosure, which is forbidden. Return first node found clockwise that satisfies this condition.
for informed searches, need to calculate action cost for each move as well as check if enclosure is encountered.
"""
adjPoints = node.getPoint().adjacentPoints()
if informed == False: #BFS or DFS, don't worry about action cost.
for point in adjPoints:
if point.isLegal():
cost = node.getCost() + 1
n = Node(point, node, point.getAction(), cost)
yield n
def gen_polygons(worldfilepath):
polygons = []
with open(worldfilepath, "r") as f:
lines = f.readlines()
lines = [line[:-1] for line in lines]
for line in lines:
polygon = []
pts = line.split(';')
for pt in pts:
xy = pt.split(',')
polygon.append(Point(int(xy[0]), int(xy[1])))
polygons.append(polygon)
return polygons
if __name__ == "__main__":
epolygons = gen_polygons('TestingGrid/world1_enclosures.txt')
tpolygons = gen_polygons('TestingGrid/world1_turfs.txt')
source = Point(8,10)
dest = Point(43,45)
fig, ax = draw_board()
draw_grids(ax)
draw_source(ax, source.x, source.y) # source point
draw_dest(ax, dest.x, dest.y) # destination point
# Draw enclosure polygons
for polygon in epolygons:
for p in polygon:
draw_point(ax, p.x, p.y)
for polygon in epolygons:
for i in range(0, len(polygon)):
draw_line(ax, [polygon[i].x, polygon[(i+1)%len(polygon)].x], [polygon[i].y, polygon[(i+1)%len(polygon)].y])
# Draw turf polygons
for polygon in tpolygons:
for p in polygon:
draw_green_point(ax, p.x, p.y)
for polygon in tpolygons:
for i in range(0, len(polygon)):
draw_green_line(ax, [polygon[i].x, polygon[(i+1)%len(polygon)].x], [polygon[i].y, polygon[(i+1)%len(polygon)].y])
#### Here call search to compute and collect res_path
"""
TODO:
Breadth-First Search
Depth-First Search
Greedy Best-First Search
A* Search
*Graph search versions*
Create more turf and enclosure polygons
"""
# BFS
bfsNode = bfSearch(source, dest)
if bfsNode == -1:
print("Error with bfs algorithm")
exit(-1)
res_path = []
pCost = 0
cur = bfsNode
print(bfsNode)
while bfsNode.state != source:
print("point is " + str(bfsNode.getPoint().x) + ", " + str(bfsNode.getPoint().y))
res_path.append(bfsNode.getPoint())
bfsNode = bfsNode.parent
res_path.append(bfsNode.getPoint())
#res_path = [Point(24,17), Point(25,17), Point(26,17), Point(27,17),
#Point(28,17), Point(28,18), Point(28,19), Point(28,20)]
for i in range(len(res_path)-1):
draw_result_line(ax, [res_path[i].x, res_path[i+1].x], [res_path[i].y, res_path[i+1].y])
plt.pause(0.1)
plt.show()