forked from anyaschukin/N-Puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·359 lines (328 loc) · 8.41 KB
/
test.sh
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#### -- N-Puzzle Performance Test -- ####
## Runs Unsolvable & Solvable Unit tests from boards/
## & Random tests using boards/generator.py
## Then plots solve time, moves, size & time complexity by heuristic
## To run: ./test.sh
#### -- Config -- ####
MIN_SIZE=3 # 3 min
MAX_SIZE=4 # 4 default
TEST_CASES=5 # 11 unit cases available for sizes 3 to 9, in boards/
UNSOLVABLE_TEST=0 # 0 = off, 1 = on
SOLVABLE_TEST=1 # 0 = off, 1 = on
UNIT_TEST=1 # 0 = off, 1 = on
RANDOM_TEST=1 # 0 = off, 1 = on
declare -a heuristics=("manhattan" "nilsson" "outRowCol" "hamming" "euclidean")
#### -- Print Header -- ####
RESET="\x1b[0m"
BRIGHT="\x1b[1m"
RED="\x1b[31m"
GREEN="\x1b[32m"
YELLOW="\x1b[33m"
CLEAR_LINE="\r"
UNDERLINE="\x1b[4m"
printf "\E[H\E[2J" # Clear screen
printf $BRIGHT
echo "Launching N-Puzzle Performance Test ...$RESET\n"
go build
start=`date +%s`
#### -- Print Config -- ####
echo "$BRIGHT$UNDERLINE""Configuration$RESET"
echo "Minimum size:\t\t$MIN_SIZE"
echo "Maximum size:\t\t$MAX_SIZE"
echo "Number of test cases:\t$TEST_CASES"
if [ "$UNSOLVABLE_TEST" != 0 ]
then
echo "Unsolvable Tests:$GREEN\ton$RESET"
else
echo "Unsolvable Tests:$RED\toff$RESET"
fi
if [ "$SOLVABLE_TEST" != 0 ]
then
echo "Solvable Tests:$GREEN\t\ton$RESET"
else
echo "Solvable Tests:$RED\t\toff$RESET"
fi
if [ "$UNIT_TEST" != 0 ]
then
echo "Unit Tests:$GREEN\t\ton$RESET"
else
echo "Unit Tests:$RED\t\toff$RESET"
fi
if [ "$RANDOM_TEST" != 0 ]
then
echo "Random Tests:$GREEN\t\ton$RESET"
else
echo "Random Tests:$RED\t\toff$RESET"
fi
printf "Heuristics:\t\t"
for heuristic in "${heuristics[@]}"
do
printf "$heuristic\n\t\t\t"
done
echo
#### -- Cleanup .csv -- ####
if [ -e solve_time.csv ]
then
rm solve_time.csv
fi
if [ -e moves.csv ]
then
rm moves.csv
fi
if [ -e size_complexity.csv ]
then
rm size_complexity.csv
fi
if [ -e time_complexity.csv ]
then
rm time_complexity.csv
fi
#### -- Test Function -- ####
unit_test()
{
SOLVABLE=$1
UNIT=$2
HEURISTIC=$3
case=$TEST_CASES
solved=0
count=0
best=42
worst=0
tcumulative=0
## Loop cases
while [ $count -lt $case ]
do
test_num=$(($test_num + 1))
count=$(($count + 1))
if [ "$UNIT" == "Unit" -a "$count" -gt 10 ]
then
break
fi
## Print ...
type=$(echo $SOLVABLE $UNIT)
if [ "$solved" = 0 ]
then
printf "%s %s ...$CLEAR_LINE" $SOLVABLE $UNIT
elif [ "$solved" -lt $(($count - 1)) ]
then
printf "$YELLOW%s %s%-4s\t%s/%s ... $RESET $CLEAR_LINE" $SOLVABLE $UNIT ":" $solved $(($count - 1))
else
printf "$GREEN%s %s%-4s\t%s/%s OK ... $RESET $CLEAR_LINE" $SOLVABLE $UNIT ":" $solved $(($count - 1))
fi
## Run
if [ "$SOLVABLE" == "Unsolvable" ]
then ## Unsolvable
if [ "$UNIT" == "Unit" ]
then ## Unit
unit=$(echo "boards/Unsolvable/$size/$size""u$count.txt")
output=$(./n-puzzle -h=$HEURISTIC $unit)
else ## Random
output=$(python boards/generator.py -u $size >> rm_me.txt; ./n-puzzle -h=$HEURISTIC rm_me.txt)
fi
unsolvable=$(echo "$output" | tail -n -2 | head -n 1)
if [ "$unsolvable" = "This puzzle is unsolvable." ]
then
solved=$(($solved + 1))
total_solved=$(($total_solved + 1))
else
if [ -f "rm_me.txt" ]
then
rm rm_me.txt
fi
continue
fi
time=$(echo "$output" | tail -n -1 | cut -d " " -f 3)
else ## Solvable
if [ "$UNIT" == "Unit" ]
then ## Unit
unit=$(echo "boards/Solvable/$size/$size""s$count.txt")
output=$(./n-puzzle -h=$HEURISTIC $unit)
else ## Random
output=$(python boards/generator.py -s $size >> rm_me.txt; ./n-puzzle -h=$HEURISTIC rm_me.txt)
fi
end=$(echo "$output" | tail -n -1)
if [ "$end" != "You've finished n-puzzle!" ]
then
continue
else
solved=$(($solved + 1))
total_solved=$(($total_solved + 1))
fi
time=$(echo "$output" | tail -n -2 | head -n 1 | cut -d " " -f 3)
fi
## Time
prefix=$(echo "$time" | rev | cut -c-1-2 | rev | cut -c-1-1)
if [ "$prefix" = "m" ] # Milliseconds
then
time=$(echo "$time" | rev | cut -c3-42 | rev)
time=$(echo "scale = 9; ($time / 1000)" | bc)
elif [ "$prefix" = "µ" ] # Microseconds
then
time=$(echo "$time" | rev | cut -c3-42 | rev)
time=$(echo "scale = 9; ($time / 1000000)" | bc)
elif [ "$prefix" = "n" ] # Nanoseconds
then
time=$(echo "$time" | rev | cut -c3-42 | rev)
time=$(echo "scale = 9; ($time / 1000000000)" | bc)
elif [[ "$time" =~ "m" ]] # Minutes
then
minutes=$(echo "$time" | cut -d "m" -f 1)
seconds=$(echo "$time" | cut -d "m" -f 2 | rev | cut -c3-42 | rev)
time=$(echo "scale = 9; $seconds + ($minutes * 60)" | bc)
else # Seconds
time=$(echo "$time" | rev | cut -c2-42 | rev)
fi
tcumulative=$(echo "scale = 9; $tcumulative + $time" | bc)
time_up=$(echo "scale = 0; $time * 1000000000" | bc | cut -d "." -f 1)
worst_up=$(echo "scale = 0; $worst * 1000000000" | bc | cut -d "." -f 1)
best_up=$(echo "scale = 0; $best * 1000000000" | bc | cut -d "." -f 1)
if [ "$time_up" -gt "$worst_up" ]
then
worst=$time
fi
if [ "$time_up" -lt "$best_up" ]
then
best=$time
fi
## Write to .csv
if [ "$SOLVABLE" != "Unsolvable" ]
then
moves=$(echo "$output" | grep moves | cut -d ":" -f 2)
size_complexity=$(echo "$output" | grep Size | cut -d ":" -f 2)
time_complexity=$(echo "$output" | grep Time | cut -d ":" -f 2)
printf "%f" $time >> solve_time.csv
printf "%d" $moves >> moves.csv
printf "%d" $size_complexity >> size_complexity.csv
printf "%d" $time_complexity >> time_complexity.csv
if [ $count -lt $case -o "$UNIT" == "Unit" -a $RANDOM_TEST != "0" ]
then
printf ", " >> solve_time.csv
printf ", " >> moves.csv
printf ", " >> size_complexity.csv
printf ", " >> time_complexity.csv
else
printf "\n" >> solve_time.csv
printf "\n" >> moves.csv
printf "\n" >> size_complexity.csv
printf "\n" >> time_complexity.csv
fi
fi
## Print Solved
type=$(echo $SOLVABLE $UNIT)
if [ "$solved" = 0 ]
then
printf "$RED%s %s%-3s\t%s/%s ERROR $RESET $CLEAR_LINE" $SOLVABLE $UNIT ":" $solved $count
elif [ "$solved" -lt "$count" ]
then
printf "$YELLOW%s %s%-3s\t%s/%s $RESET $CLEAR_LINE" $SOLVABLE $UNIT ":" $solved $count
else
printf "$GREEN%s %s%-3s\t%s/%s OK $RESET $CLEAR_LINE" $SOLVABLE $UNIT ":" $solved $count
fi
## Cleanup
if [ -f "rm_me.txt" ]
then
rm rm_me.txt
fi
done
## Print Time
if [ "$solved" != 0 ]
then
mean=$(echo "scale = 9; $tcumulative / $solved" | bc)
else
mean="$RED Failed$RESET"
fi
if [ "$worst" = 0 ]
then
worst="$RED""Failed$RESET"
fi
if [ "$best" = 42 ]
then
best="$RED""Failed$RESET"
fi
echo
echo "Solve time Worst: $worst"
echo "(Seconds) Mean: $mean"
echo " Best: $best"
echo
}
#### -- Test -- ####
size=$MIN_SIZE
test_num=0
total_solved=0
if [ -f "rm_me.txt" ]
then
rm rm_me.txt
fi
## Loop size
while [ $size -lt $(expr $MAX_SIZE + 1) ]
do
if [ $size -gt 3 ]
then
declare -a heuristics=("manhattan" "nilsson")
fi
## Loop heuristic
for heuristic in "${heuristics[@]}"
do
printf $BRIGHT
printf $UNDERLINE
echo "Size - $size, Heuristic - $heuristic$RESET"
echo
if [ "$UNSOLVABLE_TEST" != 0 -a "$UNIT_TEST" != 0 -a "$size" -gt 2 -a "$size" -lt 10 ]
then
unit_test Unsolvable Unit $heuristic
fi
if [ "$UNSOLVABLE_TEST" != 0 -a "$RANDOM_TEST" != 0 ]
then
unit_test Unsolvable Random $heuristic
fi
if [ "$SOLVABLE_TEST" != 0 -a "$UNIT_TEST" != 0 -a "$size" -gt 2 -a "$size" -lt 10 ]
then
unit_test Solvable Unit $heuristic
fi
if [ "$SOLVABLE_TEST" != 0 -a "$RANDOM_TEST" != 0 ]
then
unit_test Solvable Random $heuristic
fi
echo
done
size=$(($size + 1))
python3 test_plot.py
## Cleanup .csv
if [ -e solve_time.csv ]
then
rm solve_time.csv
fi
if [ -e moves.csv ]
then
rm moves.csv
fi
if [ -e size_complexity.csv ]
then
rm size_complexity.csv
fi
if [ -e time_complexity.csv ]
then
rm time_complexity.csv
fi
done
## Print End
end=`date +%s`
runtime=$((end-start))
printf $BRIGHT
if [ "$runtime" == 1 ]
then
echo "N-Puzzle performance test finished, total runtime $runtime second.\n"
else
echo "N-Puzzle performance test finished, total runtime $runtime seconds.\n"
fi
if [ "$total_solved" == "$test_num" ]
then
echo "Passed$GREEN $total_solved / $test_num$RESET$BRIGHT total tests\n"
elif [ "$total_solved" == "0" ]
then
echo "Passed$RED $total_solved / $test_num$RESET$BRIGHT total tests\n"
else
echo "Passed$YELLOW $total_solved / $test_num$RESET$BRIGHT total tests\n"
fi
#### -- Cleanup -- ####
rm n-puzzle