Merge pull request #11 from gonmf/master
[pachi.git] / tools / autobook / walk.sh
blob7d4cd7a2d8ca3f456948ec953f2db998d7fc8841
1 #!/bin/sh
3 # A single iteration of the algorithm
5 # Config:
6 # After how many visits (including prior_sims!) a node is expanded.
7 expand_sims=26
8 # Virtual loss
9 vloss=4
10 # Twogtp path
11 twogtp_path=/home/pasky/gogui-1.3.2/bin/gogui-twogtp
13 # Expected env:
14 # $CMDDIR
15 # $SEQDIR
17 pachi="$1"; shift
18 opponent="$1"
20 seq=""
21 color=W
22 while true; do
23 { read wins; read sims; } <stats
24 xsims=$((sims+vloss))
25 { echo $wins; echo $xsims; } >stats.new
26 mv stats.new stats # atomic
28 children=$(find . -maxdepth 1 -type d | wc -l);
29 case $children in
30 1) expanded=;;
31 *) expanded=1;;
32 esac
34 if [ -z "$expanded" -a "$sims" -ge "$expand_sims" ]; then
35 echo "(;FF[4]GM[1]CA[UTF-8]RU[Chinese]SZ[9]HA[0]KM[7.5]PW[white]PB[black]$seq)" >"$SEQDIR/a.sgf"
36 "$CMDDIR"/expand.sh "$pachi"
37 expanded=1
39 if [ -z "$expanded" ]; then
40 break;
43 move=$("$CMDDIR"/eval.sh)
45 cd "$move"
46 case $color in
47 B) color=W;;
48 W) color=B;;
49 esac
51 sgfmove=$(echo "$move" | perl -nle 'my ($x,$y) = /(.)(.)/; $x=lc($x); $x=chr(ord($x)-1) if ord(lc $x) > ord("i"); $y = chr(96+10-$y); print "$x$y"')
52 seq="$seq;${color}[$sgfmove]"
53 done
55 echo " *** Sequence: $seq"
56 echo "(;FF[4]GM[1]CA[UTF-8]RU[Chinese]SZ[9]HA[0]KM[7.5]PW[white]PB[black]$seq)" >"$SEQDIR/a.sgf"
57 rm -f "$SEQDIR"/r*
59 # last move has been... - we want to simulate this being _our_ move yet,
60 # i.e. start the simulation with the opponent to play
61 case $color in
63 black="$pachi"
64 white="$opponent";;
66 black="$opponent"
67 white="$pachi";;
68 esac
69 $twogtp_path -black "$black" -white "$white" -auto -verbose -size 9 -komi 7.5 -sgffile "$SEQDIR/r" -games 1 -openings "$SEQDIR"
70 wincolor=$(cat "$SEQDIR"/r-0.sgf | sed -ne 's/.*RE\[\(.\).*/\1/p')
71 case $wincolor in
72 B) result=1;;
73 W) result=0;;
74 esac
75 echo " *** Result: $wincolor wins => $result"
76 pwd; ls
78 while [ -e stats ]; do
79 case $color in
80 B) nresult=$result;;
81 W) nresult=$((1-result));
82 esac
83 echo " + Recording $nresult .. $(pwd) color $color"
85 { read wins; read sims; } <stats
86 wins=$((${wins%.*}+nresult))
87 sims=$((sims-vloss+1))
88 { echo $wins; echo $sims; } >stats.new
89 mv stats.new stats # atomic
91 cd ..
92 case $color in
93 B) color=W;;
94 W) color=B;;
95 esac
96 done