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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [XILINX/] [BUILD_SCC_SRCH/] [synth_src/] [search_bfs.cpp] - Blame information for rev 18

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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