-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlgorithms_uninformed.py
More file actions
194 lines (168 loc) · 6.49 KB
/
Algorithms_uninformed.py
File metadata and controls
194 lines (168 loc) · 6.49 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
from collections import deque
from ProblemFormulation import *
class Node:
'''Node data structure for search space bookkeeping.'''
def __init__(self, state, parent, action, path_cost):
'''Constructor for the node state with the required parameters.'''
self.state = state
self.parent = parent
self.action = action
self.path_cost = path_cost
@classmethod
def root(cls, init_state):
'''Factory method to create the root node.'''
return cls(init_state, None, None, 0)
@classmethod
def child(cls, problem, parent, action):
'''Factory method to create a child node.'''
return cls(
problem.result(parent.state, action),
parent,
action,
parent.path_cost + problem.step_cost(parent.state, action))
def solution(node):
'''A method to extract the sequence of actions representing the solution from the goal node.'''
actions = []
cost = node.path_cost
while node.parent is not None:
actions.append(node.action)
node = node.parent
actions.reverse()
return actions, cost
def bfs_tree(problem, verbose=False):
'''Breadth-first tree search implementation.'''
if problem.goal_test(problem.init_state): return solution(Node.root(problem.init_state))
frontier = deque([Node.root(problem.init_state)])
# if verbose: visualizer = Visualizer(problem)
while frontier:
# if verbose: visualizer.visualize(frontier)
node = frontier.pop()
for action in problem.actions(node.state):
child = Node.child(problem, node, action)
if problem.goal_test(child.state):
return solution(child)
frontier.appendleft(child)
def bfs_graph(problem, verbose=False):
'''Breadth-first graph search implementation.'''
if problem.goal_test(problem.init_state): return solution(Node.root(problem.init_state))
frontier = deque([Node.root(problem.init_state)])
explored = {problem.init_state}
while frontier:
# if verbose: visualizer.visualize(frontier)
node = frontier.pop()
for action in problem.actions(node.state):
child = Node.child(problem, node, action)
if child.state not in explored:
if problem.goal_test(child.state):
return solution(child)
frontier.appendleft(child)
explored.add(child.state)
from collections import deque
import copy
def dls_tree(problem, limit,verbose=False):
'''Breadth-first tree search implementation.'''
if problem.goal_test(problem.init_state): return solution(Node.root(problem.init_state))
frontier = deque([Node.root(problem.init_state)])
# if verbose: visualizer = Visualizer(problem)
while frontier:
counter=0
cond=True
# if verbose: visualizer.visualize(frontier)
node = frontier.pop()
temp= node.parent#copy.deepcopy(node)
while cond:
if(temp!=None):
temp=temp.parent
counter+=1
else:cond=False
if counter==limit: # same parent
continue
for action in problem.actions(node.state):
child = Node.child(problem, node, action)
if problem.goal_test(child.state):
return solution(child)
frontier.append(child)
return None
def dls_graph(problem, limit, verbose=False):
counter=0
cond=True
'''Depth-First graph search implementation.'''
if problem.goal_test(problem.init_state): return solution(Node.root(problem.init_state))
frontier = deque([Node.root(problem.init_state)])
explored = {problem.init_state}
# if verbose: visualizer = Visualizer(problem)
while frontier:
# if verbose: visualizer.visualize(frontier)
node = frontier.pop()
temp= node.parent#copy.deepcopy(node)
while cond:
if(temp!=None):
temp=temp.parent
counter+=1
else:cond=False
if counter==limit: # same parent
continue
for action in problem.actions(node.state):
child = Node.child(problem, node, action)
if child.state not in explored:
if problem.goal_test(child.state):
return solution(child)
frontier.append(child)
explored.add(child.state)
counter+=1
from collections import deque
def dfs_tree(problem, verbose=False):
'''Depth-First tree search implementation.'''
if problem.goal_test(problem.init_state): return solution(Node.root(problem.init_state))
frontier = deque([Node.root(problem.init_state)])
# if verbose: visualizer = Visualizer(problem)
while frontier:
# if verbose: visualizer.visualize(frontier)
node = frontier.pop()
for action in problem.actions(node.state):
child = Node.child(problem, node, action)
if problem.goal_test(child.state):
return solution(child)
frontier.append(child)
def dfs_graph(problem, verbose=False):
'''Depth-First graph search implementation.'''
if problem.goal_test(problem.init_state): return solution(Node.root(problem.init_state))
frontier = deque([Node.root(problem.init_state)])
explored = {problem.init_state}
# if verbose: visualizer = Visualizer(problem)
while frontier:
# if verbose: visualizer.visualize(frontier)
node = frontier.pop()
for action in problem.actions(node.state):
child = Node.child(problem, node, action)
if child.state not in explored:
if problem.goal_test(child.state):
return solution(child)
frontier.append(child)
explored.add(child.state)
from collections import deque
import copy
def ids_tree(problem, limit, verbose=False):
cond = True
new_limit = limit
ret = None
while cond:
if (ret == None):
ret = dls_tree(problem, new_limit, verbose=True)
new_limit += 1
print("new limit is", new_limit)
else:
print("Path=", ret[0], "cost=", ret[1])
cond = False
def ids_graph(problem, limit, verbose=False):
cond = True
new_limit = limit
ret = None
while cond:
if (ret == None):
ret = dls_graph(problem, new_limit, verbose=True)
new_limit += 1
print("new limit is", new_limit)
else:
print("Path=", ret[0], "cost=", ret[1])
cond = False