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 15

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

powered by: WebSVN 2.1.0

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