Show HN: Sameshi – a ~1200 Elo chess engine that fits within 2KB
I made a chess engine today, and made it fit within 2KB. I used a variant of MinMax called Negamax, with alpha beta pruning. For the board representation I have used a 120-cell "mailbox". I managed to squeeze in checkmate/stalemate in there, after trimming out some edge cases.I am a great fan of demoscene (computer art subculture) since middle school, and hence it was a ritual i had to perform.For estimating the Elo, I measured 240 automated games against Stockfish Elo levels (1320 to 1600) under fixed depth-5 and some constrained rules, using equal color distribution.Then converted pooled win/draw/loss scores to Elo through some standard logistic formula with binomial 95% confidence interval.
166 points by datavorous_ - 49 comments
As you write: not implemented: castling, en passant, promotion, repetition, 50-move rule - those are all required to call the game being played modern chess.
I could see an argument for skipping repetition and 50-move rule for tiny engines, but you do need castling, en pessant and promotion for pretty much any serious play.
https://en.wikipedia.org/wiki/Video_Chess fit in 4k and supported fuller ruleset in 1980 did it not?
So I would ask what is the smallest fully UCI (https://www.chessprogramming.org/UCI) compliant engine available currently?
This would be a fun goal to beat - make something tiny that supports full ruleset.
PS my first chess computer in early 1980s was this: https://www.ismenio.com/chess_fidelity_cc3.html - it also supported castling, en pessant, not sure about 50 move rule.
2KB of JavaScript with castling, en passant, promotion, search and even a GUI
326 bytes of assembly, without the special rules
I don't think the author has a UCI-compliant one, but it should be easier than the GUI. There are forks of the JS one that might do it.
[0] https://nanochess.org/chess6.html
Bug report:
The pawn is not permitted to move two fields after it has already beeen moved once before: b6b4 isn't a valid move after b7b6. (First moving two fields, and then one would have been okay, in contrast.)Appreciate you taking the time to test it.
[1] https://github.com/thomasmueller/bau-lang/blob/main/src/test...
[1] https://cutechess.com/
[2] https://www.chessprogramming.org/Sequential_Probability_Rati...
[3] https://github.com/michiguel/Ordo
so every game stayed in the same no castling variant
and you're right, this rating is for that constrained variant, not full chess.
Do you work with it like this or do you have some sort of script you apply to get it down to a single line, single letter variable names?
also removed castling/EP rights from FEN
(Partial answer, 2kB is a very small fraction of what we'd like to think counts as human.)
2kB is a very small fraction of what we'd like to think counts as human
This doesn't seem to mean anything. Why would 2KB have any relation to "counting as human". It's the data of about 10 comments.
P.S. I assume 1200 elo in chess com scale (not lichess / fide elo) and bullet chess variant?
These things always make me think back to Westworld season 2, where the finale revealed that human minds are much simpler than they themselves believe and fit completely into an algorithm that could be printed in an average book.
So while 4k is still very impressive for the code base, it comes with a significantly larger runtime footprint.
[1] - https://en.wikipedia.org/wiki/Minimax
Now if you had a very good chess program running in very constrained (dynamic/RAM) memory, then that'd be partially more revealing. From a cursory search there's a 1800 ELO engine for the C64, which seems very impressive but very far from the best human players.
I'd be interested to see a curve of ELO x Avaliable RAM for the best chess engines (up to given RAM), and how that compares to other games and activities.
On RAM vs ROM (program size) memory, I think at a high level dynamic memory helps you keep track of search paths in a large tree search, saving you some computation. Program size tends to enable improving the effectiveness of your search heuristic, as well as pre-computing e.g. initial and final game optimal moves (potentially saving arbitrarily much compute). I like thinking about those things because I think the search paradigm is pretty informative of computation (and even intelligence) in general. Almost every problem is basically some kind of heuristic search in some kind of space. And you tend to get better at things by refining your heuristics (usually through some experimental training process or theoretical insight), considering more options, exploring deeper consequences, etc..
I think what really defines humans isn't really our ability to solve problems or play chess well etc. (although that's extremely useful and also enjoyable most of the time), it's really our emotions and inner world. We are not really Thinking Machines in essence, we're Feeling Machines most significantly. The thinking part is a neat instrumental part :) We can delegate thinking to machines but what we cannot extinguish is feeling or the human "soul", because that is the source of all meaning.
According to TCEC the time control is 30 mins + 3 sec, that’s a lot of compute!
[1] https://github.com/MinusKelvin/ice4
> It's wild to think that 4096 bytes are sufficient to play chess on a level beyond anything humans ever achieved.
It is hideous now!
Your code is useless to anyone that wants to contribute, or maybe make something better by improving on the idea.
The PR:
The gap between your 1200 Elo in 2KB and the TCEC 4K engines at ~3000 Elo is interesting. That extra 2KB buys a lot when it goes to better evaluation and move ordering. Even a simple captures-first sort in alpha-beta pruning costs only a few bytes of code but can roughly double your effective search depth.
scribbling long enough on a piece of paper is more enjoyable than prompting.