Skip to content

Commit

Permalink
Amalgamate most outstanding strength patches.
Browse files Browse the repository at this point in the history
To make it easier to make the tournament deadline.

* Bad bishop eval (kz04px#56).
* Late move pruning changes (kz04px#78).
* Improving variable (kz04px#83).
* Include promotions in qsearch (kz04px#97).

Also include kz04px#95 to make this exactly 4096 bytes,
and put in tournament settings (hash/threads) that
keep the size below target.

vs master (432c06a):

ELO   | 18.58 +- 9.34 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=64MB
LLR   | 2.97 (-2.94, 2.94) [0.00, 5.00]
GAMES | N: 3032 W: 945 L: 783 D: 1304
  • Loading branch information
gcp committed Dec 23, 2022
1 parent 432c06a commit 6969225
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sh build-mini.sh

## 4ku-mini Size
```
3,989 bytes
4,096 bytes
```

---
Expand Down
2 changes: 2 additions & 0 deletions minifier/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ def rename(tokens):
"pawn_attacked":"dw",
"no_move":"dx",
"reduction":"dy",
"quiet_moves_evaluated":"dz",
"improving":"ea",
# Labels
"do_search":"bk",
"full_window":"bl",
Expand Down
47 changes: 34 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// clang-format off
#include <random>
#include <iostream>
#include <thread>
#include <array>
#include <string>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <vector>
// clang-format on
// minify delete on
#include <sstream>
// minify delete off
Expand Down Expand Up @@ -61,6 +63,7 @@ struct [[nodiscard]] Stack {
Move moves[218];
Move move;
Move killer;
int score;
};

struct [[nodiscard]] TT_Entry {
Expand All @@ -87,6 +90,9 @@ const auto keys = []() {
// Engine options
auto num_tt_entries = 64ULL << 15; // The first value is the size in megabytes
auto thread_count = 1;
// Possible tournament settings (4095 bytes exe)
// auto num_tt_entries = 1ULL << 30; // 32 GB
// auto thread_count = 52;

vector<TT_Entry> transposition_table;

Expand Down Expand Up @@ -316,8 +322,9 @@ void generate_piece_moves(Move *const movelist,
const BB all = pos.colour[0] | pos.colour[1];
const BB to_mask = only_captures ? pos.colour[1] : ~pos.colour[0];
const BB pawns = pos.colour[0] & pos.pieces[Pawn];
generate_pawn_moves(
movelist, num_moves, north(pawns) & ~all & (only_captures ? 0xFF00000000000000ULL : 0xFFFFFFFFFFFF0000ULL), -8);
if (!only_captures) {
generate_pawn_moves(movelist, num_moves, north(pawns) & ~all, -8);
generate_pawn_moves(movelist, num_moves, north(north(pawns & 0xFF00ULL) & ~all) & ~all, -16);
}
generate_pawn_moves(movelist, num_moves, nw(pawns) & (pos.colour[1] | pos.ep), -7);
Expand Down Expand Up @@ -468,6 +475,13 @@ const int pawn_attacked[] = {S(-61, -18), S(-53, -42)};
}
}

// Bad bishops
score -= S(4, 4)
* ((!!(pos.colour[0] & pos.pieces[Bishop] & 0xAA55AA55AA55AA55ULL)
* count(pawns[0] & 0xAA55AA55AA55AA55ULL))
+ (!!(pos.colour[0] & pos.pieces[Bishop] & ~0xAA55AA55AA55AA55ULL)
* count(pawns[0] & ~0xAA55AA55AA55AA55ULL)));

flip(pos);

score = -score;
Expand Down Expand Up @@ -514,16 +528,15 @@ int alphabeta(Position &pos,
vector<BB> &hash_history,
const int do_null = true) {
const int static_eval = eval(pos);

// Don't overflow the stack
if (ply > 127) {
return static_eval;
}

stack[ply].score = static_eval;
// Check extensions
const auto in_check = attacked(pos, lsb(pos.colour[0] & pos.pieces[King]));
depth = in_check ? max(1, depth + 1) : depth;

const int improving = ply > 1 && static_eval > stack[ply - 2].score;
const int in_qsearch = depth <= 0;
if (in_qsearch && static_eval > alpha) {
if (static_eval >= beta) {
Expand All @@ -545,8 +558,8 @@ int alphabeta(Position &pos,
if (!in_check && alpha == beta - 1) {
// Reverse futility pruning
if (depth < 5) {
const int margins[] = {0, 50, 100, 200, 300};
if (static_eval - margins[depth] >= beta) {
const int margins[] = {50, 50, 100, 200, 300};
if (static_eval - margins[depth - improving] >= beta) {
return beta;
}
}
Expand Down Expand Up @@ -635,6 +648,7 @@ int alphabeta(Position &pos,
}
}

int quiet_moves_evaluated = 0;
int moves_evaluated = 0;
int best_score = -INF;
Move best_move{};
Expand Down Expand Up @@ -722,15 +736,17 @@ int alphabeta(Position &pos,
goto full_window;
}
}
moves_evaluated++;
if (piece_on(pos, move.to) == None) {
quiet_moves_evaluated++;
}

// Exit early if out of time
if (stop || now() >= stop_time) {
hash_history.pop_back();
return 0;
}

moves_evaluated++;

if (score > best_score) {
best_score = score;
best_move = move;
Expand All @@ -754,6 +770,11 @@ int alphabeta(Position &pos,
}
break;
}

// Late move pruning based on quiet move count
if (!in_check && alpha == beta - 1 && quiet_moves_evaluated > 3 + 2 * depth * depth) {
break;
}
}
hash_history.pop_back();

Expand Down

0 comments on commit 6969225

Please sign in to comment.