-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchtree.jl
110 lines (86 loc) · 1.86 KB
/
searchtree.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
"""
TreeNode(action, parent, pathcost, state)
Node structure for search tree.
"""
struct TreeNode
action::Union{Tuple, Nothing}
parent::Union{TreeNode, Nothing}
pathcost::Integer
state::Matrix
end
import Base.==
"""
==(x, y)
Define equality between Treenodes as
having equal states.
"""
function ==(x::TreeNode,y::TreeNode)
x.state==y.state
end
"""
addnode(action, parent, state)
Create a new TreeNode with pathcost of
the parent plus 1.
Return: TreeNode
"""
function addnode(action, parent, state)
cost = parent.pathcost + 1;
newnode = TreeNode(
action,
parent,
cost,
state
);
return newnode
end
"""
newtree(state)
Create the initial TreeNode for a new search tree.
The node's action and parent are set to `nothing`,
and the pathcost is set to zero.
Return: TreeNode
"""
function newtree(state)
newnode = TreeNode(
nothing,
nothing,
0,
state
);
return newnode
end
"""
iscycle(node, cynum)
Determine whether the search tree has become a cycle
by looking at the `cynum` parents of `node` for an
equal state.
Returns: Boolean
"""
function iscycle(node, cynum)
count = 0;
prnt = node;
while (count < cynum) && !isnothing(prnt.parent)
count+=1;
prnt = prnt.parent;
if node.state == prnt.state
return true
end
end
return false
end
"""
printsolve(solvenode)
Formatted print of a TreeNode and all parents
to the origin.
"""
function printsolve(solvenode)
while !isnothing(solvenode)
println("Step $(solvenode.pathcost):")
println("\t$(solvenode.state[1,:])")
println("\t$(solvenode.state[2,:])")
println("\t$(solvenode.state[3,:])")
println("\tAction: $(solvenode.action)");
solvenode = solvenode.parent;
end
print(to)
end