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
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* 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. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "board.h"
#include "hash.h"
#include "random.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/*
* This file, together with engine/hash.h implements hashing of go positions
* using a method known as Zobrist hashing. See the Texinfo documentation
* (Reading/Hashing) for more information.
*/
/* ================================================================ */
/* Random values for the board hash function. For stones and ko position. */
static Hash_data white_hash[BOARDMAX];
static Hash_data black_hash[BOARDMAX];
static Hash_data ko_hash[BOARDMAX];
static Hash_data komaster_hash[NUM_KOMASTER_STATES];
static Hash_data kom_pos_hash[BOARDMAX];
static Hash_data goal_hash[BOARDMAX];
/* Get a random Hashvalue, where all bits are used. */
static Hashvalue
hash_rand(void)
{
int i;
Hashvalue h = 0;
for (i = 0; 32*i < (int) (CHAR_BIT*sizeof(Hashvalue)); i++)
h |= (Hashvalue) gg_urand() << 32*i;
return h;
}
/* Fill an array with random numbers for Zobrist hashing. */
void
hash_init_zobrist_array(Hash_data *array, int size)
{
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < NUM_HASHVALUES; j++)
array[i].hashval[j] = hash_rand();
}
/*
* Initialize the board hash system.
*/
void
hash_init(void)
{
static int is_initialized = 0;
if (is_initialized)
return;
INIT_ZOBRIST_ARRAY(black_hash);
INIT_ZOBRIST_ARRAY(white_hash);
INIT_ZOBRIST_ARRAY(ko_hash);
INIT_ZOBRIST_ARRAY(komaster_hash);
INIT_ZOBRIST_ARRAY(kom_pos_hash);
INIT_ZOBRIST_ARRAY(goal_hash);
is_initialized = 1;
}
/* ---------------------------------------------------------------- */
/* Calculate the hashvalue from scratch. */
void
hashdata_recalc(Hash_data *hd, Intersection *p, int ko_pos)
{
int pos;
hashdata_clear(hd);
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (p[pos] == WHITE)
hashdata_xor(*hd, white_hash[pos]);
else if (p[pos] == BLACK)
hashdata_xor(*hd, black_hash[pos]);
}
if (ko_pos != 0)
hashdata_xor(*hd, ko_hash[ko_pos]);
}
/* Clear hashdata. */
void
hashdata_clear(Hash_data *hd)
{
int i;
for (i = 0; i < NUM_HASHVALUES; i++)
hd->hashval[i] = 0;
}
/* Set or remove ko in the hash value and hash position. */
void
hashdata_invert_ko(Hash_data *hd, int pos)
{
hashdata_xor(*hd, ko_hash[pos]);
}
/* Set or remove a stone of COLOR at pos in a Hash_data. */
void
hashdata_invert_stone(Hash_data *hd, int pos, int color)
{
if (color == BLACK)
hashdata_xor(*hd, black_hash[pos]);
else if (color == WHITE)
hashdata_xor(*hd, white_hash[pos]);
}
/* Set or remove the komaster value in the hash data. */
void
hashdata_invert_komaster(Hash_data *hd, int komaster)
{
hashdata_xor(*hd, komaster_hash[komaster]);
}
/* Set or remove the komaster position in the hash data. */
void
hashdata_invert_kom_pos(Hash_data *hd, int kom_pos)
{
hashdata_xor(*hd, kom_pos_hash[kom_pos]);
}
/* Calculate a transformation invariant hashvalue. */
void
hashdata_calc_orientation_invariant(Hash_data *hd, Intersection *p, int ko_pos)
{
int pos;
int rot;
Hash_data hd_rot;
for (rot = 0; rot < 8; rot++) {
hashdata_clear(&hd_rot);
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (p[pos] == WHITE)
hashdata_xor(hd_rot, white_hash[rotate1(pos, rot)]);
else if (p[pos] == BLACK)
hashdata_xor(hd_rot, black_hash[rotate1(pos, rot)]);
}
if (ko_pos != NO_MOVE)
hashdata_xor(hd_rot, ko_hash[rotate1(ko_pos, rot)]);
if (rot == 0 || hashdata_is_smaller(hd_rot, *hd))
*hd = hd_rot;
}
}
/* Compute hash value to identify the goal area. */
Hash_data
goal_to_hashvalue(const signed char *goal)
{
int pos;
Hash_data return_value;
hashdata_clear(&return_value);
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos) && goal[pos])
hashdata_xor(return_value, goal_hash[pos]);
return return_value;
}
#define HASHVALUE_NUM_DIGITS (1 + (CHAR_BIT * SIZEOF_HASHVALUE - 1) / 4)
#define BUFFER_SIZE (1 + NUM_HASHVALUES * HASHVALUE_NUM_DIGITS)
char *
hashdata_to_string(Hash_data *hashdata)
{
static char buffer[BUFFER_SIZE];
int n = 0;
int k;
/* Loop backwards for consistency between 32 and 64 bit platforms. */
for (k = NUM_HASHVALUES - 1; k >= 0; k--) {
n += sprintf(buffer + n, HASHVALUE_PRINT_FORMAT,
HASHVALUE_NUM_DIGITS, hashdata->hashval[k]);
gg_assert(n < BUFFER_SIZE);
}
return buffer;
}
#if NUM_HASHVALUES > 2
int
hashdata_is_equal_func(Hash_data *hd1, Hash_data *hd2)
{
int i;
for (i = 0; i < NUM_HASHVALUES; i++)
if (hd1->hashval[i] != hd2->hashval[i])
return 0;
return 1;
}
int
hashdata_is_smaller_func(Hash_data *hd1, Hash_data *hd2)
{
int i;
for (i = 0; i < NUM_HASHVALUES; i++)
if (hd1->hashval[i] < hd2->hashval[i])
return 1;
else if (hd1->hashval[i] > hd2->hashval[i])
return 0;
return 0;
}
#endif
/*
* Local Variables:
* tab-width: 8
* c-basic-offset: 2
* End:
*/
|