-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_breadthfirst.jl
50 lines (39 loc) · 1.33 KB
/
search_breadthfirst.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
"""
breadthfirstsearch(goal, puzzle)
Perform a breadth-first search of a tree structure initiated
with `puzzle`. Search proceeds until either the `goal`
state is found, or no solution is possible.
Return: TreeNode containing the goal state or nothing.
"""
function breadthfirstsearch(goal, puzzle)
"""Perform breadth first search.
Return solution or nothing.
"""
# Reset timer
reset_timer!(to::TimerOutput)
node = newtree(puzzle);
if node.state == goal
return node
end
frontier = [node];
reached = [node.state];
while !isempty(frontier)
@timeit to "pop" node = popfirst!(frontier);
@timeit to "expand" states = expand(node.state);
@timeit to "possibleactions" acts = possibleactions(node.state);
for (cstate, caction) in zip(states, acts)
if cstate == goal
solvnode = addnode(caction, node, cstate);
printsolve(solvnode)
return solvnode
end
if !in(cstate, reached)
@timeit to "push" push!(reached, cstate);
@timeit to "create node" cnode = addnode(caction, node, cstate);
@timeit to "push" push!(frontier, cnode)
end
end
end
println("No solutions found.")
return nothing
end