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

feat: implement DeflectionEval #579

Conversation

KartikWatts
Copy link
Member

closes #458

This PR implements TDD for DeflectionEval

Copy link
Member Author

@KartikWatts KartikWatts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@programarivm Thanks a lot for providing this opportunity :), I have tried my best to evaluate deflection on board programmatically. Let me know your thoughts and I will update the PR accordingly.

Thanks to this feature, past few weeks I did a PhD on Deflection lol. Also generally speaking compared to many other Eval, Deflection was somewhat complicated, as per the link shared: Deflection in chess is a tactic that forces an opposing piece to leave the square, rank or file it occupies, thus exposing the king or a valuable piece. So, it is not limited to just one move or static position, rather the following move also should have some advantage due to a valid move played to be called deflection, and thus required a lot of checks,
Also, I found some interesting cases of deflection, where it is not just limited to the above definition but it could also be a case like: rook_deflection_for_pawn_promotion, which I felt would be unfair to not include.

Also pointing for information, I found most of the Eval classes are using detach rather than actual play, as understandably that is a much more simplified way, but for Deflection to analyze it clearly, I found it better to check with playLan, otherwise, many cases would be missed due to detach.

To my limit, I have tested with a lot of possible test cases not limited to cases in TDD, seems to work fine, would be glad for your review, regards.

Comment on lines 46 to 55
if (!empty($exposedPieceList) || !empty($protectedPawnsList)) {
$this->deflectionExists = true;
$this->elaborateAdvantage($primaryDeflectionPhrase, $exposedPieceList, $protectedPawnsList, $legalMovesCount);
break;
}
}
}
if ($this->deflectionExists) {
break;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case deflection exists, the optimal result for the first case for that piece and move found will be returned to keep things simple, if we rather want to evaluate for all the pieces, we can extend onto it, which would require a few updates, but for simplicity and optimization, I limited it to a single case, looks fine to me.

Comment on lines +59 to +62
/**
* sorts the legal moves array, so that it arranges the moves for capturing the attacking pieces on top
*/
private function sortLegalMoves(&$legalMoveSquares, $attackingPieces)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorting the legal moves to arrange the moves for capturing the attacking pieces on top. As during testing, I realized, that in most cases, deflection usually happens for these moves, also are more practical.

Comment on lines +167 to +168
// $board = (new StrToBoard('2R3k1/1q5p/4P1pQ/5p2/8/8/1B2r1PP/1K6 b - - 1 1'))
// ->create();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a lot of research for looking into all possible deflection cases, and I formed some cases where it could be due to a square being unprotected, I can already write code extension for this, but let me know your thoughts, should I do it already, this is the only case I feel left uncovered, as if we check for attacks on each square it would have a higher time complexity,
For that, I was thinking, can we first have another Eval class or AbstractBoard method that checks for attack and defence on any square. What are your thoughts @programarivm

Comment on lines +72 to +75
public function getEval(): array
{
return $this->eval;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention, as I have made updates to this file long ago, I think after latest rebase with conflict resolution, there are now some extra methods, let me know, if they are to be removed.

@KartikWatts KartikWatts marked this pull request as draft August 31, 2024 05:55
@KartikWatts KartikWatts marked this pull request as ready for review August 31, 2024 06:13
Copy link
Member Author

@KartikWatts KartikWatts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit, I had limited knowledge about the evaluation setup, only today did I realize the process flow for GoodPgnEvaluation, and how it automatically plays the best move to then call PgnEvaluation, which then looks for all the heuristics.

Based on this new knowledge, I have made some updates to DeflectionEval and also implemented the InverseEvalInterface interface (I was wondering what's the use of it, only now I know 😊)

With this new knowledge, I believe there might be some updates required for deflection, as for optimization, I was only calculating possibilities for the current turn on the board, only now I get the concept of why it was being calculated for both sides.

The more I'm exploring I realise, that so much effort has been put into this whole Evaluation setup, and is a fabulous feature of this package, capable of competing with mainstream platforms. Kudos.

@programarivm
Copy link
Member

Thank you @KartikWatts for contributing to this repo.

The implementation of the deflection heuristic is not too obvious at all. Also I am still trying to understand what it is about. It seems as if this one is often used as part of a combination involving other tactics like overloaded pieces and the back-rank checkmate.

@gurpreetkaits how about the implementation of Chess\Eval\BackRankEval?

Happy learning!

@gurpreetkaits
Copy link
Contributor

Hi, Happy Coding!!

I'll be back to the work this weekend.

@KartikWatts
Copy link
Member Author

@programarivm If the PR is closed, I would appreciate it if closed with a comment 🙂, efforts were put into it. Thanks.

@programarivm
Copy link
Member

Thank you @KartikWatts for asking this question.

The deflecion heuristic is probably one of the most complicated until now.

Let's stick to this definition:

Deflection is a tactic that forces an opposing piece to leave an important square. Deflections are often used as part of a combination which involves other tactics as well, typically piece overloading and the back-rank checkmate.

Chess\Eval\OverloadingEval is already implemented. The next thing to do is to implement Chess\Eval\BackRankEval.

Please let me know if you'd want to be assigned #467

Happy coding and learning!

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

Successfully merging this pull request may close these issues.

TDD the Chess\Eval\DeflectionEval class
3 participants