Subversion Repositories Games.Chess Giants

Rev

Rev 108 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 pmbaty 1
#include <math.h>
2
#include <time.h>
3
#include "chess.h"
4
#include "data.h"
5
#if defined(UNIX)
6
#  include <unistd.h>
7
#endif
8
 
9
/* last modified 02/24/14 */
10
/*
11
 *******************************************************************************
12
 *                                                                             *
13
 *   LearnAdjust() us used to scale the learn value, which can be used to      *
14
 *   limit the aggressiveness of the learning algorithm.  All we do here is    *
15
 *   divide the learn value passed in by "learning / 10".                      *
16
 *                                                                             *
17
 *******************************************************************************
18
 */
19
int LearnAdjust(int value) {
20
 
21
  if (learning / 10 > 0)
22
    return value / (learning / 10);
23
  else
24
    return 0;
25
}
26
 
27
/* last modified 02/24/14 */
28
/*
29
 *******************************************************************************
30
 *                                                                             *
31
 *   LearnBook() is used to update the book database when a game ends for any  *
32
 *   reason.  It uses the global "learn_value" variable and updates the book   *
33
 *   based on the moves played and the value that was "learned".               *
34
 *                                                                             *
35
 *   The global learn_value has two possible sources.  If a game ends with a   *
36
 *   real result (win, lose or draw) then the learrn_value will be set to a    *
37
 *   number in the interval {-300, 300} depending on the result.  If there is  *
38
 *   no result (the operator exits the program prior to reaching a conclusion  *
39
 *   (quit, end, ^C) then we will use the values from the first few searches   *
40
 *   after leaving book to compute a learrn_value (see LearnValue() comments   *
41
 *   later in this file).                                                      *
42
 *                                                                             *
43
 *******************************************************************************
44
 */
45
void LearnBook() {
46
  int nplies = 0, thisply = 0;
47
  unsigned char buf32[4];
48
  int i, j, cluster;
49
  float book_learn[64], t_learn_value;
50
 
51
/*
52
 ************************************************************
53
 *                                                          *
54
 *  If we have not been "out of book" for N moves, all we   *
55
 *  we need to do is take the search evaluation for the     *
56
 *  search just completed and tuck it away in the book      *
57
 *  learning array (book_learn_eval[]) for use later.       *
58
 *                                                          *
59
 ************************************************************
60
 */
61
  if (!book_file)
62
    return;
63
  if (!learn)
64
    return;
65
  if (Abs(learn_value) != learning)
66
    learn_value = LearnAdjust(learn_value);
67
  learn = 0;
68
  Print(128, "LearnBook() updating book database\n");
69
/*
70
 ************************************************************
71
 *                                                          *
72
 *  Now we build a vector of book learning results.  We     *
73
 *  give every book move below the last point where there   *
74
 *  were alternatives 100% of the learned score.  We give   *
75
 *  the book move played at that point 100% of the learned  *
76
 *  score as well.  Then we divide the learned score by the *
77
 *  number of alternatives, and propagate this score back   *
78
 *  until there was another alternative, where we do this   *
79
 *  again and again until we reach the top of the book      *
80
 *  tree.                                                   *
81
 *                                                          *
82
 ************************************************************
83
 */
84
  t_learn_value = ((float) learn_value) / 100.0f; // Pierre-Marie Baty -- added float suffix
85
  for (i = 0; i < 64; i++)
86
    if (learn_nmoves[i] > 1)
87
      nplies++;
88
  nplies = Max(nplies, 1);
89
  for (i = 0; i < 64; i++) {
90
    if (learn_nmoves[i] > 1)
91
      thisply++;
92
    book_learn[i] = t_learn_value * (float) thisply / (float) nplies;
93
  }
94
/*
95
 ************************************************************
96
 *                                                          *
97
 *  Now find the appropriate cluster, find the key we were  *
98
 *  passed, and update the resulting learn value.           *
99
 *                                                          *
100
 ************************************************************
101
 */
102
  for (i = 0; i < 64 && learn_seekto[i]; i++) {
103
    if (learn_seekto[i] > 0) {
104
      fseek(book_file, learn_seekto[i], SEEK_SET);
105
      fread(buf32, 4, 1, book_file);
106
      cluster = BookIn32(buf32);
107
      BookClusterIn(book_file, cluster, book_buffer);
108
      for (j = 0; j < cluster; j++)
109
        if (!(learn_key[i] ^ book_buffer[j].position))
110
          break;
111
      if (j >= cluster)
112
        return;
113
      if (fabs(book_buffer[j].learn) < 0.0001)
114
        book_buffer[j].learn = book_learn[i];
115
      else
116
        book_buffer[j].learn = (book_buffer[j].learn + book_learn[i]) / 2.0f; // Pierre-Marie Baty -- added float suffix
117
      fseek(book_file, learn_seekto[i] + 4, SEEK_SET);
118
      BookClusterOut(book_file, cluster, book_buffer);
119
      fflush(book_file);
120
    }
121
  }
122
}
123
 
124
/* last modified 02/24/14 */
125
/*
126
 *******************************************************************************
127
 *                                                                             *
128
 *   LearnFunction() is called to compute the adjustment value added to the    *
129
 *   learn counter in the opening book.  It takes three pieces of information  *
130
 *   into consideration to do this:  the search value, the search depth that   *
131
 *   produced this value, and the rating difference (Crafty-opponent) so that  *
132
 *   + numbers means Crafty is expected to win, - numbers mean Crafty is ex-   *
133
 *   pected to lose.                                                           *
134
 *                                                                             *
135
 *******************************************************************************
136
 */
137
int LearnFunction(int sv, int search_depth, int rating_difference,
138
    int trusted_value) {
139
  static const float rating_mult_t[11] = { .00625f, .0125f, .025f, .05f, .075f, .1f,
140
    0.15f, 0.2f, 0.25f, 0.3f, 0.35f // Pierre-Marie Baty -- added float suffixes
141
  };
142
  static const float rating_mult_ut[11] = { .25f, .2f, .15f, .1f, .05f, .025f, .012f,
143
    .006f, .003f, .001f // Pierre-Marie Baty -- added float suffixes
144
  };
145
  float multiplier;
146
  int sd, rd, value;
147
 
148
  sd = Max(Min(search_depth - 10, 19), 0);
149
  rd = Max(Min(rating_difference / 200, 5), -5) + 5;
150
  if (trusted_value)
151
    multiplier = rating_mult_t[rd] * sd;
152
  else
153
    multiplier = rating_mult_ut[rd] * sd;
154
  sv = Max(Min(sv, 5 * learning), -5 * learning);
155
  value = (int) (sv * multiplier);
156
  return value;
157
}
158
 
159
/* last modified 02/24/14 */
160
/*
161
 *******************************************************************************
162
 *                                                                             *
163
 *   LearnValue() is used to monitor the scores over the first N moves out of  *
164
 *   book.  After these moves have been played, the evaluations are then used  *
165
 *   to decide whether the last book move played was a reasonable choice or    *
166
 *   not.  (N is set by the #define LEARN_INTERVAL definition.)                *
167
 *                                                                             *
168
 *   This procedure does not directly update the book.  Rather, it sets the    *
169
 *   global learn_value variable to represent the goodness or badness of the   *
170
 *   position where we left the opening book.  This will be used later to      *
171
 *   update the book in the event the game ends without any sort of actual     *
172
 *   result.  In a normal situation, we will base our learning on the result   *
173
 *   of the game, win-lose-draw.  But it is possible that the game ends before *
174
 *   the final result is known.  In this case, we will use the score from the  *
175
 *   learn_value we compute here so that we learn _something_ from playing a   *
176
 *   game fragment.                                                            *
177
 *                                                                             *
178
 *   There are three cases to be handled.  (1) If the evaluation is bad right  *
179
 *   out of book, or it drops enough to be considered a bad line, then the     *
180
 *   book move will have its "learn" value reduced to discourage playing this  *
181
 *   move again.  (2) If the evaluation is even after N moves, then the learn  *
182
 *   value will be increased, but by a relatively modest amount, so that a few *
183
 *   even results will offset one bad result.  (3) If the evaluation is very   *
184
 *   good after N moves, the learn value will be increased by a large amount   *
185
 *   so that this move will be favored the next time the game is played.       *
186
 *                                                                             *
187
 *******************************************************************************
188
 */
189
void LearnValue(int search_value, int search_depth) {
190
  int i;
191
  int interval;
192
  int best_eval = -999999, best_eval_p = 0;
193
  int worst_eval = 999999, worst_eval_p = 0;
194
  int best_after_worst_eval = -999999, worst_after_best_eval = 999999;
195
 
196
/*
197
 ************************************************************
198
 *                                                          *
199
 *  If we have not been "out of book" for N moves, all we   *
200
 *  need to do is take the search evaluation for the search *
201
 *  just completed and tuck it away in the book learning    *
202
 *  array (book_learn_eval[]) for use later.                *
203
 *                                                          *
204
 ************************************************************
205
 */
206
  if (!book_file)
207
    return;
208
  if (!learn || learn_value != 0)
209
    return;
210
  if (moves_out_of_book <= LEARN_INTERVAL) {
211
    if (moves_out_of_book) {
212
      book_learn_eval[moves_out_of_book - 1] = search_value;
213
      book_learn_depth[moves_out_of_book - 1] = search_depth;
214
    }
215
  }
216
/*
217
 ************************************************************
218
 *                                                          *
219
 *  Check the evaluations we've seen so far.  If they are   *
220
 *  within reason (+/- 1/3 of a pawn or so) we simply keep  *
221
 *  playing and leave the book alone.  If the eval is much  *
222
 *  better or worse, we need to update the learning data.   *
223
 *                                                          *
224
 ************************************************************
225
 */
226
  else if (moves_out_of_book == LEARN_INTERVAL + 1) {
227
    if (moves_out_of_book < 1)
228
      return;
229
    interval = Min(LEARN_INTERVAL, moves_out_of_book);
230
    if (interval < 2)
231
      return;
232
    for (i = 0; i < interval; i++) {
233
      if (book_learn_eval[i] > best_eval) {
234
        best_eval = book_learn_eval[i];
235
        best_eval_p = i;
236
      }
237
      if (book_learn_eval[i] < worst_eval) {
238
        worst_eval = book_learn_eval[i];
239
        worst_eval_p = i;
240
      }
241
    }
242
    if (best_eval_p < interval - 1) {
243
      for (i = best_eval_p; i < interval; i++)
244
        if (book_learn_eval[i] < worst_after_best_eval)
245
          worst_after_best_eval = book_learn_eval[i];
246
    } else
247
      worst_after_best_eval = book_learn_eval[interval - 1];
248
    if (worst_eval_p < interval - 1) {
249
      for (i = worst_eval_p; i < interval; i++)
250
        if (book_learn_eval[i] > best_after_worst_eval)
251
          best_after_worst_eval = book_learn_eval[i];
252
    } else
253
      best_after_worst_eval = book_learn_eval[interval - 1];
254
/*
255
 ************************************************************
256
 *                                                          *
257
 *  We now have the best eval for the first N moves out of  *
258
 *  book, the worst eval for the first N moves out of book, *
259
 *  and the worst eval that follows the best eval.  This    *
260
 *  will be used to recognize the following cases of        *
261
 *  results that follow a book move:                        *
262
 *                                                          *
263
 ************************************************************
264
 */
265
/*
266
 ************************************************************
267
 *                                                          *
268
 *  (1) The best score is very good, and it doesn't drop    *
269
 *  after following the game further.  This case detects    *
270
 *  those moves in book that are "good" and should be       *
271
 *  played whenever possible, while avoiding the sound      *
272
 *  gambits that leave us ahead in material for a short     *
273
 *  while until the score starts to drop as the gambit      *
274
 *  begins to show its effect.                              *
275
 *                                                          *
276
 ************************************************************
277
 */
278
    if (best_eval == best_after_worst_eval) {
279
      learn_value = best_eval;
280
      for (i = 0; i < interval; i++)
281
        if (learn_value == book_learn_eval[i])
282
          search_depth = Max(search_depth, book_learn_depth[i]);
283
    }
284
/*
285
 ************************************************************
286
 *                                                          *
287
 *  (2) The worst score is bad, and doesn't improve any     *
288
 *  after the worst point, indicating that the book move    *
289
 *  chosen was "bad" and should be avoided in the future.   *
290
 *                                                          *
291
 ************************************************************
292
 */
293
    else if (worst_eval == worst_after_best_eval) {
294
      learn_value = worst_eval;
295
      for (i = 0; i < interval; i++)
296
        if (learn_value == book_learn_eval[i])
297
          search_depth = Max(search_depth, book_learn_depth[i]);
298
    }
299
/*
300
 ************************************************************
301
 *                                                          *
302
 *  (3) Things seem even out of book and remain that way    *
303
 *  for N moves.  We will just average the 10 scores and    *
304
 *  use that as an approximation.                           *
305
 *                                                          *
306
 ************************************************************
307
 */
308
    else {
309
      learn_value = 0;
310
      search_depth = 0;
311
      for (i = 0; i < interval; i++) {
312
        learn_value += book_learn_eval[i];
313
        search_depth += book_learn_depth[i];
314
      }
315
      learn_value /= interval;
316
      search_depth /= interval;
317
    }
318
    learn_value =
319
        LearnFunction(learn_value, search_depth,
320
        crafty_rating - opponent_rating, learn_value < 0);
321
  }
322
}