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 14

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

powered by: WebSVN 2.1.0

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