-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.sh
executable file
·95 lines (84 loc) · 2.5 KB
/
benchmark.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
# print usage function
function usage {
echo "Usage: ./benchmark.sh <data_dir> <algo> (<time_limit> for the mip solver)"
echo "Where algo can be one of these: --mip, --greedy, --bruteforce, --simulated_annealing"
echo "<time_limit> is the time limit in seconds (default 600)."
exit 1
}
# if the number of arguments is less than 2 or greater than 3, print the usage
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
usage
fi
# if the first argument is not a directory, print the usage
if [ ! -d $1 ]; then
usage
fi
# if the second argument is not one of the algorithms, print the usage
if [ $2 != "--mip" ] && [ $2 != "--greedy" ] && [ $2 != "--bruteforce" ] && [ $2 != "--simulated_annealing" ]; then
usage
fi
# if the third argument is not a number, print the usage
if [ $# -eq 3 ] && ! [[ $3 =~ ^[0-9]+$ ]]; then
usage
fi
# if the user provides a time limit, use it
if [ $# -eq 3 ]; then
time_limit=$3
else
time_limit=600
fi
algo_dir="${2#*--}"
mkdir -p log # create the output directory if it does not already exist
cd log && mkdir -p $algo_dir
echo `date` > $algo_dir/date.txt
cd ..
echo "Experimental Campaign:"
echo "Data directory: $1"
echo "Output directory: log/$algo_dir"
echo "Algorithm: $2"
# if the algorithm is mip, print the time limit
if [ $2 == "--mip" ]; then
echo "Time limit: $time_limit"
fi
echo
# Compile the code to have the latest versions of the executables
mkdir -p build
cd build
cmake ..
echo
make 'unit_test.out'
# Run the unit tests and check that they pass, otherwise exit
./unit_test.out
if [ $? -ne 0 ]; then
echo "Unit tests failed, exiting."
exit 1
fi
echo
# Run the benchmark
echo "Unit tests passed, running the benchmark now..."
echo
# if the algorithm is mip, run the mip solver for each instance
if [ $2 == "--mip" ]; then
make 'mip.out'
echo
# for all subdirectories in the data directory
for dir in $1/*; do
# create a variable instance that contains the name of the instance (split by '/')
instance=${dir##*/}
echo "Solving $instance"
./mip.out $dir $time_limit > ../log/$algo_dir/log_$instance.txt
done
else
make 'test.out'
echo
# otherwise, run the algorithm for each instance
for dir in $1/*; do
instance=${dir##*/}
echo "Solving $instance"
./test.out $dir $2 $time_limit > ../log/$algo_dir/log_$instance.txt
done
fi
echo
grep "Result:" ../log/$algo_dir/*.txt > ../log/$algo_dir/results.csv
echo "Benchmark finished, results are in log/$algo_dir/results.csv"
exit 0