-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_iterativedeepening.jl
62 lines (48 loc) · 1.76 KB
/
search_iterativedeepening.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
"""
depthlimitedsearch(depth, goal, puzzle)
Perform a depth-limited 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 depthlimitedsearch(depth, goal, puzzle)
frontier = [newtree(puzzle)];
while !isempty(frontier)
@timeit to "pop" node = pop!(frontier);
if node.state == goal
return node
end
# Use pathcost as depth
if node.pathcost > depth
continue;
elseif !iscycle(node, 3)
@timeit to "possibleactions" acts = possibleactions(node.state);
@timeit to "expand" res = expand(node.state);
for (a, r) in zip(acts, res)
@timeit to "create node" newnode = addnode(a, node, r);
@timeit to "push" push!(frontier, newnode);
end
end
end
return nothing
end
"""
iterativedfs(goal, puzzle; initdepth=1, limit=100, step=1)
Perform a depth-limited 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 iterativedfs(goal, puzzle; initdepth=1, limit=100, step=1)
reset_timer!(to::TimerOutput)
searchrange = initdepth:step:limit;
for d in searchrange
@timeit to "depth limited search" solnode = depthlimitedsearch(d, goal, puzzle)
if !isnothing(solnode)
printsolve(solnode)
return solnode
end
end
println("No solutions found within depth limit. (limit=$limit)")
return nothing
end