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
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *
* http://www.gnu.org/software/gnugo/ for more information. *
* *
* Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
* 2008 and 2009 by the Free Software Foundation. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation - version 3 or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License in file COPYING for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02111, USA. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* The code in this file implements persistent caching.
*
* The idea is that reading results are stored together with an
* "active area", i.e. the part of the board having an effect on the
* reading result. Thus if only moves outside of the active area has
* been played since the result was stored, it can be reused.
*
* The active areas are not known exactly but are estimated
* heuristically. The effects are that too large an active area
* reduces the efficiency of the caching scheme while too small an
* active area may cause an incorrect read result to be retrieved from
* the cache.
*
* Persistent caching has so far been implemented for tactical reading,
* owl reading, connection reading and break-in reading (with semeai
* reading planned for the future).
*
* The hotspot functions are intended to locate where the most
* expensive reading of either type is going on. This information can
* be estimated from the contents of the persistent caches since the
* most expensive readings are stored there with full information of
* spent reading nodes, involved strings or dragons, and active areas.
*/
#include "gnugo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "liberty.h"
#include "cache.h"
/* ================================================================ */
/* Data structures */
/* ================================================================ */
/* Used in active area. */
#define HIGH_LIBERTY_BIT 4
#define HIGH_LIBERTY_BIT2 8
#define MAX_READING_CACHE_DEPTH 5
#define MAX_READING_CACHE_SIZE 100
#define MAX_OWL_CACHE_DEPTH 0
#define MAX_OWL_CACHE_SIZE 150
#define MAX_CONNECTION_CACHE_DEPTH 5
#define MAX_CONNECTION_CACHE_SIZE 100
#define MAX_BREAKIN_CACHE_DEPTH 1
#define MAX_BREAKIN_CACHE_SIZE 150
#define MAX_SEMEAI_CACHE_DEPTH 0
#define MAX_SEMEAI_CACHE_SIZE 150
#define MAX_CACHE_DEPTH 5
/* We use the same data structure for all of the caches. Some of the entries
* below are unused for some of the caches.
*/
struct persistent_cache_entry {
int boardsize;
int movenum;
Intersection board[BOARDMAX];
int stack[MAX_CACHE_DEPTH];
int move_color[MAX_CACHE_DEPTH];
enum routine_id routine;
int apos; /* first input coordinate */
int bpos; /* second input coordinate */
int cpos; /* third input coordinate */
int color; /* Move at (cpos) by (color) in analyze_semeai_after_move() */
Hash_data goal_hash; /* hash of the goals in break-in and semeai reading */
int result;
int result2;
int result_certain;
int remaining_depth;
int node_limit;
int move; /* first result coordinate */
int move2;/* second result coordinate */
int cost; /* Usually no. of tactical nodes spent on this reading result. */
int score; /* Heuristic guess of the worth of the cache entry. */
};
/* Callback function that implements the computation of the active area.
* This function has to be provided by each cache.
*/
typedef void (*compute_active_area_fn)(struct persistent_cache_entry *entry,
const signed char goal[BOARDMAX],
int goal_color);
struct persistent_cache {
const int max_size; /* Size of above array. */
const int max_stackp; /* Don't store positions with stackp > max_stackp. */
const float age_factor; /* Reduce value of old entries with this factor. */
const char *name; /* For debugging purposes. */
const compute_active_area_fn compute_active_area;
struct persistent_cache_entry *table; /* Array of actual results. */
int current_size; /* Current number of entries. */
int last_purge_position_number;
};
static void compute_active_owl_area(struct persistent_cache_entry *entry,
const signed char goal[BOARDMAX],
int goal_color);
static void compute_active_semeai_area(struct persistent_cache_entry *entry,
const signed char goal[BOARDMAX],
int dummy);
static void compute_active_reading_area(struct persistent_cache_entry *entry,
const signed char
reading_shadow[BOARDMAX],
int dummy);
static void compute_active_connection_area(struct persistent_cache_entry *entry,
const signed char
connection_shadow[BOARDMAX],
int goal_color);
static void compute_active_breakin_area(struct persistent_cache_entry *entry,
const signed char
breakin_shadow[BOARDMAX],
int dummy);
static struct persistent_cache reading_cache =
{ MAX_READING_CACHE_SIZE, MAX_READING_CACHE_DEPTH, 1.0,
"reading cache", compute_active_reading_area,
NULL, 0, -1 };
static struct persistent_cache connection_cache =
{ MAX_CONNECTION_CACHE_SIZE, MAX_CONNECTION_CACHE_DEPTH, 1.0,
"connection cache", compute_active_connection_area,
NULL, 0, -1 };
static struct persistent_cache breakin_cache =
{ MAX_BREAKIN_CACHE_SIZE, MAX_BREAKIN_CACHE_DEPTH, 0.75,
"breakin cache", compute_active_breakin_area,
NULL, 0, -1 };
static struct persistent_cache owl_cache =
{ MAX_OWL_CACHE_SIZE, MAX_OWL_CACHE_DEPTH, 1.0,
"owl cache", compute_active_owl_area,
NULL, 0, -1 };
static struct persistent_cache semeai_cache =
{ MAX_SEMEAI_CACHE_SIZE, MAX_SEMEAI_CACHE_DEPTH, 0.75,
"semeai cache", compute_active_semeai_area,
NULL, 0, -1 };
/* ================================================================ */
/* Common helper functions. */
static void
draw_active_area(Intersection board[BOARDMAX], int apos)
{
int i, j, ii;
int c = ' ';
int cw = (apos == NO_MOVE) ? 'O' : 'o';
int cb = (apos == NO_MOVE) ? 'X' : 'x';
start_draw_board();
for (i = 0; i < board_size; i++) {
ii = board_size - i;
fprintf(stderr, "\n%2d", ii);
for (j = 0; j < board_size; j++) {
int pos = POS(i, j);
if (board[pos] == EMPTY)
c = '.';
else if (board[pos] == WHITE)
c = cw;
else if ((board[pos] & 3) == WHITE)
c = 'O';
else if (board[pos] == BLACK)
c = cb;
else if ((board[pos] & 3) == BLACK)
c = 'X';
if (board[pos] == GRAY)
c = '?';
if (pos == apos)
fprintf(stderr, "[%c", c);
else if (j > 0 && POS(i, j-1) == apos)
fprintf(stderr, "]%c", c);
else
fprintf(stderr, " %c", c);
}
fprintf(stderr, " %d", ii);
}
end_draw_board();
}
/* Returns 1 if the stored board is compatible with the current board,
* 0 otherwise.
*/
static int
verify_stored_board(Intersection p[BOARDMAX])
{
int pos;
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos))
continue;
else if (p[pos] == GRAY)
continue;
else if ((p[pos] & 3) != board[pos])
return 0;
else if (!(p[pos] & (HIGH_LIBERTY_BIT | HIGH_LIBERTY_BIT2)))
continue;
else if (((p[pos] & HIGH_LIBERTY_BIT) && countlib(pos) <= 4)
|| (p[pos] & HIGH_LIBERTY_BIT2 && countlib(pos) <= 3))
return 0;
}
return 1;
}
/* Prints out all relevant information for a cache entry, and prints
* a board showing the active area.
*/
static void
print_persistent_cache_entry(struct persistent_cache_entry *entry)
{
int r;
gprintf("%omovenum = %d\n", entry->movenum);
gprintf("%oscore = %d\n", entry->score);
gprintf("%ocost = %d\n", entry->cost);
gprintf("%oroutine = %s\n", routine_id_to_string(entry->routine));
gprintf("%oapos = %1m\n", entry->apos);
if (entry->bpos != NO_MOVE)
gprintf("%obpos = %1m\n", entry->bpos);
if (entry->cpos != NO_MOVE)
gprintf("%ocpos = %1m\n", entry->cpos);
gprintf("%oresult = %s\n", result_to_string(entry->result));
if (entry->result2 != 0)
gprintf("%oresult2 = %s\n", result_to_string(entry->result2));
if (entry->result_certain != -1)
gprintf("%oresult_certain = %d\n", entry->result_certain);
if (entry->node_limit != -1)
gprintf("%onode_limit = %d\n", entry->node_limit);
if (entry->move != NO_MOVE)
gprintf("%omove = %1m\n", entry->move);
if (entry->move2 != NO_MOVE)
gprintf("%omove2 = %1m\n", entry->move2);
for (r = 0; r < MAX_CACHE_DEPTH; r++) {
if (entry->stack[r] == 0)
break;
gprintf("%ostack[%d] = %C %1m\n", r, entry->move_color[r],
entry->stack[r]);
}
draw_active_area(entry->board, entry->apos);
}
/* To keep GCC happy and have the function included in the
* gnugo executable. Can be used from gdb.
*/
void print_persistent_cache(struct persistent_cache *cache);
/* Can be used from gdb. */
void
print_persistent_cache(struct persistent_cache *cache)
{
int k;
gprintf("Entire content of %s:\n", cache->name);
for (k = 0; k < cache->current_size; k++)
print_persistent_cache_entry(cache->table + k);
}
/* ================================================================ */
/* Core functions. */
/* ================================================================ */
/* The static functions below implement the core infrastructure of the
* persistent caches. Each cache only has to provide a function
* computing the active area, and wrappers around the search_.. and store_..
* function below.
*/
/* Remove persistent cache entries which are no longer compatible with
* the board. For efficient use of the cache, it's recommended to call
* this function once per move, before starting the owl reading. It's
* not required for correct operation though.
*/
static void
purge_persistent_cache(struct persistent_cache *cache)
{
int k;
int r;
gg_assert(stackp == 0);
/* Never do this more than once per move. */
if (cache->last_purge_position_number == position_number)
return;
else
cache->last_purge_position_number = position_number;
for (k = 0; k < cache->current_size; k++) {
int played_moves = 0;
int entry_ok = 1;
struct persistent_cache_entry *entry = &(cache->table[k]);
if (entry->boardsize != board_size)
entry_ok = 0;
else {
for (r = 0; r < MAX_CACHE_DEPTH; r++) {
int apos = entry->stack[r];
int color = entry->move_color[r];
if (apos == 0)
break;
if (board[apos] == EMPTY
&& trymove(apos, color, "purge_persistent_cache", 0))
played_moves++;
else {
entry_ok = 0;
break;
}
}
}
if (!entry_ok
|| !verify_stored_board(entry->board)) {
/* Move the last entry in the cache here and back up the loop
* counter to redo the test at this position in the cache.
*/
if (0)
gprintf("Purging entry %d from cache.\n", k);
if (k < cache->current_size - 1)
*entry = cache->table[cache->current_size - 1];
k--;
cache->current_size--;
}
else {
/* Reduce score here to penalize entries getting old. */
entry->score *= cache->age_factor;
}
while (played_moves > 0) {
popgo();
played_moves--;
}
}
}
/* Find a cache entry matching the data given in the parameters.
* Important: We assume that unused parameters are normalized to NO_MOVE
* when storing or retrieving, so that we can ignore them here.
*/
static struct persistent_cache_entry *
find_persistent_cache_entry(struct persistent_cache *cache,
enum routine_id routine, int apos, int bpos,
int cpos, int color,
Hash_data *goal_hash, int node_limit)
{
int k;
for (k = 0; k < cache->current_size; k++) {
struct persistent_cache_entry *entry = cache->table + k;
if (entry->routine == routine
&& entry->apos == apos
&& entry->bpos == bpos
&& entry->cpos == cpos
&& entry->color == color
&& depth - stackp <= entry->remaining_depth
&& (entry->node_limit >= node_limit || entry->result_certain)
&& (goal_hash == NULL
|| hashdata_is_equal(entry->goal_hash, *goal_hash))
&& verify_stored_board(entry->board))
return entry;
}
return NULL;
}
/* Search through a persistent cache. Returns 0 if no matching entry was
* found; returns 1 and sets the relevant return values otherwise. See
* comment above find_persistent_cache_entry() about unused parameters.
*/
static int
search_persistent_cache(struct persistent_cache *cache,
enum routine_id routine, int apos, int bpos,
int cpos, int color,
Hash_data *goal_hash, int node_limit,
int *result, int *result2, int *move, int *move2,
int *certain)
{
/* Try to find entry. */
struct persistent_cache_entry *entry;
entry = find_persistent_cache_entry(cache, routine, apos, bpos, cpos, color,
goal_hash, node_limit);
if (entry == NULL)
return 0;
/* Set return values. */
*result = entry->result;
if (result2)
*result2 = entry->result2;
if (move)
*move = entry->move;
if (move2)
*move2 = entry->move2;
if (certain)
*certain = entry->result_certain;
/* Increase score for entry. */
entry->score += entry->cost;
if (debug & DEBUG_PERSISTENT_CACHE) {
gprintf("%oRetrieved position from %s:\n", cache->name);
print_persistent_cache_entry(entry);
}
/* FIXME: This is an ugly hack. */
if (strcmp(cache->name, "reading cache") == 0
&& (debug & DEBUG_READING_PERFORMANCE)
&& entry->cost >= MIN_READING_NODES_TO_REPORT) {
if (entry->result != 0)
gprintf("%o%s %1m = %d %1m, cached (%d nodes) ",
routine == ATTACK ? "attack" : "defend",
apos, entry->result, entry->move, entry->cost);
else
gprintf("%o%s %1m = %d, cached (%d nodes) ",
routine == ATTACK ? "attack" : "defend",
apos, entry->result, entry->cost);
dump_stack();
}
return 1;
}
/* Generic function that tries to store a cache entry. If the cache
* is full, we delete the lowest scoring entry.
*
* Unused parameters have to be normalized to NO_MOVE by the calling
* function.
*/
static void
store_persistent_cache(struct persistent_cache *cache,
enum routine_id routine,
int apos, int bpos, int cpos, int color,
Hash_data *goal_hash,
int result, int result2, int move, int move2,
int certain, int node_limit,
int cost, const signed char goal[BOARDMAX],
int goal_color)
{
int r;
struct persistent_cache_entry *entry;
if (stackp > cache->max_stackp)
return;
/* If cache is still full, consider kicking out an old entry. */
if (cache->current_size == cache->max_size) {
int worst_entry = -1;
int worst_score = cost;
int k;
for (k = 0; k < cache->current_size; k++) {
if (cache->table[k].score < worst_score) {
worst_score = cache->table[k].score;
worst_entry = k;
}
}
if (worst_entry != -1) {
/* Move the last entry in the cache here to make space.
*/
if (worst_entry < cache->current_size - 1)
cache->table[worst_entry] = cache->table[cache->current_size - 1];
cache->current_size--;
}
else
return;
}
entry = &(cache->table[cache->current_size]);
entry->boardsize = board_size;
entry->routine = routine;
entry->apos = apos;
entry->bpos = bpos;
entry->cpos = cpos;
entry->color = color;
if (goal_hash)
entry->goal_hash = *goal_hash;
entry->result = result;
entry->result2 = result2;
entry->result_certain = certain;
entry->node_limit = node_limit;
entry->remaining_depth = depth - stackp;
entry->move = move;
entry->move2 = move2;
entry->score = cost;
entry->cost = cost;
entry->movenum = movenum;
for (r = 0; r < MAX_CACHE_DEPTH; r++) {
if (r < stackp)
get_move_from_stack(r, &(entry->stack[r]), &(entry->move_color[r]));
else {
entry->stack[r] = 0;
entry->move_color[r] = EMPTY;
}
}
/* Remains to set the board. */
cache->compute_active_area(&(cache->table[cache->current_size]),
goal, goal_color);
cache->current_size++;
if (debug & DEBUG_PERSISTENT_CACHE) {
gprintf("%oEntered position in %s:\n", cache->name);
print_persistent_cache_entry(entry);
gprintf("%oCurrent size: %d\n", cache->current_size);
}
}
/* ================================================================ */
/* Interface functions relevant to all caches. */
/* ================================================================ */
/* Allocate the actual cache table. */
static void
init_cache(struct persistent_cache *cache)
{
cache->table = malloc(cache->max_size*sizeof(struct persistent_cache_entry));
gg_assert(cache->table);
}
/* Initializes all persistent caches.
* Needs to be called only once at startup.
*/
void
persistent_cache_init()
{
init_cache(&reading_cache);
init_cache(&breakin_cache);
init_cache(&connection_cache);
init_cache(&owl_cache);
init_cache(&semeai_cache);
}
/* Discards all persistent cache entries. */
void
clear_persistent_caches()
{
reading_cache.current_size = 0;
connection_cache.current_size = 0;
breakin_cache.current_size = 0;
owl_cache.current_size = 0;
semeai_cache.current_size = 0;
}
/* Discards all persistent cache entries that are no longer useful.
* Should be called once per move for optimal performance (but is not
* necessary for proper operation).
*/
void
purge_persistent_caches()
{
purge_persistent_cache(&reading_cache);
purge_persistent_cache(&connection_cache);
purge_persistent_cache(&breakin_cache);
purge_persistent_cache(&owl_cache);
purge_persistent_cache(&semeai_cache);
}
/* ================================================================ */
/* Tactical reading functions */
/* ================================================================ */
/* Look for a valid read result in the persistent cache.
* Return 1 if found, 0 otherwise.
*/
int
search_persistent_reading_cache(enum routine_id routine, int str,
int *result, int *move)
{
return search_persistent_cache(&reading_cache,
routine, str, NO_MOVE, NO_MOVE, EMPTY, NULL,
-1, result, NULL, move, NULL, NULL);
}
/* Store a new read result in the persistent cache. */
void
store_persistent_reading_cache(enum routine_id routine, int str,
int result, int move, int nodes)
{
store_persistent_cache(&reading_cache, routine,
str, NO_MOVE, NO_MOVE, EMPTY, NULL,
result, NO_MOVE, move, NO_MOVE, -1, -1,
nodes, shadow, EMPTY);
}
static void
compute_active_reading_area(struct persistent_cache_entry *entry,
const signed char goal[BOARDMAX], int dummy)
{
signed char active[BOARDMAX];
int pos, r;
UNUSED(dummy);
/* Remains to set the board. We let the active area be the contested
* string and reading shadow + adjacent empty and strings +
* neighbors of active area so far + one more expansion from empty
* to empty.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
active[pos] = goal[pos];
mark_string(entry->apos, active, 1);
/* To be safe, also add the successful move. */
if (entry->result != 0 && entry->move != 0)
active[entry->move] = 1;
/* Add adjacent strings and empty. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos))
continue;
if (active[pos] != 0)
continue;
if ((ON_BOARD(SOUTH(pos)) && active[SOUTH(pos)] == 1)
|| (ON_BOARD(WEST(pos)) && active[WEST(pos)] == 1)
|| (ON_BOARD(NORTH(pos)) && active[NORTH(pos)] == 1)
|| (ON_BOARD(EAST(pos)) && active[EAST(pos)] == 1)) {
if (IS_STONE(board[pos]))
mark_string(pos, active, 2);
else
active[pos] = 2;
}
}
/* Remove invincible strings. No point adding their liberties and
* neighbors.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos))
continue;
if (IS_STONE(board[pos]) && worm[pos].invincible)
active[pos] = 0;
}
/* Expand empty to empty. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (IS_STONE(board[pos]) || active[pos] != 0)
continue;
if ((board[SOUTH(pos)] == EMPTY && active[SOUTH(pos)] == 2)
|| (board[WEST(pos)] == EMPTY && active[WEST(pos)] == 2)
|| (board[NORTH(pos)] == EMPTY && active[NORTH(pos)] == 2)
|| (board[EAST(pos)] == EMPTY && active[EAST(pos)] == 2))
active[pos] = 3;
}
/* Add neighbors of active area so far. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos))
continue;
if (active[pos] != 0)
continue;
if ((ON_BOARD(SOUTH(pos)) && active[SOUTH(pos)] > 0
&& active[SOUTH(pos)] < 4)
|| (ON_BOARD(WEST(pos)) && active[WEST(pos)] > 0
&& active[WEST(pos)] < 4)
|| (ON_BOARD(NORTH(pos)) && active[NORTH(pos)] > 0
&& active[NORTH(pos)] < 4)
|| (ON_BOARD(EAST(pos)) && active[EAST(pos)] > 0
&& active[EAST(pos)] < 4))
active[pos] = 4;
}
/* Also add the previously played stones to the active area. */
for (r = 0; r < stackp; r++)
active[entry->stack[r]] = 5;
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos))
continue;
entry->board[pos] =
active[pos] != 0 ? board[pos] : GRAY;
}
}
/* Helper for the reading_hotspots() function below. */
static void
mark_string_hotspot_values(float values[BOARDMAX],
int m, int n, float contribution)
{
int i, j, k;
/* If p[m][n] is EMPTY, we just give the contribution to close empty
* vertices. This is a rough simplification.
*/
if (BOARD(m, n) == EMPTY) {
for (i = -1; i <= 1; i++)
for (j = -1; j <= 1; j++)
if (BOARD(m+i, n+j) == EMPTY)
values[POS(m+i, n+j)] += contribution;
return;
}
/* Otherwise we give contribution to liberties and diagonal
* neighbors of the string at (m, n).
*/
for (i = 0; i < board_size; i++)
for (j = 0; j < board_size; j++) {
if (BOARD(i, j) != EMPTY)
continue;
for (k = 0; k < 8; k++) {
int di = deltai[k];
int dj = deltaj[k];
if (IS_STONE(BOARD(i+di, j+dj))
&& same_string(POS(i+di, j+dj), POS(m, n))) {
if (k < 4) {
values[POS(i, j)] += contribution;
break;
}
else {
if (BOARD(i+di, j) == EMPTY || countlib(POS(i+di, j)) <= 2
|| BOARD(i, j+dj) == EMPTY || countlib(POS(i, j+dj)) <= 2)
values[POS(i, j)] += contribution;
break;
}
}
}
}
}
/* Based on the entries in the reading cache and their nodes field,
* compute where the relatively most expensive tactical reading is
* going on.
*/
void
reading_hotspots(float values[BOARDMAX])
{
int pos;
int k;
int sum_nodes = 0;
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
values[pos] = 0.0;
/* Compute the total number of nodes for the cached entries. */
for (k = 0; k < reading_cache.current_size; k++)
sum_nodes += reading_cache.table[k].cost;
if (sum_nodes <= 100)
return;
/* Loop over all entries and increase the value of vertices adjacent
* to dragons involving expensive tactical reading.
*/
for (k = 0; k < reading_cache.current_size; k++) {
struct persistent_cache_entry *entry = &(reading_cache.table[k]);
float contribution = entry->cost / (float) sum_nodes;
if (0) {
gprintf("Reading hotspots: %d %1m %f\n", entry->routine, entry->apos,
contribution);
}
switch (entry->routine) {
case ATTACK:
case FIND_DEFENSE:
mark_string_hotspot_values(values, I(entry->apos), J(entry->apos),
contribution);
break;
default:
gg_assert(0); /* Shouldn't happen. */
break;
}
}
}
/* ================================================================ */
/* Connection reading functions */
/* ================================================================ */
/* Look for a valid read result in the persistent connection cache.
* Return 1 if found, 0 otherwise.
*/
int
search_persistent_connection_cache(enum routine_id routine, int str1,
int str2, int *result, int *move)
{
return search_persistent_cache(&connection_cache, routine,
str1, str2, NO_MOVE, EMPTY, NULL,
connection_node_limit,
result, NULL, move, NULL, NULL);
}
/* Store a new connection result in the persistent cache. */
void
store_persistent_connection_cache(enum routine_id routine,
int str1, int str2,
int result, int move, int tactical_nodes,
signed char connection_shadow[BOARDMAX])
{
store_persistent_cache(&connection_cache, routine,
str1, str2, NO_MOVE, EMPTY, NULL,
result, NO_MOVE, move, NO_MOVE, -1,
connection_node_limit,
tactical_nodes, connection_shadow, EMPTY);
}
/* Computes the active area for the current board position and the
* connection read result that has just been stored in *entry.
*/
static void
compute_active_connection_area(struct persistent_cache_entry *entry,
const signed char connection_shadow[BOARDMAX],
int dummy)
{
int pos;
int k, r;
signed char active[BOARDMAX];
int other = OTHER_COLOR(board[entry->apos]);
UNUSED(dummy);
/* Remains to set the board. We let the active area be
* the two strings to connect +
* the connection shadow +
* distance two expansion through empty intersections and own stones +
* adjacent opponent strings +
* liberties and neighbors of adjacent opponent strings with less than
* five liberties +
* liberties and neighbors of low liberty neighbors of adjacent opponent
* strings with less than five liberties.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
active[pos] = connection_shadow[pos];
mark_string(entry->apos, active, 1);
mark_string(entry->bpos, active, 1);
/* To be safe, also add the successful move. */
if (entry->result != 0 && entry->move != 0)
active[entry->move] = 1;
/* Distance two expansion through empty intersections and own stones. */
for (k = 1; k < 3; k++) {
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos) || board[pos] == other || active[pos] != 0)
continue;
if ((ON_BOARD(SOUTH(pos)) && active[SOUTH(pos)] == k)
|| (ON_BOARD(WEST(pos)) && active[WEST(pos)] == k)
|| (ON_BOARD(NORTH(pos)) && active[NORTH(pos)] == k)
|| (ON_BOARD(EAST(pos)) && active[EAST(pos)] == k)) {
if (board[pos] == EMPTY)
active[pos] = k + 1;
else
mark_string(pos, active, (signed char) (k + 1));
}
}
}
/* Adjacent opponent strings. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] != other || active[pos] != 0)
continue;
for (r = 0; r < 4; r++) {
int pos2 = pos + delta[r];
if (ON_BOARD(pos2) && board[pos2] != other && active[pos2] != 0) {
mark_string(pos, active, 1);
break;
}
}
}
/* Liberties of adjacent opponent strings with less than five liberties +
* liberties of low liberty neighbors of adjacent opponent strings
* with less than five liberties.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] == other && active[pos] > 0 && countlib(pos) < 5) {
int libs[4];
int liberties = findlib(pos, 4, libs);
int adjs[MAXCHAIN];
int adj;
for (r = 0; r < liberties; r++)
active[libs[r]] = 1;
/* Also add liberties of neighbor strings if these are three
* or less.
*/
adj = chainlinks(pos, adjs);
for (r = 0; r < adj; r++) {
mark_string(adjs[r], active, -1);
if (countlib(adjs[r]) <= 3) {
int s;
int adjs2[MAXCHAIN];
int adj2;
liberties = findlib(adjs[r], 3, libs);
for (s = 0; s < liberties; s++)
active[libs[s]] = 1;
adj2 = chainlinks(pos, adjs2);
for (s = 0; s < adj2; s++)
mark_string(adjs2[s], active, -1);
}
}
}
}
/* Also add the previously played stones to the active area. */
for (r = 0; r < stackp; r++)
active[entry->stack[r]] = 1;
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
int value = board[pos];
if (!ON_BOARD(pos))
continue;
if (!active[pos])
value = GRAY;
else if (IS_STONE(board[pos]) && countlib(pos) > 4 && active[pos] > 0)
value |= HIGH_LIBERTY_BIT;
entry->board[pos] = value;
}
}
/* ================================================================ */
/* Break-in reading functions */
/* ================================================================ */
/* Look for a valid read result in the persistent breakin cache.
* Return 1 if found, 0 otherwise.
*/
int
search_persistent_breakin_cache(enum routine_id routine,
int str, Hash_data *goal_hash,
int node_limit, int *result, int *move)
{
return search_persistent_cache(&breakin_cache, routine,
str, NO_MOVE, NO_MOVE, EMPTY, goal_hash,
node_limit, result, NULL, move, NULL, NULL);
}
/* Store a new breakin result in the persistent cache. */
void
store_persistent_breakin_cache(enum routine_id routine,
int str, Hash_data *goal_hash,
int result, int move, int tactical_nodes,
int breakin_node_limit,
signed char breakin_shadow[BOARDMAX])
{
store_persistent_cache(&breakin_cache, routine,
str, NO_MOVE, NO_MOVE, EMPTY, goal_hash,
result, NO_MOVE, move, NO_MOVE, -1, breakin_node_limit,
tactical_nodes, breakin_shadow, EMPTY);
}
/* Computes the active area for the current board position and the
* read result that has just been stored in *entry.
*/
static void
compute_active_breakin_area(struct persistent_cache_entry *entry,
const signed char breakin_shadow[BOARDMAX],
int dummy)
{
int pos;
int k, r;
signed char active[BOARDMAX];
int other = OTHER_COLOR(board[entry->apos]);
UNUSED(dummy);
/* We let the active area be
* the string to connect +
* the breakin shadow (which contains the goal) +
* distance two expansion through empty intersections and own stones +
* adjacent opponent strings +
* liberties and neighbors of adjacent opponent strings with less than
* five liberties +
* liberties and neighbors of low liberty neighbors of adjacent opponent
* strings with less than five liberties.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
active[pos] = breakin_shadow[pos];
mark_string(entry->apos, active, 1);
/* To be safe, also add the successful move. */
if (entry->result != 0 && entry->move != 0)
active[entry->move] = 1;
/* Distance two expansion through empty intersections and own stones. */
for (k = 1; k < 3; k++) {
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos) || board[pos] == other || active[pos] != 0)
continue;
if ((ON_BOARD(SOUTH(pos)) && active[SOUTH(pos)] == k)
|| (ON_BOARD(WEST(pos)) && active[WEST(pos)] == k)
|| (ON_BOARD(NORTH(pos)) && active[NORTH(pos)] == k)
|| (ON_BOARD(EAST(pos)) && active[EAST(pos)] == k)) {
if (board[pos] == EMPTY)
active[pos] = k + 1;
else
mark_string(pos, active, (signed char) (k + 1));
}
}
}
/* Adjacent opponent strings. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] != other || active[pos] != 0)
continue;
for (r = 0; r < 4; r++) {
int pos2 = pos + delta[r];
if (ON_BOARD(pos2)
&& board[pos2] != other
&& active[pos2] && active[pos2] <= 2) {
mark_string(pos, active, 1);
break;
}
}
}
/* Liberties of adjacent opponent strings with less than four liberties +
* liberties of low liberty neighbors of adjacent opponent strings
* with less than five liberties.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] == other && active[pos] > 0 && countlib(pos) < 4) {
int libs[4];
int liberties = findlib(pos, 3, libs);
int adjs[MAXCHAIN];
int adj;
for (r = 0; r < liberties; r++)
active[libs[r]] = 1;
/* Also add liberties of neighbor strings if these are three
* or less.
*/
adj = chainlinks(pos, adjs);
for (r = 0; r < adj; r++) {
mark_string(adjs[r], active, -1);
if (countlib(adjs[r]) <= 3) {
int s;
int adjs2[MAXCHAIN];
int adj2;
liberties = findlib(adjs[r], 3, libs);
for (s = 0; s < liberties; s++)
active[libs[s]] = 1;
adj2 = chainlinks(pos, adjs2);
for (s = 0; s < adj2; s++)
mark_string(adjs2[s], active, -1);
}
}
}
}
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
Intersection value = board[pos];
if (!ON_BOARD(pos))
continue;
if (!active[pos])
value = GRAY;
else if (IS_STONE(board[pos]) && countlib(pos) > 3 && active[pos] > 0)
value |= HIGH_LIBERTY_BIT2;
entry->board[pos] = value;
}
}
/* ================================================================ */
/* Owl reading functions */
/* ================================================================ */
int
search_persistent_owl_cache(enum routine_id routine,
int apos, int bpos, int cpos,
int *result, int *move, int *move2, int *certain)
{
return search_persistent_cache(&owl_cache,
routine, apos, bpos, cpos, EMPTY, NULL,
owl_node_limit,
result, NULL, move, move2, certain);
}
void
store_persistent_owl_cache(enum routine_id routine,
int apos, int bpos, int cpos,
int result, int move, int move2, int certain,
int tactical_nodes,
signed char goal[BOARDMAX], int goal_color)
{
store_persistent_cache(&owl_cache, routine, apos, bpos, cpos, EMPTY, NULL,
result, NO_MOVE, move, move2, certain, owl_node_limit,
tactical_nodes, goal, goal_color);
}
/* This function is used by owl and semai active area computation. We assume
* that (goal) marks a dragon of color (goal_color), i.e. all intersections
* in the goal that are not a stone of this color are ignored. The calling
* functions must have zeroed the active area, and is allowed to preset
* some intersection to be active.
*/
static void
compute_active_owl_type_area(const signed char goal[BOARDMAX], int goal_color,
signed char active[BOARDMAX])
{
int k, r;
int pos;
int other = OTHER_COLOR(goal_color);
/* We let the active area be the goal +
* distance four expansion through empty intersections and own stones +
* adjacent opponent strings +
* liberties and neighbors of adjacent opponent strings with less than
* five liberties +
* liberties and neighbors of low liberty neighbors of adjacent opponent
* strings with less than five liberties.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos) && goal[pos])
active[pos] = 1;
/* Distance four expansion through empty intersections and own stones. */
for (k = 1; k < 5; k++) {
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos) || board[pos] == other || active[pos] > 0)
continue;
if ((ON_BOARD(SOUTH(pos)) && active[SOUTH(pos)] == k)
|| (ON_BOARD(WEST(pos)) && active[WEST(pos)] == k)
|| (ON_BOARD(NORTH(pos)) && active[NORTH(pos)] == k)
|| (ON_BOARD(EAST(pos)) && active[EAST(pos)] == k)) {
if (board[pos] == EMPTY)
active[pos] = k + 1;
else
mark_string(pos, active, (signed char) (k + 1));
}
}
}
/* Adjacent opponent strings. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] != other || active[pos] != 0)
continue;
for (r = 0; r < 4; r++) {
int pos2 = pos + delta[r];
if (ON_BOARD(pos2) && board[pos2] != other && active[pos2] != 0) {
mark_string(pos, active, 1);
break;
}
}
}
/* Liberties of adjacent opponent strings with less than five liberties +
* liberties of low liberty neighbors of adjacent opponent strings
* with less than five liberties.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] == other && active[pos] > 0 && countlib(pos) < 5) {
int libs[4];
int liberties = findlib(pos, 4, libs);
int adjs[MAXCHAIN];
int adj;
for (r = 0; r < liberties; r++)
active[libs[r]] = 1;
/* Also add liberties of neighbor strings if these are three
* or less.
*/
adj = chainlinks(pos, adjs);
for (r = 0; r < adj; r++) {
mark_string(adjs[r], active, -1);
if (countlib(adjs[r]) <= 3) {
int s;
int adjs2[MAXCHAIN];
int adj2;
liberties = findlib(adjs[r], 3, libs);
for (s = 0; s < liberties; s++)
active[libs[s]] = 1;
adj2 = chainlinks(pos, adjs2);
for (s = 0; s < adj2; s++)
mark_string(adjs2[s], active, -1);
}
}
}
}
}
static void
compute_active_owl_area(struct persistent_cache_entry *entry,
const signed char goal[BOARDMAX], int goal_color)
{
int pos;
signed char active[BOARDMAX];
memset(active, 0, BOARDMAX);
/* Add critical moves to the active area. */
if (ON_BOARD1(entry->move))
active[entry->move] = 1;
if (ON_BOARD1(entry->move2))
active[entry->move2] = 1;
compute_active_owl_type_area(goal, goal_color, active);
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
int value = board[pos];
if (!ON_BOARD(pos))
continue;
if (!active[pos])
value = GRAY;
else if (IS_STONE(board[pos]) && countlib(pos) > 4 && active[pos] > 0)
value |= HIGH_LIBERTY_BIT;
entry->board[pos] = value;
}
}
/* ================================================================ */
/* Semeai reading functions */
/* ================================================================ */
/* Look for stored result in semeai cache. Returns 1 if result found, 0
* otherwise.
*/
int
search_persistent_semeai_cache(enum routine_id routine,
int apos, int bpos, int cpos, int color,
Hash_data *goal_hash,
int *resulta, int *resultb,
int *move, int *certain)
{
return search_persistent_cache(&semeai_cache, routine, apos, bpos, cpos,
color, goal_hash, semeai_node_limit,
resulta, resultb, move, NULL, certain);
}
/* Store a new read result in the persistent semeai cache. */
void
store_persistent_semeai_cache(enum routine_id routine,
int apos, int bpos, int cpos, int color,
Hash_data *goal_hash,
int resulta, int resultb, int move, int certain,
int tactical_nodes,
signed char goala[BOARDMAX],
signed char goalb[BOARDMAX])
{
signed char goal[BOARDMAX];
int pos;
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos))
goal[pos] = goala[pos] || goalb[pos];
store_persistent_cache(&semeai_cache, routine,
apos, bpos, cpos, color, goal_hash,
resulta, resultb, move, NO_MOVE,
certain, semeai_node_limit,
tactical_nodes, goal, EMPTY);
}
static void
compute_active_semeai_area(struct persistent_cache_entry *entry,
const signed char goal[BOARDMAX], int dummy)
{
int pos;
signed char active_b[BOARDMAX];
signed char active_w[BOARDMAX];
UNUSED(dummy);
memset(active_b, 0, BOARDMAX);
memset(active_w, 0, BOARDMAX);
/* Add critical move to the active area. */
if (ON_BOARD1(entry->move)) {
active_b[entry->move] = 1;
active_w[entry->move] = 1;
}
if (ON_BOARD1(entry->cpos)) {
active_b[entry->cpos] = 1;
active_w[entry->cpos] = 1;
}
compute_active_owl_type_area(goal, BLACK, active_b);
compute_active_owl_type_area(goal, WHITE, active_w);
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
int value = board[pos];
if (!ON_BOARD(pos))
continue;
if (!active_b[pos] && !active_w[pos])
value = GRAY;
else if (IS_STONE(board[pos]) && countlib(pos) > 4
&& (active_b[pos] > 0 || active_w[pos] > 0))
value |= HIGH_LIBERTY_BIT;
entry->board[pos] = value;
}
}
/* Helper for the owl_hotspots() function below. */
static void
mark_dragon_hotspot_values(float values[BOARDMAX], int dr,
float contribution,
Intersection active_board[BOARDMAX])
{
int pos;
int k;
if (!IS_STONE(board[dr]))
return;
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] != EMPTY)
continue;
for (k = 0; k < 8; k++) {
int pos2 = pos + delta[k];
if (IS_STONE(board[pos2])
&& (is_same_dragon(pos2, dr)
|| (are_neighbor_dragons(pos2, dr)
&& board[pos2] == board[dr]))
&& (countlib(pos2) <= 4
|| is_edge_vertex(pos))) {
if (k < 4) {
if (is_same_dragon(pos2, dr))
values[pos] += contribution;
else
values[pos] += 0.5 * contribution;
break;
}
else {
/* If pos2 = SOUTHWEST(pos), this construction makes
* pos3 = SOUTH(pos) and
* pos4 = WEST(pos)
* and corresponding for all other diagonal movements.
*/
int pos3 = pos + delta[k % 4];
int pos4 = pos + delta[(k+1) % 4];
if (board[pos3] == EMPTY || countlib(pos3) <= 2
|| board[pos4] == EMPTY || countlib(pos4) <= 2)
values[pos] += 0.5 * contribution;
break;
}
}
}
/* If not close to the dragon, but within the active area, give
* negative hotspot contribution.
*/
if (k == 8 && active_board[pos] == EMPTY) {
values[pos] -= 0.5 * contribution;
}
}
}
/* Based on the entries in the owl cache and their tactical_nodes
* field, compute where the relatively most expensive owl reading is
* going on.
*/
void
owl_hotspots(float values[BOARDMAX])
{
int pos;
int k, r;
int libs[MAXLIBS];
int liberties;
int sum_tactical_nodes = 0;
/* Don't bother checking out of board. Set values[] to zero there too. */
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
values[pos] = 0.0;
/* Compute the total number of tactical nodes for the cached entries. */
for (k = 0; k < owl_cache.current_size; k++)
sum_tactical_nodes += owl_cache.table[k].score;
if (sum_tactical_nodes <= 100)
return;
/* Loop over all entries and increase the value of vertices adjacent
* to dragons involving expensive owl reading.
*/
for (k = 0; k < owl_cache.current_size; k++) {
struct persistent_cache_entry *entry = &(owl_cache.table[k]);
float contribution = entry->score / (float) sum_tactical_nodes;
if (debug & DEBUG_PERSISTENT_CACHE) {
gprintf("Owl hotspots: %d %1m %f\n", entry->routine, entry->apos,
contribution);
}
switch (entry->routine) {
case OWL_ATTACK:
case OWL_THREATEN_ATTACK:
case OWL_DEFEND:
case OWL_THREATEN_DEFENSE:
mark_dragon_hotspot_values(values, entry->apos,
contribution, entry->board);
break;
case OWL_DOES_DEFEND:
case OWL_DOES_ATTACK:
case OWL_CONFIRM_SAFETY:
mark_dragon_hotspot_values(values, entry->bpos,
contribution, entry->board);
break;
case OWL_CONNECTION_DEFENDS:
mark_dragon_hotspot_values(values, entry->bpos,
contribution, entry->board);
mark_dragon_hotspot_values(values, entry->cpos,
contribution, entry->board);
break;
case OWL_SUBSTANTIAL:
/* Only consider the liberties of (apos). */
if (!IS_STONE(board[entry->apos]))
continue;
liberties = findlib(entry->apos, MAXLIBS, libs);
for (r = 0; r < liberties; r++)
values[libs[r]] += contribution;
break;
default:
gg_assert(0); /* Shouldn't happen. */
break;
}
}
}
/*
* Local Variables:
* tab-width: 8
* c-basic-offset: 2
* End:
*/
|