File: patterns.texi

package info (click to toggle)
gnugo 3.8-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 17,312 kB
  • ctags: 4,228
  • sloc: ansic: 56,439; perl: 3,771; lisp: 2,789; sh: 730; makefile: 700; python: 682; awk: 113; sed: 22
file content (1924 lines) | stat: -rw-r--r-- 64,940 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
@menu
* Patterns Overview::             Overview of the pattern database.
* Pattern Classification::        The classification field
* Pattern Values::                The value field
* Helper Functions::              Helper Functions
* Autohelpers and Constraints::   Automatic generation of helper functions.
* Autohelper Actions::            Autohelper Actions
* Autohelper Functions::          Autohelper Functions
* Attack and Defense DB::         The Attack and defense moves database.
* Connections Database::          The connection database.
* Connection Functions::          Functions in @file{connections.c}
* Tuning::                        Tuning the pattern database.
* PM Implementation::             Implementation.
* Symmetry & transformations::    Symmetry and transformations.
* Details::                       Details of implementation.
* Grid optimization::             The ``grid'' optimization.
* Joseki Compiler::               The joseki compiler.
* Ladders in Joseki::             Example: ladders in joseki.
* Corner Matcher::                A special matcher for joseki patterns.
* Editing Patterns::              Emacs major mode for pattern files.
@end menu

@node Patterns Overview
@section Overview

@cindex pattern overview
@cindex pattern matching
@cindex pattern database
@cindex eye shapes database
@cindex defence shapes database
@cindex attack shapes database
@cindex connection shapes database
@cindex pattern.h
@cindex pattern.c

Several pattern databases are in the patterns directory.  This chapter
primarily discusses the patterns in @file{patterns.db}, @file{patterns2.db},
and the pattern files @file{hoshi.db} etc.  which are compiled from the SGF
files @file{hoshi.sgf} (@pxref{Joseki Compiler}). There is no essential
difference between these files, except that the ones in @file{patterns.db} and
@file{patterns2.db} are hand written. They are concatenated before being
compiled by @code{mkpat} into @file{patterns.c}. The purpose of the separate
file @file{patterns2.db} is that it is handy to move patterns into a new
directory in the course of organizing them. The patterns in @file{patterns.db}
are more disorganized, and are slowly being moved to @file{patterns2.db}.

During the execution of @code{genmove()}, the patterns are matched in
@file{shapes.c} in order to find move reasons.

The same basic pattern format is used by @file{attack.db}, @file{defense.db},
@file{conn.db}, @file{apats.db} and @file{dpats.db}.  However these patterns
are used for different purposes. These databases are discussed in other parts
of this documentation. The patterns in @file{eyes.db} are entirely different
and are documented elsewhere (@pxref{Eyes}).

@cindex format of the pattern database
@cindex description of shapes
@cindex ascii description of shapes

The patterns described in the databases are ascii representations, of
the form:

Pattern EB112

@example

  ?X?.?       jump under
  O.*oo
  O....
  o....
  -----
  
  :8,ed,NULL
@end example

Here @samp{O} marks a friendly stone, @samp{X} marks an enemy stone, @samp{.} marks
an empty vertex, @samp{*} marks O's next move, @samp{o} marks a square either
containing @samp{O} or empty but not @samp{X}. (The symbol @samp{x}, which does not
appear in this pattern, means @samp{X} or @samp{.}.) Finally @samp{?} Indicates a
location where we don't care what is there, except that it cannot be
off the edge of the board.

The line of @samp{-}'s along the bottom in this example is the edge of the
board itself---this is an edge pattern. Corners can also be indicated.
Elements are not generated for @samp{?} markers, but they are not
completely ignored - see below.
        
The line beginning @samp{:} describes various attributes of the pattern, such
as its symmetry and its class. Optionally, a function called a
``helper'' can be provided to assist the matcher in deciding whether
to accept move. Most patterns do not require a helper, and this field
is filled with NULL.

@findex shapes_callback
The matcher in @file{matchpat.c} searches the board for places where this
layout appears on the board, and the callback function
@code{shapes_callback()} in @file{shapes.c} registers the appropriate move
reasons.

After the pattern, there is some supplementary information in the format:
@example

  :trfno, classification, [values], helper_function

@end example

Here trfno represents the number of transformations of the pattern to
consider, usually @samp{8} (no symmetry, for historical reasons), or one of 
@samp{|}, @samp{\}, @samp{/}, @samp{-}, @samp{+}, @samp{X}, where the line 
represents the axis of symmetry. (E.g. @samp{|} means symmetrical about a
vertical axis.)

The above pattern could equally well be written on the left edge:

@example

  |oOO?
  |...X
  |..*?
  |..o.
  |..o?

  :8,ed,NULL
@end example

The program @code{mkpat} is capable of parsing patterns written this
way, or for that matter, on the top or right edges, or in any
of the four corners. As a matter of convention all the edge patterns 
in @file{patterns.db} are written on the bottom edge or in the lower left
corners. In the @file{patterns/} directory there is a program called
@code{transpat} which can rotate or otherwise transpose patterns.
This program is not built by default---if you think you need it,
@code{make transpat} in the @file{patterns/} directory and
consult the usage remarks at the beginning of @file{patterns/transpat.c}.

@node  Pattern Classification
@section Pattern Attributes

@cindex pattern attributes
@cindex shape attributes

The attribute field in the @samp{:} line of a pattern consists of a sequence
of zero or more of the following characters, each with a different
meaning. The attributes may be roughly classified as @dfn{constraints},
which determine whether or not the pattern is matched, and @dfn{actions},
which describe what is to be done when the pattern is matched, typically
to add a move reason.

@subsection Constraint Pattern Attributes

@itemize
@item @samp{s}
@quotation
Safety of the move is not checked.  This is appropriate for sacrifice
patterns. If this classification is omitted, the matcher requires that the
stone played cannot be trivially captured. Even with s classification, a check
for legality is made, though.
@end quotation
@item @samp{n}
@quotation
In addition to usual check that the stone played cannot be
trivially captured, it is also confirmed that an opponent
move here could not be captured.
@end quotation
@item @samp{O}
@quotation
It is checked that every friendly (@samp{O}) stone of the pattern belongs to a
dragon which has status (@pxref{Dragons}) @code{ALIVE} or
@code{UNKNOWN}. The @code{CRITICAL} matcher status is excluded. It is possible
for a string to have @code{ALIVE} status and still be tactically
critical, since it might be amalgamated into an ALIVE dragon, and the matcher
status is constant on the dragon.  Therefore, an additional test is performed:
if the pattern contains a string which is tactically critical, and if @samp{*}
does not rescue it, the pattern is rejected.
@end quotation
@item @samp{o}
@quotation
It is checked that every friendly (@samp{O}) stone of the pattern
belongs to a dragon which is classified as @code{DEAD} or @code{UNKNOWN}.
@end quotation
@item @samp{X}
@quotation
It is checked that every opponent (@samp{X}) stone of the pattern
belongs to a dragon with status @code{ALIVE}, @code{UNKNOWN} or 
@code{CRITICAL}. Note that there is an asymmetry with @samp{O} patterns, where
@code{CRITICAL} dragons are rejected.
@end quotation
@item @samp{x}
@quotation
It is checked that every opponent (@samp{X}) stone of the pattern
belongs to a dragon which is classified as @code{DEAD} or @code{UNKNOWN}
@end quotation
@end itemize

@subsection Action Attributes

@itemize
@item @samp{C}
@quotation
If two or more distinct O dragons occur in the pattern, the
move is given the move reasons that it connects each pair of
dragons. An exception is made for dragons where the underlying
worm can be tactically captured and is not defended by the
considered move.
@end quotation
@item @samp{c}
@quotation
Add strategical defense move reason for all our
dragons and a small shape bonus. This classification is
appropriate for weak connection patterns.
@end quotation
@item @samp{B}
@quotation
If two or more distinct @samp{X} dragons occur in the pattern, the
move is given the move reasons that it cuts each pair of
dragons.
@end quotation
@item @samp{e}
@quotation
The move makes or secures territory.
@end quotation
@item @samp{E}
@quotation
The move attempts increase influence and create/expand a moyo.
@end quotation
@item @samp{d}
@quotation
The move strategically defends all O dragons in the pattern,
except those that can be tactically captured and are not
tactically defended by this move. If any O dragon should
happen to be perfectly safe already, this only reflects in
the move reason being valued to zero.
@end quotation
@item @samp{a}
@quotation
The move strategically attacks all @samp{X} dragons in the pattern.
@end quotation
@item @samp{J}
@quotation
Standard joseki move. Unless there is an urgent move on the board these moves
are made as soon as they can be.  This is equivalent to adding the @samp{d}
and @samp{a} classifications together with a minimum accepted value of 27.
@end quotation
@item @samp{F}
@quotation
This indicates a fuseki pattern. This is only effective together with
either the @samp{j} or @samp{t} classification, and is used to ensure
indeterministic play during fuseki.
@end quotation
@item @samp{j}
@quotation
Slightly less urgent joseki move. These moves will be made after those
with the @samp{J} classification. This adds the
@samp{e} and @samp{E} classifications. If the move has the @samp{F} 
classification, it also gets a fixed value of 20.1, otherwise it
gets a minimum accepted value of 20. (This makes sure that GNU Go chooses
randomly between different moves that have @samp{jF} as classification.)
@end quotation
@item @samp{t}
@quotation
Minor joseki move (tenuki OK). This is equivalent to adding the @samp{e} and
@samp{E} classifications together with either a fixed value of 15.07 (if
the move has @samp{F} classification) or a minimum value of 15 (otherwise).
@end quotation
@item @samp{U}
@quotation
Urgent joseki move (never tenuki). This is equivalent to the @samp{d} and
@samp{a} classifications together with a shape bonus of 15 and a minimum
accepted value of 40.  
@end quotation
@end itemize

A commonly used class is @code{OX} (which rejects pattern if either side
has dead stones). The string @samp{-} may be used as a placeholder. (In
fact any characters other than the above and @samp{,} are ignored.)

The types @samp{o} and @samp{O} could conceivably appear in a class, meaning it 
applies only to @code{UNKNOWN}. @samp{X} and @samp{x} could similarly be used together.
All classes can be combined arbitrarily.

@node  Pattern Values
@section Pattern Attributes

The second and following fields in the @samp{:} line of a pattern
are optional and of the form @code{value1(x),value2(y),...}. The available set
of values are as follows.

@itemize @bullet
@item @code{terri(x)}
@findex terri
@quotation
Forces the territorial value of the move to be at least x.
@end quotation
@item @code{minterri(x)}
@findex minterri
@quotation
Forces the territorial value of the move to be at least x.
@end quotation
@item @code{maxterri(x)}
@findex maxterri
@quotation
Forces the territorial value of the move to be at most x.
@end quotation
@item @code{value(x)}
@findex value
@quotation
Forces the final value of the move to be at least x.
@end quotation
@item @code{minvalue(x)}, @code{maxvalue(x)}
@findex minvalue
@findex maxvalue
@quotation
Forces the final value of the move to be at least/most x.
@end quotation
@item @code{shape(x)}
@findex shape
@quotation
Adds x to the move's shape value.
@end quotation
@item @code{followup(x)}
@findex followup
@quotation
Adds x to the move's followup value.
@end quotation
@end itemize

The meaning of these values is documented in @xref{Move Generation}.

@node  Helper Functions
@section Helper Functions

@cindex helper functions in pattern matching

Helper functions can be provided to assist the matcher in deciding
whether to accept a pattern, register move reasons, and setting
various move values. The helper is supplied with the compiled pattern
entry in the table, and the (absolute) position on the board of the
@samp{*} point.

One difficulty is that the helper must be able to cope with all the
possible transformations of the pattern. To help with this, the OFFSET
macro is used to transform relative pattern coordinates to absolute
board locations.

The actual helper functions are in @file{helpers.c}. They are declared
in @file{patterns.h}.

As an example to show how to write a helper function, we consider
a hypothetical helper called @code{wedge_helper}. Such a helper
used to exist, but has been replaced by a constraint. Due to
its simplicity it's still a good example. The helper begins with a
comment:

@example
/*

?O.           ?Ob
.X*           aX*
?O.           ?Oc

:8,C,wedge_helper
*/
@end example

The image on the left is the actual pattern. On the right we've taken
this image and added letters to label @code{apos}, @code{bpos}, and
@code{cpos}. The position of *, the point where GNU Go will move if the
pattern is adopted, is passed through the parameter @code{move}.

@example
int 
wedge_helper(ARGS)
@{
  int apos, bpos, cpos;
  int other = OTHER_COLOR(color);
  int success = 0;
  
  apos = OFFSET(0, -2);
  bpos = OFFSET(-1, 0);
  cpos = OFFSET(1, 0);

  if (TRYMOVE(move, color)) @{
    if (TRYMOVE(apos, other)) @{
      if (board[apos] == EMPTY || attack(apos, NULL))
        success = 1;
      else if (TRYMOVE(bpos, color)) @{
        if (!safe_move(cpos, other))
          success = 1;
        popgo();
      @}
      popgo();
    @}
    popgo();
  @}
  
  return success;
@}

@end example

The @code{OFFSET} lines tell GNU Go the positions of the three stones at
@samp{a}, @samp{b}, and @samp{c}. To decide whether the pattern
guarantees a connection, we do some reading. First we use the
@code{TRYMOVE} macro to place an @samp{O} at @samp{move} and let
@samp{X} draw back to @samp{a}. Then we ask whether @samp{O} can capture
these stones by calling @code{attack()}. The test if there is a stone at
@samp{a} before calling @code{attack()} is in this position not really
necessary but it's good practice to do so, because if the attacked stone
should happen to already have been captured while placing stones, GNU Go
would crash with an assertion failure.

If this attack fails we let @samp{O} connect at @samp{b} and use the
@code{safe_move()} function to examine whether a cut by @samp{X} at
@samp{c} could be immediately captured. Before we return the result we
need to remove the stones we placed from the reading stack. This is done
with the function @code{popgo()}.

@node  Autohelpers and Constraints
@section Autohelpers and Constraints

@cindex generation of helper functions
@cindex Autohelpers

In addition to the hand-written helper functions in @file{helpers.c}, GNU Go
can automatically generate helper functions from a diagram with labels
and an expression describing a constraint. The constraint diagram,
specifying the labels, is placed below the @samp{:} line and the constraint
expression is placed below the diagram on line starting with a @samp{;}.
Constraints can only be used to accept or reject a pattern. If the
constraint evaluates to zero (false) the pattern is rejected,
otherwise it's accepted (still conditioned on passing all other tests
of course). To give a simple example we consider a connection pattern.

@example

Pattern Conn311

O*.
?XO

:8,C,NULL

O*a
?BO

;oplay_attack_either(*,a,a,B)

@end example

Here we have given the label @samp{a} to the empty spot to the right of
the considered move and the label @samp{B} to the @samp{X} stone in the
pattern. In addition to these, @samp{*} can also be used as a label. A
label may be any lowercase or uppercase ascii letter except @code{OoXxt}. By
convention we use uppercase letters for @samp{X} stones and lowercase for @samp{O}
stones and empty intersections. When labeling a stone that's part of a
larger string in the pattern, all stones of the string should be marked
with the label. (These conventions are not enforced by the pattern
compiler, but to make the database consistent and easy to read they
should be followed.)

The labels can now be used in the constraint expression. In this example
we have a reading constraint which should be interpreted as "Play an
@samp{O} stone at @samp{*} followed by an @samp{X} stone at
@samp{a}. Accept the pattern if @samp{O} now can capture either at
@samp{a} or at @samp{B} (or both strings)."

The functions that are available for use in the constraints are listed
in the section `Autohelpers Functions' below. Technically the
constraint expression is transformed by mkpat into an automatically
generated helper function in @file{patterns.c}. The functions in the
constraint are replaced by C expressions, often functions calls. In
principle any valid C code can be used in the constraints, but there
is in practice no reason to use anything more than boolean and
arithmetic operators in addition to the autohelper functions.
Constraints can span multiple lines, which are then concatenated.

@node  Autohelper Actions
@section Autohelper Actions

@cindex autohelper actions

As a complement to the constraints, which only can accept or reject a
pattern, one can also specify an action to perform when the pattern
has passed all tests and finally has been accepted.

Example:

@example

Pattern EJ4

...*.     continuation
.OOX.
..XOX
.....
-----

:8,Ed,NULL

...*.     never play a here
.OOX.
.aXOX
.....
-----

>antisuji(a)

@end example

The line starting with @samp{>} is the action line. In this case it tells
the move generation that the move at a should not be considered,
whatever move reasons are found by other patterns. The action line
uses the labels from the constraint diagram. Both constraint and
action can be used in the same pattern. If the action only needs to
refer to @samp{*}, no constraint diagram is required. Like constraints,
actions can span multiple lines.

Here is a partial list of the autohelper macros which are typically
called from action lines. This list is not complete. If you cannot find an
autohelper macro in an action line in this list, consult @file{mkpat.c} to
find out what function in the engine is actually called. If no
macro exists which does what you want, you can add macros by
editing the list in @file{mkpat.c}.

@itemize
@item @code{antisuji(a);}
@quotation
Mark @samp{a} as an antisuji, that is, a move that must never
be played.
@end quotation
@item @code{replace(a,*)}
@quotation
This is appropriate if the move at @samp{*} is definitely better
than the move at @samp{a}. The macro adds a point redistribution rule. Any 
points which are assigned to @samp{a} during the move generation
by any move reason are redistributed to @samp{*}.
@end quotation
@item @code{prevent_attack_threat(a)}
@quotation
Add a reverse followup value because the opponent's move here
would threaten to capture @samp{a}.
@end quotation
@item @code{threaten_to_save(a)}
@quotation
Add a followup value because the move at @samp{*} threatens to
rescue the dead string at @samp{a}.
@end quotation
@item @code{defend_against_atari(a)}
@quotation
Estimate the value of defending the string @samp{a} which can be put into
atari and add this as a reverse followup value.
@end quotation
@item @code{add_defend_both_move(a,b);}
@quotation
@end quotation
@item @code{add_cut_move(c,d);}
@quotation
Add to the move reasons that the move at @samp{*} threatens to cut
@samp{c} and @samp{d}.
@end quotation
@end itemize

@node  Autohelper Functions
@section Autohelper Functions

The autohelper functions are translated into C code by the program in
@file{mkpat.c}. To see exactly how the functions are implemented,
consult the autohelper function definitions in that file. Autohelper
functions can be used in both constraint and action lines.

@example

@code{lib(x)}
@code{lib2(x)}
@code{lib3(x)}
@code{lib4(x)}

@end example

Number of first, second, third, and fourth order liberties of a worm
respectively. @xref{Worms and Dragons}, the documentation on worms for
definitions.

@example

@code{xlib(x)}
@code{olib(x)}

@end example

The number of liberties that an enemy or own stone, respectively,
would obtain if played at the empty intersection @samp{x}.

@example
@code{xcut(x)}
@code{ocut(x)}
@end example

Calls @code{cut_possible} (@pxref{General Utilities}) to determine
whether @samp{X} or @samp{O} can cut at the empty intersection @samp{x}. 

@example
@code{ko(x)}
@end example

True if @samp{x} is either a stone or an empty point involved in a ko
position.

@example
@code{status(x)}
@end example 

The matcher status of a dragon. status(x) returns an integer that can have the
values @code{ALIVE}, @code{UNKNOWN}, @code{CRITICAL}, or @code{DEAD}
(@pxref{Worms and Dragons}).

@example
@code{alive(x)}
@code{unknown(x)}
@code{critical(x)}
@code{dead(x)}
@end example

Each function true if the dragon has the corresponding matcher status and
false otherwise (@pxref{Worms and Dragons}).

@example
@code{status(x)}
@end example

Returns the status of the dragon at @samp{x} (@pxref{Worms and Dragons}).

@example
@code{genus(x)}
@end example

The number of eyes of a dragon. It is only meaningful to compare this
value against 0, 1, or 2.

@example

@code{xarea(x)}
@code{oarea(x)}
@code{xmoyo(x)}
@code{omoyo(x)}
@code{xterri(x)}
@code{oterri(x)}

@end example

These functions call @code{whose_territory()}, @code{whose_moyo()}
and @code{whose_area()} (@pxref{Territory and Moyo}). For example
@code{xarea(x)} evaluates to true if @samp{x} is either a living enemy stone
or an empty point within the opponent's ``area''. The function @code{oarea(x)}
is analogous but with respect to our stones and area.  The main difference
between area, moyo, and terri is that area is a very far reaching kind of
influence, moyo gives a more realistic estimate of what may turn in to
territory, and terri gives the points that already are believed to be secure
territory.

@example
@code{weak(x)}
@end example

True for a dragon that is perceived as weak.

@example

@code{attack(x)}
@code{defend(x)}

@end example

Results of tactical reading. @code{attack(x)} is true if the worm can be
captured, @code{defend(x)} is true if there also is a defending move. Please
notice that @code{defend(x)} will return false if there is no attack on the
worm.

@example

@code{safe_xmove(x)}
@code{safe_omove(x)}

@end example

True if an enemy or friendly stone, respectively, can safely be played at
@samp{x}. By safe it is understood that the move is legal and that it cannot
be captured right away.

@example

@code{legal_xmove(x)}
@code{legal_omove(x)}

@end example

True if an enemy or friendly stone, respectively, can legally be played at x.

@example

o_somewhere(x,y,z, ...)
x_somewhere(x,y,z, ...)

@end example

True if O (respectively X) has a stone at one of the labelled vertices.
In the diagram, these vertices should be marked with a @samp{?}.

@example

odefend_against(x,y)
xdefend_against(x,y)

@end example

True if an own stone at @samp{x} would stop the enemy from safely playing at
@samp{y}, and conversely for the second function.

@example

@code{does_defend(x,y)}
@code{does_attack(x,y)}

@end example

True if a move at @samp{x} defends/attacks the worm at @samp{y}. For 
defense a move of the same color as @samp{y} is tried and for attack a move of
the opposite color.

@example

@code{xplay_defend(a,b,c,...,z)}
@code{oplay_defend(a,b,c,...,z)}
@code{xplay_attack(a,b,c,...,z)}
@code{oplay_attack(a,b,c,...,z)}

@end example

These functions make it possible to do more complex reading
experiments in the constraints. All of them work so that first the
sequence of moves @samp{a},@samp{b},@samp{c},... is played through with
alternating colors, starting with @samp{X} or @samp{O} as indicated by
the name. Then it is tested whether the worm at @samp{z} can be attacked or
defended, respectively. It doesn't matter who would be in turn to move,
a worm of either color may be attacked or defended. For attacks the
opposite color of the string being attacked starts moving and for
defense the same color starts. The defend functions return true if the
worm cannot be attacked in the position or if it can be attacked but
also defended.  The attack functions return true if there is a way to
capture the worm, whether or not it can also be defended. If there is no
stone present at @samp{z} after the moves have been played, it is assumed that
an attack has already been successful or a defense has already failed.
If some of the moves should happen to be illegal, typically because it
would have been suicide, the following moves are played as if nothing
has happened and the attack or defense is tested as usual. It is assumed
that this convention will give the relevant result without requiring a
lot of special cases.

The special label @samp{?} can be used to represent a tenuki.
Thus @code{oplay_defend(a,?,b,c)} tries moves by @samp{O} at @samp{a} and
@samp{b}, as if @samp{X} plays the second move in another part of
the board, then asks if @samp{c} can be defended. The tenuki cannot
be the first move of the sequence, nor does it need to be:
instead of @code{oplay_defend(?,a,b,c)} you can use
@code{xplay_defend(a,b,c)}.

@example
@code{xplay_defend_both(a,b,c,...,y,z)}
@code{oplay_defend_both(a,b,c,...,y,z)}
@code{xplay_attack_either(a,b,c,...,y,z)}
@code{oplay_attack_either(a,b,c,...,y,z)}

@end example

These functions are similar to the previous ones. The difference is
that the last *two* arguments denote worms to be attacked or defended
simultaneously. Obviously @samp{y} and @samp{z} must have the same color. If either
location is empty, it is assumed that an attack has been successful or
a defense has failed. The typical use for these functions is in
cutting patterns, where it usually suffices to capture either
cutstone.

The function @code{xplay_defend_both} plays alternate moves
beginning with an @samp{X} at @samp{a}. Then it passes the last
two arguments to @code{defend_both} in
@file{engine/utils.c}. This function checks to determine
whether the two strings can be simultaneously defended.

The function @code{xplay_attack_either} plays alternate
moves beginning with an @samp{X} move at @samp{a}. Then it passes
the last two arguments to @code{attack_either} in
@file{engine/utils.c}. This function looks for a move
which captures at least one of the two strings. In its
current implementation @code{attack_either} only looks
for uncoordinated attacks and would thus miss a double
atari.

@example
@code{xplay_connect(a,b,c,...,y,z)}
@code{oplay_connect(a,b,c,...,y,z)}
@code{xplay_disconnect(a,b,c,...,y,z)}
@code{oplay_disconnect(a,b,c,...,y,z)}

@end example

The function @code{xplay_connect(a,b,c,...,y,z)} begins
with an @samp{X} move at @samp{a}, then an @samp{O}
move at @samp{b}, and so forth, then finally calls
@code{string_connect()} to determine whether
@samp{x} and @samp{y} can be connected. The other
functions are similar (@pxref{Connection Reading}).

@example

@code{xplay_break_through(a,b,c,...,x,y,z)}
@code{oplay_break_through(a,b,c,...,x,y,z)}

@end example

These functions are used to set up a position like

@example

.O.    .y.
OXO    xXz

@end example

@noindent
and @samp{X} aims at capturing at least one of @samp{x}, @samp{y}, and
@samp{z}. If this succeeds @samp{1} is returned. If it doesn't, @samp{X}
tries instead to cut through on either side and if this succeeds,
@samp{2} is returned. Of course the same shape with opposite colors can
also be used.

Important notice: @samp{x}, @samp{y}, and @samp{z} must be given in the
order they have in the diagram above, or any reflection and/or rotation
of it.

@example
seki_helper(x)
@end example

Checks whether the string at @samp{x} can attack any surrounding
string. If so, return false as the move to create a seki (probably)
wouldn't work.

@example
threaten_to_save(x)
@end example

Calls @code{add_followup_value} to add as a move reason a conservative
estimate of the value of saving the string @samp{x} by capturing one opponent
stone.

@example
area_stone(x)
@end example

Returns the number of stones in the area around @samp{x}.

@example
area_space(x)
@end example

Returns the amount of space in the area around @samp{x}.

@example
@code{eye(x)}
@code{proper_eye(x)}
@code{marginal_eye(x)}
@end example

True if @samp{x} is an eye space for either color, a non-marginal eye space
for either color, or a marginal eye space for either color,
respectively.

@example
@code{antisuji(x)}
@end example

Tell the move generation that @samp{x} is a substandard move that never should
be played.

@example
same_dragon(x,y)
same_worm(x,y)
@end example

Return true if @samp{x} and @samp{y} are the same dragon or worm respectively.

@example
@code{dragonsize(x)}
@code{wormsize(x)}
@end example

Number of stones in the indicated dragon or worm.

@example
@code{add_connect_move(x,y)}
@code{add_cut_move(x,y)}
@code{add_attack_either_move(x,y)}
@code{add_defend_both_move(x,y)}
@end example

Explicitly notify the move generation about move reasons for the move
in the pattern.

@example
@code{halfeye(x)}
@end example

Returns true if the empty intersection at @samp{x} is a half eye.

@example
@code{remove_attack(x)}
@end example

Inform the tactical reading that a supposed attack does in fact not
work.

@example
@code{potential_cutstone(x)}
@end example

True if @code{cutstone2} field from worm data is larger than one. This
indicates that saving the worm would introduce at least two new
cutting points.

@example
@code{not_lunch(x,y)}
@end example

Prevents the misreporting of @samp{x} as lunch for @samp{y}.
For example, the following pattern tells GNU Go that even
though the stone at @samp{a} can be captured, it should not
be considered ``lunch'' for the dragon at @samp{b}, because
capturing it does not produce an eye:

@example
XO|          ba|
O*|          O*|
oo|          oo|
?o|          ?o|

> not_lunch(a,b)
@end example

@example
@code{vital_chain(x)}
@end example

Calls @code{vital_chain} to determine whether capturing
the stone at @samp{x} will result in one eye for an adjacent
dragon. The current implementation just checks that the stone
is not a singleton on the first line.

@example
@code{amalgamate(x,y)}
@end example

Amalgamate (join) the dragons at @samp{x} and @samp{y} (@pxref{Worms and Dragons}).

@example
@code{amalgamate_most_valuable(x,y,z)}
@end example

Called when @samp{x}, @samp{y}, @samp{z} point to three (preferably distinct)
dragons, in situations such as this:

@example

.O.X
X*OX
.O.X

@end example

In this situation, the opponent can play at @samp{*}, preventing
the three dragons from becoming connected. However @samp{O}
can decide which cut to allow. The helper amalgamates the
dragon at @samp{y} with either @samp{x} or @samp{z}, 
whichever is largest.

@example
make_proper_eye(x)
@end example

This autohelper should be called when @samp{x} is an eyespace
which is misidentified as marginal. It is reclassified as
a proper eyespace (@pxref{Eye Space}).

@example
remove_halfeye(x)
@end example

Remove a half eye from the eyespace. This helper should not be run after
@code{make_dragons} is finished, since by that time the eyespaces have
already been analyzed.

@example
remove_eyepoint(x)
@end example

Remove an eye point. This function can only be used before the
segmentation into eyespaces.

@example
@code{owl_topological_eye(x,y)}
@end example

Here @samp{x} is an empty intersection which may be an
eye or half eye for some dragon, and @samp{y} is a 
stone of the dragon, used only to determine the color
of the eyespace in question. Returns the sum of the values
of the diagonal intersections, relative to @samp{x}, as
explained in @xref{Eye Topology}, equal to 4 or more if the
eye at @samp{x} is false, 3 if it is a half eye, and 2 if it
is a true eye. 

@example
@code{owl_escape_value(x)}
@end example

Returns the escape value at @samp{x}. This is only useful in owl
attack and defense patterns.

@node  Attack and Defense DB
@section Attack and Defense Database

The patterns in @file{attack.db} and @file{defense.db} are used to assist the
tactical reading in finding moves that attacks or defends worms. The
matching is performed during @code{make_worms()}, at the time when the
tactical status of all worms is decided. None of the classes described
above are useful in these databases, instead we have two other
classes.

@table @samp
@item D
For each @samp{O} worm in the pattern that can be tactically captured
(@code{worm[m][n].attack_code != 0}), the move at @samp{*} is
tried. If it is found to defend the stone, this is registered as a
reason for the move @samp{*} and the defense point of the worm is set to
@samp{*}.
@item A
For each @samp{X} worm in the pattern, it's tested whether the move
at @samp{*} captures the worm. If that is the case, this is
registered as a reason for the move at @samp{*}. The attack point of
the worm is set to @samp{*} and if it wasn't attacked before, a
defense is searched for.
@end table

Furthermore, @samp{A} patterns can only be used in @file{attack.db} and
@samp{D} patterns only in @file{defense.db}. Unclassified patterns may
appear in these databases, but then they must work through actions to be
effective.

@node  Connections Database
@section The Connections Database

@cindex connections database
@cindex connection shapes database

The patterns in @file{conn.db} are used for helping @code{make_dragons()}
amalgamate worms into dragons and to some extent for modifying eye spaces.
The patterns in this database use the classifications @samp{B}, 
@samp{C}, and @samp{e}. @samp{B} patterns are used for finding cutting points,
where amalgamation should not be performed, @samp{C} patterns are used for
finding existing connections, over which amalgamation is to be done, and 
@samp{e} patterns are used for modifying eye spaces and reevaluating lunches.
There are also some patterns without classification, which use action lines to
have an impact. These are matched together with the @samp{C} patterns. Further
details and examples can be found in @xref{Worms and Dragons}.

We will illustrate these databases by example. In this situation:

@example
XOO
O.O
...
@end example
@noindent
@samp{X} cannot play safely at the cutting point, so the @samp{O} dragons
are to be amalgamated. Two patterns are matched here:

@example
Pattern CC204

O
.
O

:+,C

O
A
O

;!safe_xmove(A) && !ko(A) && !xcut(A)

Pattern CC205

XO
O.

:\,C

AO
OB

;attack(A) || (!safe_xmove(B) && !ko(B) && !xcut(B))
@end example

The constraints are mostly clear. For example the second
pattern should not be matched if the @samp{X} stone cannot
be attacked and @samp{X} can play safely at @samp{B}, or
if @samp{B} is a ko. The constraint @code{!xcut(B)} means
that connection has not previously been inhibited by 
@code{find_cuts}. For example consider this situation:

@example

OOXX
O.OX
X..O
X.OO
@end example
@noindent
The previous pattern is matched here twice, yet @samp{X} can push
in and break one of the connections. To fix this, we include
a pattern:

@example
Pattern CB11

?OX?
O!OX
?*!O
??O?

:8,B

?OA?
OaOB
?*bO
??O?

; !attack(A) && !attack(B) && !xplay_attack(*,a,b,*) && !xplay_attack(*,b,a,*)
@end example

After this pattern is found, the @code{xcut} autohelper macro will return
true at any of the points @samp{*}, @samp{a} and @samp{b}. Thus the
patterns @code{CB204} and @code{CB205} will not be matched, and the dragons will
not be amalgamated.

@node Connection Functions
@section Connections Functions

Here are the public functions in @file{connections.c}.

@itemize @bullet
@item @code{static void cut_connect_callback(int m, int n, int color, 
        struct pattern *pattern, int ll, void *data)}
@findex cut_connect_callback
@quotation
Try to match all (permutations of) connection patterns at @code{(m,n)}.
For each match, if it is a B pattern, set cutting point in worm
data structure and make eye space marginal for the connection
inhibiting entries of the pattern. If it is a @samp{C} pattern, amalgamate
the dragons in the pattern.
@end quotation
@item @code{void find_cuts(void)}
@findex find_cuts
@quotation
Find cutting points which should inhibit amalgamations and sever
the adjacent eye space. This goes through the connection database
consulting only patterns of type B. When such a function is found,
the function @code{cut_connect_callback} is invoked.
@end quotation
@item @code{void find_connections(void)}
@findex find_connections
@quotation 
Find explicit connection patterns and amalgamate the involved dragons.
This goes through the connection database consulting patterns except those of
type B, E or e. When such a function is found, the function
@code{cut_connect_callback} is invoked.
@end quotation
@item void modify_eye_spaces1(void)
@findex modify_eye_spaces1
@quotation 
Find explicit connection patterns and amalgamate the involved dragons.
This goes through the connection database consulting only patterns
of type E (@pxref{Connections Database}). When such a function is found, the
function @code{cut_connect_callback} is invoked.  
@end quotation
@item void modify_eye_spaces1(void)
@findex modify_eye_spaces1
@quotation 
Find explicit connection patterns and amalgamate the involved dragons.
This goes through the connection database consulting only patterns
of type e (@pxref{Connections Database}). When such a function is found, the
function @code{cut_connect_callback} is invoked.  
@end quotation
@end itemize

@node  Tuning
@section Tuning the Pattern databases

@cindex tuning the pattern database
@cindex tuning the shapes database

Since the pattern databases, together with the valuation of move
reasons, decide GNU Go's personality, much time can be devoted to
``tuning'' them. Here are some suggestions.

If you want to experiment with modifying the pattern database, invoke
with the @option{-a} option. This will cause every pattern to be evaluated,
even when some of them may be skipped due to various optimizations.

You can obtain a Smart Game Format (SGF) record of your game in at least
two different ways. One is to use CGoban to record the game. You can
also have GNU Go record the game in Smart Game Format, using the @option{-o}
option. It is best to combine this with @option{-a}. Do not try to read the SGF
file until the game is finished and you have closed the game
window. This does not mean that you have to play the game out to its
conclusion. You may close the CGoban window on the game and GNU Go
will close the SGF file so that you can read it.

If you record a game in SGF form using the @option{-o} option, GNU Go will add
labels to the board to show all the moves it considered, with their
values. This is an extremely useful feature, since one can see at a
glance whether the right moves with appropriate weights are being
proposed by the move generation.

First, due to a bug of unknown nature, it occasionally happens
that GNU Go will not receive the @code{SIGTERM} signal from CGoban that it
needs to know that the game is over. When this happens, the SGF file
ends without a closing parenthesis, and CGoban will not open the
file. You can fix the file by typing:

@example

 echo ")" >>[filename]  

@end example

@noindent
at the command line to add this closing parenthesis. Or you could
add the ) using an editor.

Move values exceeding 99 (these should be rare) can be displayed by
CGoban but you may have to resize the window in order to see all three
digits. Grab the lower right margin of the CGoban window and pull it
until the window is large. All three digits should be visible.

If you are playing a game without the @option{-o} option and you wish to
analyze a move, you may still use CGoban's ``Save Game'' button to get
an SGF file. It will not have the values of the moves labelled, of
course.

Once you have a game saved in SGF format, you can analyze any
particular move by running:

@example

  gnugo -l [filename] -L [move number] -t -a -w

@end example

@noindent
to see why GNU Go made that move, and if you make changes to the
pattern database and recompile the program, you may ask GNU Go to
repeat the move to see how the behavior changes. If you're using
emacs, it's a good idea to run GNU Go in a shell in a buffer (M-x
shell) since this gives good navigation and search facilities.

Instead of a move number, you can also give a board coordinate to @option{-L}
in order to stop at the first move played at this location. If you
omit the @option{-L} option, the move after those in the file will be
considered. 

If a bad move is proposed, this can have several reasons. To begin
with, each move should be valued in terms of actual points on the
board, as accurately as can be expected by the program. If it's not,
something is wrong. This may have two reasons. One possibility is that
there are reasons missing for the move or that bogus reasons have been
found. The other possibility is that the move reasons have been
misevaluated by the move valuation functions. Tuning of patterns is
with a few exceptions a question of fixing the first kind of problems.

If there are bogus move reasons found, search through the trace output
for the pattern that is responsible. (Some move reasons, e.g. most
tactical attack and defense, do not originate from patterns. If no
pattern produced the bogus move reason, it is not a tuning problem.)
Probably this pattern was too general or had a faulty constraint. Try
to make it more specific or correct bugs if there were any. If the
pattern and the constraint looks right, verify that the tactical
reading evaluates the constraint correctly. If not, this is either a
reading bug or a case where the reading is too complicated for GNU Go.

If a connecting move reason is found, but the strings are already
effectively connected, there may be missing patterns in @file{conn.db}.
Similarly, worms may be incorrectly amalgamated due to some too
general or faulty pattern in @file{conn.db}. To get trace output from the
matching of patterns in @file{conn.db} you need to add a second
@option{-t} option.

If a move reason is missing, there may be a hole in the database. It
could also be caused by some existing pattern being needlessly
specific, having a faulty constraint, or being rejected due to a
reading mistake. Unless you are familiar with the pattern databases,
it may be hard to verify that there really is a pattern missing. Look
around the databases to try to get a feeling for how they are
organized. (This is admittedly a weak point of the pattern databases,
but the goal is to make them more organized with time.) If you decide
that a new pattern is needed, try to make it as general as possible,
without allowing incorrect matches, by using proper classification
from among snOoXx and constraints. The reading functions can be put to
good use. The reason for making the patterns as general as they can be
is that we need a smaller number of them then, which makes the
database much easier to maintain. Of course, if you need too
complicated constraints, it's usually better to split the pattern.

If a move has the correct set of reasons but still is misevaluated,
this is usually not a tuning problem. There are, however, some
possibilities to work around these mistakes with the use of patterns.
In particular, if the territorial value is off because @code{delta_terri()}
give strange results, the (min)terri and maxterri values can be set by
patterns as a workaround. This is typically done by the endgame
patterns, where we can know the (minimum) value fairly well from the
pattern. If it should be needed, (min)value and maxvalue can be used
similarly. These possibilities should be used conservatively though,
since such patterns are likely to become obsolete when better (or at
least different) functions for e.g. territory estimation are being
developed.

In order to choose between moves with the same move reasons, e.g.
moves that connect two dragons in different ways, patterns with a
nonzero shape value should be used. These should give positive shape
values for moves that give good shape or good aji and negative values
for bad shape and bad aji. Notice that these values are additive, so
it's important that the matches are unique.

Sente moves are indicated by the use of the pattern followup value.
This can usually not be estimated very accurately, but a good rule is
to be rather conservative. As usual it should be measured in terms of
actual points on the board. These values are also additive so the same
care must be taken to avoid unintended multiple matches.

You can also get a visual display of the dragons using the @option{-T}
option. The default GNU Go configuration tries to build a
version with color support using either curses or the
ansi escape sequences. You are more likely to find color
support in rxvt than xterm, at least on many systems, so
we recommend running:

@example
  gnugo -l [filename] -L [move number] -T
@end example

@noindent
in an rxvt window. If you do not see a color display,
and if your host is a GNU/Linux machine, try this again
in the Linux console.

Worms belonging to the same dragon are labelled with the same letters.
The colors indicate the value of the field @code{dragon.safety}, which
is set in @file{moyo.c}.

@format
Green:  GNU Go thinks the dragon is alive
Yellow: Status unknown
Blue:   GNU Go thinks the dragon is dead
Red:    Status critical (1.5 eyes) or weak by the algorithm
        in @file{moyo.c}
@end format

@cindex eliminate the randomness

If you want to get the same game over and over again, you can
eliminate the randomness in GNU Go's play by providing a fixed
random seed with the @option{-r} option.


@node PM Implementation
@section Implementation

@cindex implementation of pattern matching

The pattern code in GNU Go is fairly straightforward conceptually, but
because the matcher consumes a significant part of the time in
choosing a move, the code is optimized for speed. Because of this
there are implementation details which obscure things slightly.


In GNU Go, the ascii @file{.db} files are precompiled into tables (see
@file{patterns.h}) by a standalone program @file{mkpat.c}, and the resulting 
@file{.c} files are compiled and linked into the main GNU Go executable.

Each pattern is compiled to a header, and a sequence of elements,
which are (notionally) checked sequentially at every position and
orientation of the board. These elements are relative to the pattern
'anchor' (or origin).  One @samp{X} or @samp{O} stone is (arbitrarily) chosen to
represent the origin of the pattern. (We cannot dictate one or the
other since some patterns contain only one colour or the other.)  All
the elements are in co-ordinates relative to this position. So a
pattern matches "at" board position @code{(m,n,o)} if the the pattern anchor
stone is on @code{(m,n)}, and the other elements match the board when the
pattern is transformed by transformation number @samp{o}. (See below for
the details of the transformations, though these should not be
necessary)


@node  Symmetry & transformations, Details, PM Implementation, Patterns
@section Symmetry and transformations

@cindex symmetry and transformations
@cindex symmetry and transformations of shapes

In general, each pattern must be tried in each of 8 different
permutations, to reflect the symmetry of the board. But some
patterns have symmetries which mean that it is unnecessary
(and therefore inefficient) to try all eight. The first
character after the @samp{:} can be one of @samp{8},@samp{|},@samp{\},@samp{/},
@samp{X}, @samp{-}, @samp{+}, representing the axes of symmetry. It can also
be @samp{O}, representing symmetry under 180 degrees rotation.

@format
transformation   I    -    |     .     \    l    r     /
                ABC  GHI  CBA   IHG   ADG  CFI  GDA   IFC
                DEF  DEF  FED   FED   BEH  BEH  HEB   HEB
                GHI  ABC  IHG   CBA   CFI  ADG  IFC   GDA

                 a    b    c     d     e    f    g     h
@end format

Then if the pattern has the following symmetries, the
following are true:

@example

|  c=a, d=b, g=e, h=f
-  b=a, c=d, e=f, g=h
\  e=a, g=b, f=c, h=d
/  h=a, f=b, g=c, e=d
O  a=d, b=c, e=h, f=g
X  a=d=e=h, b=c=f=g
+  a=b=c=d, e=f=g=h

@end example

We can choose to use transformations a,d,f,g as the unique
transformations for patterns with either @samp{|}, @samp{-}, @samp{\}, or
@samp{/} symmetry.

Thus we choose to order the transformations a,g,d,f,h,b,e,c and choose
first 2 for @samp{X} and @samp{+}, the first 4 for @samp{|}, @samp{-},
@samp{/}, and @samp{\}, the middle 4 for @samp{O}, and all 8 for
non-symmetrical patterns.

Each of the reflection operations (e-h) is equivalent to reflection
about one arbitrary axis followed by one of the rotations (a-d).  We
can choose to reflect about the axis of symmetry (which causes no
net change) and can therefore conclude that each of e-h is
equivalent to the reflection (no-op) followed by a-d.  This argument
therefore extends to include @samp{-} and @samp{/} as well as @samp{|}
and @samp{\}.

@node  Details
@section Implementation Details

@cindex pattern matching optimization
@cindex grid optimization

@enumerate
@item An entry in the pattern header states whether the anchor is an @samp{X} or
an @samp{O}. This helps performance, since all transformations can be
rejected at once if the anchor stone does not match. (Ideally, we
could just define that the anchor is always @samp{O} or always @samp{X}, but some
patterns contain no @samp{O} and some contain no @samp{X}.)

@item The pattern header contains the size of the pattern (ie the
co-ordinates of the top left and bottom right elements) relative to
the anchor. This allows the pattern can be rejected quickly if there
is not room for the pattern to fit around the anchor stone in a given
orientation (ie it is too near the edge of the board).  The bounding
box information must first be transformed like the elements before it
can be tested, and after transforming, we need to work out where the
top-left and bottom-right corners are.

@item The edge constraints are implemented by notionally padding the
pattern with rows or columns of @samp{?} until it is exactly 19 (or
whatever the current board size is) elements wide or high. Then the
pattern is quickly rejected by (ii) above if it is not at the edge. So
the example pattern above is compiled as if it was written


@example

"example"
.OO????????????????
*XX????????????????
o??????????????????
:8,80

@end example

@item The elements in a pattern are sorted so that non-space
elements are checked before space elements. It is hoped that,
for most of the game, more squares are empty, and so the
pattern can be more quickly rejected doing it this way.

@item The actual tests are performed using an 'and-compare'
sequence. Each board position is a 2-bit quantity.
%00 for empty, %01 for @samp{O}, %10 for @samp{X}.
We can test for an exact match by and-ing with %11 (no-op),
then comparing with 0, 1 or 2. The test for @samp{o} is the
same as a test for 'not-X', ie not %10. So and with %01
should give 0 if it matches. Similarly @samp{x} is a test that
bit 0 is not set.

@end enumerate

@node Grid optimization
@section The ``Grid'' Optimization

The comparisons between pattern and board are performed as 2-bit
bitwise operations. Therefore they can be performed in parallel,
16-at-a-time on a 32-bit machine.

Suppose the board is layed out as follows :

@example

 .X.O....OO
 XXXXO.....
 .X..OOOOOO
 X.X.......
 ....X...O.

@end example

@noindent
which is internally stored internally in a 2d array (binary)

@example

 00 10 00 01 00 00 00 00 01 01
 10 10 10 10 01 00 00 00 00 00
 00 10 00 00 01 01 01 01 01 01
 10 00 10 00 00 00 00 00 00 00
 00 00 00 00 10 00 00 00 01 00

@end example

@noindent
we can compile this to a composite array in which each element
stores the state of a 4x4 grid of squares :

@example

 ????????  ????????  ???????? ...
 ??001000  00100001  10000100
 ??101010  10101010  10101001
 ??001000  00100000  10000001

 ??001000  00100001  ...
 ??101010  10101010
 ??001000  00100000
 ??001000  10001000 

...

 ??100010  ...
 ??000000
 ????????
 ????????

@end example

Where '??' is off the board.

We can store these 32-bit composites in a 2d merged-board array,
substituting the illegal value %11 for '??'.

Similarly, for each pattern, mkpat produces appropriate 32-bit and-value
masks for the pattern elements near the anchor. It is a simple matter
to test the pattern with a similar test to (5) above, but for 32-bits
at a time.

@node  Joseki Compiler
@section The Joseki Compiler

@cindex joseki
@cindex how GNU Go learns new joseki
@cindex the joseki compiler
@cindex teaching josekis to GNU Go

GNU Go includes a joseki compiler in @file{patterns/joseki.c}. This processes
an SGF file (with variations) and produces a sequence of patterns
which can then be fed back into mkpat. The joseki database is currently in files
in @file{patterns/} called @file{hoshi.sgf}, @file{komoku.sgf}, @file{sansan.sgf}, 
@file{mokuhazushi.sgf} and @file{takamoku.sgf}.  This division can be revised
whenever need arises.

The SGF files are transformed into the pattern database @file{.db} format by
the program in @file{joseki.c}. These files are in turn transformed into C
code by the program in @file{mkpat.c} and the C files are compiled and linked
into the GNU Go binary.

Not every node in the SGF file contributes a pattern. The nodes which
contribute patterns have the joseki in the upper right corner, with
the boundary marked with a square mark and other information to determine
the resulting pattern marked in the comments.

The intention is that the move valuation should be able to choose
between the available variations by normal valuation. When this fails
the primary workaround is to use shape values to increase or decrease
the value. It is also possible to add antisuji variations to forbid
popular suboptimal moves. As usual constraints can be used, e.g. to
condition a variation on a working ladder.

The joseki format has the following components for each SGF node:

@itemize @bullet
@item
A square mark (@code{SQ} or @code{MA} property) to decide how large part of the
board should be included in the pattern.
@item A move (@samp{W} or @samp{B} property) with the natural interpretation.
If the square mark is missing or the move is a pass, no pattern is
produced for the node.
@item Optional labels (@code{LB} property), which must be a single letter each.
If there is at least one label, a constraint diagram will be
produced with these labels.
@item A comment (@samp{C} property). As the first character it should have one of the
following characters to decide its classification:
@itemize @minus
@item @samp{U} - urgent move
@item @samp{S} or @samp{J} - standard move
@item @samp{s} or @samp{j} - lesser joseki
@item @samp{T} - trick move
@item @samp{t} - minor joseki move (tenuki OK)
@item @samp{0} - antisuji (@samp{A} can also be used)
@end itemize
The rest of the line is ignored, as is the case of the letter. If neither of
these is found, it's assumed to be a standard joseki move.

In addition to this, rows starting with the following characters are
recognized:
@itemize @minus
@item @samp{#} - Comments. These are copied into the patterns file, above the diagram.
@item @samp{;} - Constraints. These are copied into the patterns file, below the
constraint diagram.
@item  @samp{>} - Actions. These are copied into the patterns file, below the
constraint diagram.
@item @samp{:} - Colon line. This is a little more complicated, but the colon
line of the produced patterns always start out with ":8,s" for
transformation number and sacrifice pattern class (it usually
isn't a sacrifice, but it's pointless spending time checking for
tactical safety). Then a joseki pattern class character is
appended and finally what is included on the colon line in the
comment for the SGF node.
@end itemize
@end itemize

Example: If the comment in the SGF file looks like

@example
F
:C,shape(3)
;xplay_attack(A,B,C,D,*)
@end example

@noindent
the generated pattern will have a colon line

@example
:8,sjC,shape(3)
@end example

@noindent
and a constraint

@example
;xplay_attack(A,B,C,D,*)
@end example

@node Ladders in Joseki
@section Ladders in Joseki

As an example of how to use autohelpers with the
Joseki compiler, we consider an example where a Joseki
is bad if a ladder fails. Assume we have the taisha and are
considering connecting on the outside with the pattern

@example
--------+
........|
........|
...XX...|
...OXO..|
...*O...|
....X...|
........|
........|
@end example

But this is bad unless we have a ladder in our favor. To check this we
add a constraint which may look like

@example
--------+
........|
........|
...XX...|
...OXO..|
...*OAC.|
....DB..|
........|
........|

;oplay_attack(*,A,B,C,D)
@end example

In order to accept the pattern we require that the constraint on the
semicolon line evaluates to true. This particular constraint has the
interpretation "Play with alternating colors, starting with @samp{O}, 
on the intersections @samp{*}, @samp{A}, @samp{B}, and @samp{C}. Then check
whether the stone at @samp{D} can be captured." I.e. play to this position

@example
--------+
........|
........|
...XX...|
...OXO..|
...OOXX.|
....XO..|
........|
........|
@end example

@noindent
and call @code{attack()} to see whether the lower @samp{X} stone can be
captured.  This is not limited to ladders, but in this particular case the
reading will of course involve a ladder. 

The constraint diagram above with letters is how it looks in the @file{.db}
file. The joseki compiler knows how to create these from labels in
the SGF node. @file{Cgoban} has an option to create one letter labels,
but this ought to be a common feature for SGF editors.

Thus in order to implement this example in SGF, one would add labels
to the four intersections and a comment:

@example
;oplay_attack(*,A,B,C,D)
@end example

The appropriate constraint (autohelper macro) will then be added
to the Joseki @file{.db} file.

@node Corner Matcher
@section Corner Matcher

@cindex implementation of pattern matching
@cindex joseki
@cindex corner matcher

GNU Go uses a special matcher for joseki patterns.  It has certain constraints
on the patterns it can match, but is much faster and takes far less space to
store patterns than the standard matcher.

Patterns used with corner matcher have to qualify the following conditions:

@itemize @bullet
@item They must be matchable only at a corner of the board (hence the name of
the matcher).
@item They can consist only of @samp{O}, @samp{X} and @samp{.} elements.
@item Of all pattern values (@pxref{Pattern Values}), corner matcher only
support @code{shape(x)}.  This is not because the matcher cannot handle other
values in principle, just they are currently not used in joseki databases.
@end itemize

Corner matcher was specifically designed for joseki patterns and they of
course satisfy all the conditions above.  With some modifications corner
matcher could be used for fuseki patterns as well, but fullboard matcher
does its work just fine.

The main idea of the matcher is very same to the one of DFA matcher
(@pxref{Pattern matching with DFA}): check all available patterns at once,
not a single pattern at a time.  A modified version of DFA matcher could be
used for joseki pattern matching, but its database would be very large.
Corner matcher capitalizes on the fact that there are relatively few
stones in each such pattern.

Corner pattern database is organized into a tree.  Nodes of the tree are
called ``variations''.  Variations represent certain sets of stones in a
corner of the board.  Root variation corresponds to an empty corner and a
step down the tree is equivalent to adding a stone to the corner.  Each
variation has several properties:

@itemize @minus
@item stone position relative to the corner,
@item a flag determining whether the stone color must be equal to the first
matched stone color,
@item number of stones in the corner area (see below) of the variation stone.
@end itemize

By corner area we define a rectangle which corners are the current corner of
the board and the position of the stone (inclusive).  For instance, if the
current board corner is A19 then corner area of a stone at C18 consists
of A18, A19, B18, B19, C18 and C19.

Variation which is a direct child of the root variation matches if there
is any stone at the variation position and the stone is alone in its
corner area.

Variation at a deeper level of the tree matches if there is a stone of
specified color in variation position and the number of stones in its
corner area is equal to the number specified in variation structure.

When a certain variation matches, all its children has to be checked
recursively for a match.

All leaf variations and some inner ones have patterns attached to them.
For a pattern to match, it is required that its @emph{parent} variation
matches.  In addition, it is checked that pattern is being matched for
the appropriate color (using its variation ``stone color'' field) and
that the number of stones in the area where the pattern is being matched
is indeed equal to the number of stones in the pattern.  The ``stone
position'' property of the pattern variation determines the move suggested
by the pattern.

Consider this joseki pattern which has four stones:

@example
------+
......|
......|
.O*...|
.XXO..|
......|
......|
@end example

To encode it for the corner matcher, we have to use five variations,
each next being a child of previous:

@multitable {Tree level} {Position} {``other''} {Number of stones}
@item Tree level @tab Position @tab Color @tab Number of stones
@item 1 @tab R16 @tab ``same'' @tab 1
@item 2 @tab P17 @tab ``same'' @tab 1
@item 3 @tab Q16 @tab ``other'' @tab 2
@item 4 @tab P16 @tab ``other'' @tab 4
@item 5 @tab Q17 @tab ``same'' @tab 1
@end multitable

The fifth variation should have an attached pattern.  Note that the stone
color for the fifth variation is ``same'' because the first matched stone
for this pattern is @samp{O} which stands for the stones of the player
to whom moves are being suggested with @samp{*}.

The tree consists of all variations for all patterns combined together.
Variations for each patterns are sorted to allow very quick tree branch
rejection and at the same time keep the database small enough.  More
details can be found in comments in file @file{mkpat.c}

Corner matcher resides in @file{matchpat.c} in two functions:
@code{corner_matchpat()} and @code{do_corner_matchpat()}.  The former computes
@code{num_stones[]} array which holds number of stones in corner areas of
different intersections of the board for all possible transformations.
@code{corner_matchpat()} also matches top-level variations.
@code{do_corner_matchpat()} is responsible for recursive matching on the
variation tree and calling callback function upon pattern match.

Tree-like database for corner matcher is generated by @code{mkpat} program.
Database generator consists of several functions, most important are:
@code{corner_best_element()}, @code{corner_variation_new()},
@code{corner_follow_variation()} and @code{corner_add_pattern()}.

@node Editing Patterns
@section Emacs Mode for Editing Patterns

@cindex editing patterns
@cindex editing pattern database

If you use GNU Emacs (XEmacs might work too), you can try a special
mode for editing GNU Go pattern databases.  The mode resides in
@file{patterns/gnugo-db.el}.

Copy the file to @file{emacs/site-lisp} directory.  You can then load
the mode with @code{(require 'gnugo-db)}.  It makes sense to put this
line into your configuration file (@file{~/.emacs}).  You can either
use @command{gnugo-db-mode} command to switch to pattern editing mode,
or use the following code snippet to make Emacs do this automatically
upon opening a file with @file{.db} suffix:

@example
	(setq auto-mode-alist
	      (append
	       auto-mode-alist
	       '(("\\.db\\'" . gnugo-db-mode))))
@end example

Pattern editing mode provides the following features:

@itemize @minus
@item
highlighting of keywords (@code{Pattern}, @code{goal_elements} and
@code{callback_data}) and comments,

@item
making paragraphs equal to patterns (@kbd{M-h}, @kbd{M-@{}, @kbd{M-@}}
and others operate on patterns),

@item
commands for pattern creation with automatic name selection (@kbd{C-c
C-p}) and copying main diagram to constraint diagram (@kbd{C-c C-c}),

@item
automated indentation of constraints and side comments (pattern
descriptions).
@end itemize