Updated README: Equal sign not required with `--mode` flag.
[sgk-go] / doc / patterns.texi
CommitLineData
7eeb782e
AT
1@menu
2* Patterns Overview:: Overview of the pattern database.
3* Pattern Classification:: The classification field
4* Pattern Values:: The value field
5* Helper Functions:: Helper Functions
6* Autohelpers and Constraints:: Automatic generation of helper functions.
7* Autohelper Actions:: Autohelper Actions
8* Autohelper Functions:: Autohelper Functions
9* Attack and Defense DB:: The Attack and defense moves database.
10* Connections Database:: The connection database.
11* Connection Functions:: Functions in @file{connections.c}
12* Tuning:: Tuning the pattern database.
13* PM Implementation:: Implementation.
14* Symmetry & transformations:: Symmetry and transformations.
15* Details:: Details of implementation.
16* Grid optimization:: The ``grid'' optimization.
17* Joseki Compiler:: The joseki compiler.
18* Ladders in Joseki:: Example: ladders in joseki.
19* Corner Matcher:: A special matcher for joseki patterns.
20* Editing Patterns:: Emacs major mode for pattern files.
21@end menu
22
23@node Patterns Overview
24@section Overview
25
26@cindex pattern overview
27@cindex pattern matching
28@cindex pattern database
29@cindex eye shapes database
30@cindex defence shapes database
31@cindex attack shapes database
32@cindex connection shapes database
33@cindex pattern.h
34@cindex pattern.c
35
36Several pattern databases are in the patterns directory. This chapter
37primarily discusses the patterns in @file{patterns.db}, @file{patterns2.db},
38and the pattern files @file{hoshi.db} etc. which are compiled from the SGF
39files @file{hoshi.sgf} (@pxref{Joseki Compiler}). There is no essential
40difference between these files, except that the ones in @file{patterns.db} and
41@file{patterns2.db} are hand written. They are concatenated before being
42compiled by @code{mkpat} into @file{patterns.c}. The purpose of the separate
43file @file{patterns2.db} is that it is handy to move patterns into a new
44directory in the course of organizing them. The patterns in @file{patterns.db}
45are more disorganized, and are slowly being moved to @file{patterns2.db}.
46
47During the execution of @code{genmove()}, the patterns are matched in
48@file{shapes.c} in order to find move reasons.
49
50The same basic pattern format is used by @file{attack.db}, @file{defense.db},
51@file{conn.db}, @file{apats.db} and @file{dpats.db}. However these patterns
52are used for different purposes. These databases are discussed in other parts
53of this documentation. The patterns in @file{eyes.db} are entirely different
54and are documented elsewhere (@pxref{Eyes}).
55
56@cindex format of the pattern database
57@cindex description of shapes
58@cindex ascii description of shapes
59
60The patterns described in the databases are ascii representations, of
61the form:
62
63Pattern EB112
64
65@example
66
67 ?X?.? jump under
68 O.*oo
69 O....
70 o....
71 -----
72
73 :8,ed,NULL
74@end example
75
76Here @samp{O} marks a friendly stone, @samp{X} marks an enemy stone, @samp{.} marks
77an empty vertex, @samp{*} marks O's next move, @samp{o} marks a square either
78containing @samp{O} or empty but not @samp{X}. (The symbol @samp{x}, which does not
79appear in this pattern, means @samp{X} or @samp{.}.) Finally @samp{?} Indicates a
80location where we don't care what is there, except that it cannot be
81off the edge of the board.
82
83The line of @samp{-}'s along the bottom in this example is the edge of the
84board itself---this is an edge pattern. Corners can also be indicated.
85Elements are not generated for @samp{?} markers, but they are not
86completely ignored - see below.
87
88The line beginning @samp{:} describes various attributes of the pattern, such
89as its symmetry and its class. Optionally, a function called a
90``helper'' can be provided to assist the matcher in deciding whether
91to accept move. Most patterns do not require a helper, and this field
92is filled with NULL.
93
94@findex shapes_callback
95The matcher in @file{matchpat.c} searches the board for places where this
96layout appears on the board, and the callback function
97@code{shapes_callback()} in @file{shapes.c} registers the appropriate move
98reasons.
99
100After the pattern, there is some supplementary information in the format:
101@example
102
103 :trfno, classification, [values], helper_function
104
105@end example
106
107Here trfno represents the number of transformations of the pattern to
108consider, usually @samp{8} (no symmetry, for historical reasons), or one of
109@samp{|}, @samp{\}, @samp{/}, @samp{-}, @samp{+}, @samp{X}, where the line
110represents the axis of symmetry. (E.g. @samp{|} means symmetrical about a
111vertical axis.)
112
113The above pattern could equally well be written on the left edge:
114
115@example
116
117 |oOO?
118 |...X
119 |..*?
120 |..o.
121 |..o?
122
123 :8,ed,NULL
124@end example
125
126The program @code{mkpat} is capable of parsing patterns written this
127way, or for that matter, on the top or right edges, or in any
128of the four corners. As a matter of convention all the edge patterns
129in @file{patterns.db} are written on the bottom edge or in the lower left
130corners. In the @file{patterns/} directory there is a program called
131@code{transpat} which can rotate or otherwise transpose patterns.
132This program is not built by default---if you think you need it,
133@code{make transpat} in the @file{patterns/} directory and
134consult the usage remarks at the beginning of @file{patterns/transpat.c}.
135
136@node Pattern Classification
137@section Pattern Attributes
138
139@cindex pattern attributes
140@cindex shape attributes
141
142The attribute field in the @samp{:} line of a pattern consists of a sequence
143of zero or more of the following characters, each with a different
144meaning. The attributes may be roughly classified as @dfn{constraints},
145which determine whether or not the pattern is matched, and @dfn{actions},
146which describe what is to be done when the pattern is matched, typically
147to add a move reason.
148
149@subsection Constraint Pattern Attributes
150
151@itemize
152@item @samp{s}
153@quotation
154Safety of the move is not checked. This is appropriate for sacrifice
155patterns. If this classification is omitted, the matcher requires that the
156stone played cannot be trivially captured. Even with s classification, a check
157for legality is made, though.
158@end quotation
159@item @samp{n}
160@quotation
161In addition to usual check that the stone played cannot be
162trivially captured, it is also confirmed that an opponent
163move here could not be captured.
164@end quotation
165@item @samp{O}
166@quotation
167It is checked that every friendly (@samp{O}) stone of the pattern belongs to a
168dragon which has status (@pxref{Dragons}) @code{ALIVE} or
169@code{UNKNOWN}. The @code{CRITICAL} matcher status is excluded. It is possible
170for a string to have @code{ALIVE} status and still be tactically
171critical, since it might be amalgamated into an ALIVE dragon, and the matcher
172status is constant on the dragon. Therefore, an additional test is performed:
173if the pattern contains a string which is tactically critical, and if @samp{*}
174does not rescue it, the pattern is rejected.
175@end quotation
176@item @samp{o}
177@quotation
178It is checked that every friendly (@samp{O}) stone of the pattern
179belongs to a dragon which is classified as @code{DEAD} or @code{UNKNOWN}.
180@end quotation
181@item @samp{X}
182@quotation
183It is checked that every opponent (@samp{X}) stone of the pattern
184belongs to a dragon with status @code{ALIVE}, @code{UNKNOWN} or
185@code{CRITICAL}. Note that there is an asymmetry with @samp{O} patterns, where
186@code{CRITICAL} dragons are rejected.
187@end quotation
188@item @samp{x}
189@quotation
190It is checked that every opponent (@samp{X}) stone of the pattern
191belongs to a dragon which is classified as @code{DEAD} or @code{UNKNOWN}
192@end quotation
193@end itemize
194
195@subsection Action Attributes
196
197@itemize
198@item @samp{C}
199@quotation
200If two or more distinct O dragons occur in the pattern, the
201move is given the move reasons that it connects each pair of
202dragons. An exception is made for dragons where the underlying
203worm can be tactically captured and is not defended by the
204considered move.
205@end quotation
206@item @samp{c}
207@quotation
208Add strategical defense move reason for all our
209dragons and a small shape bonus. This classification is
210appropriate for weak connection patterns.
211@end quotation
212@item @samp{B}
213@quotation
214If two or more distinct @samp{X} dragons occur in the pattern, the
215move is given the move reasons that it cuts each pair of
216dragons.
217@end quotation
218@item @samp{e}
219@quotation
220The move makes or secures territory.
221@end quotation
222@item @samp{E}
223@quotation
224The move attempts increase influence and create/expand a moyo.
225@end quotation
226@item @samp{d}
227@quotation
228The move strategically defends all O dragons in the pattern,
229except those that can be tactically captured and are not
230tactically defended by this move. If any O dragon should
231happen to be perfectly safe already, this only reflects in
232the move reason being valued to zero.
233@end quotation
234@item @samp{a}
235@quotation
236The move strategically attacks all @samp{X} dragons in the pattern.
237@end quotation
238@item @samp{J}
239@quotation
240Standard joseki move. Unless there is an urgent move on the board these moves
241are made as soon as they can be. This is equivalent to adding the @samp{d}
242and @samp{a} classifications together with a minimum accepted value of 27.
243@end quotation
244@item @samp{F}
245@quotation
246This indicates a fuseki pattern. This is only effective together with
247either the @samp{j} or @samp{t} classification, and is used to ensure
248indeterministic play during fuseki.
249@end quotation
250@item @samp{j}
251@quotation
252Slightly less urgent joseki move. These moves will be made after those
253with the @samp{J} classification. This adds the
254@samp{e} and @samp{E} classifications. If the move has the @samp{F}
255classification, it also gets a fixed value of 20.1, otherwise it
256gets a minimum accepted value of 20. (This makes sure that GNU Go chooses
257randomly between different moves that have @samp{jF} as classification.)
258@end quotation
259@item @samp{t}
260@quotation
261Minor joseki move (tenuki OK). This is equivalent to adding the @samp{e} and
262@samp{E} classifications together with either a fixed value of 15.07 (if
263the move has @samp{F} classification) or a minimum value of 15 (otherwise).
264@end quotation
265@item @samp{U}
266@quotation
267Urgent joseki move (never tenuki). This is equivalent to the @samp{d} and
268@samp{a} classifications together with a shape bonus of 15 and a minimum
269accepted value of 40.
270@end quotation
271@end itemize
272
273A commonly used class is @code{OX} (which rejects pattern if either side
274has dead stones). The string @samp{-} may be used as a placeholder. (In
275fact any characters other than the above and @samp{,} are ignored.)
276
277The types @samp{o} and @samp{O} could conceivably appear in a class, meaning it
278applies only to @code{UNKNOWN}. @samp{X} and @samp{x} could similarly be used together.
279All classes can be combined arbitrarily.
280
281@node Pattern Values
282@section Pattern Attributes
283
284The second and following fields in the @samp{:} line of a pattern
285are optional and of the form @code{value1(x),value2(y),...}. The available set
286of values are as follows.
287
288@itemize @bullet
289@item @code{terri(x)}
290@findex terri
291@quotation
292Forces the territorial value of the move to be at least x.
293@end quotation
294@item @code{minterri(x)}
295@findex minterri
296@quotation
297Forces the territorial value of the move to be at least x.
298@end quotation
299@item @code{maxterri(x)}
300@findex maxterri
301@quotation
302Forces the territorial value of the move to be at most x.
303@end quotation
304@item @code{value(x)}
305@findex value
306@quotation
307Forces the final value of the move to be at least x.
308@end quotation
309@item @code{minvalue(x)}, @code{maxvalue(x)}
310@findex minvalue
311@findex maxvalue
312@quotation
313Forces the final value of the move to be at least/most x.
314@end quotation
315@item @code{shape(x)}
316@findex shape
317@quotation
318Adds x to the move's shape value.
319@end quotation
320@item @code{followup(x)}
321@findex followup
322@quotation
323Adds x to the move's followup value.
324@end quotation
325@end itemize
326
327The meaning of these values is documented in @xref{Move Generation}.
328
329@node Helper Functions
330@section Helper Functions
331
332@cindex helper functions in pattern matching
333
334Helper functions can be provided to assist the matcher in deciding
335whether to accept a pattern, register move reasons, and setting
336various move values. The helper is supplied with the compiled pattern
337entry in the table, and the (absolute) position on the board of the
338@samp{*} point.
339
340One difficulty is that the helper must be able to cope with all the
341possible transformations of the pattern. To help with this, the OFFSET
342macro is used to transform relative pattern coordinates to absolute
343board locations.
344
345The actual helper functions are in @file{helpers.c}. They are declared
346in @file{patterns.h}.
347
348As an example to show how to write a helper function, we consider
349a hypothetical helper called @code{wedge_helper}. Such a helper
350used to exist, but has been replaced by a constraint. Due to
351its simplicity it's still a good example. The helper begins with a
352comment:
353
354@example
355/*
356
357?O. ?Ob
358.X* aX*
359?O. ?Oc
360
361:8,C,wedge_helper
362*/
363@end example
364
365The image on the left is the actual pattern. On the right we've taken
366this image and added letters to label @code{apos}, @code{bpos}, and
367@code{cpos}. The position of *, the point where GNU Go will move if the
368pattern is adopted, is passed through the parameter @code{move}.
369
370@example
371int
372wedge_helper(ARGS)
373@{
374 int apos, bpos, cpos;
375 int other = OTHER_COLOR(color);
376 int success = 0;
377
378 apos = OFFSET(0, -2);
379 bpos = OFFSET(-1, 0);
380 cpos = OFFSET(1, 0);
381
382 if (TRYMOVE(move, color)) @{
383 if (TRYMOVE(apos, other)) @{
384 if (board[apos] == EMPTY || attack(apos, NULL))
385 success = 1;
386 else if (TRYMOVE(bpos, color)) @{
387 if (!safe_move(cpos, other))
388 success = 1;
389 popgo();
390 @}
391 popgo();
392 @}
393 popgo();
394 @}
395
396 return success;
397@}
398
399@end example
400
401The @code{OFFSET} lines tell GNU Go the positions of the three stones at
402@samp{a}, @samp{b}, and @samp{c}. To decide whether the pattern
403guarantees a connection, we do some reading. First we use the
404@code{TRYMOVE} macro to place an @samp{O} at @samp{move} and let
405@samp{X} draw back to @samp{a}. Then we ask whether @samp{O} can capture
406these stones by calling @code{attack()}. The test if there is a stone at
407@samp{a} before calling @code{attack()} is in this position not really
408necessary but it's good practice to do so, because if the attacked stone
409should happen to already have been captured while placing stones, GNU Go
410would crash with an assertion failure.
411
412If this attack fails we let @samp{O} connect at @samp{b} and use the
413@code{safe_move()} function to examine whether a cut by @samp{X} at
414@samp{c} could be immediately captured. Before we return the result we
415need to remove the stones we placed from the reading stack. This is done
416with the function @code{popgo()}.
417
418@node Autohelpers and Constraints
419@section Autohelpers and Constraints
420
421@cindex generation of helper functions
422@cindex Autohelpers
423
424In addition to the hand-written helper functions in @file{helpers.c}, GNU Go
425can automatically generate helper functions from a diagram with labels
426and an expression describing a constraint. The constraint diagram,
427specifying the labels, is placed below the @samp{:} line and the constraint
428expression is placed below the diagram on line starting with a @samp{;}.
429Constraints can only be used to accept or reject a pattern. If the
430constraint evaluates to zero (false) the pattern is rejected,
431otherwise it's accepted (still conditioned on passing all other tests
432of course). To give a simple example we consider a connection pattern.
433
434@example
435
436Pattern Conn311
437
438O*.
439?XO
440
441:8,C,NULL
442
443O*a
444?BO
445
446;oplay_attack_either(*,a,a,B)
447
448@end example
449
450Here we have given the label @samp{a} to the empty spot to the right of
451the considered move and the label @samp{B} to the @samp{X} stone in the
452pattern. In addition to these, @samp{*} can also be used as a label. A
453label may be any lowercase or uppercase ascii letter except @code{OoXxt}. By
454convention we use uppercase letters for @samp{X} stones and lowercase for @samp{O}
455stones and empty intersections. When labeling a stone that's part of a
456larger string in the pattern, all stones of the string should be marked
457with the label. (These conventions are not enforced by the pattern
458compiler, but to make the database consistent and easy to read they
459should be followed.)
460
461The labels can now be used in the constraint expression. In this example
462we have a reading constraint which should be interpreted as "Play an
463@samp{O} stone at @samp{*} followed by an @samp{X} stone at
464@samp{a}. Accept the pattern if @samp{O} now can capture either at
465@samp{a} or at @samp{B} (or both strings)."
466
467The functions that are available for use in the constraints are listed
468in the section `Autohelpers Functions' below. Technically the
469constraint expression is transformed by mkpat into an automatically
470generated helper function in @file{patterns.c}. The functions in the
471constraint are replaced by C expressions, often functions calls. In
472principle any valid C code can be used in the constraints, but there
473is in practice no reason to use anything more than boolean and
474arithmetic operators in addition to the autohelper functions.
475Constraints can span multiple lines, which are then concatenated.
476
477@node Autohelper Actions
478@section Autohelper Actions
479
480@cindex autohelper actions
481
482As a complement to the constraints, which only can accept or reject a
483pattern, one can also specify an action to perform when the pattern
484has passed all tests and finally has been accepted.
485
486Example:
487
488@example
489
490Pattern EJ4
491
492...*. continuation
493.OOX.
494..XOX
495.....
496-----
497
498:8,Ed,NULL
499
500...*. never play a here
501.OOX.
502.aXOX
503.....
504-----
505
506>antisuji(a)
507
508@end example
509
510The line starting with @samp{>} is the action line. In this case it tells
511the move generation that the move at a should not be considered,
512whatever move reasons are found by other patterns. The action line
513uses the labels from the constraint diagram. Both constraint and
514action can be used in the same pattern. If the action only needs to
515refer to @samp{*}, no constraint diagram is required. Like constraints,
516actions can span multiple lines.
517
518Here is a partial list of the autohelper macros which are typically
519called from action lines. This list is not complete. If you cannot find an
520autohelper macro in an action line in this list, consult @file{mkpat.c} to
521find out what function in the engine is actually called. If no
522macro exists which does what you want, you can add macros by
523editing the list in @file{mkpat.c}.
524
525@itemize
526@item @code{antisuji(a);}
527@quotation
528Mark @samp{a} as an antisuji, that is, a move that must never
529be played.
530@end quotation
531@item @code{replace(a,*)}
532@quotation
533This is appropriate if the move at @samp{*} is definitely better
534than the move at @samp{a}. The macro adds a point redistribution rule. Any
535points which are assigned to @samp{a} during the move generation
536by any move reason are redistributed to @samp{*}.
537@end quotation
538@item @code{prevent_attack_threat(a)}
539@quotation
540Add a reverse followup value because the opponent's move here
541would threaten to capture @samp{a}.
542@end quotation
543@item @code{threaten_to_save(a)}
544@quotation
545Add a followup value because the move at @samp{*} threatens to
546rescue the dead string at @samp{a}.
547@end quotation
548@item @code{defend_against_atari(a)}
549@quotation
550Estimate the value of defending the string @samp{a} which can be put into
551atari and add this as a reverse followup value.
552@end quotation
553@item @code{add_defend_both_move(a,b);}
554@quotation
555@end quotation
556@item @code{add_cut_move(c,d);}
557@quotation
558Add to the move reasons that the move at @samp{*} threatens to cut
559@samp{c} and @samp{d}.
560@end quotation
561@end itemize
562
563@node Autohelper Functions
564@section Autohelper Functions
565
566The autohelper functions are translated into C code by the program in
567@file{mkpat.c}. To see exactly how the functions are implemented,
568consult the autohelper function definitions in that file. Autohelper
569functions can be used in both constraint and action lines.
570
571@example
572
573@code{lib(x)}
574@code{lib2(x)}
575@code{lib3(x)}
576@code{lib4(x)}
577
578@end example
579
580Number of first, second, third, and fourth order liberties of a worm
581respectively. @xref{Worms and Dragons}, the documentation on worms for
582definitions.
583
584@example
585
586@code{xlib(x)}
587@code{olib(x)}
588
589@end example
590
591The number of liberties that an enemy or own stone, respectively,
592would obtain if played at the empty intersection @samp{x}.
593
594@example
595@code{xcut(x)}
596@code{ocut(x)}
597@end example
598
599Calls @code{cut_possible} (@pxref{General Utilities}) to determine
600whether @samp{X} or @samp{O} can cut at the empty intersection @samp{x}.
601
602@example
603@code{ko(x)}
604@end example
605
606True if @samp{x} is either a stone or an empty point involved in a ko
607position.
608
609@example
610@code{status(x)}
611@end example
612
613The matcher status of a dragon. status(x) returns an integer that can have the
614values @code{ALIVE}, @code{UNKNOWN}, @code{CRITICAL}, or @code{DEAD}
615(@pxref{Worms and Dragons}).
616
617@example
618@code{alive(x)}
619@code{unknown(x)}
620@code{critical(x)}
621@code{dead(x)}
622@end example
623
624Each function true if the dragon has the corresponding matcher status and
625false otherwise (@pxref{Worms and Dragons}).
626
627@example
628@code{status(x)}
629@end example
630
631Returns the status of the dragon at @samp{x} (@pxref{Worms and Dragons}).
632
633@example
634@code{genus(x)}
635@end example
636
637The number of eyes of a dragon. It is only meaningful to compare this
638value against 0, 1, or 2.
639
640@example
641
642@code{xarea(x)}
643@code{oarea(x)}
644@code{xmoyo(x)}
645@code{omoyo(x)}
646@code{xterri(x)}
647@code{oterri(x)}
648
649@end example
650
651These functions call @code{whose_territory()}, @code{whose_moyo()}
652and @code{whose_area()} (@pxref{Territory and Moyo}). For example
653@code{xarea(x)} evaluates to true if @samp{x} is either a living enemy stone
654or an empty point within the opponent's ``area''. The function @code{oarea(x)}
655is analogous but with respect to our stones and area. The main difference
656between area, moyo, and terri is that area is a very far reaching kind of
657influence, moyo gives a more realistic estimate of what may turn in to
658territory, and terri gives the points that already are believed to be secure
659territory.
660
661@example
662@code{weak(x)}
663@end example
664
665True for a dragon that is perceived as weak.
666
667@example
668
669@code{attack(x)}
670@code{defend(x)}
671
672@end example
673
674Results of tactical reading. @code{attack(x)} is true if the worm can be
675captured, @code{defend(x)} is true if there also is a defending move. Please
676notice that @code{defend(x)} will return false if there is no attack on the
677worm.
678
679@example
680
681@code{safe_xmove(x)}
682@code{safe_omove(x)}
683
684@end example
685
686True if an enemy or friendly stone, respectively, can safely be played at
687@samp{x}. By safe it is understood that the move is legal and that it cannot
688be captured right away.
689
690@example
691
692@code{legal_xmove(x)}
693@code{legal_omove(x)}
694
695@end example
696
697True if an enemy or friendly stone, respectively, can legally be played at x.
698
699@example
700
701o_somewhere(x,y,z, ...)
702x_somewhere(x,y,z, ...)
703
704@end example
705
706True if O (respectively X) has a stone at one of the labelled vertices.
707In the diagram, these vertices should be marked with a @samp{?}.
708
709@example
710
711odefend_against(x,y)
712xdefend_against(x,y)
713
714@end example
715
716True if an own stone at @samp{x} would stop the enemy from safely playing at
717@samp{y}, and conversely for the second function.
718
719@example
720
721@code{does_defend(x,y)}
722@code{does_attack(x,y)}
723
724@end example
725
726True if a move at @samp{x} defends/attacks the worm at @samp{y}. For
727defense a move of the same color as @samp{y} is tried and for attack a move of
728the opposite color.
729
730@example
731
732@code{xplay_defend(a,b,c,...,z)}
733@code{oplay_defend(a,b,c,...,z)}
734@code{xplay_attack(a,b,c,...,z)}
735@code{oplay_attack(a,b,c,...,z)}
736
737@end example
738
739These functions make it possible to do more complex reading
740experiments in the constraints. All of them work so that first the
741sequence of moves @samp{a},@samp{b},@samp{c},... is played through with
742alternating colors, starting with @samp{X} or @samp{O} as indicated by
743the name. Then it is tested whether the worm at @samp{z} can be attacked or
744defended, respectively. It doesn't matter who would be in turn to move,
745a worm of either color may be attacked or defended. For attacks the
746opposite color of the string being attacked starts moving and for
747defense the same color starts. The defend functions return true if the
748worm cannot be attacked in the position or if it can be attacked but
749also defended. The attack functions return true if there is a way to
750capture the worm, whether or not it can also be defended. If there is no
751stone present at @samp{z} after the moves have been played, it is assumed that
752an attack has already been successful or a defense has already failed.
753If some of the moves should happen to be illegal, typically because it
754would have been suicide, the following moves are played as if nothing
755has happened and the attack or defense is tested as usual. It is assumed
756that this convention will give the relevant result without requiring a
757lot of special cases.
758
759The special label @samp{?} can be used to represent a tenuki.
760Thus @code{oplay_defend(a,?,b,c)} tries moves by @samp{O} at @samp{a} and
761@samp{b}, as if @samp{X} plays the second move in another part of
762the board, then asks if @samp{c} can be defended. The tenuki cannot
763be the first move of the sequence, nor does it need to be:
764instead of @code{oplay_defend(?,a,b,c)} you can use
765@code{xplay_defend(a,b,c)}.
766
767@example
768@code{xplay_defend_both(a,b,c,...,y,z)}
769@code{oplay_defend_both(a,b,c,...,y,z)}
770@code{xplay_attack_either(a,b,c,...,y,z)}
771@code{oplay_attack_either(a,b,c,...,y,z)}
772
773@end example
774
775These functions are similar to the previous ones. The difference is
776that the last *two* arguments denote worms to be attacked or defended
777simultaneously. Obviously @samp{y} and @samp{z} must have the same color. If either
778location is empty, it is assumed that an attack has been successful or
779a defense has failed. The typical use for these functions is in
780cutting patterns, where it usually suffices to capture either
781cutstone.
782
783The function @code{xplay_defend_both} plays alternate moves
784beginning with an @samp{X} at @samp{a}. Then it passes the last
785two arguments to @code{defend_both} in
786@file{engine/utils.c}. This function checks to determine
787whether the two strings can be simultaneously defended.
788
789The function @code{xplay_attack_either} plays alternate
790moves beginning with an @samp{X} move at @samp{a}. Then it passes
791the last two arguments to @code{attack_either} in
792@file{engine/utils.c}. This function looks for a move
793which captures at least one of the two strings. In its
794current implementation @code{attack_either} only looks
795for uncoordinated attacks and would thus miss a double
796atari.
797
798@example
799@code{xplay_connect(a,b,c,...,y,z)}
800@code{oplay_connect(a,b,c,...,y,z)}
801@code{xplay_disconnect(a,b,c,...,y,z)}
802@code{oplay_disconnect(a,b,c,...,y,z)}
803
804@end example
805
806The function @code{xplay_connect(a,b,c,...,y,z)} begins
807with an @samp{X} move at @samp{a}, then an @samp{O}
808move at @samp{b}, and so forth, then finally calls
809@code{string_connect()} to determine whether
810@samp{x} and @samp{y} can be connected. The other
811functions are similar (@pxref{Connection Reading}).
812
813@example
814
815@code{xplay_break_through(a,b,c,...,x,y,z)}
816@code{oplay_break_through(a,b,c,...,x,y,z)}
817
818@end example
819
820These functions are used to set up a position like
821
822@example
823
824.O. .y.
825OXO xXz
826
827@end example
828
829@noindent
830and @samp{X} aims at capturing at least one of @samp{x}, @samp{y}, and
831@samp{z}. If this succeeds @samp{1} is returned. If it doesn't, @samp{X}
832tries instead to cut through on either side and if this succeeds,
833@samp{2} is returned. Of course the same shape with opposite colors can
834also be used.
835
836Important notice: @samp{x}, @samp{y}, and @samp{z} must be given in the
837order they have in the diagram above, or any reflection and/or rotation
838of it.
839
840@example
841seki_helper(x)
842@end example
843
844Checks whether the string at @samp{x} can attack any surrounding
845string. If so, return false as the move to create a seki (probably)
846wouldn't work.
847
848@example
849threaten_to_save(x)
850@end example
851
852Calls @code{add_followup_value} to add as a move reason a conservative
853estimate of the value of saving the string @samp{x} by capturing one opponent
854stone.
855
856@example
857area_stone(x)
858@end example
859
860Returns the number of stones in the area around @samp{x}.
861
862@example
863area_space(x)
864@end example
865
866Returns the amount of space in the area around @samp{x}.
867
868@example
869@code{eye(x)}
870@code{proper_eye(x)}
871@code{marginal_eye(x)}
872@end example
873
874True if @samp{x} is an eye space for either color, a non-marginal eye space
875for either color, or a marginal eye space for either color,
876respectively.
877
878@example
879@code{antisuji(x)}
880@end example
881
882Tell the move generation that @samp{x} is a substandard move that never should
883be played.
884
885@example
886same_dragon(x,y)
887same_worm(x,y)
888@end example
889
890Return true if @samp{x} and @samp{y} are the same dragon or worm respectively.
891
892@example
893@code{dragonsize(x)}
894@code{wormsize(x)}
895@end example
896
897Number of stones in the indicated dragon or worm.
898
899@example
900@code{add_connect_move(x,y)}
901@code{add_cut_move(x,y)}
902@code{add_attack_either_move(x,y)}
903@code{add_defend_both_move(x,y)}
904@end example
905
906Explicitly notify the move generation about move reasons for the move
907in the pattern.
908
909@example
910@code{halfeye(x)}
911@end example
912
913Returns true if the empty intersection at @samp{x} is a half eye.
914
915@example
916@code{remove_attack(x)}
917@end example
918
919Inform the tactical reading that a supposed attack does in fact not
920work.
921
922@example
923@code{potential_cutstone(x)}
924@end example
925
926True if @code{cutstone2} field from worm data is larger than one. This
927indicates that saving the worm would introduce at least two new
928cutting points.
929
930@example
931@code{not_lunch(x,y)}
932@end example
933
934Prevents the misreporting of @samp{x} as lunch for @samp{y}.
935For example, the following pattern tells GNU Go that even
936though the stone at @samp{a} can be captured, it should not
937be considered ``lunch'' for the dragon at @samp{b}, because
938capturing it does not produce an eye:
939
940@example
941XO| ba|
942O*| O*|
943oo| oo|
944?o| ?o|
945
946> not_lunch(a,b)
947@end example
948
949@example
950@code{vital_chain(x)}
951@end example
952
953Calls @code{vital_chain} to determine whether capturing
954the stone at @samp{x} will result in one eye for an adjacent
955dragon. The current implementation just checks that the stone
956is not a singleton on the first line.
957
958@example
959@code{amalgamate(x,y)}
960@end example
961
962Amalgamate (join) the dragons at @samp{x} and @samp{y} (@pxref{Worms and Dragons}).
963
964@example
965@code{amalgamate_most_valuable(x,y,z)}
966@end example
967
968Called when @samp{x}, @samp{y}, @samp{z} point to three (preferably distinct)
969dragons, in situations such as this:
970
971@example
972
973.O.X
974X*OX
975.O.X
976
977@end example
978
979In this situation, the opponent can play at @samp{*}, preventing
980the three dragons from becoming connected. However @samp{O}
981can decide which cut to allow. The helper amalgamates the
982dragon at @samp{y} with either @samp{x} or @samp{z},
983whichever is largest.
984
985@example
986make_proper_eye(x)
987@end example
988
989This autohelper should be called when @samp{x} is an eyespace
990which is misidentified as marginal. It is reclassified as
991a proper eyespace (@pxref{Eye Space}).
992
993@example
994remove_halfeye(x)
995@end example
996
997Remove a half eye from the eyespace. This helper should not be run after
998@code{make_dragons} is finished, since by that time the eyespaces have
999already been analyzed.
1000
1001@example
1002remove_eyepoint(x)
1003@end example
1004
1005Remove an eye point. This function can only be used before the
1006segmentation into eyespaces.
1007
1008@example
1009@code{owl_topological_eye(x,y)}
1010@end example
1011
1012Here @samp{x} is an empty intersection which may be an
1013eye or half eye for some dragon, and @samp{y} is a
1014stone of the dragon, used only to determine the color
1015of the eyespace in question. Returns the sum of the values
1016of the diagonal intersections, relative to @samp{x}, as
1017explained in @xref{Eye Topology}, equal to 4 or more if the
1018eye at @samp{x} is false, 3 if it is a half eye, and 2 if it
1019is a true eye.
1020
1021@example
1022@code{owl_escape_value(x)}
1023@end example
1024
1025Returns the escape value at @samp{x}. This is only useful in owl
1026attack and defense patterns.
1027
1028@node Attack and Defense DB
1029@section Attack and Defense Database
1030
1031The patterns in @file{attack.db} and @file{defense.db} are used to assist the
1032tactical reading in finding moves that attacks or defends worms. The
1033matching is performed during @code{make_worms()}, at the time when the
1034tactical status of all worms is decided. None of the classes described
1035above are useful in these databases, instead we have two other
1036classes.
1037
1038@table @samp
1039@item D
1040For each @samp{O} worm in the pattern that can be tactically captured
1041(@code{worm[m][n].attack_code != 0}), the move at @samp{*} is
1042tried. If it is found to defend the stone, this is registered as a
1043reason for the move @samp{*} and the defense point of the worm is set to
1044@samp{*}.
1045@item A
1046For each @samp{X} worm in the pattern, it's tested whether the move
1047at @samp{*} captures the worm. If that is the case, this is
1048registered as a reason for the move at @samp{*}. The attack point of
1049the worm is set to @samp{*} and if it wasn't attacked before, a
1050defense is searched for.
1051@end table
1052
1053Furthermore, @samp{A} patterns can only be used in @file{attack.db} and
1054@samp{D} patterns only in @file{defense.db}. Unclassified patterns may
1055appear in these databases, but then they must work through actions to be
1056effective.
1057
1058@node Connections Database
1059@section The Connections Database
1060
1061@cindex connections database
1062@cindex connection shapes database
1063
1064The patterns in @file{conn.db} are used for helping @code{make_dragons()}
1065amalgamate worms into dragons and to some extent for modifying eye spaces.
1066The patterns in this database use the classifications @samp{B},
1067@samp{C}, and @samp{e}. @samp{B} patterns are used for finding cutting points,
1068where amalgamation should not be performed, @samp{C} patterns are used for
1069finding existing connections, over which amalgamation is to be done, and
1070@samp{e} patterns are used for modifying eye spaces and reevaluating lunches.
1071There are also some patterns without classification, which use action lines to
1072have an impact. These are matched together with the @samp{C} patterns. Further
1073details and examples can be found in @xref{Worms and Dragons}.
1074
1075We will illustrate these databases by example. In this situation:
1076
1077@example
1078XOO
1079O.O
1080...
1081@end example
1082@noindent
1083@samp{X} cannot play safely at the cutting point, so the @samp{O} dragons
1084are to be amalgamated. Two patterns are matched here:
1085
1086@example
1087Pattern CC204
1088
1089O
1090.
1091O
1092
1093:+,C
1094
1095O
1096A
1097O
1098
1099;!safe_xmove(A) && !ko(A) && !xcut(A)
1100
1101Pattern CC205
1102
1103XO
1104O.
1105
1106:\,C
1107
1108AO
1109OB
1110
1111;attack(A) || (!safe_xmove(B) && !ko(B) && !xcut(B))
1112@end example
1113
1114The constraints are mostly clear. For example the second
1115pattern should not be matched if the @samp{X} stone cannot
1116be attacked and @samp{X} can play safely at @samp{B}, or
1117if @samp{B} is a ko. The constraint @code{!xcut(B)} means
1118that connection has not previously been inhibited by
1119@code{find_cuts}. For example consider this situation:
1120
1121@example
1122
1123OOXX
1124O.OX
1125X..O
1126X.OO
1127@end example
1128@noindent
1129The previous pattern is matched here twice, yet @samp{X} can push
1130in and break one of the connections. To fix this, we include
1131a pattern:
1132
1133@example
1134Pattern CB11
1135
1136?OX?
1137O!OX
1138?*!O
1139??O?
1140
1141:8,B
1142
1143?OA?
1144OaOB
1145?*bO
1146??O?
1147
1148; !attack(A) && !attack(B) && !xplay_attack(*,a,b,*) && !xplay_attack(*,b,a,*)
1149@end example
1150
1151After this pattern is found, the @code{xcut} autohelper macro will return
1152true at any of the points @samp{*}, @samp{a} and @samp{b}. Thus the
1153patterns @code{CB204} and @code{CB205} will not be matched, and the dragons will
1154not be amalgamated.
1155
1156@node Connection Functions
1157@section Connections Functions
1158
1159Here are the public functions in @file{connections.c}.
1160
1161@itemize @bullet
1162@item @code{static void cut_connect_callback(int m, int n, int color,
1163 struct pattern *pattern, int ll, void *data)}
1164@findex cut_connect_callback
1165@quotation
1166Try to match all (permutations of) connection patterns at @code{(m,n)}.
1167For each match, if it is a B pattern, set cutting point in worm
1168data structure and make eye space marginal for the connection
1169inhibiting entries of the pattern. If it is a @samp{C} pattern, amalgamate
1170the dragons in the pattern.
1171@end quotation
1172@item @code{void find_cuts(void)}
1173@findex find_cuts
1174@quotation
1175Find cutting points which should inhibit amalgamations and sever
1176the adjacent eye space. This goes through the connection database
1177consulting only patterns of type B. When such a function is found,
1178the function @code{cut_connect_callback} is invoked.
1179@end quotation
1180@item @code{void find_connections(void)}
1181@findex find_connections
1182@quotation
1183Find explicit connection patterns and amalgamate the involved dragons.
1184This goes through the connection database consulting patterns except those of
1185type B, E or e. When such a function is found, the function
1186@code{cut_connect_callback} is invoked.
1187@end quotation
1188@item void modify_eye_spaces1(void)
1189@findex modify_eye_spaces1
1190@quotation
1191Find explicit connection patterns and amalgamate the involved dragons.
1192This goes through the connection database consulting only patterns
1193of type E (@pxref{Connections Database}). When such a function is found, the
1194function @code{cut_connect_callback} is invoked.
1195@end quotation
1196@item void modify_eye_spaces1(void)
1197@findex modify_eye_spaces1
1198@quotation
1199Find explicit connection patterns and amalgamate the involved dragons.
1200This goes through the connection database consulting only patterns
1201of type e (@pxref{Connections Database}). When such a function is found, the
1202function @code{cut_connect_callback} is invoked.
1203@end quotation
1204@end itemize
1205
1206@node Tuning
1207@section Tuning the Pattern databases
1208
1209@cindex tuning the pattern database
1210@cindex tuning the shapes database
1211
1212Since the pattern databases, together with the valuation of move
1213reasons, decide GNU Go's personality, much time can be devoted to
1214``tuning'' them. Here are some suggestions.
1215
1216If you want to experiment with modifying the pattern database, invoke
1217with the @option{-a} option. This will cause every pattern to be evaluated,
1218even when some of them may be skipped due to various optimizations.
1219
1220You can obtain a Smart Game Format (SGF) record of your game in at least
1221two different ways. One is to use CGoban to record the game. You can
1222also have GNU Go record the game in Smart Game Format, using the @option{-o}
1223option. It is best to combine this with @option{-a}. Do not try to read the SGF
1224file until the game is finished and you have closed the game
1225window. This does not mean that you have to play the game out to its
1226conclusion. You may close the CGoban window on the game and GNU Go
1227will close the SGF file so that you can read it.
1228
1229If you record a game in SGF form using the @option{-o} option, GNU Go will add
1230labels to the board to show all the moves it considered, with their
1231values. This is an extremely useful feature, since one can see at a
1232glance whether the right moves with appropriate weights are being
1233proposed by the move generation.
1234
1235First, due to a bug of unknown nature, it occasionally happens
1236that GNU Go will not receive the @code{SIGTERM} signal from CGoban that it
1237needs to know that the game is over. When this happens, the SGF file
1238ends without a closing parenthesis, and CGoban will not open the
1239file. You can fix the file by typing:
1240
1241@example
1242
1243 echo ")" >>[filename]
1244
1245@end example
1246
1247@noindent
1248at the command line to add this closing parenthesis. Or you could
1249add the ) using an editor.
1250
1251Move values exceeding 99 (these should be rare) can be displayed by
1252CGoban but you may have to resize the window in order to see all three
1253digits. Grab the lower right margin of the CGoban window and pull it
1254until the window is large. All three digits should be visible.
1255
1256If you are playing a game without the @option{-o} option and you wish to
1257analyze a move, you may still use CGoban's ``Save Game'' button to get
1258an SGF file. It will not have the values of the moves labelled, of
1259course.
1260
1261Once you have a game saved in SGF format, you can analyze any
1262particular move by running:
1263
1264@example
1265
1266 gnugo -l [filename] -L [move number] -t -a -w
1267
1268@end example
1269
1270@noindent
1271to see why GNU Go made that move, and if you make changes to the
1272pattern database and recompile the program, you may ask GNU Go to
1273repeat the move to see how the behavior changes. If you're using
1274emacs, it's a good idea to run GNU Go in a shell in a buffer (M-x
1275shell) since this gives good navigation and search facilities.
1276
1277Instead of a move number, you can also give a board coordinate to @option{-L}
1278in order to stop at the first move played at this location. If you
1279omit the @option{-L} option, the move after those in the file will be
1280considered.
1281
1282If a bad move is proposed, this can have several reasons. To begin
1283with, each move should be valued in terms of actual points on the
1284board, as accurately as can be expected by the program. If it's not,
1285something is wrong. This may have two reasons. One possibility is that
1286there are reasons missing for the move or that bogus reasons have been
1287found. The other possibility is that the move reasons have been
1288misevaluated by the move valuation functions. Tuning of patterns is
1289with a few exceptions a question of fixing the first kind of problems.
1290
1291If there are bogus move reasons found, search through the trace output
1292for the pattern that is responsible. (Some move reasons, e.g. most
1293tactical attack and defense, do not originate from patterns. If no
1294pattern produced the bogus move reason, it is not a tuning problem.)
1295Probably this pattern was too general or had a faulty constraint. Try
1296to make it more specific or correct bugs if there were any. If the
1297pattern and the constraint looks right, verify that the tactical
1298reading evaluates the constraint correctly. If not, this is either a
1299reading bug or a case where the reading is too complicated for GNU Go.
1300
1301If a connecting move reason is found, but the strings are already
1302effectively connected, there may be missing patterns in @file{conn.db}.
1303Similarly, worms may be incorrectly amalgamated due to some too
1304general or faulty pattern in @file{conn.db}. To get trace output from the
1305matching of patterns in @file{conn.db} you need to add a second
1306@option{-t} option.
1307
1308If a move reason is missing, there may be a hole in the database. It
1309could also be caused by some existing pattern being needlessly
1310specific, having a faulty constraint, or being rejected due to a
1311reading mistake. Unless you are familiar with the pattern databases,
1312it may be hard to verify that there really is a pattern missing. Look
1313around the databases to try to get a feeling for how they are
1314organized. (This is admittedly a weak point of the pattern databases,
1315but the goal is to make them more organized with time.) If you decide
1316that a new pattern is needed, try to make it as general as possible,
1317without allowing incorrect matches, by using proper classification
1318from among snOoXx and constraints. The reading functions can be put to
1319good use. The reason for making the patterns as general as they can be
1320is that we need a smaller number of them then, which makes the
1321database much easier to maintain. Of course, if you need too
1322complicated constraints, it's usually better to split the pattern.
1323
1324If a move has the correct set of reasons but still is misevaluated,
1325this is usually not a tuning problem. There are, however, some
1326possibilities to work around these mistakes with the use of patterns.
1327In particular, if the territorial value is off because @code{delta_terri()}
1328give strange results, the (min)terri and maxterri values can be set by
1329patterns as a workaround. This is typically done by the endgame
1330patterns, where we can know the (minimum) value fairly well from the
1331pattern. If it should be needed, (min)value and maxvalue can be used
1332similarly. These possibilities should be used conservatively though,
1333since such patterns are likely to become obsolete when better (or at
1334least different) functions for e.g. territory estimation are being
1335developed.
1336
1337In order to choose between moves with the same move reasons, e.g.
1338moves that connect two dragons in different ways, patterns with a
1339nonzero shape value should be used. These should give positive shape
1340values for moves that give good shape or good aji and negative values
1341for bad shape and bad aji. Notice that these values are additive, so
1342it's important that the matches are unique.
1343
1344Sente moves are indicated by the use of the pattern followup value.
1345This can usually not be estimated very accurately, but a good rule is
1346to be rather conservative. As usual it should be measured in terms of
1347actual points on the board. These values are also additive so the same
1348care must be taken to avoid unintended multiple matches.
1349
1350You can also get a visual display of the dragons using the @option{-T}
1351option. The default GNU Go configuration tries to build a
1352version with color support using either curses or the
1353ansi escape sequences. You are more likely to find color
1354support in rxvt than xterm, at least on many systems, so
1355we recommend running:
1356
1357@example
1358 gnugo -l [filename] -L [move number] -T
1359@end example
1360
1361@noindent
1362in an rxvt window. If you do not see a color display,
1363and if your host is a GNU/Linux machine, try this again
1364in the Linux console.
1365
1366Worms belonging to the same dragon are labelled with the same letters.
1367The colors indicate the value of the field @code{dragon.safety}, which
1368is set in @file{moyo.c}.
1369
1370@format
1371Green: GNU Go thinks the dragon is alive
1372Yellow: Status unknown
1373Blue: GNU Go thinks the dragon is dead
1374Red: Status critical (1.5 eyes) or weak by the algorithm
1375 in @file{moyo.c}
1376@end format
1377
1378@cindex eliminate the randomness
1379
1380If you want to get the same game over and over again, you can
1381eliminate the randomness in GNU Go's play by providing a fixed
1382random seed with the @option{-r} option.
1383
1384
1385@node PM Implementation
1386@section Implementation
1387
1388@cindex implementation of pattern matching
1389
1390The pattern code in GNU Go is fairly straightforward conceptually, but
1391because the matcher consumes a significant part of the time in
1392choosing a move, the code is optimized for speed. Because of this
1393there are implementation details which obscure things slightly.
1394
1395
1396In GNU Go, the ascii @file{.db} files are precompiled into tables (see
1397@file{patterns.h}) by a standalone program @file{mkpat.c}, and the resulting
1398@file{.c} files are compiled and linked into the main GNU Go executable.
1399
1400Each pattern is compiled to a header, and a sequence of elements,
1401which are (notionally) checked sequentially at every position and
1402orientation of the board. These elements are relative to the pattern
1403'anchor' (or origin). One @samp{X} or @samp{O} stone is (arbitrarily) chosen to
1404represent the origin of the pattern. (We cannot dictate one or the
1405other since some patterns contain only one colour or the other.) All
1406the elements are in co-ordinates relative to this position. So a
1407pattern matches "at" board position @code{(m,n,o)} if the the pattern anchor
1408stone is on @code{(m,n)}, and the other elements match the board when the
1409pattern is transformed by transformation number @samp{o}. (See below for
1410the details of the transformations, though these should not be
1411necessary)
1412
1413
1414@node Symmetry & transformations, Details, PM Implementation, Patterns
1415@section Symmetry and transformations
1416
1417@cindex symmetry and transformations
1418@cindex symmetry and transformations of shapes
1419
1420In general, each pattern must be tried in each of 8 different
1421permutations, to reflect the symmetry of the board. But some
1422patterns have symmetries which mean that it is unnecessary
1423(and therefore inefficient) to try all eight. The first
1424character after the @samp{:} can be one of @samp{8},@samp{|},@samp{\},@samp{/},
1425@samp{X}, @samp{-}, @samp{+}, representing the axes of symmetry. It can also
1426be @samp{O}, representing symmetry under 180 degrees rotation.
1427
1428@format
1429transformation I - | . \ l r /
1430 ABC GHI CBA IHG ADG CFI GDA IFC
1431 DEF DEF FED FED BEH BEH HEB HEB
1432 GHI ABC IHG CBA CFI ADG IFC GDA
1433
1434 a b c d e f g h
1435@end format
1436
1437Then if the pattern has the following symmetries, the
1438following are true:
1439
1440@example
1441
1442| c=a, d=b, g=e, h=f
1443- b=a, c=d, e=f, g=h
1444\ e=a, g=b, f=c, h=d
1445/ h=a, f=b, g=c, e=d
1446O a=d, b=c, e=h, f=g
1447X a=d=e=h, b=c=f=g
1448+ a=b=c=d, e=f=g=h
1449
1450@end example
1451
1452We can choose to use transformations a,d,f,g as the unique
1453transformations for patterns with either @samp{|}, @samp{-}, @samp{\}, or
1454@samp{/} symmetry.
1455
1456Thus we choose to order the transformations a,g,d,f,h,b,e,c and choose
1457first 2 for @samp{X} and @samp{+}, the first 4 for @samp{|}, @samp{-},
1458@samp{/}, and @samp{\}, the middle 4 for @samp{O}, and all 8 for
1459non-symmetrical patterns.
1460
1461Each of the reflection operations (e-h) is equivalent to reflection
1462about one arbitrary axis followed by one of the rotations (a-d). We
1463can choose to reflect about the axis of symmetry (which causes no
1464net change) and can therefore conclude that each of e-h is
1465equivalent to the reflection (no-op) followed by a-d. This argument
1466therefore extends to include @samp{-} and @samp{/} as well as @samp{|}
1467and @samp{\}.
1468
1469@node Details
1470@section Implementation Details
1471
1472@cindex pattern matching optimization
1473@cindex grid optimization
1474
1475@enumerate
1476@item An entry in the pattern header states whether the anchor is an @samp{X} or
1477an @samp{O}. This helps performance, since all transformations can be
1478rejected at once if the anchor stone does not match. (Ideally, we
1479could just define that the anchor is always @samp{O} or always @samp{X}, but some
1480patterns contain no @samp{O} and some contain no @samp{X}.)
1481
1482@item The pattern header contains the size of the pattern (ie the
1483co-ordinates of the top left and bottom right elements) relative to
1484the anchor. This allows the pattern can be rejected quickly if there
1485is not room for the pattern to fit around the anchor stone in a given
1486orientation (ie it is too near the edge of the board). The bounding
1487box information must first be transformed like the elements before it
1488can be tested, and after transforming, we need to work out where the
1489top-left and bottom-right corners are.
1490
1491@item The edge constraints are implemented by notionally padding the
1492pattern with rows or columns of @samp{?} until it is exactly 19 (or
1493whatever the current board size is) elements wide or high. Then the
1494pattern is quickly rejected by (ii) above if it is not at the edge. So
1495the example pattern above is compiled as if it was written
1496
1497
1498@example
1499
1500"example"
1501.OO????????????????
1502*XX????????????????
1503o??????????????????
1504:8,80
1505
1506@end example
1507
1508@item The elements in a pattern are sorted so that non-space
1509elements are checked before space elements. It is hoped that,
1510for most of the game, more squares are empty, and so the
1511pattern can be more quickly rejected doing it this way.
1512
1513@item The actual tests are performed using an 'and-compare'
1514sequence. Each board position is a 2-bit quantity.
1515%00 for empty, %01 for @samp{O}, %10 for @samp{X}.
1516We can test for an exact match by and-ing with %11 (no-op),
1517then comparing with 0, 1 or 2. The test for @samp{o} is the
1518same as a test for 'not-X', ie not %10. So and with %01
1519should give 0 if it matches. Similarly @samp{x} is a test that
1520bit 0 is not set.
1521
1522@end enumerate
1523
1524@node Grid optimization
1525@section The ``Grid'' Optimization
1526
1527The comparisons between pattern and board are performed as 2-bit
1528bitwise operations. Therefore they can be performed in parallel,
152916-at-a-time on a 32-bit machine.
1530
1531Suppose the board is layed out as follows :
1532
1533@example
1534
1535 .X.O....OO
1536 XXXXO.....
1537 .X..OOOOOO
1538 X.X.......
1539 ....X...O.
1540
1541@end example
1542
1543@noindent
1544which is internally stored internally in a 2d array (binary)
1545
1546@example
1547
1548 00 10 00 01 00 00 00 00 01 01
1549 10 10 10 10 01 00 00 00 00 00
1550 00 10 00 00 01 01 01 01 01 01
1551 10 00 10 00 00 00 00 00 00 00
1552 00 00 00 00 10 00 00 00 01 00
1553
1554@end example
1555
1556@noindent
1557we can compile this to a composite array in which each element
1558stores the state of a 4x4 grid of squares :
1559
1560@example
1561
1562 ???????? ???????? ???????? ...
1563 ??001000 00100001 10000100
1564 ??101010 10101010 10101001
1565 ??001000 00100000 10000001
1566
1567 ??001000 00100001 ...
1568 ??101010 10101010
1569 ??001000 00100000
1570 ??001000 10001000
1571
1572...
1573
1574 ??100010 ...
1575 ??000000
1576 ????????
1577 ????????
1578
1579@end example
1580
1581Where '??' is off the board.
1582
1583We can store these 32-bit composites in a 2d merged-board array,
1584substituting the illegal value %11 for '??'.
1585
1586Similarly, for each pattern, mkpat produces appropriate 32-bit and-value
1587masks for the pattern elements near the anchor. It is a simple matter
1588to test the pattern with a similar test to (5) above, but for 32-bits
1589at a time.
1590
1591@node Joseki Compiler
1592@section The Joseki Compiler
1593
1594@cindex joseki
1595@cindex how GNU Go learns new joseki
1596@cindex the joseki compiler
1597@cindex teaching josekis to GNU Go
1598
1599GNU Go includes a joseki compiler in @file{patterns/joseki.c}. This processes
1600an SGF file (with variations) and produces a sequence of patterns
1601which can then be fed back into mkpat. The joseki database is currently in files
1602in @file{patterns/} called @file{hoshi.sgf}, @file{komoku.sgf}, @file{sansan.sgf},
1603@file{mokuhazushi.sgf} and @file{takamoku.sgf}. This division can be revised
1604whenever need arises.
1605
1606The SGF files are transformed into the pattern database @file{.db} format by
1607the program in @file{joseki.c}. These files are in turn transformed into C
1608code by the program in @file{mkpat.c} and the C files are compiled and linked
1609into the GNU Go binary.
1610
1611Not every node in the SGF file contributes a pattern. The nodes which
1612contribute patterns have the joseki in the upper right corner, with
1613the boundary marked with a square mark and other information to determine
1614the resulting pattern marked in the comments.
1615
1616The intention is that the move valuation should be able to choose
1617between the available variations by normal valuation. When this fails
1618the primary workaround is to use shape values to increase or decrease
1619the value. It is also possible to add antisuji variations to forbid
1620popular suboptimal moves. As usual constraints can be used, e.g. to
1621condition a variation on a working ladder.
1622
1623The joseki format has the following components for each SGF node:
1624
1625@itemize @bullet
1626@item
1627A square mark (@code{SQ} or @code{MA} property) to decide how large part of the
1628board should be included in the pattern.
1629@item A move (@samp{W} or @samp{B} property) with the natural interpretation.
1630If the square mark is missing or the move is a pass, no pattern is
1631produced for the node.
1632@item Optional labels (@code{LB} property), which must be a single letter each.
1633If there is at least one label, a constraint diagram will be
1634produced with these labels.
1635@item A comment (@samp{C} property). As the first character it should have one of the
1636following characters to decide its classification:
1637@itemize @minus
1638@item @samp{U} - urgent move
1639@item @samp{S} or @samp{J} - standard move
1640@item @samp{s} or @samp{j} - lesser joseki
1641@item @samp{T} - trick move
1642@item @samp{t} - minor joseki move (tenuki OK)
1643@item @samp{0} - antisuji (@samp{A} can also be used)
1644@end itemize
1645The rest of the line is ignored, as is the case of the letter. If neither of
1646these is found, it's assumed to be a standard joseki move.
1647
1648In addition to this, rows starting with the following characters are
1649recognized:
1650@itemize @minus
1651@item @samp{#} - Comments. These are copied into the patterns file, above the diagram.
1652@item @samp{;} - Constraints. These are copied into the patterns file, below the
1653constraint diagram.
1654@item @samp{>} - Actions. These are copied into the patterns file, below the
1655constraint diagram.
1656@item @samp{:} - Colon line. This is a little more complicated, but the colon
1657line of the produced patterns always start out with ":8,s" for
1658transformation number and sacrifice pattern class (it usually
1659isn't a sacrifice, but it's pointless spending time checking for
1660tactical safety). Then a joseki pattern class character is
1661appended and finally what is included on the colon line in the
1662comment for the SGF node.
1663@end itemize
1664@end itemize
1665
1666Example: If the comment in the SGF file looks like
1667
1668@example
1669F
1670:C,shape(3)
1671;xplay_attack(A,B,C,D,*)
1672@end example
1673
1674@noindent
1675the generated pattern will have a colon line
1676
1677@example
1678:8,sjC,shape(3)
1679@end example
1680
1681@noindent
1682and a constraint
1683
1684@example
1685;xplay_attack(A,B,C,D,*)
1686@end example
1687
1688@node Ladders in Joseki
1689@section Ladders in Joseki
1690
1691As an example of how to use autohelpers with the
1692Joseki compiler, we consider an example where a Joseki
1693is bad if a ladder fails. Assume we have the taisha and are
1694considering connecting on the outside with the pattern
1695
1696@example
1697--------+
1698........|
1699........|
1700...XX...|
1701...OXO..|
1702...*O...|
1703....X...|
1704........|
1705........|
1706@end example
1707
1708But this is bad unless we have a ladder in our favor. To check this we
1709add a constraint which may look like
1710
1711@example
1712--------+
1713........|
1714........|
1715...XX...|
1716...OXO..|
1717...*OAC.|
1718....DB..|
1719........|
1720........|
1721
1722;oplay_attack(*,A,B,C,D)
1723@end example
1724
1725In order to accept the pattern we require that the constraint on the
1726semicolon line evaluates to true. This particular constraint has the
1727interpretation "Play with alternating colors, starting with @samp{O},
1728on the intersections @samp{*}, @samp{A}, @samp{B}, and @samp{C}. Then check
1729whether the stone at @samp{D} can be captured." I.e. play to this position
1730
1731@example
1732--------+
1733........|
1734........|
1735...XX...|
1736...OXO..|
1737...OOXX.|
1738....XO..|
1739........|
1740........|
1741@end example
1742
1743@noindent
1744and call @code{attack()} to see whether the lower @samp{X} stone can be
1745captured. This is not limited to ladders, but in this particular case the
1746reading will of course involve a ladder.
1747
1748The constraint diagram above with letters is how it looks in the @file{.db}
1749file. The joseki compiler knows how to create these from labels in
1750the SGF node. @file{Cgoban} has an option to create one letter labels,
1751but this ought to be a common feature for SGF editors.
1752
1753Thus in order to implement this example in SGF, one would add labels
1754to the four intersections and a comment:
1755
1756@example
1757;oplay_attack(*,A,B,C,D)
1758@end example
1759
1760The appropriate constraint (autohelper macro) will then be added
1761to the Joseki @file{.db} file.
1762
1763@node Corner Matcher
1764@section Corner Matcher
1765
1766@cindex implementation of pattern matching
1767@cindex joseki
1768@cindex corner matcher
1769
1770GNU Go uses a special matcher for joseki patterns. It has certain constraints
1771on the patterns it can match, but is much faster and takes far less space to
1772store patterns than the standard matcher.
1773
1774Patterns used with corner matcher have to qualify the following conditions:
1775
1776@itemize @bullet
1777@item They must be matchable only at a corner of the board (hence the name of
1778the matcher).
1779@item They can consist only of @samp{O}, @samp{X} and @samp{.} elements.
1780@item Of all pattern values (@pxref{Pattern Values}), corner matcher only
1781support @code{shape(x)}. This is not because the matcher cannot handle other
1782values in principle, just they are currently not used in joseki databases.
1783@end itemize
1784
1785Corner matcher was specifically designed for joseki patterns and they of
1786course satisfy all the conditions above. With some modifications corner
1787matcher could be used for fuseki patterns as well, but fullboard matcher
1788does its work just fine.
1789
1790The main idea of the matcher is very same to the one of DFA matcher
1791(@pxref{Pattern matching with DFA}): check all available patterns at once,
1792not a single pattern at a time. A modified version of DFA matcher could be
1793used for joseki pattern matching, but its database would be very large.
1794Corner matcher capitalizes on the fact that there are relatively few
1795stones in each such pattern.
1796
1797Corner pattern database is organized into a tree. Nodes of the tree are
1798called ``variations''. Variations represent certain sets of stones in a
1799corner of the board. Root variation corresponds to an empty corner and a
1800step down the tree is equivalent to adding a stone to the corner. Each
1801variation has several properties:
1802
1803@itemize @minus
1804@item stone position relative to the corner,
1805@item a flag determining whether the stone color must be equal to the first
1806matched stone color,
1807@item number of stones in the corner area (see below) of the variation stone.
1808@end itemize
1809
1810By corner area we define a rectangle which corners are the current corner of
1811the board and the position of the stone (inclusive). For instance, if the
1812current board corner is A19 then corner area of a stone at C18 consists
1813of A18, A19, B18, B19, C18 and C19.
1814
1815Variation which is a direct child of the root variation matches if there
1816is any stone at the variation position and the stone is alone in its
1817corner area.
1818
1819Variation at a deeper level of the tree matches if there is a stone of
1820specified color in variation position and the number of stones in its
1821corner area is equal to the number specified in variation structure.
1822
1823When a certain variation matches, all its children has to be checked
1824recursively for a match.
1825
1826All leaf variations and some inner ones have patterns attached to them.
1827For a pattern to match, it is required that its @emph{parent} variation
1828matches. In addition, it is checked that pattern is being matched for
1829the appropriate color (using its variation ``stone color'' field) and
1830that the number of stones in the area where the pattern is being matched
1831is indeed equal to the number of stones in the pattern. The ``stone
1832position'' property of the pattern variation determines the move suggested
1833by the pattern.
1834
1835Consider this joseki pattern which has four stones:
1836
1837@example
1838------+
1839......|
1840......|
1841.O*...|
1842.XXO..|
1843......|
1844......|
1845@end example
1846
1847To encode it for the corner matcher, we have to use five variations,
1848each next being a child of previous:
1849
1850@multitable {Tree level} {Position} {``other''} {Number of stones}
1851@item Tree level @tab Position @tab Color @tab Number of stones
1852@item 1 @tab R16 @tab ``same'' @tab 1
1853@item 2 @tab P17 @tab ``same'' @tab 1
1854@item 3 @tab Q16 @tab ``other'' @tab 2
1855@item 4 @tab P16 @tab ``other'' @tab 4
1856@item 5 @tab Q17 @tab ``same'' @tab 1
1857@end multitable
1858
1859The fifth variation should have an attached pattern. Note that the stone
1860color for the fifth variation is ``same'' because the first matched stone
1861for this pattern is @samp{O} which stands for the stones of the player
1862to whom moves are being suggested with @samp{*}.
1863
1864The tree consists of all variations for all patterns combined together.
1865Variations for each patterns are sorted to allow very quick tree branch
1866rejection and at the same time keep the database small enough. More
1867details can be found in comments in file @file{mkpat.c}
1868
1869Corner matcher resides in @file{matchpat.c} in two functions:
1870@code{corner_matchpat()} and @code{do_corner_matchpat()}. The former computes
1871@code{num_stones[]} array which holds number of stones in corner areas of
1872different intersections of the board for all possible transformations.
1873@code{corner_matchpat()} also matches top-level variations.
1874@code{do_corner_matchpat()} is responsible for recursive matching on the
1875variation tree and calling callback function upon pattern match.
1876
1877Tree-like database for corner matcher is generated by @code{mkpat} program.
1878Database generator consists of several functions, most important are:
1879@code{corner_best_element()}, @code{corner_variation_new()},
1880@code{corner_follow_variation()} and @code{corner_add_pattern()}.
1881
1882@node Editing Patterns
1883@section Emacs Mode for Editing Patterns
1884
1885@cindex editing patterns
1886@cindex editing pattern database
1887
1888If you use GNU Emacs (XEmacs might work too), you can try a special
1889mode for editing GNU Go pattern databases. The mode resides in
1890@file{patterns/gnugo-db.el}.
1891
1892Copy the file to @file{emacs/site-lisp} directory. You can then load
1893the mode with @code{(require 'gnugo-db)}. It makes sense to put this
1894line into your configuration file (@file{~/.emacs}). You can either
1895use @command{gnugo-db-mode} command to switch to pattern editing mode,
1896or use the following code snippet to make Emacs do this automatically
1897upon opening a file with @file{.db} suffix:
1898
1899@example
1900 (setq auto-mode-alist
1901 (append
1902 auto-mode-alist
1903 '(("\\.db\\'" . gnugo-db-mode))))
1904@end example
1905
1906Pattern editing mode provides the following features:
1907
1908@itemize @minus
1909@item
1910highlighting of keywords (@code{Pattern}, @code{goal_elements} and
1911@code{callback_data}) and comments,
1912
1913@item
1914making paragraphs equal to patterns (@kbd{M-h}, @kbd{M-@{}, @kbd{M-@}}
1915and others operate on patterns),
1916
1917@item
1918commands for pattern creation with automatic name selection (@kbd{C-c
1919C-p}) and copying main diagram to constraint diagram (@kbd{C-c C-c}),
1920
1921@item
1922automated indentation of constraints and side comments (pattern
1923descriptions).
1924@end itemize