-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
279 lines (234 loc) · 5.82 KB
/
agent.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"math"
"time"
)
// type State interface {
// equals(State) bool
// getSuccessors() []State
// heuristic() float64
// print()
// isFinal() bool
// }
type Node struct {
state Puzzle
g int8 // cost
h float32 // heuristic
prev *Node // the predecessor
}
func (n Node) getF() float32 {
return float32(n.g) + n.h
}
type Status string
const (
Solved = "solved"
Timeout = "timeout"
Unsolvable = "unsolvable"
)
/**
* Return a slice of all nodes with successor states to the current
*
* Note: All info other than the state is invalid as these states
* are likely repeats and have already been calculated
**/
func (n Node) getSuccessorStates(ignore_prev_moves bool) []Puzzle {
var succStates = []Puzzle{}
succStates = append(succStates, n.state.getSuccessors(ignore_prev_moves)...)
return succStates
}
type Heuristic func(p Puzzle) float32
var heuristics = [...]Heuristic{h1, h2, h3, h4}
func h1(p Puzzle) float32 {
var cost float32 = 0
for i := 0; i < p.size(); i++ {
if e := p.getN(i); (e != 0) && (e != i) {
cost++
}
}
return cost
}
func h2(p Puzzle) float32 {
var cost float32 = 0
for r := 0; r < p.len(); r++ {
for c := 0; c < p.len(); c++ {
goalPos := p.getGoalPos(p.get(RowCol{row: r, col: c}))
cost += float32(math.Abs(float64(r - goalPos.row)))
cost += float32(math.Abs(float64(c - goalPos.col)))
}
}
return cost
}
func h3(p Puzzle) float32 {
var cost float32 = 0
var arr []int = make([]int, p.size())
var idx int = 0
for _, row := range p.arr {
for _, e := range row {
arr[idx] = e
idx++
}
}
for i := len(arr) - 1; i > 0; i-- {
max := 0
for j := 0; j <= i; j++ {
if arr[max] < arr[j] {
max = j
}
}
if i != max {
temp := arr[i]
arr[i] = arr[max]
arr[max] = temp
cost++
}
}
return cost
}
func h4(p Puzzle) float32 {
var cost float32 = 0
for r := 0; r < p.len(); r++ {
for c := 0; c < p.len(); c++ {
goalPos := p.getGoalPos(p.get(RowCol{row: r, col: c}))
cost += euclidean_dist(goalPos.row-r, goalPos.col-c)
}
}
return float32(cost)
}
func indexOf(list []*Node, state Puzzle) int {
for i, e := range list {
if e.state.equals(state) {
return i
}
}
return -1
}
func minNodeIdx(list []*Node) int {
var minF float32 = list[0].getF()
var idx int = 0
for i, e := range list {
test := e.getF()
if test < minF {
minF = test
idx = i
}
}
return idx
}
func popLowest(list []*Node) (*Node, []*Node) {
var idx int = minNodeIdx(list)
var minNode *Node = list[idx]
copy(list[idx:], list[idx+1:])
list[len(list)-1] = nil
list = list[:len(list)-1]
return minNode, list
}
func (n Node) isFinal() bool {
return n.state.isSolved()
}
/**
* return solution path, open list size, closed list size, avg branching factor
* calc runtime outside of func
**/
func a_star(initial Puzzle, h Heuristic, time_limit int, ignore_prev_moves bool) (status Status, path []Puzzle, openSize int, closedSize int) {
start := time.Now()
var openList = []*Node{{
state: initial,
g: 0,
h: h(initial),
}} // frontier starts with the initial state
var closedList = []*Node{} // explored is empty
var cur *Node
for len(openList) > 0 { // while there are nodes to explore
// timeout condition
if time_limit > 0 { // 0 or negative time limit is ignored
if time.Since(start).Seconds() >= float64(time_limit) {
return Timeout, make([]Puzzle, 0), len(openList), len(closedList)
}
}
cur, openList = popLowest(openList)
if cur.isFinal() { // found solution
var steps int = 0
var path []Puzzle = make([]Puzzle, 0)
for node := cur; node != nil; node = node.prev {
path = append(path, node.state.copy())
steps++
}
return Solved, path, len(openList), len(closedList)
} else { // still exploring
closedList = append(closedList, cur)
for _, state := range cur.getSuccessorStates(ignore_prev_moves) {
nIdx := indexOf(openList, state)
if nIdx != -1 { // state is in open list
if cur.g+1 < openList[nIdx].g { // update with better path
openList[nIdx].g = cur.g + 1
openList[nIdx].prev = cur
}
} else if indexOf(closedList, state) != -1 { // state is in closed list
continue
} else { // state has not been seen yet
openList = append(openList, &Node{
state: state,
g: cur.g + 1,
h: h(state),
prev: cur,
})
}
}
}
}
return Unsolvable, make([]Puzzle, 0), len(openList), len(closedList)
}
func solve(initial Puzzle, heuristic_num int, time_limit int, ignore_prev_moves bool) {
start := time.Now()
status, path, openLen, closedLen := a_star(initial, heuristics[heuristic_num-1], time_limit, ignore_prev_moves)
duration := time.Since(start)
if config.Metrics.Status {
if status == Solved {
logger.Printf("Status: %v, Valid: %v\n", status, verifySolution(path))
} else {
logger.Printf("Status: %v\n", status)
}
}
if config.Metrics.Execution_time {
logger.Printf("Execution time: %.3fs\n", duration.Seconds())
}
if status == Solved && config.Metrics.Solution_length {
logger.Printf("Solution Length: %v\n", len(path)-1)
}
if config.Metrics.Nodes_explored {
logger.Printf("Nodes Explored: %v\n", openLen+closedLen)
}
if config.Metrics.Frontier_size {
logger.Printf("Frontier Size: %v\n", openLen)
}
if config.Metrics.Nodes_evaluated {
logger.Printf("Nodes Evaluated: %v\n", closedLen)
}
if status == Solved && config.Metrics.Solution_path {
logger.Printf("Solution Path:\n")
for i := len(path) - 1; i >= 0; i-- {
logger.Print(path[i].toStr())
logger.Println()
}
}
}
func verifySolution(path []Puzzle) bool {
if len(path) == 0 {
return false
}
if !path[0].isSolved() {
return false
}
var cur, prev Puzzle
for i := range path {
if i >= len(path)-1 {
break
}
cur = path[i]
prev = path[i+1]
if !cur.isSuccessorTo(prev) {
return false
}
}
return true
}