Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossibly bad search performance #29

Open
JamesLear92 opened this issue Mar 15, 2020 · 1 comment
Open

Impossibly bad search performance #29

JamesLear92 opened this issue Mar 15, 2020 · 1 comment

Comments

@JamesLear92
Copy link

JamesLear92 commented Mar 15, 2020

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?

@thomas-daniels
Copy link
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants