Fix bugs and documentate stuff
[progcomp2013.git] / run.sh
1 #!/bin/bash
2
3 # This script runs a single round of the competition
4
5 . vars
6
7 agents=""
8
9
10
11 # Copy messages to a log file
12 if [ "$BASH_ARGV" != "_worker_" ]; then
13         # Get the round number
14         mkdir -p $results_dir
15         cd $results_dir
16         round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
17         if [ "$round" == "" ]; then round=0; fi
18         round=$(( $round + 1 ))
19         mkdir -p round$round
20         cd $root_dir
21         exec $0 "$@" _worker_ 2>&1 | tee $results_dir/round$round/run.log
22         exit $?
23 else
24         cd $results_dir
25         round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
26 fi
27
28 echo "Start at $(date)"
29
30 # Setup the swarm
31 if [ "$swarm_hosts" != "" ]; then
32         swarm --daemon
33         for h in $swarm_hosts; do
34                 swarm -c "#ABSORB $h#"
35         done
36         swarm -c "#.*# cd $root_dir; mkdir -p $results_dir/round$round"
37 fi
38
39
40
41 cd $root_dir/$agent_dir
42 count=0
43 for f in $(ls); do
44         if [ -d $f ]; then
45                 info_file=$(ls $f | grep -w "info")
46                 if [ "$info_file" == "" ]; then
47                         echo "Skipping agent $f (missing info file)"
48                 else
49                         count=$(( $count + 1 ))
50                         agents="$agents $f"
51                 fi
52         fi
53 done
54 echo "Found $count agents in $agent_dir/"
55
56
57
58 # Add all the games to swarm
59 cd $root_dir
60
61 if [ "$agents" == "" ]; then
62         echo "No agents to run round $round with" 1>&2
63         rm -rf "$results_dir/round$round"
64         exit 1
65 fi
66
67 echo "Start round $round"
68
69 game=0
70 for a in $agents; do
71         runa="$agent_dir/$a/$(head --lines=1 $agent_dir/$a/info)"
72         for b in $agents; do
73                 if [ "$a" == "$b" ]; then continue; fi
74                 for ((i=1;i<=$games_per_pair;++i)); do
75                         runb="$agent_dir/$b/$(head --lines=1 $agent_dir/$b/info)"
76                         f="${a}_${b}_${i}"
77
78                         game=$(( $game + 1))    
79                         l="$results_dir/round$round/$f.log"
80                         err="$results_dir/round$round/$f.err"
81
82                         echo "Game #$game: $a .vs. $b ($i of $games_per_pair)"
83                         if [ "$swarm_hosts" != "" ]; then
84                                 swarm -c "$qchess --no-graphics \"$runa\" \"$runb\" --log=$l --log=@web/current.log 2>$err" -o $f.result
85                         else
86                                 $qchess --no-graphics "$runa" "$runb" --log=$l --log=@web/current.log 1> $f.result 2> $err
87                                 if [ "$(wc -l $err | awk '{print $1}')" == "0" ]; then rm $err; fi
88                         fi
89                 done
90         done
91 done
92
93
94
95 if [ "$swarm_hosts" != "" ]; then
96         swarm -c "#BARRIER BLOCK#" # Wait for all games to finish
97
98         #Copy over log files (start before updating scores as the scp may take some time)
99         for h in "local $swarm_hosts"; do
100                 cmd="
101                 if [ \"\$(hostname)\" != \"$webserver\" ]; then
102                         cd $root_dir/$results_dir/round$round;
103                         for i in *.log *.err; do
104                                 if [ \"\$(wc -l \$i | awk '{print \$1}')\" != 0 ]; then
105                                         scp \$i $webserver/$root_dir/$results_dir/round$round/\$i
106                                 fi
107                         done
108                 fi"
109                 swarm -c "#$h:.* \$# $cmd" # Execute once on each host
110         done
111 fi
112
113 echo "Completed $games games with $count agents"
114
115 # A bash function. Yes, they do exist.
116 function update_score
117 {
118         if [ -e $agent_dir/$1/score.dat ]; then
119                 score=$(tail --lines=1 $agent_dir/$1/score.dat | awk '{print $1}')
120         else
121                 score=0
122         fi
123         
124
125         score=$(( $score + $3 ))
126         echo "$3 $score $2 $4 $5" >> $agent_dir/$1/score.dat
127         return $score
128 }
129
130 # Go through results
131 for f in *.result; do
132         log=round$round/$(echo $f.log | sed -e "s:.result::g")
133         white=$(echo $f | tr '_' '\t' | awk '{print $1}')
134         black=$(echo $f | tr '_' '\t' | awk '{print $2}')
135         if [ "$(cat $f)" == "white" ]; then
136                 update_score $white $black $win_score WIN $log
137                 update_score $black $white $loss_score LOSS $log
138         elif [ "$(cat $f)" == "black" ]; then
139                 update_score $white $black $loss_score LOSS $log
140                 update_score $black $white $win_score WIN $log
141         elif [ "$(cat $f)" == "DRAW" ]; then
142                 update_score $white $black $draw_score DRAW $log
143                 update_score $black $white $draw_score DRAW $log
144         else
145                 echo "Unrecognised result \"$(cat $f)\" in $f" 1>&2
146         fi
147
148         rm $f
149 done
150
151 echo "Updated scores"
152
153 #Close the swarm
154 if [ "$swarm_hosts" != "" ]; then
155         swarm -c "#BARRIER BLOCK#"
156         swarm -c "#.*# exit"
157 fi
158
159 echo "Finished at $(date)"
160

UCC git Repository :: git.ucc.asn.au