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 12

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

powered by: WebSVN 2.1.0

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