-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
57 lines (47 loc) · 1.32 KB
/
search.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
package search
import (
"fmt"
"strings"
"github.com/porgull/go-search/pkg/environments"
)
// Context passes search args to search algorithms
type Context struct {
CustomSearchParams CustomSearchParams
}
// CustomSearchParams contains any custom values
// a search algorithm would need, e.g. max depth
// for depth limited search
type CustomSearchParams map[string]string
// Result contains statistics about a single run
// of the search algorithm on an environment
type Result struct {
Node environments.Node
Environment environments.Environment
Iterations int
CustomResultStats map[string]string
}
// Print prints the results to stdout
func (r Result) Print() {
fmt.Printf("Found node %s in %d iterations.\n", r.Node.Name(), r.Iterations)
steps := r.Node.Steps()
fmt.Printf("Steps (%d): %s\n", len(steps), strings.Join(steps, ", "))
fmt.Println("Total cost of solution:", r.TotalCost())
r.Environment.VisualizeSolution(r.Node)
if len(r.CustomResultStats) > 0 {
fmt.Println("Custom result data for this run:")
for key, val := range r.CustomResultStats {
fmt.Printf("%s: %s\n", key, val)
}
}
}
// TotalCost returns the total cost of the
// steps taken
func (r Result) TotalCost() int {
total := 0
parent := r.Node
for parent != nil {
total += parent.Cost()
parent = parent.Parent()
}
return total
}