From 3a5bad93837928dfac88fe617b71217f557ea66e Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 17 Nov 2011 23:42:28 +0100 Subject: [PATCH] Autobook: Simple framework for automated opening book building --- HACKING | 4 ++- tools/autobook/README | 31 ++++++++++++++++ tools/autobook/autobook.sh | 19 ++++++++++ tools/autobook/eval.sh | 24 +++++++++++++ tools/autobook/expand.sh | 25 +++++++++++++ tools/autobook/walk.sh | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 tools/autobook/README create mode 100755 tools/autobook/autobook.sh create mode 100755 tools/autobook/eval.sh create mode 100755 tools/autobook/expand.sh create mode 100755 tools/autobook/walk.sh diff --git a/HACKING b/HACKING index 22262c8..4f01214 100644 --- a/HACKING +++ b/HACKING @@ -198,7 +198,9 @@ used by the UCT engine when found. Alternatively, there is a support for directly used opening book (so-called fbook, a.k.a. "forced book" or "fuseki book"). The book is stored in a text file in Fuego-compatible format and can be loaded -using the ./pachi -f parameter. +using the ./pachi -f parameter. A naive way to build such a book +based on shell-based, twogtp-based UCT is available through the +tools/autobook/ framework. Local Trees diff --git a/tools/autobook/README b/tools/autobook/README new file mode 100644 index 0000000..a324f46 --- /dev/null +++ b/tools/autobook/README @@ -0,0 +1,31 @@ +This is a shell-based UCT implementation. ;-) + +From another perspective, this is a tool for automated opening book +construction. The book is based on UCT tree with large exploration +coefficient and fed with results from games against a compenent +opponent. + +The UCT tree is stored in a directory tree, directories are nodes, +UCT statistics are stored in the 'stats' file of each directory +(first line is # of wins, second is # of playouts). Start the book +building by running the + + autobook.sh PACHI_CMD OPPONENT_CMD BOOKDIR + +script, specifying complete Pachi and opponent's invocation details +in the two parameters. If BOOKDIR exists, autobook.sh will continue +an existing opening book build; multiple autobook.sh scripts may +be running too. Just break it (Ctrl-C) when you want to stop. + +These helper scripts are used by autobook.sh: + + walk.sh A single iteration of the algorithm + eval.sh Choose next move to choose in the current directory (node) + expand.sh Create subdirectories (followup nodes) in the cwd + +The built autobook can be converted to an opening book in the fbook +format (on the most-simulated basis) using: + + autobook2fbook.sh BOOKDIR + +(TODO) diff --git a/tools/autobook/autobook.sh b/tools/autobook/autobook.sh new file mode 100755 index 0000000..bacbc70 --- /dev/null +++ b/tools/autobook/autobook.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +if [ $# -ne 3 ]; then + echo "Usage: $0 PLAYER1 PLAYER2 BOOKDIR" >&2 + exit 1 +fi + +export CMDDIR=$(pwd) +export SEQDIR=$(mktemp -d) + +if [ ! -d "$3" ]; then + mkdir "$3" + { echo 8; echo 16; } >"$3"/stats +fi + +while true; do + (cd "$3"; "$CMDDIR"/walk.sh "$1" "$2") + sleep 5 +done diff --git a/tools/autobook/eval.sh b/tools/autobook/eval.sh new file mode 100755 index 0000000..027ccd5 --- /dev/null +++ b/tools/autobook/eval.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Choose next move to choose in the current directory (node) + +# Config: +# Exploration coefficient +explore_p=0.8 + +best_move= +best_val=-9999 + +{ read mwins; read msims; } "$move"/stats + done diff --git a/tools/autobook/walk.sh b/tools/autobook/walk.sh new file mode 100755 index 0000000..e00799c --- /dev/null +++ b/tools/autobook/walk.sh @@ -0,0 +1,90 @@ +#!/bin/sh +# +# A single iteration of the algorithm + +# Config: +# After how many visits (including prior_sims!) a node is expanded. +expand_sims=18 +# Virtual loss +vloss=8 +# Twogtp path +twogtp_path=/home/pasky/gogui-1.3.2/bin/gogui-twogtp + +# Expected env: +# $CMDDIR +# $SEQDIR + +pachi="$1"; shift +opponent="$1" + +seq="" +color=B +while true; do + { read wins; read sims; } stats.new + mv stats.new stats # atomic + + children=$(find . -maxdepth 1 -type d | wc -l); + case $children in + 1) expanded=;; + *) expanded=1;; + esac + + if [ -z "$expanded" -a "$sims" -ge "$expand_sims" ]; then + 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" + "$CMDDIR"/expand.sh "$pachi" + expanded=1 + fi + if [ -z "$expanded" ]; then + break; + fi + + move=$("$CMDDIR"/eval.sh) + + 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"') + seq="$seq;${color}[$sgfmove]" + + cd "$move" + case $color in + B) color=W;; + W) color=B;; + esac +done + +echo "*** Sequence: $seq" +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" +rm -f "$SEQDIR"/r* + +if [ $((RANDOM%2)) = 1 ]; then + black="$pachi" + white="$opponent" +else + black="$opponent" + white="$pachi" +fi +$twogtp_path -black "$black" -white "$white" -auto -verbose -size 9 -komi 7.5 -sgffile "$SEQDIR/r" -games 1 -openings "$SEQDIR" +wincolor=$(cat "$SEQDIR"/r-0.sgf | sed -ne 's/.*RE\[\(.\).*/\1/p') +case $wincolor in + B) result=1;; + W) result=0;; +esac + +while [ -e stats ]; do + case $color in + B) nresult=$result;; + W) nresult=$((1-result)); + esac + + { read wins; read sims; } stats.new + mv stats.new stats # atomic + + cd .. + case $color in + B) color=W;; + W) color=B;; + esac +done -- 2.11.4.GIT