-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_astar.jl
182 lines (142 loc) · 4.22 KB
/
search_astar.jl
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
using DataStructures;
"""
heurmanhattan(goal, state)
Compute the manhattan distance of nonzero tiles in `state`
with respect to their locations in `goal`.
Return: Heuristic score
"""
function heurmanhattan(goal, state)
m,n = size(state);
h = 0;
for row in 1:m
for col in 1:n
if goal[row,col] == 0
continue;
end
cord = findfirst(isequal(goal[row,col]), state);
hm = abs(cord[1] - row);
hn = abs(cord[2] - col);
h += hm + hn;
end
end
return h
end
"""
conflictresolution(rowcol, slice, state)
Search for linear conflict elements within a `slice`
representing the intersection of a row/column of
the goal and puzzle `state`.
rowcol: 2 if searching a row. 1 if searching a column.
Represents the index element into the `state`
Return: 2 times the conflicts found.
"""
function conflictresolution(rowcol, slice, state)
if 0 in slice
setdiff!(slice, [0]);
end
# Collect row/col index of elements
if length(slice) > 1
sliceord = [];
for el in slice
idx = findfirst(isequal(el), state)[rowcol]
push!(sliceord, idx)
end
# Increase score if out of order
slicescore = 0
for a in 1:(length(sliceord)-1)
for b in 2:length(sliceord)
if sliceord[b] < sliceord[a]
slicescore += 2;
end
end
end
return slicescore
end
return 0
end
"""
heurmanhattanconflict(goal, state)
Supplement the manhattan distance calculation by accounting
for linear conflict.
For every pair of tiles in reverse order in their correct
row or column in `state`, increase the score by 2.
Return: Heuristic score
"""
function heurmanhattanconflict(goal, state)
h = heurmanhattan(goal, state);
sidelen = size(goal)[1];
for i in 1:sidelen
# Check intersection of goal,puzzle
# row for shared elements
inrow = intersect(goal[i,:], state[i,:]);
h += conflictresolution(2, inrow, state)
incol = intersect(goal[:,i], state[:,i]);
h += conflictresolution(1, inrow, state);
end
return h
end
"""
heurmisplaced(goal, state)
Calculate the number of misplaced tiles in `state`
with respect to their location in `goal`.
Return: Heuristic score
"""
function heurmisplaced(goal, state)
m,n = size(state);
h = 0
for j in 1:m
for i in 1:n
if goal[j,i] == 0
continue;
elseif goal[j,i] == state[j,i]
continue;
else
h += 1;
end
end
end
return h
end
"""
astarsearch(goal, heur, puzzle)
Perform an a* search of a tree structure initiated with
`puzzle`. Child states are scored according to heuristic
`heur` and stored in a priority queue until either the
`goal` state is found, or no solution is possible.
Return: TreeNode containing the goal state or nothing.
"""
function astarsearch(goal, heur, puzzle)
"""Perform A* search using an input heuristic function.
Return solution or nothing.
"""
# Reset timer
reset_timer!(to::TimerOutput)
node = newtree(puzzle);
frontier = PriorityQueue{TreeNode, Integer}()
frontier[node] = 0;
basecamp = [];
while !isempty(frontier)
@timeit to "dequeue" node = dequeue!(frontier);
@timeit to "push" push!(basecamp, node.state);
@timeit to "possibleactions" acts = possibleactions(node.state);
@timeit to "expand" states = expand(node.state);
for (a, s) in zip(acts, states)
if in(basecamp, s)
continue;
end
@timeit to "create node" cnode = addnode(a, node, s);
@timeit to "heuristic score" score = node.pathcost + heur(goal, cnode.state);
if s == goal
printsolve(cnode)
return cnode
end
if !(cnode in keys(frontier))
frontier[cnode] = score;
elseif frontier[cnode] > score
frontier[cnode] = score;
end
end
end
println("No solutions found.")
return nothing
end