-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
executable file
·165 lines (147 loc) · 4.64 KB
/
statistics.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
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python2.3
from solvers import BreadthSolver, DepthSolver, HillClimbingSolver, PruningBreadthSolver, AStarSolver
import slot
import slot_dumb
import queens
import queens_dumb
import timing
class Statistics:
def __init__(self):
self.algorithm = None
self.time = None
self.solution = None
self.analyzed_nodes = None
self.inmemory_nodes = None
def run(self, function):
timing.start()
self.solution = function()
timing.finish()
self.time = timing.milli()
def print_stat(self):
print "Algorithm: %s" % self.algorithm
print "Time: %s s" % (self.time/1000.0)
if self.solution:
print "Solution found"
else:
print "Solution not found"
print "Nodes analyzed: %s" % self.analyzed_nodes
print "Nodes in memory: %s" % self.inmemory_nodes
print
def breadth(self, initial_state):
self.algorithm = "Breadth First"
solver = BreadthSolver(initial_state)
self.run(solver.solve)
self.analyzed_nodes = len(solver.CLOSED)
self.inmemory_nodes = len(solver.CLOSED) + len(solver.OPEN)
def depth(self, initial_state):
self.algorithm = "Depth First"
solver = DepthSolver(initial_state)
self.run(solver.solve)
self.analyzed_nodes = len(solver.CLOSED)
self.inmemory_nodes = len(solver.CLOSED) + len(solver.OPEN)
def hillclimbing(self, initial_state):
self.algorithm = "Hill Climbing"
solver = HillClimbingSolver(initial_state)
self.run(solver.solve)
self.analyzed_nodes = len(solver.CLOSED)
self.inmemory_nodes = len(solver.CLOSED) + len(solver.OPEN)
def astar(self, initial_state):
self.algorithm = "A*"
solver = AStarSolver(initial_state)
self.run(solver.solve)
self.analyzed_nodes = len(solver.CLOSED)
self.inmemory_nodes = len(solver.CLOSED) + len(solver.OPEN)
def pruningbreadth(self, initial_state, beta):
self.algorithm = "Pruning Breadth First (beta = %d)" % beta
solver = PruningBreadthSolver(initial_state, beta)
self.run(solver.solve)
self.analyzed_nodes = len(solver.CLOSED)
self.inmemory_nodes = len(solver.CLOSED) + len(solver.OPEN)
class Queens(Statistics):
def start(self):
initial_state = queens.State(6)
self.breadth (initial_state)
self.print_stat()
self.depth (initial_state)
self.print_stat()
self.hillclimbing (initial_state)
self.print_stat()
self.astar (initial_state)
self.print_stat()
self.pruningbreadth (initial_state, 300)
self.print_stat()
for i in xrange(40, 34, -1):
self.pruningbreadth (initial_state, i)
self.print_stat()
class QueensDumb(Statistics):
def start(self):
initial_state = queens_dumb.State(6)
self.hillclimbing (initial_state)
self.print_stat()
self.astar (initial_state)
self.print_stat()
self.pruningbreadth (initial_state, 300)
self.print_stat()
for i in xrange(40, 34, -1):
self.pruningbreadth (initial_state, i)
self.print_stat()
class Slot(Statistics):
def start(self):
board = [
[5, 6, 1],
[2, 7, 0],
[4, 8, 3]
]
initial_state = slot.State(board)
self.breadth (initial_state)
self.print_stat()
self.depth (initial_state)
self.print_stat()
self.hillclimbing (initial_state)
self.print_stat()
self.astar (initial_state)
self.print_stat()
self.pruningbreadth (initial_state, 3)
self.print_stat()
self.pruningbreadth (initial_state, 2)
self.print_stat()
class SlotDumb(Statistics):
def start(self):
board = [
[5, 6, 1],
[2, 7, 0],
[4, 8, 3]
]
initial_state = slot_dumb.State(board)
self.hillclimbing (initial_state)
self.print_stat()
self.astar (initial_state)
self.print_stat()
self.pruningbreadth (initial_state, 3)
self.print_stat()
self.pruningbreadth (initial_state, 2)
self.print_stat()
def main():
print 'Slot'
print
a = Slot()
a.start()
print '---------'
print 'SlotDumb'
print
a = SlotDumb()
a.start()
print '---------'
print 'Queens'
print
b = Queens()
b.start()
print '---------'
print 'QueensDumb'
print
b = QueensDumb()
b.start()
if __name__ == '__main__':
for i in range(2):
main()
print '=========='