[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [gnugo-devel] fast_defense() improvement
From: |
Portela Fernando |
Subject: |
RE: [gnugo-devel] fast_defense() improvement |
Date: |
Sun, 30 May 2004 19:22:07 +0200 |
Hi,
A revised patch. The 'heuristical' stuff has been removed. Still no
breakage. Performance improvement is down to around 1%.
nando
- new function count_adjacent_stones() in board.c
- fast_defense() looks for neighbor opponent strings in atari
Index: engine/board.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/board.c,v
retrieving revision 1.98
diff -u -r1.98 board.c
--- engine/board.c 27 May 2004 00:08:46 -0000 1.98
+++ engine/board.c 30 May 2004 16:45:12 -0000
@@ -2383,6 +2383,41 @@
}
+/* Counts how many stones in str1 are directly adjacent to str2.
+ * A limit can be given in the maxstones parameter so that the
+ * function returns immediately. See fast_defense() in reading.c
+ */
+
+int
+count_adjacent_stones(int str1, int str2, int maxstones)
+{
+ int s1, s2;
+ int size;
+ int pos;
+ int k;
+ int count = 0;
+
+ ASSERT_ON_BOARD1(str1);
+ ASSERT1(IS_STONE(board[str1]), str1);
+ ASSERT_ON_BOARD1(str2);
+ ASSERT1(IS_STONE(board[str2]), str2);
+
+ s1 = string_number[str1];
+ s2 = string_number[str2];
+ size = string[s1].size;
+
+ /* Traverse the stones of the string, by following the cyclic chain. */
+ pos = FIRST_STONE(s1);
+ for (k = 0; k < size && count < maxstones; k++) {
+ if (NEIGHBOR_OF_STRING(pos, s2, board[str2]))
+ count++;
+ pos = NEXT_STONE(pos);
+ }
+
+ return count;
+}
+
+
/* chainlinks returns (in the (adj) array) the chains surrounding
* the string at (str). The number of chains is returned.
*/
Index: engine/board.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/board.h,v
retrieving revision 1.12
diff -u -r1.12 board.h
--- engine/board.h 27 May 2004 00:08:46 -0000 1.12
+++ engine/board.h 30 May 2004 16:45:12 -0000
@@ -302,6 +302,7 @@
/* Count the number of stones in a string. */
int countstones(int str);
int findstones(int str, int maxstones, int *stones);
+int count_adjacent_stones(int str1, int str2, int maxstones);
/* Special function for reading.c */
void incremental_order_moves(int move, int color, int string,
Index: engine/reading.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/reading.c,v
retrieving revision 1.143
diff -u -r1.143 reading.c
--- engine/reading.c 25 May 2004 03:13:45 -0000 1.143
+++ engine/reading.c 30 May 2004 16:45:16 -0000
@@ -1240,8 +1240,9 @@
fast_defense(int str, int liberties, int *libs, int *move)
{
int color = board[str];
- int k;
+ int j, k;
int goal_liberties = (stackp < fourlib_depth ? 5 : 4);
+ int adj, adjs[MAXCHAIN];
ASSERT1(libs != NULL, str);
ASSERT1(move != NULL, str);
@@ -1255,6 +1256,28 @@
return 1;
}
}
+
+ /* Check the cases where an opponent neighbor string is in
+ * atari.
+ */
+ adj = chainlinks2(str, adjs, 1);
+ for (j = 0; j < adj; j++) {
+ int lib;
+ int missing = goal_liberties - liberties;
+
+ findlib(adjs[j], 1, &lib);
+ /* Would the capture already gain enough liberties ?
+ * No need to test the case if the move is one of our liberties,
+ * it has already been done in the first loop of this function.
+ * Ko is not an option here, we're only looking for WINs.
+ */
+ if (!liberty_of_string(lib, str) && !is_ko(lib, color, NULL)
+ && count_adjacent_stones(adjs[j], str, missing) >= missing) {
+ *move = lib;
+ return 1;
+ }
+ }
+
return 0;
}