-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_search.py
155 lines (122 loc) · 4.57 KB
/
tree_search.py
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
from abc import ABC, abstractmethod
class SearchDomain(ABC):
# Construtor
@abstractmethod
def __init__(self):
pass
# Possible actions in a state
@abstractmethod
def actions(self, state):
pass
# Result of an action over a state, i.e., the new state
@abstractmethod
def result(self, state, action):
pass
# Cost of an action
@abstractmethod
def cost(self, state, action):
pass
# Estimated cost from a state to another
@abstractmethod
def heuristic(self, state, goal):
pass
# Test if the given "goal" is satisfied in "state"
@abstractmethod
def satisfies(self, state, goal):
pass
# Needed-to-be-solved problem in a certain domain
class SearchProblem:
def __init__(self, domain, initial, goal):
self.domain = domain
self.initial = initial
self.goal = goal
def goal_test(self, state):
return self.domain.satisfies(state, self.goal)
# Search tree nodes
class SearchNode:
def __init__(self, state, parent, depth, cost, heuristic):
self.state = state
self.parent = parent
self.depth = depth
self.cost = cost
self.heuristic = heuristic
def in_parent(self, newstate):
if self.parent is None:
return False
if self.parent.state == newstate:
return True
return self.parent.in_parent(newstate)
def __str__(self):
return "no(" + str(self.state) + "," + str(self.parent) + ")"
def __repr__(self):
return str(self)
# Arvores de pesquisa
class SearchTree:
# construtor
def __init__(self, problem, strategy='breadth'):
self.problem = problem
root = SearchNode(problem.initial, None, 0, 0, problem.domain.heuristic(problem.initial, problem.goal))
self.open_nodes = [root]
self.strategy = strategy
self.solution = None
self.non_terminals = 0
self.highest_cost_nodes = [root]
self.average_depth = root.depth
@property
def terminals(self):
return len(self.open_nodes) + 1
@property
def length(self):
return self.solution.depth
@property
def avg_branching(self):
return (self.non_terminals + self.terminals - 1) / self.non_terminals
@property
def cost(self):
return self.solution.cost
# Get the path from the root to a node
def get_path(self, node):
if node.parent is None:
return [node.state]
path = self.get_path(node.parent)
path += [node.state]
return path
# Find the solution
def search(self, limit=None):
while self.open_nodes:
node = self.open_nodes.pop(0)
if self.problem.goal_test(node.state):
self.solution = node
self.average_depth /= self.terminals + self.non_terminals
return self.get_path(node)
self.non_terminals += 1
lnewnodes = []
for a in self.problem.domain.actions(node.state):
newstate = self.problem.domain.result(node.state, a)
if not node.in_parent(newstate) and (limit is None or node.depth < limit):
newnode = SearchNode(newstate, node, node.depth + 1,
node.cost + self.problem.domain.cost(node.state, a),
self.problem.domain.heuristic(newstate, self.problem.goal))
self.average_depth += newnode.depth
if newnode.cost > self.highest_cost_nodes[0].cost:
self.highest_cost_nodes = [newnode]
elif newnode.cost == self.highest_cost_nodes[0].cost:
self.highest_cost_nodes.append(newnode)
lnewnodes.append(newnode)
self.add_to_open(lnewnodes)
return None
# Add new nodes to 'open_nodes' list depending on the chosen strategy
def add_to_open(self, lnewnodes):
if self.strategy == 'breadth':
self.open_nodes.extend(lnewnodes)
elif self.strategy == 'depth':
self.open_nodes[:0] = lnewnodes
elif self.strategy == 'uniform':
self.open_nodes.extend(lnewnodes)
self.open_nodes.sort(key=lambda n: n.cost)
elif self.strategy == 'greedy':
self.open_nodes.extend(lnewnodes)
self.open_nodes.sort(key=lambda n: n.heuristic)
elif self.strategy == 'a*':
self.open_nodes.extend(lnewnodes)
self.open_nodes.sort(key=lambda n: n.cost + n.heuristic)