Compare commits

..

2 Commits

Author SHA1 Message Date
Karma Riuk
e40c41ddfb feat: implemented a* algorithm 2023-08-15 18:43:18 +02:00
Karma Riuk
966387ffcd refactor: moved out the sorted_stack to utils 2023-08-15 18:42:58 +02:00
4 changed files with 97 additions and 22 deletions

66
solver/a-star.go Normal file
View File

@ -0,0 +1,66 @@
package solver
import (
"maze-solver/maze"
"maze-solver/utils"
"sort"
)
type AStarSolver struct {
dist_from_start map[*maze.Node]int
dist_from_end map[*maze.Node]int
parent map[*maze.Node]*maze.Node
stack sorted_stack
}
func (s *AStarSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("A* algorithm", 2)()
s.dist_from_start = make(map[*maze.Node]int, len(m.Nodes))
s.dist_from_end = make(map[*maze.Node]int, len(m.Nodes))
s.parent = make(map[*maze.Node]*maze.Node, len(m.Nodes))
current, end := m.Nodes[0], m.Nodes[len(m.Nodes)-1]
for _, node := range m.Nodes {
s.dist_from_start[node] = 0
s.dist_from_end[node] = int(node.Coords.Distance(end.Coords))
}
for current != end {
current.Visited = true
for _, child := range []*maze.Node{current.Left, current.Right, current.Up, current.Down} {
if child != nil {
dist := s.dist_from_start[current] + int(current.Coords.Distance(child.Coords))
if !child.Visited {
s.parent[child] = current
s.dist_from_start[child] = dist
s.stack.insert(child, &s.dist_from_end)
} else if s.dist_from_start[child] > dist {
s.parent[child] = current
s.dist_from_start[child] = dist
sort.Slice(s.stack, func(i, j int) bool {
return s.dist_from_end[s.stack[i]] < s.dist_from_end[s.stack[j]]
})
}
}
}
current = s.stack.pop()
}
solution := make([]*maze.Node, 0, len(m.Nodes))
for current != m.Nodes[0] {
solution = append(solution, current)
current = s.parent[current]
}
solution = append(solution, m.Nodes[0])
for i, j := 0, len(solution)-1; i < j; i, j = i+1, j-1 {
solution[i], solution[j] = solution[j], solution[i]
}
return &maze.SolvedMaze{
Maze: m,
Solution: solution,
}
}

View File

@ -3,7 +3,6 @@ package solver
import ( import (
"maze-solver/maze" "maze-solver/maze"
"maze-solver/utils" "maze-solver/utils"
"slices"
"sort" "sort"
) )
@ -13,8 +12,6 @@ type DijkstraSolver struct {
stack sorted_stack stack sorted_stack
} }
type sorted_stack []*maze.Node
func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze { func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("Dijkstra algorithm", 2)() defer utils.Timer("Dijkstra algorithm", 2)()
s.dist_from_start = make(map[*maze.Node]int, len(m.Nodes)) s.dist_from_start = make(map[*maze.Node]int, len(m.Nodes))
@ -64,22 +61,3 @@ func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
Solution: solution, Solution: solution,
} }
} }
func (s *sorted_stack) insert(node *maze.Node, dists *map[*maze.Node]int) {
var dummy *maze.Node
*s = append(*s, dummy) // extend the slice
i, _ := slices.BinarySearchFunc(*s, node, func(e, t *maze.Node) int {
return (*dists)[t] - (*dists)[e]
})
copy((*s)[i+1:], (*s)[i:]) // make room
(*s)[i] = node
}
func (s *sorted_stack) pop() *maze.Node {
last_i := len(*s) - 1
ret := (*s)[last_i]
*s = (*s)[:last_i]
return ret
}

View File

@ -17,12 +17,14 @@ const (
_DFS = "dfs" _DFS = "dfs"
_BFS = "bfs" _BFS = "bfs"
_Dijkstra = "dijkstra" _Dijkstra = "dijkstra"
_AStar = "a-star"
) )
var TYPES = []string{ var TYPES = []string{
_DFS, _DFS,
_BFS, _BFS,
_Dijkstra, _Dijkstra,
_AStar,
} }
func (f *SolverFactory) Get() Solver { func (f *SolverFactory) Get() Solver {
@ -31,6 +33,8 @@ func (f *SolverFactory) Get() Solver {
return &DFSSolver{} return &DFSSolver{}
case _BFS: case _BFS:
return &BFSSolver{} return &BFSSolver{}
case _AStar:
return &AStarSolver{}
case _Dijkstra: case _Dijkstra:
return &DijkstraSolver{} return &DijkstraSolver{}
} }

27
solver/utils.go Normal file
View File

@ -0,0 +1,27 @@
package solver
import (
"maze-solver/maze"
"slices"
)
type sorted_stack []*maze.Node
func (s *sorted_stack) insert(node *maze.Node, weights *map[*maze.Node]int) {
var dummy *maze.Node
*s = append(*s, dummy) // extend the slice
i, _ := slices.BinarySearchFunc(*s, node, func(e, t *maze.Node) int {
return (*weights)[t] - (*weights)[e]
})
copy((*s)[i+1:], (*s)[i:]) // make room
(*s)[i] = node
}
func (s *sorted_stack) pop() *maze.Node {
last_i := len(*s) - 1
ret := (*s)[last_i]
*s = (*s)[:last_i]
return ret
}