Answers.Body: <p>If you're trying to make the strongest engine possible, absolutely go for NN engines.</p>
  <p>Traditional engines are great - Stockfish is arguably still the strongest engine on the planet on consensus equal hardware - but they are hard to write. These engines didn't get where they were overnight; they took years and years of work. Stockfish for example has been <a href="https://nextchessmove.com/dev-builds" rel="nofollow noreferrer">steadily gaining elo for six years</a>. If you start from scratch, you will not get anywhere near Stockfish strength quickly; in fact you are likely to stall several hundred elo from where Stockfish currently is. For comparison some of the strongest single-author engines right now are <a href="https://github.com/AndyGrant/Ethereal" rel="nofollow noreferrer">Ethereal</a>, <a href="https://github.com/jeffreyan11/laser-chess-engine" rel="nofollow noreferrer">Laser</a>, and <a href="https://github.com/milostatarevic/xiphos" rel="nofollow noreferrer">Xiphos</a> (I neglect Houdini &amp; Fire since they aren't open source). These engines are all substantially weaker than Stockfish.</p>
  <p>Why are they so much weaker? Two reasons:</p>
  <ol>
  <li><p>At this level, developing an engine takes a lot of computational power. What you do is look through your code and identify an idea that could gain elo. An example idea is, &quot;if we know this move is probably good, do not search branches that reverse that move&quot; (if this doesn't make sense to you, it means there's going to be a high learning curve as well). You then write a patch that implements the idea, and test the modified engine against the previous version. At this level, it takes tens of thousands of games to get a large-enough sample size to tell if the patch is effective. Playing these games takes huge amounts of computational power. Stockfish has access to super hardware: as of time of writing, the Stockfish testing ground <a href="http://tests.stockfishchess.org/tests" rel="nofollow noreferrer">Fishtesting</a> is running with 1038 cores. For comparison a typical desktop computer might have 4-8 cores.</p>
  </li>
  <li><p>The other reason is that Stockfish is backed by many brains. As of time of writing, there are 8 patches written by 5 people being tested. If you look through the patch histories you'll find many more developers who've written patches. I don't know how many active Stockfish developers there are, but it's certainly &gt;20.</p>
  </li>
  </ol>
  <p>On the other hand, NN engines (relatively) easily reach a strength that's well above Ethereal/Laser/Xiphos. See for yourself in the latest <a href="https://www.tcec-chess.com/archive.html?season=16&amp;div=p&amp;game=1" rel="nofollow noreferrer">Top Chess Engine Championship tournament</a>. Ethereal/Laser/Xiphos are in League 1, which is creditable enough, but not at the level of the top division (Division P). These Division P engines are:</p>
  <ul>
  <li>Stockfish (community-backed traditional engine, runs with Fishtesting)</li>
  <li>Komodo (commercial traditional engine, has full-time developers)</li>
  <li>Komodo MCTS (commercial semi-traditional engine, has full-time developers)</li>
  <li><s>Houdini (commercial traditional engine, is a one-man effort, hasn't been updated for two years)</s></li>
  <li>Leela Chess Zero (community-backed NN engine, runs with the analog of Fishtesting aka a ton of hardware)</li>
  <li><a href="http://www.chessdom.com/alliestein-the-new-neural-network-entering-tcec-s15/" rel="nofollow noreferrer">AllieStein</a> (2-man NN engine)</li>
  <li>Stoofvlees (NN engine)</li>
  <li>ScorpioNN (NN engine)</li>
  </ul>
  <p>The last three engines - AllieStein, Stoofvlees and ScorpioNN - are all small collaborations by people who, as far as I know, are enthusiasts like you and not full-time developers. They've gotten to Div P strength after less than two years of trying (all these NN engines only appeared after AlphaZero). No single author has ever written a traditional engine that competed successfully with Stockfish/Komodo.* <s>For comparison, in the recent history of computer chess there has only been one person who's ever written a traditional engine that competed successfully with Stockfish &amp; Komodo on his own (Robert Houdart, the author of Houdini).</s></p>
  <p>It's true that you probably won't have the hardware to compete with Lc0, but Lc0 takes this much computational power because it's a &quot;zero&quot; engine - it's supposed to play chess without any kind of human knowledge except for rules. You don't need to use the same methodology. You could use e.g. the <a href="https://www.patreon.com/jjosh" rel="nofollow noreferrer">Stein methodology</a> that uses supervised learning. It's arguably even better than going &quot;zero&quot; - after all it's AllieStein playing in the superfinal, not Lc0.</p>
  <p>The upshot is: <strong>if your aim is to compete with the best engines in the world, you are far more likely to succeed with NN engines than traditional ones.</strong></p>
  <p>*It might look like Houdini is such an example, but <a href="https://chess.stackexchange.com/questions/29181/is-houdini-6-a-stockfish-clone">Houdini is plagiarized from Stockfish</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T11:55:20.740Z
Answers.Text: Maybe mention the other side of your conclusion: If your aim is to deepen your understanding of chess while doing some programming, go for traditional.
Answers.UserDisplayName: blues
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T14:38:48.647Z
Answers.Text: I agree with your conclusion, but I would also recommend using a recent network from a strong engine (with permission of course) as a starting point rather than starting from scratch (but continue in a way different from "train via self-play and update weights", by either adding or removing layers, adding input features, demanding additional outputs, etc.). I can't remember if Lc0 is single head or dual head, but I believe 3 head (https://www.ijcai.org/proceedings/2018/0523.pdf) is not currently implemented by any chess engines, and could end up improving on state-of-the-art.
Answers.UserDisplayName: Steven Jackson
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T22:21:02.473Z
Answers.Text: I think Allie's search that makes it stronger against the weaker engines (KMCST, Stoofvlees), not the Stein network.
Answers.UserDisplayName: me'
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T23:31:55.427Z
Answers.Text: @blues I think programming a chess engine doesn't actually make one a better chess player - with the exception of Larry Kaufman (one of the developers of Komodo), none of the top engines have titled players as developers, or had their developers become titled players.
Answers.UserDisplayName: Allure
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T23:32:53.657Z
Answers.Text: @me' that's possible but Leelenstein is the Stein network using the Lc0 binary, and it's done well at chess.com's computer chess championship, so the Stein networks are at least comparable to the Lc0 networks.
Answers.UserDisplayName: Allure
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T00:01:57.707Z
Answers.Text: Yes, the Stein networks are excellent unless they are playing stockfish (in both TCEC Sufi and CCC).
Answers.UserDisplayName: me'
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T04:38:07.790Z
Answers.Text: according to some testing, allie+stein is about the same strength as lc0. It appears to have been largely small sample of a tournament and some unfortunate SF crashes that put allie ahead
Answers.UserDisplayName: Oscar Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T11:03:27.370Z
Answers.Text: You could be clearer with the terminology; Allie is an engine, Stein is a network (although Leelenstein is an engine)
Answers.UserDisplayName: JollyJoker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T11:15:38.513Z
Answers.Text: @JollyJoker I only used AllieStein in the answer and that's an engine, no?
Answers.UserDisplayName: Allure
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T11:19:31.437Z
Answers.Text: https://github.com/manyoso/allie The engine is just Allie
Answers.UserDisplayName: JollyJoker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T11:24:33.190Z
Answers.Text: @JollyJoker What's an engine to you? Further, if you consider Leelenstein an engine, then AllieStein is also an engine.
Answers.UserDisplayName: Allure
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T11:31:18.797Z
Answers.Text: Semantics, I guess. It seems AllieStein actually is an engine in TCEC terminology. http://www.chessdom.com/alliestein-the-new-neural-network-entering-tcec-s15/
Answers.UserDisplayName: JollyJoker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T00:36:58.833Z
Answers.LastActivityDate: 2024-06-06T06:04:10.157Z
Answers.LastEditDate: 2024-06-06T06:04:10.157Z
Answers.UserDisplayName: Allure
Answers.Body: <blockquote>
    <p>So, in terms of creating the strongest chess engine possible, should I go neural network or hard-coded?</p>
  </blockquote>
  
  <p>Don't choose a NN unless you have access to ridiculous(A few hundred Nvidia V100s). Training a NN to play chess takes so much hardware. See the people contributing to <a href="https://lczero.org/" rel="nofollow noreferrer">Lc0</a> to train over 200 million games. Since you will probably have trouble accessing the hardware (you could try to get some at <a href="https://colab.research.google.com" rel="nofollow noreferrer">Google Colabatory</a>, but with only that, training will be very slow).</p>
  
  <p><strong>Edit: Using a NN With Supervised learning, you MAY get away with just Google Colab and possibly one strong GPU (2080, 2080Ti, Radeon VII).</strong></p>
  
  <blockquote>
    <p>Another thing I am considering is whether it is important which language I use to code the engine. I know many chess engines use C++, which I have never used before. I have used other C-based languages which do many of the same things, with mostly varying syntax. I am most familiar with Swift and Javascript, but am also familiar with Python and feel that it could do the trick for me.</p>
  </blockquote>
  
  <p>Python and Javascript are probably too slow for a strong chess engine. I haven't used Swift but it probably won't like platforms other than macOS, so it's probably better to use C or C++. You could also possibly use Rust, but that has many safety features that get annoying and you don't really need, which can get annoying. It's also going to be harder to get good performance out of it since certain low-level optimizations are hard to make. Of course, you can always write it in assembly but that's probably going to be too much work. see <a href="https://www.chessprogramming.org/Languages/" rel="nofollow noreferrer">https://www.chessprogramming.org/Languages/</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-30T22:06:53.210Z
Answers.Text: Awesome, thanks a ton for the info! I'll probably try to create an API that takes in an FEN position and outputs the calculated move. I figure this is the best way to integrate with Lichess bots.
Answers.UserDisplayName: David Chopin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T00:31:35.153Z
Answers.Text: There is https://github.com/careless25/lichess-bot
Answers.UserDisplayName: me'
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T08:53:42.570Z
Answers.Text: Is it an option to implement a different NN engine that just uses lc0's network?
Answers.UserDisplayName: RemcoGerlich
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T09:14:40.620Z
Answers.Text: @RemcoGerlich as I understand it, NN engines use two things: the binary, and the neural network. Writing a binary is not easy, but training a neural network is relatively easy. In this sense Lc0 derivatives already exist - Leelenstein & Deus X (aka. Fat Fritz) are such derivatives.
Answers.UserDisplayName: Allure
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T13:55:28.670Z
Answers.Text: @RemcoGerlich in that case that wouldn't really be a different engine, that would be the Ic0 engine with just a different wrapping/interface/whatever.
Answers.UserDisplayName: Peteris
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-30T22:01:15.053Z
Answers.LastActivityDate: 2019-10-01T22:34:17.883Z
Answers.LastEditDate: 2019-10-01T22:34:17.883Z
Answers.UserDisplayName: me'
Answers.Body: <p>I built a purely toy chess engine using: <a href="https://pypi.org/project/python-chess/" rel="nofollow noreferrer">python chess</a> it was really nice not having to code the rules of the game myself and just focus on the logic; however, the number of position I was able evaluate per second is very low. This might be a good starting point.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T05:19:12.703Z
Answers.LastActivityDate: 2019-10-01T05:19:12.703Z
Answers.UserDisplayName: Akavall
Answers.Body: <blockquote>
  <p>In wake of AlphaZero's crushing performance against Stockfish last year, I am considering whether I should create this engine with machine learning (some type of neural network, possibly using Tensorflow) or traditional, hard coded heuristics.</p>
  </blockquote>
  <p>Let us start with the basics:</p>
  <ul>
  <li>Chess programs - all chess programs - are a matter of CPU cycles and how efficiently theay are used. In the AlphaZero-Stockfish match AlphaZero had an overwhelmingly big advantage in terms of pure calculation power. They used a 4-<a href="https://en.wikipedia.org/wiki/Tensor_Processing_Unit" rel="nofollow noreferrer">TPU</a> system to play (for the training they used a 5000-TPU system) and the used second-generation TPUs where benchmarked at 11.5 petaFLOPS each.</li>
  </ul>
  <p>There was already a similarly designed (from the hardware POV) system: playing strength achieved by a high level of parallelization. It was based on FPGAs instead of ASICs and called <a href="https://en.wikipedia.org/wiki/Hydra_(chess)" rel="nofollow noreferrer">Hydra</a>. In 2005 it defeated then-world-Nr7 Michael Adams <a href="https://www.theguardian.com/technology/2005/jun/30/chess.sport" rel="nofollow noreferrer">5.5-0.5</a> with half of its 64 cored disabled.</p>
  <ul>
  <li>In chess programming (that is: the &quot;classical&quot; chess programming) it was found over time that too much &quot;chess knowledge&quot; is bad for performance. There are basically three parts to a classical chess program: a move generator which produces all sorts of legal moves from a given position, an evaluation function and an algorithm to manage the search tree (AlphaBeta, MiniMax, etc.). The more chess knowledge is built into the evaluation function the slower it is and the less positions the program can evaluate - the smaller the search tree is, therefore.</li>
  </ul>
  <p>It is possible to build an evaluation function that is close to an Elo 1500 player, but it would be so slow that the resulting program wouldn't be much better than that. Take away most of the knowledge and you get an Elo-1000-function which is many times faster and you get an overall better performing program - its &quot;chess knowledge&quot; may be marginal, but by calculating fast (and therefore very far ahead) it more then makes up for that.</p>
  <ul>
  <li>In light of this - speed is everything - the programming language of choice is simple: get the fastest one you can lay your hands on! Forget the interpreted languages (arguably with the exception of <a href="https://en.wikipedia.org/wiki/Forth_(programming_language)" rel="nofollow noreferrer">FORTH</a>), stay away from compiled languages (yes, even C) and go for the lowest possible overhead: Assembler! Use C for rapid prototyping. (Yes, I admit, that sounds extreme, but this was actually the way I worked for a long time, not building chess programs but real-time signal-processing applications with DSPs.)</li>
  </ul>
  <p>If you want to try NN algorithms for your program you either go the way Google went - throw an awful lot of parallel-processing-capable hardware at it - or you will probably get mediocre results because these algorithms have a lot of overhead and only perform that well because of the raw power they are able to put to work.</p>
  <p>If you want to build the best possible program with your run-of-the-mill hardware (most probably a standard PC with some Linux/Windows, no multi-core datacenter GPU like the <a href="https://www.nvidia.com/en-us/data-center/v100/" rel="nofollow noreferrer">Nvidia V100</a> or access to Google Cloud TPU), you should probably go for a &quot;classic&quot; program and try your hand on optimizing assembler code, together with the development of better pruning strategies in the search-tree to maximise the number of plies the program is able to calculate ahead. A rule of thumb says that every additional ply is worth ~50 pts Elo.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-07T05:25:51.353Z
Answers.Text: I believe AlphaZero only used that many TPUs during training. During play, it had much less hardware (don't recall exactly how much though).
Answers.UserDisplayName: Allure
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-07T07:27:55.470Z
Answers.Text: @Allure: See the text, they used 5k TPUs for training, 4TPUs for play. But notice that "a TPU" is not a processor, it is a massively parallel device with many, many cores.
Answers.UserDisplayName: bakunin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-06T19:54:22.653Z
Answers.LastActivityDate: 2024-06-06T19:54:22.653Z
Answers.UserDisplayName: bakunin
Answers.Body: <h1>First step: Define your goals/reasons</h1>
  
  <p>I think this is the predominant factor. Which of these best fits you? <em>(Choose <strong>only one</strong>)</em></p>
  
  <ol>
  <li>You want to enjoy a fun, challenging coding task</li>
  <li>You want to create an extremely good chess engine</li>
  <li>You want to learn about how chess engines work</li>
  <li>You want to learn/practice coding skills</li>
  <li>You want to learn/implement computer science concepts/theory (e.g. machine learning)</li>
  <li>(Other)</li>
  </ol>
  
  <p>IMO it's fine to "toss a coin" for anything except 2. For all the others, you will meet your goal whether you choose ML or hard-coding. However you probably want a comparison between the choices to help you decide.</p>
  
  <h1>The case for hard-coding</h1>
  
  <p>Playing chess (as a human) involves logical thinking. You explore the space of possible actions you and the opponent can take. This has spawned a field called <em>game theory</em> which contains theoretical frameworks for analysing games in general.</p>
  
  <p>If you enjoy working with details and being specific and reasoning about things then this could work well for you. In comparison, machine learning involves a lot more "black box" algorithms which are fuzzy and opaque. You don't know exactly what is going on.</p>
  
  <p>Also I reckon you will have an easier time "figuring it out on your own" if you go the hard-coding route rather than machine learning. Less copy-pasting stuff you don't fully understand.</p>
  
  <h1>The case for machine learning</h1>
  
  <p>It can be exciting to give birth to a creation and watch it take on a life of its own.  While hard-coding is all about precision and detail, machine learning is flexible. Take away some neurons and the result will probably be similar.</p>
  
  <p>Hard-coding is about studying chess. Machine learning is about studying the creature you have created.</p>
  
  <p>And machine learning is, of course, a very hot topic.</p>
  
  <h1>Language choice for hard-coded</h1>
  
  <p>I am not sure what you mean by "other C-based languages". C++ is the only mainstream language that is anything like C. The advantage of C/C++ is that they are <em>fast</em>. Although other languages have caught up over the years, C++ still gives them a run for their money.</p>
  
  <p>C++ isn't easy. You will get great performance out of more modern compiled languages like Rust, Golang or Swift. But it shouldn't be much worse if you go for a JIT language. I.e. <strong>don't use the CPython interpreter</strong>; use IronPython or Jython, or Node, or C# or Java.</p>
  
  <p>GPU programming requires a different approach and I would advise against it at this point.</p>
  
  <h1>Language choice for machine learning</h1>
  
  <p>The problem with TensorFlow is that it is very low-level. It is more about writing number-crunching algorithms (which can be farmed out to parallel hardware) than about an interface dedicated to machine learning.</p>
  
  <p>Of course, it can be a great learning experience! And is certainly very worthwhile learning today. However, you may want to start with Keras or PyTorch.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T14:20:01.850Z
Answers.Text: This is a phenomenal response, really hits all the points I mentioned. It’s kind of hard to say why exactly I want to make an engine. Realistically, I likely won’t compete with the likes of Stockfish, Komodo, and Leela (I am only a college student after all). Still, it would be fun to see my work put up toe-to-toe against other engines and not get crushed every time. Even if my primary reasoning is to make a strong engine, I will likely learn a new programming language and improve my chess knowledge as a side product of making the engine.
Answers.UserDisplayName: David Chopin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T14:21:56.030Z
Answers.Text: I think more than anything I want something I can make, nurture, and fine tune over the long term. I also want a project that will objectively tell me how good my code is (Wins/Draws/Losses, ELO, etc.).
Answers.UserDisplayName: David Chopin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T14:24:37.003Z
Answers.Text: This a very interesting answer but are you seriously recommending Node over Cpython for **performance** concerns?
Answers.UserDisplayName: Evpok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T14:59:31.830Z
Answers.Text: >"C++ is the only mainstream language that is anything like C."
  C#? Rust? Java? 
  There's a lot of languages that are at least "anything like C"
Answers.UserDisplayName: Maaark
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T17:35:36.317Z
Answers.Text: I took "like C" to be a compiled, highly optimized, implemented-to-the-machine language with C-style syntax. C#, Java, and PHP all use virtual machines, with their code compiled down to opcodes rather than CPU-specific bytecode (and PHP's compilation is completely transparent, like Python and Bash). And Rust isn't mainstream.  Only C++ is "like C" in these respects, despite sharing syntax styles with several other mainstream languages.
Answers.UserDisplayName: Ghedipunk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T17:48:05.407Z
Answers.Text: That is, (since comments are intended to improve answers rather than have discussions), "C++ is the only mainstream language that is anything like C" might stand a bit of work to clarify what is meant, but absolutely stands on its own. @Maaark, if you feel there is a better way to explain the point of that part of the answer, such as helping rephrase why C++ is a C syntax-style language that is far beyond the speed capabilities of other C syntax-style languages, please feel free to make suggestions or suggest edits.
Answers.UserDisplayName: Ghedipunk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T19:33:38.833Z
Answers.Text: @Ghedipunk Generally speaking, the common use of the term "C-based" or "C-like" refers to languages with a syntax similar to or reminiscent of C and has nothing to do with non-syntax C-like traits such as highly-optimized compilation or whether the compiled binary runs CPU byte-code or in a virtual machine. Considering OP mentions Swift, Javascript, and Python in their question, I assume this is the interpretation they were using as well, though Artelius clearly meant differently. There are many "C-based" languages, mainstream or no, but how "C-based" they are can vary widely.
Answers.UserDisplayName: Abion47
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T11:49:20.587Z
Answers.LastActivityDate: 2019-10-02T11:49:20.587Z
Answers.UserDisplayName: Artelius
Body: <p>I am both an avid chess player and computer programmer. I would say that playing chess and programming are the two things I spend the most time doing. Naturally, I am wanting to create my own engine and, ultimately, Lichess bot.</p>
  <p>In wake of AlphaZero's crushing performance against Stockfish last year, I am considering whether I should create this engine with machine learning (some type of neural network, possibly using Tensorflow) or traditional, hard coded heuristics.</p>
  <p>I am less familiar with neural networks than other kinds of hard-coding. Still, it could be a good way for me to learn to work with neural networks.</p>
  <p>Another thing I am considering is whether it is important which language I use to code the engine. I know many chess engines use C++, which I have never used before. I have used other C-based languages which do many of the same things, with mostly varying syntax. I am most familiar with Swift and Javascript, but am also familiar with Python and feel that it could do the trick for me.</p>
  <p>So, in terms of creating the strongest chess engine possible, should I go neural network or hard-coded?</p>
  <p><strong>Update:</strong>
  I am writing a traditional engine in C++. It is currently <em>somewhat</em> UCI compatible and plays at what I estimate is 1100ish ELO. But it generates legal moves and I’ll be posting updates here.</p>
  <p><a href="https://github.com/ChopinDavid/Maestro-cpp" rel="nofollow noreferrer" title="This is the link to the github repo.">This is the link to the github repo for the engine.</a> Feel free to fork and make PRs, or just make general suggestions/tips.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T07:19:14.720Z
Comments.Text: They key to the new AI engines is not so much NN as reinforcement learning. RL is a complex topic, but there are good introductory resources online like David Silver's RL lectures at UCL (slides on his website and lectures on YouTube. He worked with Deepmind and traditionally covered alphago in the last lecture. You could probably develop an interesting AI engine without NN using hard coded heuristics and RL. This is how alphago started!
Comments.UserDisplayName: asac
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T10:37:11.380Z
Comments.Text: It isn't chess, but the recent book "Deep Learning and the Game Go" shows how to use NNs for a game-engine. Alpha-Go and Alpha-Zero are similar under the hood.
Comments.UserDisplayName: John Coleman
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T15:16:25.743Z
Comments.Text: Given your Python experience it might be worth looking at Nim
Comments.UserDisplayName: Darren H
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T17:50:01.520Z
Comments.Text: If you want to have a go at NN, this is interesting: https://arxiv.org/abs/1509.01549 There is also code on github :)
Comments.UserDisplayName: Ant
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-02T04:39:54.360Z
Comments.Text: @Ant note that Giraffe was never very strong compared to top engines. (~2800 elo)
Comments.UserDisplayName: Oscar Smith
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-04T23:30:37.913Z
Comments.Text: I recommend doing a small neural network like the standard toy MNIST classifier first before trying to combine it with something quite complex like a chess engine.
Comments.UserDisplayName: qwr
Tags: <engines><programming><computer-chess>
Title: Creating chess engine, machine learning vs. traditional engine?
ThreadText: # Creating chess engine, machine learning vs. traditional engine?
  
  (Asked by: username_0 on 2019-09-30)
  
  I am both an avid chess player and computer programmer. I would say that playing chess and programming are the two things I spend the most time doing. Naturally, I am wanting to create my own engine and, ultimately, Lichess bot.
  
  In wake of AlphaZero's crushing performance against Stockfish last year, I am considering whether I should create this engine with machine learning (some type of neural network, possibly using Tensorflow) or traditional, hard coded heuristics.
  
  I am less familiar with neural networks than other kinds of hard-coding. Still, it could be a good way for me to learn to work with neural networks.
  
  Another thing I am considering is whether it is important which language I use to code the engine. I know many chess engines use C++, which I have never used before. I have used other C-based languages which do many of the same things, with mostly varying syntax. I am most familiar with Swift and Javascript, but am also familiar with Python and feel that it could do the trick for me.
  
  So, in terms of creating the strongest chess engine possible, should I go neural network or hard-coded?
  
  **Update:**
  I am writing a traditional engine in C++. It is currently *somewhat* UCI compatible and plays at what I estimate is 1100ish ELO. But it generates legal moves and I’ll be posting updates here.
  
  [This is the link to the github repo for the engine.](https://github.com/ChopinDavid/Maestro-cpp "This is the link to the github repo.") Feel free to fork and make PRs, or just make general suggestions/tips.
  
  **Tags:** engines, programming, computer-chess
  
  > **Comments:**
  > **username_1**: They key to the new AI engines is not so much NN as reinforcement learning. RL is a complex topic, but there are good introductory resources online like David Silver's RL lectures at UCL (slides on his website and lectures on YouTube. He worked with Deepmind and traditionally covered alphago in the last lecture. You could probably develop an interesting AI engine without NN using hard coded heuristics and RL. This is how alphago started!  
  > **username_2**: It isn't chess, but the recent book "Deep Learning and the Game Go" shows how to use NNs for a game-engine. Alpha-Go and Alpha-Zero are similar under the hood.  
  > **username_3**: Given your Python experience it might be worth looking at Nim  
  > **username_4**: If you want to have a go at NN, this is interesting: https://arxiv.org/abs/1509.01549 There is also code on github :)  
  > **username_5**: @username_4 note that Giraffe was never very strong compared to top engines. (~2800 elo)  
  > **username_6**: I recommend doing a small neural network like the standard toy MNIST classifier first before trying to combine it with something quite complex like a chess engine.  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_16 on 2019-10-02. Score: 12 | Accepted answer
  
  # First step: Define your goals/reasons
  
  I think this is the predominant factor. Which of these best fits you? *(Choose **only one**)*
  
  1. You want to enjoy a fun, challenging coding task
  2. You want to create an extremely good chess engine
  3. You want to learn about how chess engines work
  4. You want to learn/practice coding skills
  5. You want to learn/implement computer science concepts/theory (e.g. machine learning)
  6. (Other)
  
  IMO it's fine to "toss a coin" for anything except 2. For all the others, you will meet your goal whether you choose ML or hard-coding. However you probably want a comparison between the choices to help you decide.
  
  # The case for hard-coding
  
  Playing chess (as a human) involves logical thinking. You explore the space of possible actions you and the opponent can take. This has spawned a field called *game theory* which contains theoretical frameworks for analysing games in general.
  
  If you enjoy working with details and being specific and reasoning about things then this could work well for you. In comparison, machine learning involves a lot more "black box" algorithms which are fuzzy and opaque. You don't know exactly what is going on.
  
  Also I reckon you will have an easier time "figuring it out on your own" if you go the hard-coding route rather than machine learning. Less copy-pasting stuff you don't fully understand.
  
  # The case for machine learning
  
  It can be exciting to give birth to a creation and watch it take on a life of its own. While hard-coding is all about precision and detail, machine learning is flexible. Take away some neurons and the result will probably be similar.
  
  Hard-coding is about studying chess. Machine learning is about studying the creature you have created.
  
  And machine learning is, of course, a very hot topic.
  
  # Language choice for hard-coded
  
  I am not sure what you mean by "other C-based languages". C++ is the only mainstream language that is anything like C. The advantage of C/C++ is that they are *fast*. Although other languages have caught up over the years, C++ still gives them a run for their money.
  
  C++ isn't easy. You will get great performance out of more modern compiled languages like Rust, Golang or Swift. But it shouldn't be much worse if you go for a JIT language. I.e. **don't use the CPython interpreter**; use IronPython or Jython, or Node, or C# or Java.
  
  GPU programming requires a different approach and I would advise against it at this point.
  
  # Language choice for machine learning
  
  The problem with TensorFlow is that it is very low-level. It is more about writing number-crunching algorithms (which can be farmed out to parallel hardware) than about an interface dedicated to machine learning.
  
  Of course, it can be a great learning experience! And is certainly very worthwhile learning today. However, you may want to start with Keras or PyTorch.
  
  > **Comments:**
  > **username_0**: This is a phenomenal response, really hits all the points I mentioned. It’s kind of hard to say why exactly I want to make an engine. Realistically, I likely won’t compete with the likes of Stockfish, Komodo, and Leela (I am only a college student after all). Still, it would be fun to see my work put up toe-to-toe against other engines and not get crushed every time. Even if my primary reasoning is to make a strong engine, I will likely learn a new programming language and improve my chess knowledge as a side product of making the engine.  
  > **username_0**: I think more than anything I want something I can make, nurture, and fine tune over the long term. I also want a project that will objectively tell me how good my code is (Wins/Draws/Losses, ELO, etc.).  
  > **username_17**: This a very interesting answer but are you seriously recommending Node over Cpython for **performance** concerns?  
  > **username_18**: >"C++ is the only mainstream language that is anything like C." C#? Rust? Java?  There's a lot of languages that are at least "anything like C"  
  > **username_19**: I took "like C" to be a compiled, highly optimized, implemented-to-the-machine language with C-style syntax. C#, Java, and PHP all use virtual machines, with their code compiled down to opcodes rather than CPU-specific bytecode (and PHP's compilation is completely transparent, like Python and Bash). And Rust isn't mainstream.  Only C++ is "like C" in these respects, despite sharing syntax styles with several other mainstream languages.  
  > **username_19**: That is, (since comments are intended to improve answers rather than have discussions), "C++ is the only mainstream language that is anything like C" might stand a bit of work to clarify what is meant, but absolutely stands on its own. @username_18, if you feel there is a better way to explain the point of that part of the answer, such as helping rephrase why C++ is a C syntax-style language that is far beyond the speed capabilities of other C syntax-style languages, please feel free to make suggestions or suggest edits.  
  > **username_20**: @username_19 Generally speaking, the common use of the term "C-based" or "C-like" refers to languages with a syntax similar to or reminiscent of C and has nothing to do with non-syntax C-like traits such as highly-optimized compilation or whether the compiled binary runs CPU byte-code or in a virtual machine. Considering OP mentions Swift, Javascript, and Python in their question, I assume this is the interpretation they were using as well, though username_16 clearly meant differently. There are many "C-based" languages, mainstream or no, but how "C-based" they are can vary widely.  
  
  ---
  
  ### Answer by username_7 on 2019-10-01. Score: 26
  
  If you're trying to make the strongest engine possible, absolutely go for NN engines.
  
  Traditional engines are great - Stockfish is arguably still the strongest engine on the planet on consensus equal hardware - but they are hard to write. These engines didn't get where they were overnight; they took years and years of work. Stockfish for example has been [steadily gaining elo for six years](https://nextchessmove.com/dev-builds). If you start from scratch, you will not get anywhere near Stockfish strength quickly; in fact you are likely to stall several hundred elo from where Stockfish currently is. For comparison some of the strongest single-author engines right now are [Ethereal](https://github.com/AndyGrant/Ethereal), [Laser](https://github.com/jeffreyan11/laser-chess-engine), and [Xiphos](https://github.com/milostatarevic/xiphos) (I neglect Houdini & Fire since they aren't open source). These engines are all substantially weaker than Stockfish.
  
  Why are they so much weaker? Two reasons:
  
  1. At this level, developing an engine takes a lot of computational power. What you do is look through your code and identify an idea that could gain elo. An example idea is, "if we know this move is probably good, do not search branches that reverse that move" (if this doesn't make sense to you, it means there's going to be a high learning curve as well). You then write a patch that implements the idea, and test the modified engine against the previous version. At this level, it takes tens of thousands of games to get a large-enough sample size to tell if the patch is effective. Playing these games takes huge amounts of computational power. Stockfish has access to super hardware: as of time of writing, the Stockfish testing ground [Fishtesting](http://tests.stockfishchess.org/tests) is running with 1038 cores. For comparison a typical desktop computer might have 4-8 cores.
  2. The other reason is that Stockfish is backed by many brains. As of time of writing, there are 8 patches written by 5 people being tested. If you look through the patch histories you'll find many more developers who've written patches. I don't know how many active Stockfish developers there are, but it's certainly >20.
  
  On the other hand, NN engines (relatively) easily reach a strength that's well above Ethereal/Laser/Xiphos. See for yourself in the latest [Top Chess Engine Championship tournament](https://www.tcec-chess.com/archive.html?season=16&div=p&game=1). Ethereal/Laser/Xiphos are in League 1, which is creditable enough, but not at the level of the top division (Division P). These Division P engines are:
  
  * Stockfish (community-backed traditional engine, runs with Fishtesting)
  * Komodo (commercial traditional engine, has full-time developers)
  * Komodo MCTS (commercial semi-traditional engine, has full-time developers)
  * ~~Houdini (commercial traditional engine, is a one-man effort, hasn't been updated for two years)~~
  * Leela Chess Zero (community-backed NN engine, runs with the analog of Fishtesting aka a ton of hardware)
  * [AllieStein](http://www.chessdom.com/alliestein-the-new-neural-network-entering-tcec-s15/) (2-man NN engine)
  * Stoofvlees (NN engine)
  * ScorpioNN (NN engine)
  
  The last three engines - AllieStein, Stoofvlees and ScorpioNN - are all small collaborations by people who, as far as I know, are enthusiasts like you and not full-time developers. They've gotten to Div P strength after less than two years of trying (all these NN engines only appeared after AlphaZero). No single author has ever written a traditional engine that competed successfully with Stockfish/Komodo.\* ~~For comparison, in the recent history of computer chess there has only been one person who's ever written a traditional engine that competed successfully with Stockfish & Komodo on his own (Robert Houdart, the author of Houdini).~~
  
  It's true that you probably won't have the hardware to compete with Lc0, but Lc0 takes this much computational power because it's a "zero" engine - it's supposed to play chess without any kind of human knowledge except for rules. You don't need to use the same methodology. You could use e.g. the [Stein methodology](https://www.patreon.com/jjosh) that uses supervised learning. It's arguably even better than going "zero" - after all it's AllieStein playing in the superfinal, not Lc0.
  
  The upshot is: **if your aim is to compete with the best engines in the world, you are far more likely to succeed with NN engines than traditional ones.**
  
  \*It might look like Houdini is such an example, but [Houdini is plagiarized from Stockfish](https://chess.stackexchange.com/questions/29181/is-houdini-6-a-stockfish-clone).
  
  > **Comments:**
  > **username_8**: Maybe mention the other side of your conclusion: If your aim is to deepen your understanding of chess while doing some programming, go for traditional.  
  > **username_9**: I agree with your conclusion, but I would also recommend using a recent network from a strong engine (with permission of course) as a starting point rather than starting from scratch (but continue in a way different from "train via self-play and update weights", by either adding or removing layers, adding input features, demanding additional outputs, etc.). I can't remember if Lc0 is single head or dual head, but I believe 3 head (https://www.ijcai.org/proceedings/2018/0523.pdf) is not currently implemented by any chess engines, and could end up improving on state-of-the-art.  
  > **username_10**: I think Allie's search that makes it stronger against the weaker engines (KMCST, Stoofvlees), not the Stein network.  
  > **username_7**: @username_8 I think programming a chess engine doesn't actually make one a better chess player - with the exception of Larry Kaufman (one of the developers of Komodo), none of the top engines have titled players as developers, or had their developers become titled players.  
  > **username_7**: @username_10 that's possible but Leelenstein is the Stein network using the Lc0 binary, and it's done well at chess.com's computer chess championship, so the Stein networks are at least comparable to the Lc0 networks.  
  > **username_10**: Yes, the Stein networks are excellent unless they are playing stockfish (in both TCEC Sufi and CCC).  
  > **username_5**: according to some testing, allie+stein is about the same strength as lc0. It appears to have been largely small sample of a tournament and some unfortunate SF crashes that put allie ahead  
  > **username_11**: You could be clearer with the terminology; Allie is an engine, Stein is a network (although Leelenstein is an engine)  
  > **username_7**: @username_11 I only used AllieStein in the answer and that's an engine, no?  
  > **username_11**: https://github.com/manyoso/allie The engine is just Allie  
  > **username_7**: @username_11 What's an engine to you? Further, if you consider Leelenstein an engine, then AllieStein is also an engine.  
  > **username_11**: Semantics, I guess. It seems AllieStein actually is an engine in TCEC terminology. http://www.chessdom.com/alliestein-the-new-neural-network-entering-tcec-s15/  
  
  ---
  
  ### Answer by username_10 on 2019-09-30. Score: 10
  
  > So, in terms of creating the strongest chess engine possible, should I go neural network or hard-coded?
  
  Don't choose a NN unless you have access to ridiculous(A few hundred Nvidia V100s). Training a NN to play chess takes so much hardware. See the people contributing to [Lc0](https://lczero.org/) to train over 200 million games. Since you will probably have trouble accessing the hardware (you could try to get some at [Google Colabatory](https://colab.research.google.com), but with only that, training will be very slow).
  
  **Edit: Using a NN With Supervised learning, you MAY get away with just Google Colab and possibly one strong GPU (2080, 2080Ti, Radeon VII).**
  
  > Another thing I am considering is whether it is important which language I use to code the engine. I know many chess engines use C++, which I have never used before. I have used other C-based languages which do many of the same things, with mostly varying syntax. I am most familiar with Swift and Javascript, but am also familiar with Python and feel that it could do the trick for me.
  
  Python and Javascript are probably too slow for a strong chess engine. I haven't used Swift but it probably won't like platforms other than macOS, so it's probably better to use C or C++. You could also possibly use Rust, but that has many safety features that get annoying and you don't really need, which can get annoying. It's also going to be harder to get good performance out of it since certain low-level optimizations are hard to make. Of course, you can always write it in assembly but that's probably going to be too much work. see <https://www.chessprogramming.org/Languages/>
  
  > **Comments:**
  > **username_0**: Awesome, thanks a ton for the info! I'll probably try to create an API that takes in an FEN position and outputs the calculated move. I figure this is the best way to integrate with Lichess bots.  
  > **username_10**: There is https://github.com/careless25/lichess-bot  
  > **username_12**: Is it an option to implement a different NN engine that just uses lc0's network?  
  > **username_7**: @username_12 as I understand it, NN engines use two things: the binary, and the neural network. Writing a binary is not easy, but training a neural network is relatively easy. In this sense Lc0 derivatives already exist - Leelenstein & Deus X (aka. Fat Fritz) are such derivatives.  
  > **username_13**: @username_12 in that case that wouldn't really be a different engine, that would be the Ic0 engine with just a different wrapping/interface/whatever.  
  
  ---
  
  ### Answer by username_14 on 2019-10-01. Score: 3
  
  I built a purely toy chess engine using: [python chess](https://pypi.org/project/python-chess/) it was really nice not having to code the rules of the game myself and just focus on the logic; however, the number of position I was able evaluate per second is very low. This might be a good starting point.
  
  
  ---
  
  ### Answer by username_15 on 2024-06-06. Score: 0
  
  > In wake of AlphaZero's crushing performance against Stockfish last year, I am considering whether I should create this engine with machine learning (some type of neural network, possibly using Tensorflow) or traditional, hard coded heuristics.
  
  Let us start with the basics:
  
  * Chess programs - all chess programs - are a matter of CPU cycles and how efficiently theay are used. In the AlphaZero-Stockfish match AlphaZero had an overwhelmingly big advantage in terms of pure calculation power. They used a 4-[TPU](https://en.wikipedia.org/wiki/Tensor_Processing_Unit) system to play (for the training they used a 5000-TPU system) and the used second-generation TPUs where benchmarked at 11.5 petaFLOPS each.
  
  There was already a similarly designed (from the hardware POV) system: playing strength achieved by a high level of parallelization. It was based on FPGAs instead of ASICs and called [Hydra](https://en.wikipedia.org/wiki/Hydra_(chess)). In 2005 it defeated then-world-Nr7 Michael Adams [5.5-0.5](https://www.theguardian.com/technology/2005/jun/30/chess.sport) with half of its 64 cored disabled.
  
  * In chess programming (that is: the "classical" chess programming) it was found over time that too much "chess knowledge" is bad for performance. There are basically three parts to a classical chess program: a move generator which produces all sorts of legal moves from a given position, an evaluation function and an algorithm to manage the search tree (AlphaBeta, MiniMax, etc.). The more chess knowledge is built into the evaluation function the slower it is and the less positions the program can evaluate - the smaller the search tree is, therefore.
  
  It is possible to build an evaluation function that is close to an Elo 1500 player, but it would be so slow that the resulting program wouldn't be much better than that. Take away most of the knowledge and you get an Elo-1000-function which is many times faster and you get an overall better performing program - its "chess knowledge" may be marginal, but by calculating fast (and therefore very far ahead) it more then makes up for that.
  
  * In light of this - speed is everything - the programming language of choice is simple: get the fastest one you can lay your hands on! Forget the interpreted languages (arguably with the exception of [FORTH](https://en.wikipedia.org/wiki/Forth_(programming_language))), stay away from compiled languages (yes, even C) and go for the lowest possible overhead: Assembler! Use C for rapid prototyping. (Yes, I admit, that sounds extreme, but this was actually the way I worked for a long time, not building chess programs but real-time signal-processing applications with DSPs.)
  
  If you want to try NN algorithms for your program you either go the way Google went - throw an awful lot of parallel-processing-capable hardware at it - or you will probably get mediocre results because these algorithms have a lot of overhead and only perform that well because of the raw power they are able to put to work.
  
  If you want to build the best possible program with your run-of-the-mill hardware (most probably a standard PC with some Linux/Windows, no multi-core datacenter GPU like the [Nvidia V100](https://www.nvidia.com/en-us/data-center/v100/) or access to Google Cloud TPU), you should probably go for a "classic" program and try your hand on optimizing assembler code, together with the development of better pruning strategies in the search-tree to maximise the number of plies the program is able to calculate ahead. A rule of thumb says that every additional ply is worth ~50 pts Elo.
  
  > **Comments:**
  > **username_7**: I believe AlphaZero only used that many TPUs during training. During play, it had much less hardware (don't recall exactly how much though).  
  > **username_15**: @username_7: See the text, they used 5k TPUs for training, 4TPUs for play. But notice that "a TPU" is not a processor, it is a massively parallel device with many, many cores.  
  
  ---
  
  