OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [search.cpp] - Blame information for rev 11

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 sumanta.ch
 
2
/*
3
 
4
connectk -- UMN CSci 5512W project
5
 
6
*/
7
 
8 10 sumanta.ch
//#include "config.h"
9
//#include <string.h>
10
//#include <glib.h>
11 11 sumanta.ch
//#include <iostream>
12
#include <stdio.h>
13 10 sumanta.ch
#include "./shared.h"
14
#include "pico.h"
15
//#include "../connectk.h"
16 8 sumanta.ch
 
17
/* Variables required to check for cache hits */
18 10 sumanta.ch
//static int cache_id = -1, cache_depth = -1, cache_branch = -1;
19
//static SEARCH cache_search = -1;
20
//static AIMoves *cache_moves = NULL;
21
//static AIWEIGHT cache_best;
22
//static Player *cache_player;
23
//static AIFunc cache_func;
24
//static BCOORD cache_size;
25 8 sumanta.ch
 
26 10 sumanta.ch
int ai_stop=0;
27
static AIWEIGHT df_search(Board *b, AIMoves *moves,unsigned int *index, Player *player,
28 8 sumanta.ch
                          int depth, int cache_index,
29
                          PIECE searched, AIWEIGHT alpha, AIWEIGHT beta)
30
/* Depth is in _moves_ */
31
{
32
        int i, j;
33
 
34
        /* Halt and depth abort */
35
        if (ai_stop || depth < 1)
36
                return moves->utility;
37
 
38
        /* Alpha-beta sanity check */
39
        if (alpha >= beta) {
40 10 sumanta.ch
                //g_warning("DFS alpha-beta failed sanity check");
41 11 sumanta.ch
                        //printf("DFS alpha-beta failed sanity check\n");
42 8 sumanta.ch
                return moves->utility;
43
        }
44
 
45
        /* Search only the top moves beyond the minimum */
46 10 sumanta.ch
        //aimoves_sort(moves);
47 8 sumanta.ch
        if (moves->len > player->branch) {
48
                for (i = player->branch; i < moves->len; i++)
49
                        if (moves->data[i].weight != moves->data[0].weight)
50
                                break;
51
                moves->len = i;
52
        }
53
 
54
        /* No moves left -- its a draw */
55
        if (moves->len < 1)
56
                return AIW_DRAW;
57
 
58
        /* Search each move available in depth first order */
59
        for (i = 0; i < moves->len; i++) {
60 11 sumanta.ch
                Board b_next;
61 8 sumanta.ch
                AIMove *aim = moves->data + i;
62 10 sumanta.ch
                AIMoves moves_next;
63 8 sumanta.ch
 
64
                /* Did we get a bad move? */
65
                if (!piece_empty(piece_at(b, aim->x, aim->y))) {
66 10 sumanta.ch
                        //g_warning("DFS utility function suggested a bad move "
67
                                  //"(%s)", bcoords_to_string(aim->x, aim->y));
68 11 sumanta.ch
                        //printf("bad move\n");
69 8 sumanta.ch
                        continue;
70
                }
71
 
72
                /* Already searched here? */
73
                if (piece_at(b, aim->x, aim->y) == searched)
74
                        continue;
75
                place_piece_type(b, aim->x, aim->y, searched);
76
 
77 10 sumanta.ch
                //b_next = board_new();
78
                board_copy(b, &b_next);
79
                place_piece(&b_next, aim->x, aim->y);
80 8 sumanta.ch
 
81
                /* Did we win? */
82 10 sumanta.ch
                if (check_win(&b_next, aim->x, aim->y))
83 8 sumanta.ch
                        aim->weight = AIW_WIN;
84
 
85
                /* Otherwise, search deeper */
86
                else  {
87
                        int next_ci = cache_index + 1;
88
                        AIWEIGHT next_alpha = alpha, next_beta = beta;
89 10 sumanta.ch
                        //AIFunc func;
90 8 sumanta.ch
 
91 10 sumanta.ch
                        b_next.moves_left--;
92 8 sumanta.ch
 
93
                        /* Player has changed */
94 10 sumanta.ch
                        if (b_next.moves_left <= 0) {
95
                                b_next.moves_left = place_p;
96
                                b_next.turn = other_player(b->turn);
97 8 sumanta.ch
                                next_ci += place_p;
98
                                searched++;
99
                                next_alpha = -beta;
100
                                next_beta = -alpha;
101
                        }
102
 
103 10 sumanta.ch
                        //func = ai(player->ai)->func;
104
                        //if (!func) {
105
                        //        g_warning("DFS player has no AI function");
106
                        //        return moves->utility;
107
                        //}
108
                        //moves_next = func(b_next);
109 11 sumanta.ch
        printf("depth %d branch %d player %d moves_left %d alpha %d MOVE %d %d \n",depth,i,b_next.turn,b_next.moves_left,alpha,aim->y+1,aim->x+1);
110 10 sumanta.ch
                        ai_threats(&b_next,&moves_next,index);
111
 
112
                        aim->weight = df_search(&b_next, &moves_next, index,player,
113 8 sumanta.ch
                                                depth - 1, next_ci, searched,
114
                                                next_alpha, next_beta);
115 10 sumanta.ch
                        //aimoves_free(moves_next);
116
                        if (b_next.turn != b->turn)
117 8 sumanta.ch
                                aim->weight = -aim->weight;
118
                }
119
 
120
                /* Debug search */
121 10 sumanta.ch
                //if (opt_debug_dfsc) {
122
                //        for(j = MAX_DEPTH - depth; j > 0; j--)
123
                //                //g_print("-");
124
                //        //g_print("> d=%d, %s, u=%d, a=%d, b=%d %s\n",
125
                //        //        depth, bcoords_to_string(aim->x, aim->y),
126
                //        //        aim->weight, alpha, beta,
127
                //        //        piece_to_string(b->turn));
128
                //}
129 8 sumanta.ch
 
130 10 sumanta.ch
                //board_free(b_next);
131 8 sumanta.ch
                if (aim->weight > alpha) {
132
                        alpha = aim->weight;
133 10 sumanta.ch
                        //cache_set(cache_index, aim);
134 8 sumanta.ch
 
135
                        /* Victory abort */
136
                        if (alpha >= AIW_WIN)
137
                                return AIW_WIN;
138
 
139
                        /* Alpha-beta pruning */
140
                        if (alpha >= beta)
141
                                return alpha;
142
                }
143
        }
144
 
145
        return alpha;
146
}
147
 
148 10 sumanta.ch
int  search(const Board *b, AIMove *move, Player *player)
149 8 sumanta.ch
{
150 10 sumanta.ch
        AIMoves moves;
151
        Board copy;
152
        unsigned int index[361]={0};
153
        //AIFunc move_func = ai(player->ai)->func;
154 8 sumanta.ch
 
155
        /* Player is not configured to search */
156 10 sumanta.ch
        //if (player->search == SEARCH_NONE)
157
        //        return;
158 8 sumanta.ch
 
159
        /* Moves list does not need to be searched */
160 10 sumanta.ch
        //if (moves->len <= b->moves_left) {
161
        ////        if (opt_debug_dfsc)
162
        ////                g_debug("DFS no choice abort");
163
        //        return;
164
        //}
165 8 sumanta.ch
 
166 10 sumanta.ch
        ///* Board size changed, cache is invalidated */
167
        //if (board_size != cache_size)
168
        //        cache_moves = NULL;
169
        //cache_size = board_size;
170 8 sumanta.ch
 
171 10 sumanta.ch
        ///* Cache hit, last or same board */
172
        //if (player->cache && cache_moves && cache_moves->len &&
173
        //    cache_search == player->search &&
174
        //    cache_depth == player->depth &&
175
        //    cache_player == player &&
176
        //    cache_func == move_func &&
177
        //    cache_branch == player->branch) {
178
        //        if (b->parent && cache_id == b->parent->ac.id) {
179
        //                aimoves_remove(cache_moves, b->parent->move_x,
180
        //                               b->parent->move_y);
181
        //                cache_id = b->ac.id;
182
        //        }
183
        //        if (cache_id == b->ac.id && cache_moves->len) {
184
        //                if (cache_moves->len) {
185
        //                        aimoves_copy(cache_moves, moves);
186
        //                        if (opt_debug_dfsc)
187
        //                                g_debug("DFS cache HIT");
188
        //                        return;
189
        //                }
190
        //                aimoves_free(cache_moves);
191
        //                cache_moves = NULL;
192
        //        }
193
        //}
194 8 sumanta.ch
 
195
        /* Cache miss */
196 10 sumanta.ch
        //if (opt_debug_dfsc)
197
        //        g_debug("DFS cache MISS");
198
        //cache_id = b->ac.id;
199
        //if (!cache_moves)
200
        //        cache_moves = aimoves_new();
201
        //cache_moves->len = 0;
202
        //cache_best = AIW_MIN;
203
        //copy = board_new();
204
        board_copy(b, &copy);
205
        ai_threats(&copy,&moves,&index[0]);
206
 
207
        //if (player->search == SEARCH_DFS) {
208
                df_search(&copy, &moves, &index[0],player, player->depth, 0,
209 8 sumanta.ch
                          PIECE_SEARCHED, AIW_LOSE, AIW_WIN);
210 10 sumanta.ch
        int ret_val;
211
        ret_val=aimoves_choose(&moves, move,&index[0]);
212
        if (!ret_val)
213
                return 0;
214
        else return 1;
215
          //      if (cache_moves->len)
216
          //              aimoves_copy(cache_moves, moves);
217
        //} else {
218
        //        board_free(copy);
219
        //        g_warning("Unsupported search type %d", player->search);
220
        //        return;
221
        //}
222
        //board_free(copy);
223 8 sumanta.ch
 
224 10 sumanta.ch
        ///* Debug DFS search */
225
        //if (opt_debug_dfsc)
226
        //        dfs_cache_dump();
227 8 sumanta.ch
 
228 10 sumanta.ch
        ///* Save params so we can check if we have a hit later */
229
        //cache_player = player;
230
        //cache_search = player->search;
231
        //cache_depth = player->depth;
232
        //cache_branch = player->branch;
233
        //cache_func = move_func;
234 8 sumanta.ch
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.