Autobook: Tweak parameters
[pachi.git] / tools / autobook / walk.sh
blobd9334304cc7c49193f0b5796c213ead1e23c3455
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=B
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 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"')
46 seq="$seq;${color}[$sgfmove]"
48 cd "$move"
49 case $color in
50 B) color=W;;
51 W) color=B;;
52 esac
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 if [ $((RANDOM%2)) = 1 ]; then
60 black="$pachi"
61 white="$opponent"
62 else
63 black="$opponent"
64 white="$pachi"
66 $twogtp_path -black "$black" -white "$white" -auto -verbose -size 9 -komi 7.5 -sgffile "$SEQDIR/r" -games 1 -openings "$SEQDIR"
67 wincolor=$(cat "$SEQDIR"/r-0.sgf | sed -ne 's/.*RE\[\(.\).*/\1/p')
68 case $wincolor in
69 B) result=1;;
70 W) result=0;;
71 esac
73 while [ -e stats ]; do
74 case $color in
75 B) nresult=$result;;
76 W) nresult=$((1-result));
77 esac
79 { read wins; read sims; } <stats
80 wins=$((${wins%.*}+nresult))
81 sims=$((sims-vloss+1))
82 { echo $wins; echo $sims; } >stats.new
83 mv stats.new stats # atomic
85 cd ..
86 case $color in
87 B) color=W;;
88 W) color=B;;
89 esac
90 done