Which features offer the best return?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Which features offer the best return?

Post by emadsen »

lojic wrote: Sat Jan 30, 2021 9:56 pmRegarding bitboards, I was just reading some articles today and wondering maybe I should go ahead and bite the bullet and implement bitboards. What was your experience like switching from mailbox to bitboards?
It's certainly possible to write a strong engine using mailbox board representation. Regarding switching to bitboards now, my advice is not to switch until you fully understand the idea. It takes a while to get your head around perfect hashing, how that's leveraged to lookup pre-calculated moves, understand bit masking and shifting, and start thinking in terms of ulong bit ANDs / ORs instead of looping over arrays. If you're willing to put in the time, and your programming language of choice offers the required tools for the job, then go ahead and switch now.

It really comes down to how much complexity do you want to tackle all at once? You're already facing a learning curve understanding search algorithms, evaluation parameters, proper testing and tuning, etc. If you're doing this for the right reasons (sounds like you are) then you'll want to understand the purpose of every line of code in your program. So don't add bitboards until you understand the idea. Of course you'll achieve an understanding as you write the code, so... just be prepared for an investment.

My preference, going back a few years, was to learn how to write an engine with the more straightforward idea of mailbox arrays of pieces. I did that, in version 1, using object-oriented techniques familiar to me from my professional programming job. In version 2 I stuck with mailbox but wrote procedural code. I only got to bitboards in version 3, which I'm still writing when (infrequently) I find some time. I've enjoyed the journey from high-level abstract code to more performant code, including the switch to bitboards. That may be too long of a journey for you. Up to you.

Once you "grok" the bitboard idea, it's great. The evaluation code is so much easier to write with bitboards than with mailbox.
My C# chess engine: https://www.madchess.net
User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Which features offer the best return?

Post by emadsen »

mvanthoor wrote: Sat Jan 30, 2021 11:12 pmI referred some new engine developers to your site with regard to your strength progression log.
Thanks Marcel!
I like Madchess, and I'll certainly include it in my testing when I reach that level of play. Is there a way to compile the .NET Core version into one executable that runs on top of an installed framework? In that case, I'd do so.

(Euh... not targeted against your engine in particular, but against .NET Core... I think it's junk to have to release a 25 MB ZIP-file containing 30 bazillion files to run the engine.)
Why? The disk is the cheapest part of a computer :D. Actually, Microsoft has made great strides in this area. They've not quite achieved compilation to a single file, but they're almost there. If you clone MadChess 3 beta code from my GitHub repo, download the .NET 5 SDK*, you can compile it via...

Code: Select all

dotnet publish -c release -p:PublishSingleFile=true -p:PublishTrimmed=true -r win-x64
... which produces five files, not counting the .pdb debug file or engine logo. This will run stand-alone, no need to install a .NET framework.

Code: Select all

❯ dir

    Directory: C:\Users\Erik\Documents\Visual Studio 2019\Projects\MadChess\Engine\bin\release\net5.0\win-x64\publish

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          12/12/2020  3:46 AM         747912 clrcompression.dll
-a---          12/12/2020  3:46 AM        1323400 clrjit.dll
-a---          12/12/2020  3:46 AM        5153160 coreclr.dll
-a---           1/24/2021  1:18 PM        1048726 MadChess.Engine.bmp
-a---           1/30/2021  5:09 PM       20943513 MadChess.Engine.exe
-a---           1/30/2021  5:09 PM          65460 MadChess.Engine.pdb
-a---          12/12/2020  3:46 AM        1056632 mscordaccore.dll
Substitute another runtime identifier (the -r argument) if you're targeting a platform other than Windows x64. If you try that, let me know if you have any success. I've only compiled MadChess for Windows.

Microsoft is building an Ahead of Time (AOT) .NET compiler that produces a single machine-code .exe for a specific CPU architecture. I've played around with it. It's cool but not ready for prime time. Last I checked, it actually produces less performant code than the Just in Time (JIT) compiler. (The .exe produced by my instructions above is JIT'ed when you run it.)

Lots of cool development taking place in the .NET world!

* .NET 5 is the new version of .NET Core. Microsoft has unified the full .NET Framework and .NET Core into a single framework, .NET 5.
My C# chess engine: https://www.madchess.net
User avatar
Guenther
Posts: 4605
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: Which features offer the best return?

Post by Guenther »

emadsen wrote: Sun Jan 31, 2021 12:30 am ...
Why? The disk is the cheapest part of a computer :D. Actually, Microsoft has made great strides in this area. They've not quite achieved compilation to a single file, but they're almost there. If you clone MadChess 3 beta code from my GitHub repo, download the .NET 5 SDK*, you can compile it via...

Code: Select all

dotnet publish -c release -p:PublishSingleFile=true -p:PublishTrimmed=true -r win-x64
... which produces five files, not counting the .pdb debug file or engine logo. This will run stand-alone, no need to install a .NET framework.

Code: Select all

❯ dir

    Directory: C:\Users\Erik\Documents\Visual Studio 2019\Projects\MadChess\Engine\bin\release\net5.0\win-x64\publish

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          12/12/2020  3:46 AM         747912 clrcompression.dll
-a---          12/12/2020  3:46 AM        1323400 clrjit.dll
-a---          12/12/2020  3:46 AM        5153160 coreclr.dll
-a---           1/24/2021  1:18 PM        1048726 MadChess.Engine.bmp
-a---           1/30/2021  5:09 PM       20943513 MadChess.Engine.exe
-a---           1/30/2021  5:09 PM          65460 MadChess.Engine.pdb
-a---          12/12/2020  3:46 AM        1056632 mscordaccore.dll
Substitute another runtime identifier (the -r argument) if you're targeting a platform other than Windows x64. If you try that, let me know if you have any success. I've only compiled MadChess for Windows.

Microsoft is building an Ahead of Time (AOT) .NET compiler that produces a single machine-code .exe for a specific CPU architecture. I've played around with it. It's cool but not ready for prime time. Last I checked, it actually produces less performant code than the Just in Time (JIT) compiler. (The .exe produced by my instructions above is JIT'ed when you run it.)

Lots of cool development taking place in the .NET world!

* .NET 5 is the new version of .NET Core. Microsoft has unified the full .NET Framework and .NET Core into a single framework, .NET 5.
Hi Erik, your post made me curious and after I saw it is possible to compile directly from cmd via the plain dotnet SDK w/o Visual (which I don't have + don't want, because I usually compile with Msys2 for a lot of other languages), I gave it a try.

I have just two little problems. SingleFileTrue was not accepted it says I have to set first 'UseAppHost' to true, but how and where?

The other one obviously is that it will by default compile a popcount binary, which won't run here.
How do I change to sth like march=native for the SDK?
https://rwbc-chess.de

trollwatch:
Chessqueen + chessica + AlexChess + Eduard + Sylwy
User avatar
Guenther
Posts: 4605
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: Which features offer the best return?

Post by Guenther »

Guenther wrote: Sun Jan 31, 2021 11:39 am
emadsen wrote: Sun Jan 31, 2021 12:30 am ...

* .NET 5 is the new version of .NET Core. Microsoft has unified the full .NET Framework and .NET Core into a single framework, .NET 5.
Hi Erik, your post made me curious and after I saw it is possible to compile directly from cmd via the plain dotnet SDK w/o Visual (which I don't have + don't want, because I usually compile with Msys2 for a lot of other languages), I gave it a try.

I have just two little problems. SingleFileTrue was not accepted it says I have to set first 'UseAppHost' to true, but how and where?

The other one obviously is that it will by default compile a popcount binary, which won't run here.
How do I change to sth like march=native for the SDK?
Despite the error message when compiling, it still created two binaries, one with all dlls and the 'publish' one with just four dlls you mentioned.

Still none of them runs here of course due to the popcount problem.

Code: Select all

Unhandled exception. System.TypeInitializationException: The type initializer for 'ErikTheCoder.MadChess.Engine.Board' threw an exception.
 ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.Intrinsics.X86.Popcnt.X64.PopCount(UInt64 value)
   at ErikTheCoder.MadChess.Engine.PrecalculatedMoves.FindMagicMultipliers(Int32 Piece, WriteMessageLine WriteMessageLine)
   at ErikTheCoder.MadChess.Engine.PrecalculatedMoves..ctor()
   at ErikTheCoder.MadChess.Engine.Board..cctor()
   --- End of inner exception stack trace ---
   at ErikTheCoder.MadChess.Engine.Board..ctor(WriteMessageLine WriteMessageLine)
   at ErikTheCoder.MadChess.Engine.UciStream..ctor()
   at ErikTheCoder.MadChess.Engine.Program.Main()
https://rwbc-chess.de

trollwatch:
Chessqueen + chessica + AlexChess + Eduard + Sylwy
User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Which features offer the best return?

Post by emadsen »

Guenther wrote: Sun Jan 31, 2021 11:39 am I have just two little problems. SingleFileTrue was not accepted it says I have to set first 'UseAppHost' to true, but how and where?

The other one obviously is that it will by default compile a popcount binary, which won't run here.
Hi Guenther. Are you using an older version of .NET Core? I don't see a UseAppHost argument in the dotnet publish documentation. If so, try again with .NET 5.

I need to research a proper solution for conditional compilation for modern or older CPUs. As a quick fix, I created a new branch, nonpopcount, and replaced CPU intrinsics code in the Bitwise class with a de Bruijn sequence. I had this code in the Git history of Bitwise.cs. I wrote it using a de Bruijn sequence initially because Microsoft hadn't added support for CPU intrinsics yet.

Try this:

Code: Select all

git clone https://github.com/ekmadsen/MadChess
cd .\MadChess\
git checkout feature/nonpopcount
cd .\Engine\
dotnet publish -c release -p:PublishSingleFile=true -p:PublishTrimmed=true -r win-x64
cd .\bin\release\net5.0\win-x64\publish\
dir
.\MadChess.Engine.exe
uci
isready
?
position startpos
countmoves 6
Etc... Does that work?
My C# chess engine: https://www.madchess.net
User avatar
Guenther
Posts: 4605
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: Which features offer the best return?

Post by Guenther »

emadsen wrote: Sun Jan 31, 2021 7:24 pm
Guenther wrote: Sun Jan 31, 2021 11:39 am I have just two little problems. SingleFileTrue was not accepted it says I have to set first 'UseAppHost' to true, but how and where?

The other one obviously is that it will by default compile a popcount binary, which won't run here.
Hi Guenther. Are you using an older version of .NET Core? I don't see a UseAppHost argument in the dotnet publish documentation. If so, try again with .NET 5.

I need to research a proper solution for conditional compilation for modern or older CPUs. As a quick fix, I created a new branch, nonpopcount, and replaced CPU intrinsics code in the Bitwise class with a de Bruijn sequence. I had this code in the Git history of Bitwise.cs. I wrote it using a de Bruijn sequence initially because Microsoft hadn't added support for CPU intrinsics yet.

Try this:

Code: Select all

git clone https://github.com/ekmadsen/MadChess
cd .\MadChess\
git checkout feature/nonpopcount
cd .\Engine\
dotnet publish -c release -p:PublishSingleFile=true -p:PublishTrimmed=true -r win-x64
cd .\bin\release\net5.0\win-x64\publish\
dir
.\MadChess.Engine.exe
uci
isready
?
position startpos
countmoves 6
Etc... Does that work?
Erik, thanks for the fix for no popcount, that worked!

Code: Select all

uci
id name MadChess 3.0
id author Erik Madsen
option name UCI_EngineAbout type string default MadChess by Erik Madsen.  See https://www.madchess.net.
option name Debug type check default false
option name Log type check default false
option name Hash type spin default 128 min 0 max 1024
option name ClearHash type button
option name UCI_AnalyseMode type check default false
option name Analyze type check default false
option name MultiPV type spin default 1 min 1 max 128
option name PieceLocation type check default true
option name PassedPawns type check default true
option name Mobility type check default true
option name NPS type spin default 0 min 0 max 1000000
option name MoveError type spin default 0 min 0 max 1000
option name BlunderError type spin default 0 min 0 max 1000
option name BlunderPercent type spin default 0 min 0 max 100
option name UCI_LimitStrength type check default false
option name LimitStrength type check default false
option name UCI_Elo type spin default 400 min 400 max 2200
option name ELO type spin default 400 min 400 max 2200
uciok
ucinewgame

isready
readyok

position startpos

countmoves 6
Counted 1.000.000 nodes (3.705.038 nodes per second).
...
Counted 119.060.324 moves in 43,493 seconds.
BTW I had installed the last version of the SDK 5.0.102 IIRC, but whatever the first error/warning message is gone too.
The nps is enormous for my hardware, an old Quadcore from 2009 (core2).

Code: Select all

go infinite
info multipv 1 depth 1 seldepth 1 time 65 nodes 61 score cp 53 nps 934 pv e2e4
...
info multipv 1 depth 16 seldepth 27 time 11210 nodes 19136237 score cp 23 nps 1707113 pv d2d4 g8f6 b1c3 e7e6 h2h4 b8c6 h1h3 d7d5 c1f4 f8d6 e2e3 e8g8 a1c1 d6f4 e3f4 g8h8
How far do you think you are from your goal for releasing MadChess3?
https://rwbc-chess.de

trollwatch:
Chessqueen + chessica + AlexChess + Eduard + Sylwy
User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Which features offer the best return?

Post by emadsen »

Guenther wrote: Sun Jan 31, 2021 10:25 pmErik, thanks for the fix for no popcount, that worked! ... The nps is enormous for my hardware, an old Quadcore from 2009 (core2)... How far do you think you are from your goal for releasing MadChess3?
Excellent! I'll integrate support for older CPUs into the main branch, probably with a dotnet publish configuration switch: -c release or -c release-nonpopcount, something like that.

Did you compile it for Windows or Linux?

I'm 70 Elo away from releasing MadChess 3.0. I'd like to reach 2600 at bullet. I'm at 2530. The evaluation function is really simple. By adding evaluation terms for bishop pair, knight outposts, and rook or queen on 7th rank I should be able to reach 2600. So hopefully in a month or two I'll get there.

Problem is, I keep adjusting the search logic. I should focus on evaluation. Though, I did improve my MultiPV implementation, which is great for analysis.

MadChess counts a null move as a node, so this does inflate NPS. Feels a bit like cheating. But whatever, it's a meaningless statistic considered in isolation. I count null moves as nodes in MadChess 3.0 because 2.x and 1.x did. This gives me a relative comparison of speed.
My C# chess engine: https://www.madchess.net
lojic
Posts: 71
Joined: Thu Jan 28, 2021 9:50 pm
Full name: Brian Adkins

Re: Which features offer the best return?

Post by lojic »

lojic wrote: Fri Jan 29, 2021 3:08 am Hello:

I've just fulfilled (partially) a dream I've had for a long time - to write a chess engine in a lisp. I've implemented an engine in Racket which is essentially a version of Scheme. It plays reasonably well for how simple it is. I have alpha beta pruning and a very simple evaluation function. It can only search to ply 7 now (and be fast enough for a 10+5 game) after I added some heuristics to the evaluation function.

My goal is a modest one - simply for it to consistently beat me (I'm roughly 1,650), and it's not quite there yet, I think it's probably ~ 1,550.
...
Mission accomplished :)

My first git commit was on January 17, and as of today, I don't think I can realistically beat my chess engine anymore (more a comment on my poor chess skill than on my excellent chess coding!). It was consistently beating humans (30+ games) in the mid to upper 1900's in 15+10 and 10+5 games after adding the piece square table, simplified evaluation and a transposition table. Although I controlled the time very intensively, my time management strategy could easily be automated - basically, moved quicker early on, then increased the think time substantially after the 10th move until a decent advantage was won, then reduced the think time to not lose on time.

And probably as no surprise to anyone on this forum, after hitting my initial goal of having it beat me, I'd like to extend it just a bit further ;) However, I'm going to resist that temptation for the time being to do a massive refactoring of the codebase so it can serve the purpose (hopefully) of being a good example of both Racket code and chess engine code - well structured, commented, etc. This is a bit of a challenge - structuring it as a composition of reasonably small, well tested, functions, and minimizing side effects, w/o slowing it down too much.

After that, I think it would be fun to improve the evaluation & implement an opening book, but I still think I'll skip some of the more esoteric features, and leave the serious chess coding to others.
User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Which features offer the best return?

Post by emadsen »

Congrats!
lojic wrote: Sat Feb 20, 2021 12:07 am I still think I'll skip some of the more esoteric features, and leave the serious chess coding to others.
Beware, the chess programming virus is contagious.
My C# chess engine: https://www.madchess.net