-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (77 loc) · 1.93 KB
/
main.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
package main
import (
"fmt"
"github.com/brice-allen/csci4202/astar"
"github.com/brice-allen/csci4202/bfs"
"github.com/brice-allen/csci4202/search"
"github.com/brice-allen/csci4202/utils"
"os"
"strings"
"time"
)
func main() {
utils.PrintMemUsage()
args := os.Args
if len(args) != 2 {
fmt.Println("Usage: hw01 bfs or hw01 astar!")
return
}
var selection int
if strings.Compare(args[1], "bfs") == 0 {
fmt.Println("bfs")
selection = 1
} else if strings.Compare(args[1], "astar") == 0 {
fmt.Println("astar")
selection = 2
} else {
fmt.Println("Usage: hw01 bfs or hw01 astar!")
return
}
fmt.Println("Starting...", time.Now())
startTime := time.Now()
goal := make([][]int, 9)
goal = utils.FillGoal()
board := utils.InputParser("input.txt")
if board == nil {
return
}
utils.StatePrinter(board)
startState := search.NewState(board, goal)
var solution *search.State
var frontier, expanded int
if selection == 1 {
solution, frontier, expanded = bfs.Solve(startState, goal)
} else if selection == 2 {
solution, frontier, expanded = astar.Solve(startState, goal)
} else {
fmt.Println("Usage: hw01 bfs or hw01 astar!")
return
}
if solution == nil {
fmt.Println("Number of nodes entering:", frontier)
fmt.Println("Number of nodes from frontier:", expanded)
fmt.Println("End...", time.Now())
return
}
s := solution
steps := make([]*search.State, solution.NumMoves)
for i := 0; i < solution.NumMoves; i++ {
steps[i] = s
s = s.Parent
}
for i := len(steps)/2 - 1; i >= 0; i-- {
opp := len(steps) - 1 - i
steps[i], steps[opp] = steps[opp], steps[i]
}
for _, next := range steps {
utils.StatePrinter(next.Board)
}
fmt.Println("Solution cost:", solution.NumMoves)
fmt.Println("Number of nodes:", frontier)
fmt.Println("Number of nodes from frontier:", expanded)
fmt.Println("End...", time.Now())
elapsed := time.Since(startTime)
fmt.Println("Process took:", elapsed)
utils.PrintMemUsage()
return
}