-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_algorithms.py
104 lines (79 loc) · 2.96 KB
/
search_algorithms.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
# Utilities: enum datatype & many classes: Window, FifoQueue,
# LifoQueue, PriorityQueue, Set
from utilities import *
# Search Utilities: Stats class and Node class
from search_utilities import *
def uninformedSearch(problem, frontier, tree: bool):
stats = Stats()
node = Node(problem.initState, None, None, 0)
if problem.goalTest(node.state):
return (node.solution(), stats)
frontier.add(node)
stats.fPlusPlus(node.state) # **
explored = Set()
def alreadyExists(child): return explored.contains(
child.state) or frontier.find(lambda e: e.state == child.state)
while True:
if frontier.isEmpty():
return (None, stats)
node = frontier.pop()
stats.fMinusMinus(node.state) # **
if(not tree):
explored.add(node.state)
stats.ePlusPlus(node.state) # **
for action in problem.actions(node.state):
child = node.child(problem, action)
if problem.goalTest(child.state):
return (child.solution(), stats)
if tree or not alreadyExists(child):
frontier.add(child)
stats.fPlusPlus(child.state) # **
def informedSearch(problem, tree: bool, f):
stats = Stats()
node = Node(problem.initState, None, None, 0)
frontier = PriorityQueue()
frontier.add(node, f(node))
stats.fPlusPlus(node.state) # **
explored = Set()
def alreadyExists(child): return explored.contains(
child.state) or frontier.find(lambda e: e.state == child.state)
while True:
if frontier.isEmpty():
return (None, stats)
node = frontier.pop()
stats.fMinusMinus(node.state) # **
if(not tree):
explored.add(node.state)
if problem.goalTest(node.state):
return (node.solution(), stats)
stats.ePlusPlus(node.state) # **
for action in problem.actions(node.state):
child = node.child(problem, action)
if tree or not alreadyExists(child):
frontier.add(child, f(child))
stats.fPlusPlus(child.state) # **
# BFS Tree Search
def bfsTree(problem):
return uninformedSearch(problem, FifoQueue(), tree=True)
# BFS Graph Search
def bfsGraph(problem):
return uninformedSearch(problem, FifoQueue(), tree=False)
# DFS Graph Search
def dfsGraph(problem):
return uninformedSearch(problem, LifoQueue(), tree=False)
# UCS Tree Search
def ucsTree(problem):
return informedSearch(problem, True, lambda x: x.pathCost)
# UCS Graph Search
def ucsGraph(problem):
return informedSearch(problem, False, lambda x: x.pathCost)
# GBFS Graph Search
def gbfsGraph(problem):
def h(x): return problem.heuristic(x.state)
return informedSearch(problem, False, h)
# A* Graph Search
def aStarGraph(problem):
def h(x): return problem.heuristic(x.state)
def g(x): return x.pathCost
def f(x): return h(x) + g(x)
return informedSearch(problem, False, f)