-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_finding.rb
executable file
·311 lines (247 loc) · 6.9 KB
/
path_finding.rb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#! /usr/bin/env ruby
# My reference to the algorithm:
# https://www.youtube.com/watch?v=KNXfSOx4eEE
#
# and using the maze from maze.rb
class Graph
attr_accessor :start_node, :goal_node, :nodes, :initialized
def initialize
@initialized = false
@nodes = []
end
def initialized?
@initialized
end
def add_node(node)
@initialized = false
node.graph = self
if node.start_node?
raise ArgumentError, 'already defined one' if @start_node
@start_node = node
end
if node.goal_node?
raise ArgumentError, 'already defined one' if @goal_node
@goal_node = node
end
@nodes << node
end
def complete!
@nodes.map(&:heuristic)
@nodes.each do |node|
# MAGIC
if node.portal_node?
node.edges << Edge.new(self.goal_node, 0)
end
# up
up = @nodes.select{ |n| (n.x == node.x) && (n.y == node.y+1) }.first
node.edges << Edge.new(up, 10) if up
# up-left
upl = @nodes.select{ |n| (n.x == node.x+1) && (n.y == node.y+1) }.first
node.edges << Edge.new(upl, 14) if upl
# up-right
upr = @nodes.select{ |n| (n.x == node.x-1) && (n.y == node.y+1) }.first
node.edges << Edge.new(upr, 14) if upr
# down
down = @nodes.select{ |n| (n.x == node.x) && (n.y == node.y-1) }.first
node.edges << Edge.new(down, 10) if down
# down-left
downl = @nodes.select{ |n| (n.x == node.x-1) && (n.y == node.y-1) }.first
node.edges << Edge.new(downl, 14) if downl
# down-right
downr = @nodes.select{ |n| (n.x == node.x+1) && (n.y == node.y-1) }.first
node.edges << Edge.new(downr, 14) if downr
# left
left = @nodes.select{ |n| (n.x == node.x-1) && (n.y == node.y) }.first
node.edges << Edge.new(left, 10) if left
# right
right = @nodes.select{ |n| (n.x == node.x+1) && (n.y == node.y) }.first
node.edges << Edge.new(right, 10) if right
end
@initialized = true
end
def solve!
self.start_node.cost = self.start_node.heuristic
@open = [self.start_node]
while(current_node = @open.sort!{|a,b| a.cost <=> b.cost }.shift) do
current_node.edges.each do |edge|
# don't do anything
if edge.to.visited? || edge.to.obstacle_node?
else
total_cost = edge.to.heuristic + edge.movement + (current_node.cost)
if edge.to.goal_node?
edge.to.parent = current_node
puts "Goal Node reached, path exists through: #{current_node.x}, #{current_node.y}"
parent = edge.to
while(parent = parent.parent) do
puts "#{parent.class}: #{parent.x}, #{parent.y} #{parent.heuristic} #{parent.cost}"
end
puts "Total Cost (movement and heuristic): #{total_cost}\n\n"
else
if @open.index(edge.to)
# reassign
if edge.to.cost > total_cost
edge.to.cost = total_cost
edge.to.parent = current_node
end
else
# add to the open list, set an initial value from here, set a current parent
@open << edge.to
edge.to.cost = total_cost
edge.to.parent = current_node
end
end
end
end
current_node.visited = true
end
end
end
class Node
attr_accessor :visited, :parent, :edges, :x, :y, :cost, :graph
def initialize(x,y)
@x = x
@y = y
@visited = false
@parent = nil
@edges = []
end
def start_node?
false
end
def goal_node?
false
end
def obstacle_node?
false
end
def portal_node?
false
end
def visited?
@visited
end
def heuristic
return @heuristic if @heuristic
delta_x = (graph.goal_node.x - self.x).abs
delta_y = (graph.goal_node.y - self.y).abs
@heuristic = delta_x + delta_y
end
end
class StartNode < Node
def start_node?
true
end
end
class GoalNode < Node
def goal_node?
true
end
def heuristic
0
end
end
class ObstacleNode < Node
def obstacle_node?
true
end
def heuristic
nil
end
end
class PortalNode < Node
def portal_node?
true
end
end
class Edge
attr_accessor :to, :movement
def initialize(to, movement)
@to = to
@movement = movement
end
end
# MAZE LIKE THIS
# row, by row.
# astericks are walls/obstacles
# dashes are open space
# S is the start
# E is the goal
# '*','*','*','*','*','-','-','-'
graph = Graph.new
graph.add_node ObstacleNode.new(0,0)
graph.add_node ObstacleNode.new(1,0)
graph.add_node ObstacleNode.new(2,0)
graph.add_node ObstacleNode.new(3,0)
graph.add_node ObstacleNode.new(4,0)
graph.add_node Node.new(5,0)
graph.add_node Node.new(6,0)
graph.add_node Node.new(7,0)
# '*','-','-','-','*','-','-','-'
graph.add_node ObstacleNode.new(0,1)
graph.add_node Node.new(1,1)
graph.add_node Node.new(2,1)
graph.add_node PortalNode.new(3,1)
graph.add_node ObstacleNode.new(4,1)
graph.add_node Node.new(5,1)
graph.add_node Node.new(6,1)
graph.add_node Node.new(7,1)
# '*','S','*','*','*','-','-','-'
graph.add_node ObstacleNode.new(0,2)
graph.add_node StartNode.new(1,2)
graph.add_node ObstacleNode.new(2,2)
graph.add_node ObstacleNode.new(3,2)
graph.add_node ObstacleNode.new(4,2)
graph.add_node Node.new(5,2)
graph.add_node Node.new(6,2)
graph.add_node Node.new(7,2)
# '*','-','-','-','*','*','*','*'
graph.add_node ObstacleNode.new(0,3)
graph.add_node Node.new(1,3)
graph.add_node Node.new(2,3)
graph.add_node Node.new(3,3)
graph.add_node ObstacleNode.new(4,3)
graph.add_node ObstacleNode.new(5,3)
graph.add_node ObstacleNode.new(6,3)
graph.add_node ObstacleNode.new(7,3)
# '*','-','*','-','-','-','-','*'
graph.add_node ObstacleNode.new(0,4)
graph.add_node Node.new(1,4)
graph.add_node ObstacleNode.new(2,4)
graph.add_node Node.new(3,4)
graph.add_node Node.new(4,4)
graph.add_node Node.new(5,4)
graph.add_node Node.new(6,4)
graph.add_node ObstacleNode.new(7,4)
# '*','-','-','-','*','-','-','*'
graph.add_node ObstacleNode.new(0,5)
graph.add_node Node.new(1,5)
graph.add_node Node.new(2,5)
graph.add_node Node.new(3,5)
graph.add_node ObstacleNode.new(4,5)
graph.add_node Node.new(5,5)
graph.add_node Node.new(6,5)
graph.add_node ObstacleNode.new(7,5)
# '*','*','*','*','*','-','E','*'
graph.add_node ObstacleNode.new(0,6)
graph.add_node ObstacleNode.new(1,6)
graph.add_node ObstacleNode.new(2,6)
graph.add_node ObstacleNode.new(3,6)
graph.add_node ObstacleNode.new(4,6)
graph.add_node Node.new(5,6)
graph.add_node GoalNode.new(6,6)
graph.add_node ObstacleNode.new(7,6)
# '-','-','-','-','*','*','*','*'
graph.add_node Node.new(0,7)
graph.add_node Node.new(1,7)
graph.add_node Node.new(2,7)
graph.add_node Node.new(3,7)
graph.add_node ObstacleNode.new(4,7)
graph.add_node ObstacleNode.new(5,7)
graph.add_node ObstacleNode.new(6,7)
graph.add_node ObstacleNode.new(7,7)
graph.complete!
graph.solve!
# parent = graph.goal_node
# while(parent = parent.parent) do
# puts "#{parent.class}: #{parent.x}, #{parent.y}"
# end