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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [search_bfs.cpp] - Blame information for rev 13

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

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

powered by: WebSVN 2.1.0

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