~ubuntu-branches/ubuntu/natty/gtkboard/natty

« back to all changes in this revision

Viewing changes to .pc/debian-changes-0.11pre0-12/src/ab.c

  • Committer: Bazaar Package Importer
  • Author(s): Barak A. Pearlmutter
  • Date: 2011-03-15 12:43:00 UTC
  • mfrom: (3.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110315124300-zf9hkdc5vjyqge7e
Tags: 0.11pre0+cvs.2003.11.02-3
static size unknown gcc-4.5 fix src/{menu,wordtris}.c (closes: #564999)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is a part of gtkboard, a board games system.
2
 
    Copyright (C) 2003, Arvind Narayanan <arvindn@users.sourceforge.net>
3
 
 
4
 
    This program is free software; you can redistribute it and/or modify
5
 
    it under the terms of the GNU General Public License as published by
6
 
    the Free Software Foundation; either version 2 of the License, or
7
 
    (at your option) any later version.
8
 
 
9
 
    This program is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
    GNU General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU General Public License
15
 
    along with this program; if not, write to the Free Software
16
 
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
17
 
 
18
 
*/
19
 
 
20
 
#include "game.h"
21
 
#include "move.h"
22
 
#include "engine.h"
23
 
 
24
 
#include <signal.h>
25
 
#include <string.h>
26
 
#include <stdlib.h>
27
 
#include <assert.h>
28
 
#include <math.h>
29
 
 
30
 
extern int time_per_move;
31
 
 
32
 
extern gboolean engine_stop_search;
33
 
 
34
 
static gboolean ab_tree_exhausted = FALSE;
35
 
 
36
 
static int ab_leaf_cnt;  // how many leaves were eval'd
37
 
 
38
 
extern int hash_get_eval (byte *, int, int, int, float *);
39
 
extern void hash_print_stats ();
40
 
extern void hash_insert (byte *, int, int, int, float, byte *move);
41
 
extern void hash_clear ();
42
 
extern void hash_insert_move (byte *, int, int, byte *);
43
 
extern byte * hash_get_move (byte *, int, int);
44
 
 
45
 
extern gboolean opt_verbose;
46
 
 
47
 
// FIXME: move this to ui.c
48
 
gboolean game_use_hash = TRUE;
49
 
        
50
 
// FIXME: this function is too complicated
51
 
float ab_with_tt (Pos *pos, int player, int level, 
52
 
                float alpha, float beta, byte *best_movep)
53
 
        /* level is the number of ply to search */
54
 
{
55
 
        int to_play = player;
56
 
        float val, cacheval, best= 0, retval;
57
 
        gboolean first = TRUE;
58
 
        byte *movlist, *move;
59
 
        byte best_move [4096];
60
 
        Pos newpos;
61
 
        gboolean hashed_move = TRUE;
62
 
        float local_alpha = -1e+16, local_beta = 1e+16;
63
 
        byte *orig_move;
64
 
        best_move [0] = -1;
65
 
        
66
 
        engine_poll ();
67
 
        if (engine_stop_search) { ab_tree_exhausted = FALSE; return 0; }
68
 
 
69
 
        movlist = game_movegen (pos);
70
 
        if (movlist[0] == -2)           /* we have no move left */
71
 
        {
72
 
                free (movlist);
73
 
                game_eval (pos, to_play, &val);
74
 
                if (game_use_hash)
75
 
                        hash_insert (pos->board, board_wid * board_heit, pos->num_moves, level, val, 
76
 
                                NULL);
77
 
                return val;
78
 
        }
79
 
        move = NULL;
80
 
        orig_move = NULL;
81
 
        if (game_use_hash && level > 0)
82
 
                move = hash_get_move (pos->board, board_wid * board_heit, pos->num_moves);
83
 
        if (!move)
84
 
        {
85
 
                move = movlist;
86
 
                hashed_move = FALSE;
87
 
        }
88
 
        // origmove is the owning pointer and move is the aliasing pointer
89
 
        else orig_move = move = movdup (move);
90
 
        
91
 
        newpos.board = (char *) malloc (board_wid * board_heit);
92
 
        assert (newpos.board);
93
 
        if (game_stateful)
94
 
        {
95
 
                newpos.state = (void *) malloc (game_state_size);
96
 
                assert (newpos.state);
97
 
        }
98
 
 
99
 
        do
100
 
        {
101
 
                if (!orig_move || hashed_move || !movcmp_literal (orig_move, move))
102
 
                {
103
 
                        ResultType result = RESULT_NOTYET;
104
 
                        memcpy (newpos.board, pos->board, board_wid * board_heit);
105
 
                        if (game_stateful)
106
 
                        {
107
 
                                void *newstate = game_newstate (pos, move);
108
 
                                memcpy (newpos.state, newstate, game_state_size);
109
 
                        }
110
 
                        move_apply (newpos.board, move);
111
 
                        newpos.num_moves = pos->num_moves + 1;
112
 
                        newpos.search_depth = pos->search_depth + 1;
113
 
                        newpos.player = pos->player == WHITE ? BLACK : WHITE;
114
 
                        retval = 0;
115
 
                        if (game_use_hash && level > 0)
116
 
                                retval = hash_get_eval (newpos.board, board_wid * board_heit, 
117
 
                                                newpos.num_moves, level-1, &cacheval);
118
 
                        if (retval && fabs (cacheval) < GAME_EVAL_INFTY) val = cacheval;
119
 
                        else result = game_eval (&newpos, to_play == WHITE ? BLACK : WHITE, &val);
120
 
                        if (level == 0)
121
 
                        {
122
 
                                ab_leaf_cnt ++;
123
 
                                ab_tree_exhausted = FALSE;
124
 
/*                              if (game_use_hash)
125
 
                                        hash_insert (newpos.board, board_wid * board_heit, 
126
 
                                                pos->num_moves, level, val, NULL);
127
 
*/
128
 
                        }
129
 
                        else 
130
 
                        {
131
 
                                if (fabs (val) >= GAME_EVAL_INFTY)
132
 
                                        val *= (1 + level);
133
 
                                else if (result == RESULT_WHITE || result == RESULT_BLACK)
134
 
                                        val *= (1 + level);
135
 
                                else if (result == RESULT_TIE)
136
 
                                        ;
137
 
                                else
138
 
                                {
139
 
                                        val = ab_with_tt (&newpos, player == WHITE ? BLACK : WHITE, 
140
 
                                                                level-1, alpha, beta, best_move);
141
 
                                }
142
 
                        }
143
 
                        if((player == WHITE && val > local_alpha) 
144
 
                                        || (player == BLACK && val < local_beta))
145
 
                        {
146
 
                                if (best_movep) movcpy (best_movep, move);
147
 
                                if (player == WHITE) local_alpha = val; else local_beta = val;
148
 
                        }
149
 
 
150
 
                        if((player == WHITE && val > alpha))
151
 
                                alpha = val;
152
 
                        if ((player == BLACK && val < beta))
153
 
                                beta  = val;
154
 
                        if (alpha >= beta || alpha >= GAME_EVAL_INFTY || beta <= -GAME_EVAL_INFTY)
155
 
                                break;
156
 
                }
157
 
                if (hashed_move)
158
 
                        move = movlist;
159
 
                else
160
 
                        move = movlist_next (move);
161
 
                hashed_move = FALSE;
162
 
        }
163
 
        while (move[0] != -2);
164
 
        free (newpos.board);
165
 
        if (game_stateful) free (newpos.state);
166
 
        free (movlist);
167
 
        free (orig_move);
168
 
        if (game_use_hash)
169
 
                hash_insert (pos->board, board_wid * board_heit, pos->num_moves, level,
170
 
                        player == WHITE ? alpha : beta, best_movep);
171
 
        return player == WHITE ? alpha : beta;
172
 
}
173
 
 
174
 
#if 0
175
 
// TODO: this is currently unused, and must be merged with the previous function 
176
 
float ab_with_tt_incr (Pos *pos, int player, int level, 
177
 
                float eval, float alpha, float beta, byte *best_movep)
178
 
        /* level is the number of ply to search */
179
 
{
180
 
        int to_play = player;
181
 
        float val, cacheval, best= 0, retval;
182
 
        int first = 1;
183
 
        byte *movlist, *move;
184
 
        byte best_move [4096];
185
 
        void *oldstate = NULL; // use the recursion to implement stack of states
186
 
        engine_poll ();
187
 
        if (engine_stop_search) 
188
 
        { 
189
 
                ab_tree_exhausted = FALSE; 
190
 
                return 0; 
191
 
        }
192
 
        if (game_stateful)
193
 
        {
194
 
                oldstate = (void *) malloc (game_state_size);
195
 
                assert (oldstate);
196
 
        }
197
 
        movlist = game_movegen (pos);
198
 
        if (movlist[0] == -2)           /* we have no move left */
199
 
        {
200
 
                free (movlist);
201
 
                game_eval (pos, to_play, &val);
202
 
                return val;
203
 
        }
204
 
        move = movlist;
205
 
        do
206
 
        {
207
 
                float neweval = 0, incr_eval;
208
 
                ResultType result;
209
 
                result = game_eval_incr (pos, to_play, move, &incr_eval);
210
 
                neweval = eval + incr_eval;
211
 
                
212
 
                if (level == 0)
213
 
                {
214
 
                        ab_leaf_cnt ++;
215
 
                        ab_tree_exhausted = FALSE;
216
 
                        val = neweval;
217
 
                }
218
 
                else if (fabs (incr_eval) >= GAME_EVAL_INFTY 
219
 
                                || result != RESULT_NOTYET)
220
 
                        // one side has won; search no more
221
 
                {
222
 
                        ab_leaf_cnt ++;
223
 
                        val = neweval * (1 + level); // the sooner the better
224
 
                }
225
 
                else 
226
 
                {
227
 
                        int found = 0;
228
 
                        byte *movinv = mov_getinv (pos->board, move);
229
 
                        move_apply (pos->board, move);
230
 
                        if (game_stateful)
231
 
                        {
232
 
                                void *newstate = game_newstate (pos, move);
233
 
                                memcpy (oldstate, pos->state, game_state_size);
234
 
                                memcpy (pos->state, newstate, game_state_size);
235
 
                        }
236
 
                        pos->num_moves++;
237
 
                        pos->player = pos->player == WHITE ? BLACK : WHITE;
238
 
                        val = 0; // stop compiler warning
239
 
                        if (level >= 1)
240
 
                        {
241
 
                                retval = hash_get_eval (pos->board, board_wid * board_heit, 
242
 
                                                pos->num_moves, level, &cacheval);
243
 
                                if (retval && cacheval < GAME_EVAL_INFTY)  
244
 
                                        { val = cacheval; found = 1; }
245
 
                        }
246
 
                        if (!found)
247
 
                                val = ab_with_tt_incr
248
 
                                        (pos, player == WHITE ? BLACK : WHITE, 
249
 
                                                level-1, neweval, alpha, beta, best_move);
250
 
                        if (level >= 1)
251
 
                                hash_insert (pos->board, board_wid * board_heit, 
252
 
                                                pos->num_moves, level, val, NULL);
253
 
                        move_apply (pos->board, movinv);
254
 
                        free (movinv);
255
 
                        memcpy (pos->state, oldstate, game_state_size);
256
 
                        pos->num_moves--;
257
 
                        pos->player = pos->player == WHITE ? BLACK : WHITE;
258
 
                }
259
 
                if (first)
260
 
                {
261
 
                        if (best_movep) movcpy (best_movep, move);
262
 
                        first = 0;
263
 
                }
264
 
                if ((player == WHITE && val > alpha) || (player == BLACK && val < beta))
265
 
                {
266
 
                        if (best_movep) movcpy (best_movep, move);
267
 
                        if (player == WHITE) alpha = val; else beta = val;
268
 
                }
269
 
                if (alpha >= beta)
270
 
                        break;
271
 
                move = movlist_next (move);
272
 
        }
273
 
        while (move[0] != -2);
274
 
        free (movlist);
275
 
        if (game_stateful)
276
 
                free (oldstate);
277
 
        return player == WHITE ? alpha : beta;
278
 
}
279
 
#endif 
280
 
 
281
 
byte * ab_dfid (Pos *pos, int player)
282
 
{
283
 
        static byte best_move[4096];
284
 
        byte local_best_move[4096];
285
 
        int ply;
286
 
        float val = 0, eval = 0, oldval = 0;
287
 
        static GTimer *timer = NULL;
288
 
        gboolean found = FALSE;
289
 
        byte *move_list;
290
 
        engine_stop_search = 0;
291
 
        if (!game_movegen || !game_eval)
292
 
                return NULL;
293
 
        ab_leaf_cnt=0;
294
 
 
295
 
        move_list = game_movegen (pos);
296
 
        if (move_list[0] == -2)
297
 
        {
298
 
                free (move_list);
299
 
                if (opt_verbose) printf ("No move\n");
300
 
                return NULL;
301
 
        }
302
 
        if (movlist_next (move_list)[0] == -2)
303
 
        {
304
 
                movcpy (best_move, move_list);
305
 
                if (opt_verbose) printf ("Only one legal move\n");
306
 
                return best_move;
307
 
        }
308
 
 
309
 
        if (!timer) timer = g_timer_new ();
310
 
        g_timer_start (timer);
311
 
        
312
 
        for (ply = 0; !engine_stop_search; ply++)
313
 
        {
314
 
                oldval = val;
315
 
                ab_tree_exhausted = TRUE;
316
 
                pos->search_depth = 0;
317
 
                val = ab_with_tt (pos, player, ply, -1e+16, 1e+16, local_best_move);
318
 
                if (!engine_stop_search)
319
 
                {
320
 
                        movcpy (best_move, local_best_move);
321
 
                        found = TRUE;
322
 
                }
323
 
                
324
 
                if (ab_tree_exhausted)
325
 
                {
326
 
                        if (opt_verbose)
327
 
                                printf ("Searched whole tree. Moves=%d;\t Ply=%d\n",
328
 
                                        pos->num_moves, ply);
329
 
                        ply++;
330
 
                        break;
331
 
                }
332
 
                
333
 
                if (fabs (val) >= GAME_EVAL_INFTY)
334
 
                {
335
 
                        if (opt_verbose)
336
 
                                printf ("Solved the game. %s won. Moves=%d;\t Ply=%d\n",
337
 
                                        val > 0 ? "White" : "Black", pos->num_moves, ply);
338
 
                        ply++;
339
 
                        break;
340
 
                }
341
 
 
342
 
                {
343
 
                        gulong micro_sec;
344
 
                        float time_taken;
345
 
                        time_taken = g_timer_elapsed (timer, &micro_sec);
346
 
                        time_taken += micro_sec / 1000000.0;
347
 
                        if (time_taken * 1000 > time_per_move / 2)
348
 
                        {
349
 
                                ply++;
350
 
                                break;
351
 
                        }
352
 
                }
353
 
        }
354
 
        
355
 
        if (game_use_hash)
356
 
        {
357
 
                hash_print_stats ();
358
 
                hash_clear ();
359
 
        }
360
 
        
361
 
        if (opt_verbose) 
362
 
        { 
363
 
                printf ("ab_dfid(): leaves=%d \tply=%d\teval=%.1f\n", 
364
 
                                ab_leaf_cnt, ply, oldval);
365
 
                printf ("ab_dfid(): move= "); 
366
 
                move_fwrite (best_move, stdout); 
367
 
        }
368
 
        return found ? best_move : NULL;
369
 
}