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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [search.cpp] - Blame information for rev 17

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

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

powered by: WebSVN 2.1.0

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