Chess.NET icon indicating copy to clipboard operation
Chess.NET copied to clipboard

Impossibly bad search performance

Open JamesLear92 opened this issue 4 years ago • 1 comments

Hi, i've had a play around with this and I find a way to search moves quicky. I just tried the following to enumerate moves:

    static void Main(string[] args)
    {
        var game = new ChessGame();
        Console.WriteLine("Beginning Search...");
        var depth = 4;
        var expectedMoveCount = 206603;
        var start = DateTime.Now;
        var count = Search(game, 0, depth);
        Console.WriteLine("Moves found: " + count.Count() + " - Depth: " + depth + " Expected: " + expectedMoveCount);
        Console.WriteLine("Seconds Taken: " + (DateTime.Now - start).TotalSeconds);
    }
    static IEnumerable<Move> Search(ChessGame game, Move move, int currentDepth, int targetDepth)
    {
        game.MakeMove(move, true);
        yield return move;
        if (currentDepth < targetDepth)
            foreach (var item in (Search(game, currentDepth + 1, targetDepth)))
                yield return item;
        game.Undo();
    }
    static IEnumerable<Move> Search(ChessGame game, int currentDepth, int targetDepth)
    {
        foreach (var move in game.GetValidMoves(game.WhoseTurn))
        {
            if (currentDepth < targetDepth)
            {
                game.MakeMove(move,true);
                yield return move;
                foreach (var item in (Search(game, currentDepth + 1, targetDepth)))
                    yield return item;
                game.Undo();
            }
        }
    }

But for a depth of 4, it enumerated the right number of moves, but took 198 seconds. That's only about 1000 moves a second.

Is this a major performance issue, or is this me using the search methods incorrectly?

JamesLear92 avatar Mar 15 '20 13:03 JamesLear92

I never actually did extensive testing on it. This is much worse than I expected though...

Some background: I created this library because I needed chess functionality for other projects of mine that I wanted to do in C#. Initially I hadn't heard of the concept of bitboards that allow fast chess operations, so I had done my implementation more naively, and when I heard about bitboards I kept that in mind with the idea of "I'll make it more performant with bitboard some time later".

That never happened though, since my initial motivation for this project (being "I need this") has not carried on because (a) in the project(s) I'm using this library, performance is far from critical, (b) I'm not using C# much anymore nowadays, as Rust is now my first language of choice (in which I can use the great chess library shakmaty if I need to). So I've spent time on other projects than this one.

Lately, the thought of officially marking this library as "unmaintained" has crossed my mind a few times, because that is what it has practically boiled down to for a long time now. And considering that it's about 200000 times slower than optimal according to your test, I should probably just go ahead with that.

As for your project: I can only recommend moving to another library. For C#, https://github.com/rudzen/ChessLib looks good and performant (I didn't actually try it though). If language doesn't matter, I can wholeheartedly recommend shakmaty (Rust), python-chess (Python) or scalachess (Scala).

thomas-daniels avatar Mar 15 '20 13:03 thomas-daniels