The purpose of this Chapter is to describe the algorithm used in GNU Go to determine eyes. @menu * Local Games:: Local games * Eye Space:: Eye space * Eye Space as Local Game:: Eye space as local game * Eye Example:: An example * Graphs:: Underlying graphs * Eye Shape:: Pattern matching * Eye Local Game Values:: Pattern matching * Eye Topology:: False eyes and half eyes * Eye Topology with Ko:: False eyes and half eyes with ko * False Margins:: False margins * Eye Functions:: Functions in @file{optics.c} @end menu @node Local Games @section Local games The fundamental paradigm of combinatorial game theory is that games can be added and in fact form a group. If @samp{G} and @samp{H} are games, then @samp{G+H} is a game in which each player on his turn has the option of playing in either move. We say that the game @samp{G+H} is the sum of the local games @samp{G} and @samp{H}. Each connected eyespace of a dragon affords a local game which yields a local game tree. The score of this local game is the number of eyes it yields. Usually if the players take turns and make optimal moves, the end scores will differ by 0 or 1. In this case, the local game may be represented by a single number, which is an integer or half integer. Thus if @samp{n(O)} is the score if @samp{O} moves first, both players alternate (no passes) and make alternate moves, and similarly @samp{n(X)}, the game can be represented by @samp{@{n(O)|n(X)@}}. Thus @{1|1@} is an eye, @{2|1@} is an eye plus a half eye, etc. The exceptional game @{2|0@} can occur, though rarely. We call an eyespace yielding this local game a CHIMERA. The dragon is alive if any of the local games ends up with a score of 2 or more, so @{2|1@} is not different from @{3|1@}. Thus @{3|1@} is NOT a chimera. Here is an example of a chimera: @example @group XXXXX XOOOX XO.OOX XX..OX XXOOXX XXXXX @end group @end example @node Eye Space @section Eye spaces In order that each eyespace be assignable to a dragon, it is necessary that all the dragons surrounding it be amalgamated (@pxref{Amalgamation}). This is the function of @code{dragon_eye()}. An EYE SPACE for a black dragon is a collection of vertices adjacent to a dragon which may not yet be completely closed off, but which can potentially become eyespace. If an open eye space is sufficiently large, it will yield two eyes. Vertices at the edge of the eye space (adjacent to empty vertices outside the eye space) are called MARGINAL. Here is an example from a game: @example @group |. X . X X . . X O X O |X . . . . . X X O O O |O X X X X . . X O O O |O O O O X . O X O O O |. . . . O O O O X X O |X O . X X X . . X O O |X O O O O O O O X X O |. X X O . O X O . . X |X . . X . X X X X X X |O X X O X . X O O X O @end group @end example Here the @samp{O} dragon which is surrounded in the center has open eye space. In the middle of this open eye space are three dead @samp{X} stones. This space is large enough that O cannot be killed. We can abstract the properties of this eye shape as follows. Marking certain vertices as follows: @example @group |- X - X X - - X O X O |X - - - - - X X O O O |O X X X X - - X O O O |O O O O X - O X O O O |! . . . O O O O X X O |X O . X X X . ! X O O |X O O O O O O O X X O |- X X O - O X O - - X |X - - X - X X X X X X |O X X O X - X O O X O @end group @end example @noindent the shape in question has the form: @example @group !... .XXX.! @end group @end example The marginal vertices are marked with an exclamation point (@samp{!}). The captured @samp{X} stones inside the eyespace are naturally marked @samp{X}. The precise algorithm by which the eye spaces are determined is somewhat complex. Documentation of this algorithm is in the comments in the source to the function @code{make_domains()} in @file{optics.c}. The eyespaces can be conveniently displayed using a colored ascii diagram by running @command{gnugo -E}. @node Eye Space as Local Game @section The eyespace as local game In the abstraction, an eyespace consists of a set of vertices labelled: @example ! . X @end example Tables of many eyespaces are found in the database @file{patterns/eyes.db}. Each of these may be thought of as a local game. The result of this game is listed after the eyespace in the form @code{:max,min}, where @code{max} is the number of eyes the pattern yields if @samp{O} moves first, while @code{min} is the number of eyes the pattern yields if @samp{X} moves first. The player who owns the eye space is denoted @samp{O} throughout this discussion. Since three eyes are no better than two, there is no attempt to decide whether the space yields two eyes or three, so max never exceeds 2. Patterns with min>1 are omitted from the table. For example, we have: @example @group Pattern 548 x xX.! :0111 @end group @end example Here notation is as above, except that @samp{x} means @samp{X} or @code{EMPTY}. The result of the pattern is not different if @samp{X} has stones at these vertices or not. We may abstract the local game as follows. The two players @samp{O} and @samp{X} take turns moving, or either may pass. RULE 1: @samp{O} for his move may remove any vertex marked @samp{!} or marked @samp{.}. RULE 2: @samp{X} for his move may replace a @samp{.} by an @samp{X}. RULE 3: @samp{X} may remove a @samp{!}. In this case, each @samp{.} adjacent to the @samp{!} which is removed becomes a @samp{!} . If an @samp{X} adjoins the @samp{!} which is removed, then that @samp{X} and any which are connected to it are also removed. Any @samp{.} which are adjacent to the removed @samp{X}'s then become @samp{.}. Thus if @samp{O} moves first he can transform the eyeshape in the above example to: @example @group ... or !... .XXX.! .XXX. @end group @end example However if @samp{X} moves he may remove the @samp{!} and the @samp{.}s adjacent to the @samp{!} become @samp{!} themselves. Thus if @samp{X} moves first he may transform the eyeshape to: @example @group !.. or !.. .XXX.! .XXX! @end group @end example NOTE: A nuance which is that after the @samp{X:1}, @samp{O:2} exchange below, @samp{O} is threatening to capture three X stones, hence has a half eye to the left of 2. This is subtle, and there are other such subtleties which our abstraction will not capture. Some of these at least can be dealt with by a refinements of the scheme, but we will content ourselves for the time being with a simplified model. @example @group |- X - X X - - X O X O |X - - - - - X X O O O |O X X X X - - X O O O |O O O O X - O X O O O |1 2 . . O O O O X X O |X O . X X X . 3 X O O |X O O O O O O O X X O |- X X O - O X O - - X |X - - X - X X X X X X |O X X O X - X O O X O @end group @end example We will not attempt to characterize the terminal states of the local game (some of which could be seki) or the scoring. @node Eye Example @section An example Here is a local game which yields exactly one eye, no matter who moves first: @example @group ! ... ...! @end group @end example Here are some variations, assuming @samp{O} moves first. @example @group ! (start position) ... ...! @end group @group ... (after @samp{O}'s move) ...! @end group @group ... ..! @end group @group ... .. @end group @group .X. (nakade) .. @end group @end example Here is another variation: @example @group ! (start) ... ...! @end group @group ! (after @samp{O}'s move) . . ...! @end group @group ! (after @samp{X}'s move) . . ..X! @end group @group . . ..X! @end group @group . ! .! @end group @end example @node Graphs @section Graphs It is a useful observation that the local game associated with an eyespace depends only on the underlying graph, which as a set consists of the set of vertices, in which two elements are connected by an edge if and only if they are adjacent on the Go board. For example the two eye shapes: @example .. .. and .... @end example @noindent though distinct in shape have isomorphic graphs, and consequently they are isomorphic as local games. This reduces the number of eyeshapes in the database @file{patterns/eyes.db}. A further simplification is obtained through our treatment of half eyes and false eyes. Such patterns are identified by the topological analysis (@pxref{Eye Topology}). A half eye is isomorphic to the pattern @code{(!.)} . To see this, consider the following two eye shapes: @example @group XOOOOOO X.....O XOOOOOO @end group and: @group XXOOOOO XOa...O XbOOOOO XXXXXXX @end group @end example These are equivalent eyeshapes, with isomorphic local games @{2|1@}. The first has shape: @example !.... @end example The second eyeshape has a half eye at @samp{a} which is taken when @samp{O} or @samp{X} plays at @samp{b}. This is found by the topological criterion (@pxref{Eye Topology}). The graph of the eye_shape, ostensibly @samp{....} is modified by replacing the left @samp{.} by @samp{!.} during graph matching. A false eye is isomorphic to the pattern @code{(!)} . To see this, consider the following eye shape: @example XXXOOOOOO X.Oa....O XXXOOOOOO @end example This is equivalent to the two previous eyeshapes, with an isomorphic local game @{2|1@}. This eyeshape has a false eye at @samp{a}. This is also found by the topological criterion. The graph of the eye_shape, ostensibly @samp{.....} is modified by replacing the left @samp{.} by @samp{!}. This is made directly in the eye data, not only during graph matching. @node Eye Shape @section Eye shape analysis The patterns in @file{patterns/eyes.db} are compiled into graphs represented essentially by arrays in @file{patterns/eyes.c}. Each actual eye space as it occurs on the board is also compiled into a graph. Half eyes are handled as follows. Referring to the example @example @group XXOOOOO XOa...O XbOOOOO XXXXXX @end group @end example @noindent repeated from the preceding discussion, the vertex at @samp{b} is added to the eyespace as a marginal vertex. The adjacency condition in the graph is a macro (in @file{optics.c}): two vertices are adjacent if they are physically adjacent, or if one is a half eye and the other is its key point. In @code{recognize_eyes()}, each such graph arising from an actual eyespace is matched against the graphs in @file{eyes.c}. If a match is found, the result of the local game is known. If a graph cannot be matched, its local game is assumed to be @{2|2@}. @node Eye Local Game Values @section Eye Local Game Values The game values in @file{eyes.db} are given in a simplified scheme which is flexible enough to represent most possibilities in a useful way. The colon line below the pattern gives the eye value of the matched eye shape. This consists of four digits, each of which is the number of eyes obtained during the following conditions: @enumerate @item The attacker moves first and is allowed yet another move because the defender plays tenuki. @item The attacker moves first and the defender responds locally. @item The defender moves first and the attacker responds locally. @item The defender moves first and is allowed yet another move because the attacker plays tenuki. @end enumerate The first case does @strong{not} necessarily mean that the attacker is allowed two consecutive moves. This is explained with an example later. Also, since two eyes suffice to live, all higher numbers also count as two. The following 15 cases are of interest: @itemize @bullet @item 0000 0 eyes. @item 0001 0 eyes, but the defender can threaten to make one eye. @item 0002 0 eyes, but the defender can threaten to make two eyes. @item 0011 1/2 eye, 1 eye if defender moves first, 0 eyes if attacker does. @item 0012 3/4 eyes, 3/2 eyes if defender moves first, 0 eyes if attacker does. @item 0022 1* eye, 2 eyes if defender moves first, 0 eyes if attacker does. @item 0111 1 eye, attacker can threaten to destroy the eye. @item 0112 1 eye, attacker can threaten to destroy the eye, defender can threaten to make another eye. @item 0122 5/4 eyes, 2 eyes if defender moves first, 1/2 eye if attacker does. @item 0222 2 eyes, attacker can threaten to destroy both. @item 1111 1 eye. @item 1112 1 eye, defender can threaten to make another eye. @item 1122 3/2 eyes, 2 eyes if defender moves first, 1 eye if attacker does. @item 1222 2 eyes, attacker can threaten to destroy one eye. @item 2222 2 eyes. @end itemize The 3/4, 5/4, and 1* eye values are the same as in Howard Landman's paper Eyespace Values in Go. Attack and defense points are only marked in the patterns when they have definite effects on the eye value, i.e. pure threats are not marked. Examples of all different cases can be found among the patterns in this file. Some of them might be slightly counterintuitive, so we explain one important case here. Consider @example @group Pattern 6141 X XX.@@x :1122 @end group @end example which e.g. matches in this position: @example @group .OOOXXX OOXOXOO OXXba.O OOOOOOO @end group @end example Now it may look like @samp{X} could take away both eyes by playing @samp{a} followed by @samp{b}, giving 0122 as eye value. This is where the subtlety of the definition of the first digit in the eye value comes into play. It does not say that the attacker is allowed two consecutive moves but only that he is allowed to play "another move". The crucial property of this shape is that when @samp{X} plays at a to destroy (at least) one eye, @samp{O} can answer at @samp{b}, giving: @example @group .OOOXXX OO.OXOO O.cOX.O OOOOOOO @end group @end example Now @samp{X} has to continue at @samp{c} in order to keep @samp{O} at one eye. After this @samp{O} plays tenuki and @samp{X} cannot destroy the remaining eye by another move. Thus the eye value is indeed 1122. As a final note, some of the eye values indicating a threat depend on suicide to be allowed, e.g. @example @group Pattern 301 X.X :1222 @end group @end example We always assume suicide to be allowed in this database. It is easy enough to sort out such moves at a higher level when suicide is disallowed. @node Eye Topology @section Topology of Half Eyes and False Eyes A HALF EYE is a pattern where an eye may or may not materialize, depending on who moves first. Here is a half eye for @code{O}: @example @group OOXX O.O. OO.X @end group @end example A FALSE EYE is an eye vertex which cannot become a proper eye. Here are two examples of false eyes for @code{O}: @example @group OOX OOX O.O O.OO XOO OOX @end group @end example We describe now the topological algorithm used to find half eyes and false eyes. In this section we ignore the possibility of ko. False eyes and half eyes can locally be characterized by the status of the diagonal intersections from an eye space. For each diagonal intersection, which is not within the eye space, there are three distinct possibilities: @itemize @bullet @item occupied by an enemy (@code{X}) stone, which cannot be captured. @item either empty and @code{X} can safely play there, or occupied by an @code{X} stone that can both be attacked and defended. @item occupied by an @code{O} stone, an @code{X} stone that can be attacked but not defended, or it's empty and @code{X} cannot safely play there. @end itemize We give the first possibility a value of two, the second a value of one, and the last a value of zero. Summing the values for the diagonal intersections, we have the following criteria: @itemize @bullet @item sum >= 4: false eye @item sum == 3: half eye @item sum <= 2: proper eye @end itemize If the eye space is on the edge, the numbers above should be decreased by 2. An alternative approach is to award diagonal points which are outside the board a value of 1. To obtain an exact equivalence we must however give value 0 to the points diagonally off the corners, i.e. the points with both coordinates out of bounds. The algorithm to find all topologically false eyes and half eyes is: For all eye space points with at most one neighbor in the eye space, evaluate the status of the diagonal intersections according to the criteria above and classify the point from the sum of the values. @node Eye Topology with Ko @section Eye Topology with Ko This section extends the topological eye analysis to handle ko. We distinguish between a ko in favor of @samp{O} and one in favor of @samp{X}: @example @group .?O? good for O OO.O O.O? XOX. .X.. @end group @group .?O? good for X OO.O OXO? X.X. .X.. @end group @end example Preliminarily we give the former the symbolic diagonal value @code{a} and the latter the diagonal value @code{b}. We should clearly have @code{0 < a < 1 < b < 2}. Letting @code{e} be the topological eye value (still the sum of the four diagonal values), we want to have the following properties: @example e <= 2 - proper eye 2 < e < 3 - worse than proper eye, better than half eye e = 3 - half eye 3 < e < 4 - worse than half eye, better than false eye e >= 4 - false eye @end example In order to determine the appropriate values of @code{a} and @code{b} we analyze the typical cases of ko contingent topological eyes: @example @group .X.. (slightly) better than proper eye (a) ..OO e < 2 OO.O O.OO e = 1 + a XOX. .X.. @end group @group .X.. better than half eye, worse than proper eye (a') ..OO 2 < e < 3 OO.O OXOO e = 1 + b X.X. .X.. @end group @group .X.. better than half eye, worse than proper eye (b) .XOO 2 < e < 3 OO.O O.OO e = 2 + a XOX. .X.. @end group @group .X.. better than false eye, worse than half eye (b') .XOO 3 < e < 4 OO.O OXOO e = 2 + b X.X. .X.. @end group @group .X.. XOX. (slightly) better than proper eye (c) O.OO e < 2 OO.O O.OO e = 2a XOX. .X.. @end group @group .X.. XOX. proper eye, some aji (c') O.OO e ~ 2 OO.O OXOO e = a + b X.X. .X.. @end group @group .X.. X.X. better than half eye, worse than proper eye (c'') OXOO 2 < e < 3 OO.O OXOO e = 2b X.X. .X.. @end group @group .X... XOX.. better than half eye, worse than proper eye (d) O.O.X 2 < e < 3 OO.O. O.OO. e = 1 + 2a XOX.. .X... @end group @group .X... XOX.. half eye, some aji (d') O.O.X e ~ 3 OO.O. OXOO. e = 1 + a + b X.X.. .X... @end group @group .X... X.X.. better than false eye, worse than half eye (d'') OXO.X 3 < e < 4 OO.O. OXOO. e = 1 + 2b X.X.. .X... @end group @group .X... XOX.. better than false eye, worse than half eye (e) O.OXX 3 < e < 4 OO.O. O.OO. e = 2 + 2a XOX.. .X... @end group @group .X... XOX.. false eye, some aji (e') O.OXX e ~ 4 OO.O. OXOO. e = 2 + a + b X.X.. .X... @end group @group .X... X.X.. (slightly) worse than false eye (e'') OXOXX 4 < e OO.O. OXOO. e = 2 + 2b X.X.. .X... @end group @end example It may seem obvious that we should use @example (i) a=1/2, b=3/2 @end example but this turns out to have some drawbacks. These can be solved by using either of @example (ii) a=2/3, b=4/3 (iii) a=3/4, b=5/4 (iv) a=4/5, b=6/5 @end example Summarizing the analysis above we have the following table for the four different choices of @code{a} and @code{b}. @example case symbolic a=1/2 a=2/3 a=3/4 a=4/5 desired value b=3/2 b=4/3 b=5/4 b=6/5 interval (a) 1+a 1.5 1.67 1.75 1.8 e < 2 (a') 1+b 2.5 2.33 2.25 2.2 2 < e < 3 (b) 2+a 2.5 2.67 2.75 2.8 2 < e < 3 (b') 2+b 3.5 3.33 3.25 3.2 3 < e < 4 (c) 2a 1 1.33 1.5 1.6 e < 2 (c') a+b 2 2 2 2 e ~ 2 (c'') 2b 3 2.67 2.5 2.4 2 < e < 3 (d) 1+2a 2 2.33 2.5 2.6 2 < e < 3 (d') 1+a+b 3 3 3 3 e ~ 3 (d'') 1+2b 4 3.67 3.5 3.4 3 < e < 4 (e) 2+2a 3 3.33 3.5 3.6 3 < e < 4 (e') 2+a+b 4 4 4 4 e ~ 4 (e'') 2+2b 5 4.67 4.5 4.4 4 < e @end example We can notice that (i) fails for the cases (c''), (d), (d''), and (e). The other three choices get all values in the correct intervals. The main distinction between them is the relative ordering of (c'') and (d) (or analogously (d'') and (e)). If we do a more detailed analysis of these we can see that in both cases @samp{O} can secure the eye unconditionally if he moves first while @samp{X} can falsify it with ko if he moves first. The difference is that in (c''), @samp{X} has to make the first ko threat, while in (d), O has to make the first ko threat. Thus (c'') is better for O and ought to have a smaller topological eye value than (d). This gives an indication that (iv) is the better choice. We can notice that any value of @code{a}, @code{b} satisfying @code{a+b=2} and @code{3/4