aboutsummaryrefslogtreecommitdiff
path: root/include/negamax.c
diff options
context:
space:
mode:
Diffstat (limited to 'include/negamax.c')
-rw-r--r--include/negamax.c96
1 files changed, 52 insertions, 44 deletions
diff --git a/include/negamax.c b/include/negamax.c
index 3e3a209..fd8c7ac 100644
--- a/include/negamax.c
+++ b/include/negamax.c
@@ -95,7 +95,6 @@ static void push_stones(const int8_t location,
| ((celldat[location] + ((count << NUM_SHIFT))) & NUM_MASK);
}
-static float val;
static enum WIN_TYPE w;
#define WIN_EVALUATE_OR_RECURSE(store,reset) { \
@@ -104,53 +103,57 @@ static enum WIN_TYPE w;
if (w < 0xFF) { \
/* Somebody won, assign weights accordingly. */ \
if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) { \
- val = colour*infty; \
+ value = colour*infty; \
/* Always take the win */ \
- if (cur_depth == 0 && val > 0) { \
+ if (value > 0) { \
{ reset }; \
- { store }; \
- return infty; \
+ if (cur_depth == negamax_search_depth) { store }; \
+ goto prune; \
} \
/* Fix draw value to be completely neutral */ \
- } else if (w == WIN_DRAW) val = 0; \
- else val = -colour*infty; \
- } else if (cur_depth == negamax_search_depth) { \
+ } else if (w == WIN_DRAW) value = 0; \
+ else value = -colour*infty; \
+ } if (cur_depth == 0) { \
/* We're at the bottom, evaluate */ \
- val = colour * cnn1986_evaluate_black_win(); \
+ value = fmax(value, colour * cnn1986_evaluate_black_win()); \
} else { \
/* We're not at the bottom, recurse first */ \
next_ply(); \
- val = -negamax(cur_depth + 1, -beta, -alpha, -colour); \
+ value = fmax(value, -negamax(cur_depth - 1, -beta, -alpha, -colour)); \
previous_ply(); \
} \
{ reset }; \
- /* Prune */ \
- if (val >= beta) return beta; \
/* Update the optimal value, which alpha carries */ \
- if (val > alpha) { \
- alpha = val; \
- if (cur_depth == 0) { store }; \
+ if (value > alpha) { \
+ alpha = value; \
+ if (cur_depth == negamax_search_depth) { store }; \
+ /* Prune */ \
+ if (alpha >= beta) goto prune; \
} \
}
float negamax(const uint8_t cur_depth, float alpha, float beta,
const float colour) {
- uint64_t hash = negamax_compute_zobrist();
- tt_entry_t *entry = tt_seek(hash);
- const float alpha_orig = alpha;
-
- if (entry != NULL && entry -> depth <= cur_depth) {
- if (entry->flag == TT_EXACT) {
- return entry->value;
- } else if (entry->flag == TT_LOWERBOUND) {
- if (entry->value > alpha) alpha = entry->value;
- } else if (entry->flag == TT_UPPERBOUND) {
- if (entry->value < beta) beta = entry->value;
- }
- if (alpha >= beta) return entry->value;
- }
-
+ /*
+ * uint64_t hash = negamax_compute_zobrist();
+ * tt_entry_t *entry = tt_seek(hash);
+ * const float alpha_orig = alpha;
+ *
+ * if (entry != NULL && entry->depth >= cur_depth) {
+ * if (entry->flag == TT_EXACT) {
+ * return entry->value;
+ * } else if (entry->flag == TT_LOWERBOUND) {
+ * alpha = fmax(alpha, entry->value);
+ * } else if (entry->flag == TT_UPPERBOUND) {
+ * beta = fmin(beta, entry->value);
+ * }
+ * if (alpha >= beta) return entry->value;
+ * }
+ * enum TT_FLAG flag;
+ */
+
+ float value = -infty;
const uint8_t black = (ply & 1),
material = (black) ? black_count : white_count,
flat = material & 127,
@@ -347,19 +350,22 @@ float negamax(const uint8_t cur_depth, float alpha, float beta,
}
}
- enum TT_FLAG flag = TT_EXACT;
- if (alpha <= alpha_orig) flag = TT_UPPERBOUND;
- else if (alpha >= beta) flag = TT_UPPERBOUND;
-
- if (entry == NULL) {
- tt_insert(hash, flag, cur_depth, alpha);
- } else {
- entry->flag = flag;
- entry->value = alpha;
- entry->depth = cur_depth;
- }
-
- return alpha;
+ prune:
+ /*
+ * flag = TT_EXACT;
+ * if (value <= alpha_orig) flag = TT_UPPERBOUND;
+ * else if (value >= beta) flag = TT_UPPERBOUND;
+ *
+ * if (entry == NULL) {
+ * tt_insert(hash, flag, cur_depth, value);
+ * } else {
+ * entry->flag = flag;
+ * entry->value = value;
+ * entry->depth = cur_depth;
+ * }
+ */
+
+ return value;
}
inline float
@@ -369,7 +375,9 @@ negamax_generate(void) {
const float safe_infty = infty + 1;
tt_init();
- float result = negamax(0, -safe_infty, safe_infty, (ply&1)?1.0:-1.0);
+ float result = negamax(negamax_search_depth,
+ -safe_infty, safe_infty,
+ (ply&1)?1.0:-1.0);
tt_free();
return result;