-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
259 lines (205 loc) · 5.03 KB
/
grid.go
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
package main
import (
"container/heap"
"math"
"pathfinding/pair"
"time"
)
const BASE_WEIGHT = 1
type Status string
const (
STATUS_IDLE Status = "STATUS_IDLE"
STATUS_PATHING Status = "STATUS_PATHING"
STATUS_END_SUCCESS Status = "STATUS_END_SUCCESS"
STATUS_END_NOPATH Status = "STATUS_END_NOPATH"
)
type Node struct {
Coord pair.Pair
IsWall bool
Prev *Node
Cost float64 // Cost for Dijkstra, Fcost for A*
Gcost float64 // For A*
Visited bool
Added bool
IsPath bool
index int
}
type Grid struct {
Cells [][]Node
Start *Node
End *Node
Status Status
PathLength int
Iterations int
StartTime time.Time
EndTime time.Time
}
func NewGrid(size int, start, end pair.Pair) Grid {
cells := make([][]Node, size)
for i := 0; i < size; i++ {
cells[i] = make([]Node, size)
for j := 0; j < size; j++ {
cells[i][j] = Node{
Coord: pair.New(i, j),
Cost: math.MaxInt}
}
}
return Grid{Cells: cells, Start: &cells[start.I][start.J], End: &cells[end.I][end.J], Status: STATUS_IDLE}
}
func (g *Grid) Restart(keepLayout bool) {
cells := make([][]Node, len(g.Cells))
for i := 0; i < len(g.Cells); i++ {
cells[i] = make([]Node, len(g.Cells))
for j := 0; j < len(g.Cells); j++ {
cells[i][j] = Node{Coord: pair.New(i, j), IsWall: keepLayout && g.Cells[i][j].IsWall}
}
}
g.Start = &cells[g.Start.Coord.I][g.Start.Coord.J]
g.End = &cells[g.End.Coord.I][g.End.Coord.J]
g.PathLength = 0
g.Iterations = 0
g.Status = STATUS_IDLE
g.Cells = cells
g.StartTime = time.Now()
g.EndTime = time.Now()
}
func (grid *Grid) DoDijkstra() {
grid.StartTime = time.Now()
grid.Status = STATUS_PATHING
for i, row := range grid.Cells {
for j := range row {
grid.Cells[i][j].Cost = math.MaxInt
}
}
grid.Start.Cost = 0
pq := PriorityQueue{grid.Start}
heap.Init(&pq)
for pq.Len() > 0 {
select {
case <-stopSignal:
grid.EndTime = time.Now()
grid.Status = STATUS_IDLE
return
default:
}
grid.Iterations++
time.Sleep(time.Millisecond * time.Duration(iterationCooldownMS))
u := heap.Pop(&pq).(*Node)
if u.Visited {
continue
}
if u == grid.End {
break
}
u.Visited = true
directions := []pair.Pair{pair.Up(), pair.Down(), pair.Left(), pair.Right()}
for _, dir := range directions {
neighborPos := u.Coord.Add(dir)
if neighborPos.InBounds(0, 0, len(grid.Cells), len(grid.Cells[0])) {
neighbor := &grid.Cells[neighborPos.I][neighborPos.J]
if neighbor.IsWall || neighbor.Visited {
continue
}
alt := u.Cost + 1
if !neighbor.IsWall && alt < neighbor.Cost {
neighbor.Cost = alt
neighbor.Prev = u
if !neighbor.Added {
neighbor.Added = true
heap.Push(&pq, neighbor)
}
}
}
}
}
grid.EndTime = time.Now()
grid.Status = STATUS_END_SUCCESS
success := grid.constructPath()
if !success {
grid.Status = STATUS_END_NOPATH
}
}
func (grid *Grid) DoAStar() {
grid.StartTime = time.Now()
grid.Status = STATUS_PATHING
for i := range grid.Cells {
for j := range grid.Cells[i] {
grid.Cells[i][j].Gcost = math.MaxFloat64
grid.Cells[i][j].Cost = math.MaxFloat64
}
}
grid.Start.Gcost = 0
grid.Start.Cost = grid.h(*grid.Start)
pq := PriorityQueue{grid.Start}
heap.Init(&pq)
for pq.Len() > 0 {
select {
case <-stopSignal:
grid.EndTime = time.Now()
grid.Status = STATUS_IDLE
return
default:
}
grid.Iterations++
time.Sleep(time.Duration(iterationCooldownMS) * time.Millisecond)
current := heap.Pop(&pq).(*Node)
current.Visited = true
if current == grid.End {
break
}
directions := []pair.Pair{pair.Up(), pair.Down(), pair.Left(), pair.Right()}
for _, dir := range directions {
neighborPos := current.Coord.Add(dir)
if neighborPos.InBounds(0, 0, len(grid.Cells), len(grid.Cells[0])) {
neighbor := &grid.Cells[neighborPos.I][neighborPos.J]
if neighbor.IsWall {
continue
}
gcost := current.Gcost + g(*current, *neighbor)
if gcost < neighbor.Gcost {
neighbor.Prev = current
neighbor.Gcost = gcost
neighbor.Cost = gcost + grid.h(*neighbor)
if neighbor.Added {
heap.Fix(&pq, neighbor.index)
} else {
neighbor.Added = true
heap.Push(&pq, neighbor)
}
}
}
}
}
grid.EndTime = time.Now()
grid.Status = STATUS_END_SUCCESS
success := grid.constructPath()
if !success {
grid.Status = STATUS_END_NOPATH
}
}
func g(a, b Node) float64 {
return BASE_WEIGHT // This could be changed to use diagonals (e.g 1 for horizontal & vertical, 1.4 for diagonals)
}
func (grid Grid) h(a Node) float64 {
dy := math.Abs(float64(a.Coord.I - grid.End.Coord.I))
dx := math.Abs(float64(a.Coord.J - grid.End.Coord.J))
heuristic := BASE_WEIGHT * (dx + dy)
tb := float64(1)/float64(len(grid.Cells)*len(grid.Cells[0])) + 1 // Tie breaker
return heuristic * tb
}
func (grid *Grid) constructPath() bool {
node := grid.End
if node.Prev == nil {
grid.PathLength = -1
return false
} else {
for node != nil {
if node != grid.Start && node != grid.End {
grid.PathLength++
}
node.IsPath = true
node = node.Prev
}
}
return true
}