Introduce Fuego-compatible forcing opening book (fbook)
[pachi/derm.git] / sgf-analyse.pl
blob39ce2d3dcac4f9abd68c14e8c4062f88c35db709
1 #!/usr/bin/perl
2 # Simple script using Pachi for game analysis of SGF.
3 # Pachi will analyse each move in the SGF and suggest the winrate
4 # for either player and winrate it estimated for in-game followup.
6 # Usage: ./sgf-analyse.pl COLOR SGF PACHIARGS...
8 # Note that this script assumes dynkomi=none and does not show
9 # dynamic komi information. (Would be trivial to add but it's tricky
10 # to interpret the data.)
12 # This script is dirty and insecure for untrusted input!
14 # Example: ./sgf-analyse.pl W progame.sgf -t =2000 -d 0 dynkomi=none
15 # ...to get 2000 simulations per move, and winrates from white perspective.
17 # To plot the output in gnuplot:
18 # set yr [0:1]
19 # set ytics 0,0.25,1
20 # plot "datafile.csv" using 1:5 with lines
22 use warnings;
23 use strict;
25 my $mcolor = shift @ARGV;
26 my $sgf = shift @ARGV;
28 sub one {
29 my ($move) = @_;
31 # Move of the other color - supposedly.
32 return if ($move % 2 == ($mcolor eq 'B' ? 0 : 1));
34 # Get zzgo output from GTP stream that contains the SGF up to
35 # given move, with information about the originally made move
36 # included.
37 my $line = $move + 3; # board_size, clearboard, komi
38 my $rest = $line + 1;
39 open my $g, "./sgf2gtp.pl \"$sgf\" | sed -e '$line s/play \\(.*\\) \\(.*\\)/1 echo \\1 \\2\\n2 genmove \\1\\n3 pachi-result/' -e '$rest,\$ d' | ./zzgo @ARGV |" or die $!;
41 # Parse the GTP output.
42 my ($color, $realmove, $genmove, $winrate) = @_;
43 while (<$g>) {
44 chomp;
45 if (/^=1 (.*) (.*)/) {
46 $color = $1; $realmove = uc $2;
47 } elsif (/^=2 (.*)/) {
48 $genmove = $1;
49 } elsif (/^=3 (.*) (.*) (.*) (.*)/) {
50 $winrate = $mcolor eq $color ? $3 : 1.0 - $3;
54 # Pass value is not interesting since Pachi might want
55 # to clarify some groups yet.
56 return if $realmove eq 'PASS';
58 # Generate summary line.
59 print join(', ', $move, $color, $realmove, $genmove, $winrate) . "\n";
62 print "# $sgf @ARGV\n";
64 my $moves = `./sgf2gtp.pl \"$sgf\" | wc -l` - 3;
65 one($_) for (1 .. $moves);