-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_benchmark.rb
148 lines (128 loc) · 3.13 KB
/
performance_benchmark.rb
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
require 'benchmark'
load 'search_methods.rb'
def depth(board)
search(board, 'depth', nil, false)
end
def breadth(board)
search(board, 'breadth', nil, false)
end
def iterative_breadth(board)
search(board, 'iterative', 5, false)
end
def board_diff(board)
search(board, 'astar', 'board_diff', false)
end
def manhattan(board)
search(board, 'astar', 'manhattan', false)
end
# ----- Benchmark Preparations
puts 'Preparing benchmark...'
# Creates test cases
# 4 Moves
board1 = Board.new(
3,
[
[1, 0, 3],
[4, 2, 6],
[7, 5, 8]
]
)
# 9 Moves
board2 = Board.new(
3,
[
[1, 6, 2],
[5, 0, 3],
[4, 7, 8]
]
)
# 11 Moves
board3 = Board.new(
3,
[
[2, 4, 3],
[1, 0, 8],
[7, 6, 5]
]
)
# 17 Moves
board4 = Board.new(
3,
[
[4, 2, 3],
[8, 6, 1],
[0, 7, 5]
]
)
# 19 Moves
board5 = Board.new(
3,
[
[0, 5, 6],
[2, 1, 4],
[7, 8, 3]
]
)
# 25 Moves
board6 = Board.new(
3,
[
[7, 5, 2],
[6, 3, 8],
[4, 1, 0]
]
)
# --- Benchmark with board 1
puts 'Benchmark with Board 1:'
Benchmark.bmbm(30) do |x|
x.report('Depth Search:') { depth(board1) }
x.report('Breadth Search:') { breadth(board1) }
x.report('Iterative Breadth Search:') { iterative_breadth(board1) }
x.report('Manhattan Board Diff:') { board_diff(board1) }
x.report('Manhattan A*:') { manhattan(board1) }
end
# --- Benchmark with board 2
puts 'Benchmark with Board 2:'
Benchmark.bmbm(30) do |x|
x.report('Depth Search:') { depth(board2) }
x.report('Breadth Search:') { breadth(board2) }
x.report('Iterative Breadth Search:') { iterative_breadth(board2) }
x.report('Manhattan Board Diff:') { board_diff(board2) }
x.report('Manhattan A*:') { manhattan(board2) }
end
# --- Benchmark with board 3
puts 'Benchmark with Board 3:'
Benchmark.bmbm(30) do |x|
x.report('Depth Search:') { depth(board3) }
x.report('Breadth Search:') { breadth(board3) }
x.report('Iterative Breadth Search:') { iterative_breadth(board3) }
x.report('Manhattan Board Diff:') { board_diff(board3) }
x.report('Manhattan A*:') { manhattan(board3) }
end
# --- Benchmark with board 4
puts 'Benchmark with Board 4:'
Benchmark.bmbm(30) do |x|
x.report('Depth Search:') { depth(board4) }
x.report('Breadth Search:') { breadth(board4) }
x.report('Iterative Breadth Search:') { iterative_breadth(board4) }
x.report('Manhattan Board Diff:') { board_diff(board4) }
x.report('Manhattan A*:') { manhattan(board4) }
end
# --- Benchmark with board 5
puts 'Benchmark with Board 5:'
Benchmark.bmbm(30) do |x|
x.report('Depth Search:') { depth(board5) }
x.report('Breadth Search:') { breadth(board5) }
x.report('Iterative Breadth Search:') { iterative_breadth(board5) }
x.report('Manhattan Board Diff:') { board_diff(board5) }
x.report('Manhattan A*:') { manhattan(board5) }
end
# --- Benchmark with board 6
puts 'Benchmark with Board 6:'
Benchmark.bmbm(30) do |x|
x.report('Depth Search:') { depth(board6) }
x.report('Breadth Search:') { breadth(board6) }
x.report('Iterative Breadth Search:') { iterative_breadth(board6) }
x.report('Manhattan Board Diff:') { board_diff(board6) }
x.report('Manhattan A*:') { manhattan(board6) }
end