summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2021-01-10 21:40:38 -0500
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commitb598a3ab9e682a826516b242a710b26e0c22389f (patch)
treeeedbf2f8f2b4343fa1ae86146439090057c99258 /include
parenta627eb4b4ce13834b85dfd1ced666cc912516d52 (diff)
This is the general idea, but i'm sure order is incorrect somewhere
Diffstat (limited to 'include')
-rw-r--r--include/ct1973.c82
-rw-r--r--include/ct1973.h7
-rw-r--r--include/tai1973.h3
-rw-r--r--include/weights.h6
4 files changed, 92 insertions, 6 deletions
diff --git a/include/ct1973.c b/include/ct1973.c
new file mode 100644
index 0000000..da56d19
--- /dev/null
+++ b/include/ct1973.c
@@ -0,0 +1,82 @@
+#include "ct1973.h"
+
+static float flattened[CONV_NUM+2];
+static float dense1[DENSE1_NUM];
+static float dense2[DENSE2_NUM];
+
+#define RELU(x) ((x) = ((x)>0)?(x):0)
+
+float
+evaluate_black_win(void) {
+ /* ------------------ *
+ * Convolution layer *
+ * ------------------ */
+ // for each kernel
+ for (uint8_t k = 0; k < KERN_NUM; k++) {
+ // the stride is 1, march across the board
+ for (uint8_t by = 0; by < KERN_OSIZE; by++) {
+ for (uint8_t bx = 0; bx < KERN_OSIZE; bx++) {
+ flattened[(k*KERN_OSIZE+by)*KERN_OSIZE+bx] = conv2d_biases[k];
+ // 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++) {
+ // Where we are on the board
+ const uint8_t loc = kx+bx+(ky+by)*board_size;
+ // Look up what's on the board at this location, and
+ // multiply it. For c=0 we have to do some extra work
+ 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[k] & (1<<c)) ? +0.50 : -0.50;
+ }
+ }
+ flattened[(k*KERN_OSIZE+by)*KERN_OSIZE+bx]
+ += lookup*conv2d_weights[k][c][ky][kx];
+ }
+ }
+ }
+ RELU(flattened[(k*KERN_OSIZE+by)*KERN_OSIZE+bx]);
+ }
+ }
+ }
+ /* ------------------ *
+ * First dense layer *
+ * ------------------ */
+ flattened[CONV_NUM] = (float)(white_count & 127)/21.0;
+ flattened[CONV_NUM+1] = (float)(black_count & 127)/21.0;
+ for (uint8_t d1 = 0; d1 < DENSE1_NUM; d1++) {
+ dense1[d1] = dense1_biases[d1];
+ for (uint8_t fl = 0; fl < CONV_NUM+2; fl++) {
+ dense1[d1] += flattened[fl]*dense1_weights[fl][d1];
+ }
+ RELU(dense1[d1]);
+ }
+ /* ------------------- *
+ * Second dense layer *
+ * ------------------- */
+ for (uint8_t d2 = 0; d2 < DENSE2_NUM; d2++) {
+ dense2[d2] = dense2_biases[d2];
+ for (uint8_t d1 = 0; d1 < DENSE1_NUM; d1++) {
+ dense2[d2] += dense1[d1]*dense2_weights[d1][d2];
+ }
+ RELU(dense2[d2]);
+ }
+ /* ------------- *
+ * Output layer *
+ * ------------- */
+ float black = output_biases[1];
+ for (uint8_t d2 = 0; d2 < DENSE2_NUM; d2++) {
+ black += dense2[d2]*output_weights[d2][1];
+ }
+ RELU(black);
+ return black;
+}
diff --git a/include/ct1973.h b/include/ct1973.h
new file mode 100644
index 0000000..632f3ba
--- /dev/null
+++ b/include/ct1973.h
@@ -0,0 +1,7 @@
+#include <stdint.h>
+
+#include "tak.h"
+#include "weights.h"
+
+float
+evaluate_black_win(void);
diff --git a/include/tai1973.h b/include/tai1973.h
deleted file mode 100644
index 90ea278..0000000
--- a/include/tai1973.h
+++ /dev/null
@@ -1,3 +0,0 @@
-#include <stdint.h>
-
-#include "tak.h"
diff --git a/include/weights.h b/include/weights.h
index 63ae361..9a6257a 100644
--- a/include/weights.h
+++ b/include/weights.h
@@ -1,8 +1,8 @@
#define KERN_NUM 12
#define KERN_CHAN 8
#define KERN_SIZE 3
-#define KERN_OSZE (5-KERN_SIZE+1)
-#define CONV_NUM (KERN_NUM * KERN_OSZE * KERN_OSZE + 2)
+#define KERN_OSIZE (5-KERN_SIZE+1)
+#define CONV_NUM (KERN_NUM * KERN_OSIZE * KERN_OSIZE)
#define DENSE1_NUM 9
#define DENSE2_NUM 8
@@ -409,7 +409,7 @@ const float conv2d_biases[KERN_NUM] =
-0.49402446, -0.44366917, -0.15726285, -0.37746304,
0.03534909, -0.80909145, 0.22329862, -0.16383767};
-const float dense1_weights[CONV_NUM][DENSE1_NUM] =
+const float dense1_weights[CONV_NUM+2][DENSE1_NUM] =
{{-2.51253784e-01, -1.75899580e-01, 6.41923770e-02,
2.38801807e-01, 1.66776031e-01, -3.04984421e-01,
-2.16516569e-01, 1.90220159e-02, 6.49385035e-01},