File: MOYO

package info (click to toggle)
gnugo 2.4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,816 kB
  • ctags: 1,828
  • sloc: ansic: 22,091; tcl: 401; sh: 376; makefile: 202
file content (632 lines) | stat: -rw-r--r-- 20,833 bytes parent folder | download
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
                   BOUZY'S 5/21 ALGORITHM

Bouzy's dissertation is available at:

ftp://www.joy.ne.jp/welcome/igs/Go/computer/bbthese.ps.Z.

It contains an algorithm inspired by prior work of Zobrist and ideas from
computer vision for determining territory. This algorithm is based on two
simple operations, dilation and erosion. Applying dilation 5 times and erosion
21 times determines the territory.

The moyos found by GNU Go can be displayed from an rxvt window or from the
Linux console using the -m option. This takes a parameter:

-m [level]
   use cumulative values for printing these debug reports :
    1 = ascii printing of territorial evaluation (5/21)
    2 = table of delta_terri values
    4 = ascii printing of moyo evaluation (5/10)
    8 = table of delta_moyo values
   16 = ascii printing of area (weak groups?) (4/0)
   32 = list of area characteristics
   64 = table of meta_connect values
  128 = trace -p fearless option (big_move priority)
 (sample: -m 9)

Referring to page 86 of Bouzy's thesis:

We start with a function taking a high value (ex : +128 for black, -128 for
white) on stones on the goban, 0 to empty intersections. We may iterate the
following operations:

- dilation : for each intersection of the goban, if the intersection
is >= 0, and not adjacent to a <0 one, then add to the intersection
the number of adjacent >0 intersections. The same for other color : if
the intersection is <=0, and not adjacent to a >0 one, then sub to
it the number of <0 intersections.

- erosion : for each intersection >0 (or <0), substract (or add) the
number of adjacent <=0 (or >=0) intersection. Stop at zero.

It's unbelievable, but it works.

the alorithm is just : 5 dilations, then 21 erosion. The number of
erosions should be 1+n(n-1) where n=number of dilation, since this
permit to have an isolated stone to give no territory. Thus the
couple 4/13 also works, but it is often not good, for example when
there is territorry on the 6th line.


sample, a tobi :


           128    0    128   

1 dilation :


            1          1 

       1   128    2   128   1

            1          1
            
2 dilations :


            1          1

       2    2     3    2    2

   1   2   132    4   132   2   1

       2    2     3    2    2
              
            1          1


3 dilations :


            1          1

       2    2     3    2    2
     
   2   4    6     6    6    4   2

1  2   6   136    8   136   6   2   1

   2   4    6     6    6    4   2

       2    2     3    2    2

            1          1


and so on...

next, with the same example 

3 dilations and 1 erosion :



           2     2     2

   0  4    6     6     6    4

0  2  6   136    8    136   6    2

   0  4    6     6     6    4

           2     2     2



3 dilations and 2 erosions :



                 1

      2    6     6     6    2

      6   136    8    136   6

      2    6     6     6    2
      
                 1


3 dil. / 3 erosions :



           5     6     5

      5   136    8    136   5
      
           5     6     5
           
           
3/4 :


          3     5     3 
          
      2  136    8    136   2          
           
          3     5     3
          
          
3/5 :


          1     4     1

         136    8    136
          
          1     4     1
          

3/6 :


                3
         
         135    8    135
         
                3

3/7 :


         132    8    132
         

     I interpret this as a 1 point territory.


> A question which Gunnar raised is whether the calculation can be
> performed incrementally. Thus if the position has already been
> analyzed, is it possible to tell without fully redoing the
> calculation how placing a single stone will affect the score?

Theorically, an intersection can be reached by the consequences 
of a stone at 5 intersections for dilation, and... 21 for erosion.
But the worst is that intersections are closely inter dependent : I
don't know how to do that incrementally.

dilation is fast : no need to do complex incrementation.

erosion : long and costly (I didnt manage to do it by binary operators)
It is possible to have fast "approximation" of erosion via binary
operator, but they quickly give an absurd result. Should be possible to
do "incremental" erosion if the added stone is isolated, possible 
algorithm : 

- have a fast signature (or hash table) of the currently eroding goban
  positions,
- next move, add a stone, dilat 5 times, and begin to erode
- during the erode phase, check the signature of position : if the
  same as the precedent move, at the same position in the erode cycle,
  then the final position will be the same : shunt and go directly to
  the solution.
  
so a problem : getting a fast hash table, faster or of the same
duration 
as an erode cycle...

Another possibility : use a simplified algorithm + some additionnal
fast rules to achieve a correct estimation. Maybe 3/7 plus rules 
could be very fast.


IMPLEMENTATION 
==============

The file moyo.c currently uses this algorithm by three ways : 5/21 for 
territory evaluation, 5/10 for moyo evaluation, and 4/0 for "area ownership",
aka "big moyo" and meta cut/connect (uchikomi,...). Beware, these evaluation
don't care of the life and death status of groups. It's only a "graphical"
analysis of areas of influence.

After dragon evaluation, the function make_moyo() is called once to
make the static evaluation of the goban : make_moyo() returns the difference 
in estimated territory (terri_eval[0]) and computes terri_eval[3] and
moyo_eval[3]. It also computes the area_grid for area ownership & (future) 
weak group analysis. All functions assume that evaluated DEAD in the Dragon
structure are really dead, and act as they were removed from the board.
Technically, the dilations are made with binary operators (one row of the
goban is stored in two integer, one black and one white), then the result
is stored in a classical aray [19][19] for the erode computation. The algo
is quite simple, but the code may look obfuscated for this reason.

This functions can be used with a color argument whose value is for current 
player or for opponent color, aka OTHER_COLOR(color) :
delta_terri, diff_terri, delta_terri_color, delta_moyo, diff_moyo,
delta_moyo_color, meta_connect, delta_area_color



The 5,21,10.. values are stored in defines :

#define MAX_DILAT 5
#define MAX_ERODE 21
/* 4 or 5 */
#define MOY_DILAT 5    /* for delta_moyo */
/* must MOY_ERODE <= MAX_ERODE if MOY_DILAT != MAX_DILAT*/
#define MOY_ERODE 10

/* number of dilation for area ownership, must be <= MAX_DILAT */
#define OWNER_DILAT 4



5/21 : territory
----------------
5 dilate/21 erode to get the image of potential territory. This values 
seem to have these properties :
   - to have accurate evaluation, dead stones must be "removed" 
   - 5 dilations permit to be sure to have influence on very large territory
   - 21 erosions induce that "isolated" stones dont make territory, they just
   limit the expansion of other color influence.
   - in the end of the game, the evaluated territory match the actual score :-)


the public functions are :

int delta_terri(int ti, int tj, int color);
   - test the ti tj move as regards territorial evaluation. This evaluation
   take care of prisonners, but no komi is added. The returned value is the
   difference in territorial evaluation between terri_test and first call 
   to make_moyo() the evaluation is for "color - OTHER_COLOR".
   - tested values are cached, if different patterns test the same ti tj,
   no extra computation is needed.
   Nota: this function is not really used in GNUGo : future usage could be
   for a end game module, or for displaying who has the lead in the game.

int diff_terri(int ti, int tj, int color);
   - wrapper for : delta_terri(..,color)+delta_terri(..,other_color)

int terri_color(int m,int n);
   - returns the color (WHITE,BLACK,EMPTY) of the m,n intersection, as seen
     by the 5/21 algorithm. This is a public access to already computed 
     values during make_moyo().
     
int delta_terri_color(int ti,int tj, int color, int m, int n);
   - returns the color (WHITE,BLACK,EMPTY) of the m,n intersection, as seen
     by the 5/21 algorithm, after a test move in ti,tj. The values of this
     function are computed and cached during the delta_terri() evaluation.
     Calling this function also computes and cache delta_terri().
     (see nota about caching delta_*_color() functions)
     
extern variables :

int terri_eval[3] computed once by make_moyo()
   terri_eval[WHITE] : white territory
   terri_eval[BLACK] : black territory
   terri_eval[0] : difference in territory (color - other_color)
   
int terri_test[3] computed by delta_terri()
   terri_test[WHITE] : territory evaluation from delta_terri() for BLACK
   terri_test[BLACK] : ...
   terri_test[0] : return of delta_terri(), difference in territorial
   evaluation between terri_test and first call to make_moyo 
   
Sample: 'b' is for black estimated territory (X), 'w' for White (O)
white to play : a move to J11 will bring +7 territorial balance.  
(computers are very precise, you now)


   A B C D E F G H J K L M N
13 b b b b . . . . . . . . . 13
12 b b b X b . . . . . . . . 12
11 b b b b b . . . . . O . . 11
10 b b b X b . . . . . . . . 10
 9 b b b b . . . . . . X . . 9
 8 b b X b . . . . . . . . . 8     White territory 22
 7 b b b . . . . . . . . . . 7
 6 . b b . . . . . . . . . . 6     Black territory 30
 5 . . b . . . . . . . O . . 5
 4 . . . X . . . . w w w w w 4   
 3 . . . . . . O w w O w w w 3
 2 . . . . . . . w w w w w w 2
 1 . . . . . . . w w w w w w 1
   A B C D E F G H J K L M N
   
delta_terri :
 20 21 20 19 11 10 10  8  7  6  3  4  3
 23 23 21  X 12 11 13 10  8  6  3  3  4
 25 26 25 22 10 11  8  8  7  6  O  5  5
 24 26 27  X  9  8  8  5  5  2  3  7  7
 25 27 26 13 10  7  7  7  5  4  X 11  8
 23 25  X 12 10  9  6  8  5  6  5 10  5
 21 19 18 13 11 11 11 10  6  5  5  4  5
 14 14 14 13 11 12 13  7  5  2  2  2  2
 14 12 11 11 12 11  7  5  2  0  O  1  1
 11 10  9  X  9  5  4  2  0 -1 -1 -1  1
  9  8 14  7  4  3  O -1 -1  O -1 -1 -1
  8  7  6  7  6  2  1 -1 -1 -1 -1 -1 -1
  5  4  5  4  3  2  1  0 -1 -1 -1 -1 -1




5/10 : moyo
-----------
5 dilations and 10 erode to get the value of moyo. Moyo is more usefull
than territorial 5/21 since it permit to evaluate immediately the value
of a move. It is intended to be used in conjunction with some patterns
as an helper. The value 5 and 10 are empiric, other could have a similar
effect : 4/8, 5/9 , etc...  Using 5 dilation permit to use some common
results with territorial evaluation 5/21. The moyo evaluation does not
count prisonners neither komi, but takes in account Dragon DEAD stones.


the public functions are :

int delta_moyo(int ti, int tj, int color);
   - test the ti tj move as regards moyo evaluation.  The returned value 
   is the difference in territorial evaluation between moyo_test and first
   call to make_moyo() the evaluation is for "color - OTHER_COLOR".
   - tested values are cached, if different patterns test the same ti tj,
   no extra computation is needed.

int diff_moyo(int ti, int tj, int color);
   - wrapper for : delta_moyo(..,color)+delta_moyo(..,other_color)

int moyo_color(int m,int n);
   - returns the color (WHITE,BLACK,EMPTY) of the m,n intersection, as seen
     by the 5/10 algorithm. This is a public access to already computed 
     values during make_moyo().
     
int delta_moyo_color(int ti,int tj, int color, int m, int n);
   - returns the color (WHITE,BLACK,EMPTY) of the m,n intersection, as seen
     by the 5/10 algorithm, after a test move in ti,tj. The values of this
     function are NOT cached during the delta_moyo() evaluation. But calling
     this function caches his result for future call, and also computes and 
     cache delta_moyo(ti,tj,color).
     (see nota about caching delta_*_color() functions)

extern variables :

int moyo_eval[3] is computed once by make_moyo()
   moyo_eval[WHITE] : white moyo evaluation 
   moyo_eval[BLACK] : black moyo evaluation
   moyo_eval[0] : difference in moyo (color - other_color)
   
int moyo_test[3] is computed by delta_moyo for testing one move   
   moyo_test[WHITE] : white moyo evaluation from delta_moyo()
   moyo_test[BLACK] : ...
   moyo_test[0] : return of delta_moyo(), difference in moyo between 
   test moyo and first moyo evaluation (color - other_color)

sample: white to play. A move at F4 would increase moyo balance by 20
points for white.


   A B C D E F G H J K L M N
13 b b b b b b b b b b . . . 13
12 b b b b b b b b b b . . . 12
11 b b b b X b b b b b . . . 11
10 b b X b b b b b b X . . . 10
 9 b b b b b b . . . . . . . 9
 8 b b b b b . . . . . O w . 8     White territory 18
 7 . . b X b . . . . . w w w 7
 6 . . . . . . . . . w w w w 6     Black territory 32
 5 . . . . . . . . . w w w w 5
 4 . . w O w . . . w w w w w 4   W/B moyo 36/50 : -14
 3 . . w w w w . . w w O w w 3
 2 . . . w . . . . . w w w w 2
 1 . . . . . . . . . w w w w 1
   A B C D E F G H J K L M N
   
delta_moyo :
 15 17 19 23 24 26 21 21 18 19 15 11  9
 18 20 20 24 29 29 24 23 20 21 20 14  8
 17 23 19 16  X 26 33 31 21 19 25 14  8
 16 20  X 15 16 35 34 32 29  X 13 10  5
 16 16 18 15 16 17 23 39 19  7  4  4  2
 15 16 13 29 17 25 24 20 12  6  O  0  0
 14 16 17  X 23 23 21 18 14  6  1  0  0
 20 13 13 13 16 19 31 14 11  7  3  0  0
 17 16  6  8  9 25 25 23  8  5  2 -1  0
 13 14 12  O 17 20 21 19 17  3  2 -1 -1
 11 11  9 22 13 17 17 17 16 14  O -1 -1
 11  9 21 20 21 13 16 15 14 12 12 -1 -1
  9 21 20 20 20 21 13 14 12 12 12 12 -1





4/0 : area ownership
--------------------
This algorithm finds areas of influence, something bigger than classical moyo,
with light connection between stones. This tool is intended to find weak 
and strong groups very early in the game. Currently it is used as an helper
to find moves who cut ot connect these areas at a large scale. This module
of GNU Go will probably evolve.

The first use will be to test if a tested move will :

- cut one opponent group in two (without creating an isolated stone)
- meta-connect two groups

the public functions are :
int area_stone(int m,int n);
int area_space(int m,int n);
int area_color(int m,int n);
   - these functions return the number of stones, empty spaces and the color
     of the area around the m n intersection. They are just wrapper function
     to get data already stored in tables computed by make_moyo().

int area_tag(int m,int n);
void set_area_tag(int m,int n,int tag);
   - these funtions (currently unused) are wrappers to access to a tag value
     associated with an area (for example his weakness).

int meta_connect(int ti, int tj,int color);
   - test one move ti tj about consequences upon areas : if the number of
     areas of each color change, we can detect some of these events :
       - cut one opponent group in two (without creating an isolated stone)
       - meta-connect two groups of player color
     meta_connect returns 15 point for each connection and 10 point for each
     cut. The objective is to give GNUGo the ability to lauch early attacks
     on weak groups, and connect his own groups.
     
     Nota: the area ownership algorithm is a little more complex than 5/21
     or 5/10 ones, for two reasons: we need to correctly analyse the 
     connection of two area by a secure kosumi stone, and the sum of areas
     is computed by recursive functions.

int delta_area_color(int ti,int tj, int color, int m, int n);
   - returns the color (WHITE,BLACK,EMPTY) of the m,n intersection, as seen
     by the 4/0 algorithm, after a test move in ti,tj. The values of this
     function are NOT cached during the meta_connect() evaluation. But calling
     this function caches his result for future call, and also computes and 
     cache meta_connect(ti,tj,color).
     (see nota about caching delta_*_color() functions)
        
               
The values for cutting/connecting can be changed (all this need tuning):

/* number of bonus point for each group connected and opponent group cut */
#define GR_BONUS_CONNECT 15
#define GR_BONUS_CUT 10

Sample: 

The 'b' black area are changed to '-' for readibility. A white move at
K5 got 25 points : this means that meta_connect thinks it would separate
the J3 stone from K10, and connect the white stones together:


   A B C D E F G H J K L M N
13 . . - - . w w . - - - . . 13
12 . - - - . w w . - - - - . 12
11 - - - - . w w . - - - - - 11
10 - - - X . O w . - X - - - 10
 9 - - - - . w w . - - - - - 9
 8 - - X - - w w . - - - - . 8     White territory 2
 7 - - - - - w w . - - w w . 7
 6 - - - . . w . - - w w w w 6     Black territory 4
 5 . . . w w w - - - w w w w 5
 4 w w w w w w - - - w O w w 4   W/B moyo 19/24 : -5
 3 w w w O w w - - X - w w w 3
 2 w w w w w w - - - - w w w 2
 1 . w w w w w - - - - w w . 1
   A B C D E F G H J K L M N
   
area 2  A11: color B,  2 stone  28 spaces
area 4  A4: color W,  2 stone  39 spaces
area 9  G5: color B,  2 stone  46 spaces
area 11  K6: color W,  1 stone  21 spaces

meta_connect :
  .  .  .  .  .  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .  .  .  .  .  .
  .  .  .  X  .  O  . 10 10  X  .  .  .
  .  .  .  .  .  . 10 10 25 25 10  .  .
  .  .  X  .  . 10 10 25 25 25 25 10  .
  .  .  .  . 10 10 10 25 25 25 25 25  .
  .  .  .  .  . 10 25 25 25 25 25 10  .
  .  .  .  .  .  . 25 25 25 25 10  .  .
  .  .  .  .  .  .  . 25 25 10  O  .  .
  .  .  .  O  .  .  .  .  X  .  .  .  .
  .  .  .  .  .  .  .  . 15  .  .  .  .
  .  .  .  .  .  .  . 15 15 15  .  .  .



After white K5, black played G3, now playing in the center could connect
all white forces.


   A B C D E F G H J K L M N
13 . . - - . w w . - - - . . 13
12 . - - - . w w . - - - - . 12
11 - - - - . w w . - - - - - 11
10 - - - X . O w . - X - - - 10
 9 - - - - . w w . - - - - - 9
 8 - - X - - w w . - - - - . 8     White territory 1
 7 - - - - - w . w w w w w . 7
 6 - - - . . . - w w w w w w 6     Black territory 4
 5 . . . w w - - w w O w w w 5
 4 w w w w w - - - - w O w w 4   W/B moyo 17/26 : -9
 3 w w w O w - X - X - w w w 3
 2 w w w w w - - - - - w w w 2
 1 . w w w w - - - - - w w . 1
   A B C D E F G H J K L M N
   
area 2  A11: color B,  2 stone  28 spaces
area 4  A4: color W,  1 stone  20 spaces
area 8  F13: color W,  1 stone  12 spaces
area 9  F5: color B,  2 stone  20 spaces
area 12  H7: color W,  2 stone  27 spaces
area 13  J13: color B,  1 stone  25 spaces

meta_connect :
  .  .  .  .  .  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  . 15  .  .  .  .  .
  .  .  .  X  .  O 15 15 15  X  .  .  .
  .  .  .  . 15 30 15 15 15 15 15  .  .
  .  .  X 15 30 30 30 15 15 15 15  .  .
  .  . 15 30 30 30 30 30 15 15 15  .  .
  .  . 15 30 30 30 30 30 15 15  .  .  .
  .  .  . 15 30 30 30 30 15  O  .  .  .
  .  .  .  . 15 30 30 15  .  .  O  .  .
  .  .  .  O  . 15  X 10  X  .  .  .  .
  .  .  .  .  .  .  .  .  .  .  .  .  .
  .  .  .  .  .  .  . 15  .  .  .  .  .



                   WEAK GROUPS

Weak dragons are tagged dragon[m][n].weak=CRITICAL. These are
defined as having 2 or more stones with between 0 and
20 points of area, computed using the 4/0 algorithm.

function:
int number_weak(int color);
   - returns the number of weak groups found for one color.


                  BIG MOVE PRIORITY

(experimental) the use of search_big_move function aim to evaluate the 
value of moves by an empiric rule. Then, if the move proposed by genmove()
got a lower priority, the big_move is played. Use option -p fearless to 
select it.

int very_big_move[3];
   - public variable, contains the best territorial move found, value and 
   position.

void search_big_move(int ti, int tj, int color, int val);
   - evaluate a proposed move, and keep it if it's the bigger found
     current evaluation rule :   
          dt * 0.9 + 15 + val * 0.7
     where val is the value of the move as proposed by shapes() and other
     modules, and dt is diff_terri(ti,tj,color)
   





CACHING OF delta_*_color() FUNCTIONS

This 3 functions use the same goban stack for storing their results. The
stack size is :

#define COLOR_STACK_SIZE 70
static goban_t boardstack[COLOR_STACK_SIZE];

This is intentionally left low to minimise memory usage. When the stack 
is full, the older values are suppressed when a new need of storage come.
(the stored values are available during one "movenum" turn)

- every call to delta_terri(ti,tj,color) use a stack level, available for
  further delta_terri_color(ti,tj,color,?,?) call. 
- since delta_moyo() (and meta_connect) are often called, they do not store 
  their result in this stack every time. Only when the delta_*_color() is 
  called.
  
Beware : all dead groups are considered as removed for these functions !