aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2021-06-01 15:20:48 -0400
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commit0deb42134cb7f0ed6ec809c3de1052ab2dfe2235 (patch)
tree1ecd7fbda98295dde19c09d9b7383b9ea2c73a45 /include
parent29536c86457d83967c23a900574182b3898f04dd (diff)
Trying to make things faster
I tried the following, but they all made things worse: - moving away from the singly-linked (tail tracking) list for actions by: + using an array zipper for a deque + using an array to poorly hold a floating deque - caching the results of generating move lists in the transposition table and then + copying the resulting list/zip/deque instead of generating it + applying the move-to-front without copying, but this made the search order worse. Presumably in this case shallower nodes were messing up the search tree with garbage moves? I think some of this is not supposed to happen, but i have just the right combination of poor evaluation function and naively ordered and cheap move generation that i'm in a local minimum here.
Diffstat (limited to 'include')
-rw-r--r--include/actions.c2
-rw-r--r--include/cnn1986.c71
-rw-r--r--include/negamax.c7
3 files changed, 44 insertions, 36 deletions
diff --git a/include/actions.c b/include/actions.c
index 374f4f9..4f226e1 100644
--- a/include/actions.c
+++ b/include/actions.c
@@ -386,7 +386,7 @@ static inline void
list_append(action_list_t *list, const enum A_TYPE type,
const int8_t loc, const uint8_t data0,
const uint8_t data1) {
- action_node_t *new = malloc(sizeof(action_list_t));
+ action_node_t *new = malloc(sizeof(action_node_t));
// TODO: trap errno
new->next = NULL;
diff --git a/include/cnn1986.c b/include/cnn1986.c
index 9191ec8..b5bedfa 100644
--- a/include/cnn1986.c
+++ b/include/cnn1986.c
@@ -22,6 +22,7 @@
// Implementation of a small convolutional neural network
// ===================================================================
+static float cur_board[5*5][KERN_CHAN];
static float flattened[CONV_NUM+2];
static float dense1[DENSE1_NUM];
static float dense2[DENSE2_NUM];
@@ -29,29 +30,37 @@ static float dense2[DENSE2_NUM];
#define RELU(x) ((x) = ((x)<0)?0:(x))
float cnn1986_evaluate_black_win(void) {
/* --------------- *
- * Generate input *
+ * Populate input *
* --------------- */
+ for (unsigned int y = 0; y < board_size; y++) {
+ for (unsigned int x = 0; x < board_size; x++) {
+ const unsigned int loc = x+y*board_size;
+ const unsigned int count = COUNT_AT(loc);
+ colour_stack_t colour = colours[loc];
- float cur_board[board_size*board_size][KERN_CHAN];
- for (int y = 0; y < board_size; y++) {
- for (int x = 0; x < board_size; x++) {
- const int loc = x+y*board_size;
- for (int c = 0; c < KERN_CHAN; c++) { // heh, c++
+ if (count > 0) {
float lookup = 0;
- if (COUNT_AT(loc)>c) {
- if (c==0) {
- if (STONE_AT(loc) == STONE_STANDING) {
- lookup = (colours[loc] & 1) ? +0.25 : -0.25;
- } else if (STONE_AT(loc) == STONE_CAPSTONE) {
- lookup = (colours[loc] & 1) ? +1.00 : -1.00;
- } else {
- lookup = (colours[loc] & 1) ? +0.50 : -0.50;
- }
- } else {
- lookup = (colours[loc] & (1<<c)) ? +0.50 : -0.50;
- }
+ if (STONE_AT(loc) == STONE_STANDING) {
+ lookup = (colour & 1) ? +0.25 : -0.25;
+ } else if (STONE_AT(loc) == STONE_CAPSTONE) {
+ lookup = (colour & 1) ? +1.00 : -1.00;
+ } else {
+ lookup = (colour & 1) ? +0.50 : -0.50;
+ }
+ cur_board[loc][0] = lookup;
+
+ colour>>=1;
+ for (unsigned int c = 1; c < count && c < KERN_CHAN; c++, colour>>=1) {
+ cur_board[loc][c] = (colour & 1) ? +0.50 : -0.50;
+ }
+
+ for (unsigned int c = count; c < KERN_CHAN; c++) {
+ cur_board[loc][c] = 0;
+ }
+ } else {
+ for (unsigned int c = 0; c < KERN_CHAN; c++) {
+ cur_board[loc][c] = 0;
}
- cur_board[loc][c] = lookup;
}
}
}
@@ -59,18 +68,18 @@ float cnn1986_evaluate_black_win(void) {
* Convolution layer *
* ------------------ */
// for each kernel
- for (uint8_t kern = 0; kern < KERN_NUM; kern++) {
+ for (unsigned int kern = 0; kern < KERN_NUM; kern++) {
// the stride is 1, march across the board
- for (uint8_t bx = 0; bx < KERN_OSIZE; bx++) {
- for (uint8_t by = 0; by < KERN_OSIZE; by++) {
+ for (unsigned int bx = 0; bx < KERN_OSIZE; bx++) {
+ for (unsigned int by = 0; by < KERN_OSIZE; by++) {
flattened[kern+KERN_NUM*(bx+by*KERN_OSIZE)] =
conv2d_biases[kern];
// Compute the convolution for this position
- for (uint8_t ky = 0; ky < KERN_SIZE; ky++) {
- for (uint8_t kx = 0; kx < KERN_SIZE; kx++) {
- for (uint8_t c = 0; c < KERN_CHAN; c++) {
+ for (unsigned int ky = 0; ky < KERN_SIZE; ky++) {
+ for (unsigned int kx = 0; kx < KERN_SIZE; kx++) {
+ for (unsigned int c = 0; c < KERN_CHAN; c++) {
// Where we are on the board
- const uint8_t loc = kx+bx+(ky+by)*board_size;
+ const unsigned int loc = kx+bx+(ky+by)*board_size;
flattened[kern+KERN_NUM*(bx+by*KERN_OSIZE)]
+= cur_board[loc][c]*conv2d_weights[kern][ky][kx][c];
}
@@ -86,9 +95,9 @@ float cnn1986_evaluate_black_win(void) {
/* ------------------ *
* First dense layer *
* ------------------ */
- for (uint8_t d1 = 0; d1 < DENSE1_NUM; d1++) {
+ for (unsigned int d1 = 0; d1 < DENSE1_NUM; d1++) {
dense1[d1] = dense1_biases[d1];
- for (uint8_t fl = 0; fl < CONV_NUM+2; fl++) {
+ for (unsigned int fl = 0; fl < CONV_NUM+2; fl++) {
dense1[d1] += flattened[fl]*dense1_weights[d1][fl];
}
RELU(dense1[d1]);
@@ -96,9 +105,9 @@ float cnn1986_evaluate_black_win(void) {
/* ------------------- *
* Second dense layer *
* ------------------- */
- for (uint8_t d2 = 0; d2 < DENSE2_NUM; d2++) {
+ for (unsigned int d2 = 0; d2 < DENSE2_NUM; d2++) {
dense2[d2] = dense2_biases[d2];
- for (uint8_t d1 = 0; d1 < DENSE1_NUM; d1++) {
+ for (unsigned int d1 = 0; d1 < DENSE1_NUM; d1++) {
dense2[d2] += dense1[d1]*dense2_weights[d2][d1];
}
RELU(dense2[d2]);
@@ -107,7 +116,7 @@ float cnn1986_evaluate_black_win(void) {
* Output layer *
* ------------- */
float output = output_bias;
- for (uint8_t d2 = 0; d2 < DENSE2_NUM; d2++) {
+ for (unsigned int d2 = 0; d2 < DENSE2_NUM; d2++) {
output += dense2[d2]*output_weights[d2];
}
// 2*(clamped Pade approximant of logistic function) - 1
diff --git a/include/negamax.c b/include/negamax.c
index a970b83..30aed05 100644
--- a/include/negamax.c
+++ b/include/negamax.c
@@ -64,6 +64,8 @@ float negamax_generate(void) {
}
tt_free();
+ action_to_ptn(negamax_best_action, negamax_ptn);
+
return result;
}
@@ -138,10 +140,6 @@ static float negamax(const uint8_t cur_depth, const uint8_t init_depth,
if (node_value > best_value) {
best_value = node_value;
best_action = node->action;
- if (cur_depth == init_depth) {
- action_to_ptn(node->action, negamax_ptn);
- negamax_best_action = best_action;
- }
}
if (best_value > alpha) alpha = best_value;
@@ -149,6 +147,7 @@ static float negamax(const uint8_t cur_depth, const uint8_t init_depth,
}
action_list_free(list);
+ if (cur_depth == init_depth) negamax_best_action = best_action;
flag = TT_EXACT;
if (best_value >= beta) flag = TT_LOWERBOUND;