ref: 7b9f83e03f011c5c384e066214a81544d296c157
parent: 36fb1feedf9263ad373371e9fc85d8b057da122f
author: zamfofex <zamfofex@twdb.moe>
date: Wed Sep 18 23:29:11 EDT 2024
make improvements and simplifications
--- a/chess.c
+++ b/chess.c
@@ -3,62 +3,6 @@
#include "moonfish.h"
-moonfish_t moonfish_values[moonfish_size] = {10018,6197,13742,20265,18885,-342,0,6774,8234,19746,19826,8133,7748,0,14909,19962,16209,-626,3200,11794,0,20831,17808,2331,9960,11154,16101,0,16443,-321,9922,9553,11914,15629,0,-889,13093,2080,1464,6178,889,0,11428,7046,14154,17650,17730,2406,10440,7005,13087,19265,19949,410,7084,7102,38,19215,21044,845,9384,7266,13726,20727,19654,-81,9528,7427,11709,20251,1861,9603,9701,7048,11465,20080,9603,0,0,0,0,0,23861,19671,20199,18789,13275,7766,7405,7237,7018,-1804,-2692,-2458,3890,-4358,-4924,-4265,3114,-4929,-5637,-6308,3834,-3087,-5210,-7140,0,0,0,0,11102,8573,8899,10834,14265,11479,3003,4139,13080,3599,-8431,-7074,14841,3244,-8967,-8168,13439,1982,-10393,-10556,10011,-458,-12847,-13100,15511,10850,-1595,-1143,9901,15251,8821,9572,26448,15718,17220,17953,16633,-6319,-6154,-5215,21217,-5336,-3635,-3049,20050,-4507,-4287,-2630,19910,-5376,-4347,-4017,19474,-5900,-5030,-4681,18801,-5651,-6395,-7239,29091,17184,16394,16484,36920,16419,16372,17421,17661,-2571,-1461,-1335,16435,-2556,-2056,-2014,14483,-4074,-3829,-3995,11706,-6133,-6216,-6371,9694,-8191,-8385,-8176,7358,-10570,-9813,-9690,27362,9776,11255,11747,71012,35511,38170,40036,37937,-16976,-13070,-11601,38573,-14295,-12982,-10860,39968,-16951,-14245,-14049,38376,-17331,-18035,-18791,36345,-19106,-18880,-19951,33663,-21879,-21634,-21735,68560,31904,32329,33224,169,3435,3850,3038,1695,8128,5002,4953,691,4616,3554,2015,-2061,186,779,-471,-5789,-2687,-1912,-1942,-5593,-3130,-2840,-3162,-1159,-144,-1939,-3987,-377,1153,-3549,-2521};-
-moonfish_t moonfish_score(struct moonfish_chess *chess)
-{- static int deltas[][5] =
- {- {0}, {9, 11, 0},- {21, 19, 12, 8, 0},- {11, 9, 0},- {10, 1, 0},- {10, 1, 11, 9, 0},- {10, 1, 11, 9, 0},- };
-
- int x, y;
- int x1, y1;
- int from;
- unsigned char type, color, other;
- int i, j;
- moonfish_t score, value;
-
- score = 0;
-
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- from = (x + 1) + (y + 2) * 10;
- type = chess->board[from] % 16;
- color = chess->board[from] / 16;
-
- if (chess->board[from] != moonfish_empty)
- {- x1 = x;
- y1 = y;
- if (x1 > 3) x1 = 7 - x1;
- if (color == 1) y1 = 7 - y1;
- score -= moonfish_values[78 + x1 + y1 * 4 + (type - 1) * 32] * (color * 2 - 3);
- }
-
- for (j = 0 ; j < 2 ; j++)
- for (i = 0 ; deltas[type][i] ; i++)
- {- if (type == moonfish_pawn && j != color - 1) continue;
- other = chess->board[from + deltas[type][i] * (j * 2 - 1)];
- if (other == moonfish_outside) continue;
- if (other / 16 != color) other %= 16;
- else other = other % 16 + 6;
- value = moonfish_values[other * 6 + type - 1];
- if (color == 2) value *= -1;
- score += value;
- }
- }
-
- return score;
-}
-
static void moonfish_force_promotion(struct moonfish_chess *chess, struct moonfish_move **moves, unsigned char from, unsigned char to, unsigned char promotion)
{(*moves)->from = from;
@@ -418,13 +362,10 @@
for (i = 0 ; i < count ; i++)
{- if (moves[i].to == to)
+ if (moves[i].to == to && moonfish_validate(&moves[i].chess))
{- if (moonfish_validate(&moves[i].chess))
- {- *found = moves[i];
- return 0;
- }
+ *found = moves[i];
+ return 0;
}
}
@@ -471,6 +412,7 @@
continue;
}
+ /* note: assumes ASCII */
if (ch >= 'A' && ch <= 'Z')
ch -= 'A' - 'a',
color = 0x10;
--- a/main.c
+++ b/main.c
@@ -104,7 +104,7 @@
perror(argv[0]);
return 1;
}
- if (*end != 0 || depth < 0 || depth > 100)
+ if (*end != 0 || depth < 0 || depth > 1000)
{fprintf(stderr, "%s: malformed depth in 'go' command\n", argv[0]);
return 1;
@@ -133,6 +133,10 @@
return 1;
}
}
+ else
+ {+ fprintf(stderr, "%s: warning: unexpected '%s' in 'go' command\n", argv[0], arg);
+ }
}
if (our_time < 0) our_time = 0;
@@ -146,7 +150,7 @@
score = moonfish_best_move_clock(&chess, &move, our_time, their_time);
if (depth < 0) depth = 1;
- printf("info depth %d score cp %ld\n", depth, score / 100);+ printf("info depth %d score cp %ld\n", depth, score);moonfish_to_uci(&chess, &move, name);
printf("bestmove %s\n", name);--- a/moonfish.h
+++ b/moonfish.h
@@ -80,9 +80,6 @@
#define moonfish_empty 0
#define moonfish_outside 0xFF
-/* size of the PST */
-#define moonfish_size 270
-
/* for tuning the PST */
#ifdef moonfish_learn
#define moonfish_t double
--- a/search.c
+++ b/search.c
@@ -20,36 +20,41 @@
#ifdef moonfish_no_threads
-#define pthread_t int
-#define pthread_create(thread, attr, fn, arg) ((*fn)(arg), 0)
-#define pthread_join(thread, ret) 0
-typedef int moonfish_result_t;
+#define thrd_t int
+#define thrd_create(thread, fn, arg) ((*fn)(arg), 0)
+#define thrd_join(thread, ret) 0
+#define moonfish_result_t int
#define moonfish_value 0
+#define mtx_t int
+#define thrd_success 0
#elif defined(moonfish_c11_threads)
#include <threads.h>
-#define pthread_t thrd_t
-#define pthread_create(thread, attr, fn, arg) thrd_create(thread, fn, arg)
-#define pthread_join thrd_join
-typedef int moonfish_result_t;
+#define moonfish_result_t int
#define moonfish_value 0
#else
#include <pthread.h>
+#define thrd_t pthread_t
+#define thrd_create(thread, fn, arg) pthread_create(thread, NULL, fn, arg)
+#define thrd_join pthread_join
#define moonfish_result_t void *
#define moonfish_value NULL
+#define mtx_t pthread_mutex_t
+#define thrd_success 0
#endif
#include "moonfish.h"
-struct moonfish_info
+#define moonfish_omega 0x2000
+
+struct moonfish_thread
{+ thrd_t thread;
struct moonfish_analysis *analysis;
- struct moonfish_move moves[1024];
- pthread_t thread;
struct moonfish_move move;
int score;
};
@@ -57,7 +62,7 @@
struct moonfish_analysis
{struct moonfish_chess chess;
- struct moonfish_info info[256];
+ struct moonfish_thread threads[256];
int score;
int depth;
long int time;
@@ -91,54 +96,75 @@
#endif
-static int moonfish_search(struct moonfish_info *info, struct moonfish_chess *chess, struct moonfish_move *moves, int alpha, int beta, int depth, long int t0, long int time)
+moonfish_t moonfish_values[] = {0,0,0,0,138,180,159,139,137,167,147,150,135,159,159,167,170,190,176,191,222,260,267,253,313,370,387,366,0,0,0,0,311,363,377,386,366,390,408,413,382,416,436,433,416,448,459,462,431,459,483,483,435,479,491,505,402,418,469,477,307,390,403,431,431,422,411,426,452,467,461,456,466,470,482,483,473,475,483,493,470,485,492,508,489,483,496,505,441,465,476,483,442,451,462,465,653,686,713,726,660,684,687,698,680,703,700,711,709,726,728,729,736,755,757,757,760,781,785,777,780,772,790,785,762,764,759,775,1282,1267,1261,1274,1284,1289,1295,1297,1290,1300,1303,1301,1323,1338,1325,1325,1344,1328,1366,1361,1328,1368,1379,1392,1326,1324,1363,1384,1286,1306,1348,1351,-4,5,-51,-42,-9,-11,-30,-58,-37,-26,-36,-36,-44,-16,-17,-16,-11,14,9,-14,19,50,36,15,26,86,41,36,2,42,42,34};+
+moonfish_t moonfish_score(struct moonfish_chess *chess)
{+ int x, y;
+ int x1, y1;
+ int from;
+ unsigned char type, color;
+ moonfish_t score;
+
+ score = 0;
+
+ for (y = 0 ; y < 8 ; y++)
+ for (x = 0 ; x < 8 ; x++)
+ {+ from = (x + 1) + (y + 2) * 10;
+ type = chess->board[from] % 16;
+ color = chess->board[from] / 16 - 1;
+
+ if (chess->board[from] == moonfish_empty) continue;
+ x1 = x;
+ y1 = y;
+ if (x1 > 3) x1 = 7 - x1;
+ if (color == 1) y1 = 7 - y1;
+ score -= moonfish_values[x1 + y1 * 4 + (type - 1) * 32] * (color * 2 - 1);
+ }
+
+ return score;
+}
+
+static int moonfish_search(struct moonfish_thread *thread, struct moonfish_chess *chess, int alpha, int beta, int depth, long int t0, long int time)
+{int score;
int i;
int x, y;
int count;
long int t1, c;
+ struct moonfish_move moves[32];
- if (moves - info->moves > (int) (sizeof info->moves / sizeof *info->moves - 256))
- depth = -10;
-
if (depth < 0)
{score = moonfish_score(chess);
if (!chess->white) score *= -1;
- if (depth < -3) return score;
if (score >= beta) return beta;
+ if (score < alpha - 100) return alpha;
if (score > alpha) alpha = score;
}
- else if (info->analysis->time >= 0 && time < 5)
- {- depth = 0;
- }
- count = 0;
-
for (y = 0 ; y < 8 ; y++)
for (x = 0 ; x < 8 ; x++)
- count += moonfish_moves(chess, moves + count, (x + 1) + (y + 2) * 10);
-
- for (i = 0 ; i < count ; i++)
- if (chess->board[moves[i].to] % 16 == moonfish_king)
- return moonfish_omega * (moonfish_depth + depth);
-
- for (i = 0 ; i < count ; i++)
{- if (depth < 0)
- if (chess->board[moves[i].to] == moonfish_empty)
- if (moves[i].chess.board[moves[i].to] == chess->board[moves[i].from])
- continue;
-
- t1 = moonfish_clock();
- c = time * i / count - t1 + t0;
-
- score = -moonfish_search(info, &moves[i].chess, moves + count, -beta, -alpha, depth - 1, t1, time / count + c);
-
- if (score >= beta) return beta;
- if (score > alpha) alpha = score;
+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++)
+ {+ if (!moonfish_validate(&moves[i].chess)) continue;
+
+ if (depth < 0)
+ if (chess->board[moves[i].to] == moonfish_empty)
+ if (moves[i].chess.board[moves[i].to] == chess->board[moves[i].from])
+ continue;
+
+ t1 = moonfish_clock();
+ c = 2 * time * i / count - t1 + t0;
+
+ score = -moonfish_search(thread, &moves[i].chess, -beta, -alpha, depth - 1, t1, time / count + c);
+
+ if (score >= beta) return beta;
+ if (score > alpha) alpha = score;
+ }
}
return alpha;
@@ -146,13 +172,14 @@
static moonfish_result_t moonfish_start_search(void *data)
{- struct moonfish_info *info;
+ struct moonfish_thread *thread;
- info = data;
- info->score = -moonfish_search(
- info, &info->move.chess, info->moves,
- -100 * moonfish_omega, 100 * moonfish_omega,
- info->analysis->depth, moonfish_clock(), info->analysis->time
+ thread = data;
+
+ thread->score = -moonfish_search(
+ thread, &thread->move.chess,
+ -4 * moonfish_omega, 4 * moonfish_omega,
+ thread->analysis->depth, moonfish_clock(), thread->analysis->time
);
return moonfish_value;
@@ -197,55 +224,39 @@
{if (!moonfish_validate(&moves[i].chess)) continue;
- analysis->info[j].analysis = analysis;
- analysis->info[j].move = moves[i];
+ analysis->threads[j].analysis = analysis;
+ analysis->threads[j].move = moves[i];
- result = pthread_create(&analysis->info[j].thread, NULL, &moonfish_start_search, analysis->info + j);
+ result = thrd_create(&analysis->threads[j].thread, &moonfish_start_search, analysis->threads + j);
#ifndef moonfish_mini
-#ifdef moonfish_c11_threads
if (result != thrd_success)
{fprintf(stderr, "error creating thread\n");
exit(1);
}
-#else
- if (result)
- {- fprintf(stderr, "%s\n", strerror(result));
- exit(1);
- }
#endif
-#endif
j++;
}
}
- analysis->score = -200 * moonfish_omega;
+ analysis->score = -6 * moonfish_omega;
for (i = 0 ; i < j ; i++)
{- result = pthread_join(analysis->info[i].thread, NULL);
+ result = thrd_join(analysis->threads[i].thread, NULL);
#ifndef moonfish_mini
-#ifdef moonfish_c11_threads
if (result != thrd_success)
{fprintf(stderr, "error joining thread\n");
exit(1);
}
-#else
- if (result)
- {- fprintf(stderr, "%s\n", strerror(result));
- exit(1);
- }
#endif
-#endif
- if (analysis->info[i].score > analysis->score)
+ if (analysis->threads[i].score > analysis->score)
{- *best_move = analysis->info[i].move;
- analysis->score = analysis->info[i].score;
+ *best_move = analysis->threads[i].move;
+ analysis->score = analysis->threads[i].score;
}
}
}
@@ -280,10 +291,6 @@
int moonfish_best_move_clock(struct moonfish_chess *chess, struct moonfish_move *best_move, long int our_time, long int their_time)
{- long int time0, time1;
-
- time0 = our_time / 16;
- time1 = our_time - time0 - their_time * 7 / 8;
- if (time1 < 0) time1 = 0;
- return moonfish_best_move_time(chess, best_move, time0 + time1);
+ (void) their_time;
+ return moonfish_best_move_time(chess, best_move, our_time / 16);
}
--- a/tools/learn.c
+++ b/tools/learn.c
@@ -4,10 +4,12 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include "../moonfish.h"
-#include "tools.h"
+#define moonfish_count 192
+
static double moonfish_next_line(char *argv0, char *line, FILE *file)
{char *arg, *end;
@@ -46,7 +48,7 @@
exit(1);
}
- return score * 100;
+ return score;
}
static double moonfish_gradient(double *gradient, double score0, char *fen)
@@ -61,7 +63,7 @@
score = moonfish_score(&chess);
error = score - score0;
- for (i = 0 ; i < moonfish_size ; i++)
+ for (i = 0 ; i < moonfish_count ; i++)
{prev = moonfish_values[i];
moonfish_values[i] += 1.0 / 256 / 256 / 8;
@@ -84,7 +86,7 @@
error = 0;
- for (i = 0 ; i < moonfish_size ; i++) gradient[i] = 0;
+ for (i = 0 ; i < moonfish_count ; i++) gradient[i] = 0;
for (i = 0 ; i < 2048 ; i++)
{@@ -92,7 +94,7 @@
error += moonfish_gradient(gradient, score, line);
}
- for (i = 0 ; i < moonfish_size ; i++)
+ for (i = 0 ; i < moonfish_count ; i++)
moonfish_values[i] -= gradient[i] / 2048;
return error;
@@ -100,7 +102,7 @@
int main(int argc, char **argv)
{- static double gradient[moonfish_size];
+ static double gradient[moonfish_count];
FILE *file;
int i;
@@ -138,7 +140,7 @@
error = moonfish_step(argv[0], file, gradient);
printf("\n");- for (i = 0 ; i < moonfish_size ; i++)
+ for (i = 0 ; i < moonfish_count ; i++)
printf("%.0f,", moonfish_values[i]); printf("\n");--
⑨