Distributed engine: force a different id if requested
[pachi.git] / HACKING
blob8696eee3ea91a4496189a835c8fa816760746461
1 This is brief developer-oriented overview in Pachi structure.
3 Pachi is completely Go-specific (c.f. Fuego; though e.g. atari go support
4 should be easy to add), but fairly modular. It has been built with focus
5 on MonteCarlo-based play, but it can in principle be used for other
6 play engines as well.
9 Basic architecture
10 ==================
12 Pachi consists of the following components:
15   +------+    +--------+    +---------+
16   | core | -- | engine | -- | playout |
17   +------+    +--------+    +---------+
18                      |        |
19                   +-------------+
20                   | aux library |
21                   +-------------+
23 * "core" takes care of the program's lifetime, GTP interface and basic
24   fast Go board implementation
26         zzgo.c          global initialization and the main loop
27         version.h       current version information
28         debug.h         debugging infrastructure
29         random.[ch]     fast random number generator
30         gtp.[ch]        GTP protocol interface
31         timeinfo.[ch]   Time-keeping information
32         stone.[ch]      one board point coloring definition
33         move.[ch]       one board move definition
34         board.[ch]      board definition and basic interface
36 * "aux library" provides extra functions like static tactical evaluation
37   and pattern matching; it is somewhat interwound with "core" component
39         tactics.[ch]    extended interfaces for the go board
40         mq.h            "move queue" data structure
41         stats.h         "move statistics" data structure
42         probdist.[ch]   "probability distribution" data structure
43         ownermap.[ch]   simulation-based finalpos. "owner map" data structure
44         pattern3.[ch]   fast 3x3 spatial pattern matcher
45         pattern.[ch]    general multi-feature pattern matcher
46         network.[ch]    Network interface (useful for distributed engine)
48 * "engine" receives notifications about opponent moves and is asked
49   to generate a move to play on given board
51         engine.h        abstract engine interface
52         random/         example "random move generator" engine
53         replay/         example "playout move generator" engine
54         montecarlo/     simple treeless Monte Carlo engine, quite bitrotten
55         uct/            the main UCT-player engine, see below
56         patternscan/    auxiliary engine for harvesting patterns from
57                                 existing games
58         distributed/    "meta-engine" for distributed play by orchestrating
59                                 several UCT engines on different computers
61 * "playout" policy is asked to generate moves to play during the Monte Carlo
62   simulations, and to provide rough evaluation of moves feasibility for
63   the engine
65         playout.[ch]    abstract playout policy interface,
66                                 Monte Carlo simulation execution
67         playout/light   uniformly random playout policy
68         playout/moggy   rule-based "Mogo-like" playout policy
69         playout/elo     probdist-based "CrazyStone-like" playout policy
71 * Also, several ways of testing Pachi are provided:
73         t-unit/         interface for writing unit-tests for specific
74                                 functionality, mainly tactics
75         t-play/         interface for testing performance by playing games
76                                 against a fixed opponent (e.g. GNUGo)
79 UCT architecture
80 ================
82 The UCT engine has non-trivial structure by itself:
84   +-------------+    +-----+     +-------------------+
85   | node policy | -- | UCT | --- | node prior-hinter |
86   +-------------+    +-----+     +-------------------+
87                         |           |
88                    +---------+      |
89                    | playout | -----'
90                    +---------+
92 * "UCT" is the core of the engine
94         uct.[ch]        engine initialization, public interface
95         internal.h      internal state and data structures
96         tree.[ch]       minimax move tree with success statistics
97         walk.[ch]       filling the tree by walking it many times
98                                 and running MC simulations from leaves
100 * "node prior-hinter" assigns newly created nodes preliminary success
101   statistics ("prior values") to focus the search better
103         prior.[ch]      variety of methods for setting the priors
105 * "node policy" mainly chooses the current node's child to descend
106   through during the tree walk, based on the already recorded statistics;
107   it must balance exploration and exploitation well during the selection
109         policy/ucb1     the old-school original simple policy
110         policy/ucb1amaf the AMAF/RAVE-based policy gathering statistics rapidly
113 Board Implementation
114 ====================
116 The infrastructure is optimized for speed to make it well suited
117 for bruteforce engines, however tradeoffs are made to make it useful
118 for heavier MonteCarlo playouts as well (e.g. real liberties are
119 tracked instead of pseudoliberties). If you are looking for raw
120 light playout speed, libEGO is better choice.
122 In general, arbitrary board sizes are supported; however, board sizes
123 smaller than 9x9 have not been tested and board sizes larger than 25x25
124 are not supported by GTP - also, in theory some buffer overflows might
125 happen with board sizes larger than 19x19. The engine parameters are
126 primarily tuned for 19x19 play.
128 Ruleset
129 -------
131 While the Pachi engines generally play according to Chinese rules,
132 internally, Pachi uses Tromp-Taylor rules because they are simple,
133 fast and universal; they are very close to the New Zealand rules.
134 That means, it simply counts the number of stones and one-point eyes
135 of each color on the board, plus komi and handicap correction.
137 Tromp-Taylor rules also mean that multi-stone suicide is allowed! If you
138 do not like that (basically if you want to pretend it plays according
139 to Chinese rules), you need to rule that out in your engine, currently.
140 The provided engines DO avoid multi-stone suicide (but the UCT engine
141 will never play it itself).
143 Tromp-Taylor rules have positional superko; the board implementation
144 will set a flag if it is violated, but play the move anyway. You need
145 to enforce the superko rule in your engine.
148 GTP Implementation
149 ==================
151 ...is a very sad hack. ENSURE that only trusted parties talk to Pachi's
152 GTP interface, as it is totally non-resilient to any kind of overflow
153 or bad input attacks and allowing arbitrary input to be entered within
154 is a major security hole. Yes, this needs to be cleaned up. Also, currently
155 engines cannot plug in their own commands and there is no GoGui interface.
157 Pachi supports only few GTP commands now. Most importantly, it does not
158 support the undo command.  The final_status_list command requires engine
159 support.
162 General Pattern Matcher
163 =======================
165 Pachi has in-development general pattern matcher that can match various
166 sets of features (spatial and others), inspired by the CrazyStone pattern
167 model. Please see pattern.h for detailed description of the pattern concept
168 and recognized features.
170 To harvest patterns, use 'zzgo -e patternscan' (see patternscan/patternscan.c
171 for available options).  The output of the pattern scanner are two data
172 structures: The matched patterns
174         (feature1:payload feature2:payload ...)
176 and spatial dictionary. "Spatial" feature represents a particular
177 configuration of stones in a circle around the move-to-play; each
178 configuration has its own record in the dictionary and the spatial
179 feature references only the id in the dictionary; so you need to keep
180 both the patterns and the "patterns.spat" file.  Normally, 'patternscan'
181 will only match already existing dictionary entries, but you
182 can pass it "gen_spat_dict" so that it appends all newly found spatial
183 features to the dictionary - use "spat_threshold" to limit recording
184 only to frequently occuring spatial features; to start the dictionary
185 from scratch, simply remove any existing "patterns.spat" file.
187 There are few pre-made scripts to make the initialization of the pattern
188 matcher easy:
190 * pattern_byplayer.sh: Sorts out patterns from given SGF collection by
191   player names, one player per file in a dedicated directory. This is
192   useful if you want to use the patterns to e.g. recognize games of a
193   player by characteristic patterns. Spatial dictionary is autogenerated
194   in full.
196 * pattern_spatial_gen.sh: Initializes spatial dictionary by spatial features
197   found at least N times in given SGF collection.  This is useful for
198   further gathering of general pattern statistics while keeping the amount
199   of spatial features manageable.
201 * pattern_spatial_show.pl ID: Shows spatial pattern of given id in 2D plane.
203 * pattern_mm.sh: Combines patternsacn engine and the MM tool (see below),
204   producing gamma values for harvested patterns.
206 Minorization-majorization (CrazyStone patterns)
207 -----------------------------------------------
209 The pattern harvester can be used together with the MM tool by Remi Coulom:
211         http://remi.coulom.free.fr/Amsterdam2007/mm.tar.bz2
213 This tool will compute relative strength of individual features for teaming
214 them up and using the outcoming probability distribution for generating moves.
215 There is a script that will take you from SGF game collection to gamma values
216 in single shot - "pattern_mm.sh".
218 The resulting "patterns.gamma" file contains mapping from feature instances
219 to gamma floats, representing the features strength; note that it is totally
220 meaningless without the accompanying "patterns.spat" file generated by the
221 pattern_gather script. To make Pachi use the gamma values for tree bias and
222 in MC playouts, use the "elo" playout policy - but note that it's still in
223 heavy development.