-
Notifications
You must be signed in to change notification settings - Fork 51
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
feat: implement DeflectionEval
#579
Conversation
There was a problem hiding this 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.
src/Eval/DeflectionEval.php
Outdated
if (!empty($exposedPieceList) || !empty($protectedPawnsList)) { | ||
$this->deflectionExists = true; | ||
$this->elaborateAdvantage($primaryDeflectionPhrase, $exposedPieceList, $protectedPawnsList, $legalMovesCount); | ||
break; | ||
} | ||
} | ||
} | ||
if ($this->deflectionExists) { | ||
break; | ||
} |
There was a problem hiding this comment.
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.
/** | ||
* sorts the legal moves array, so that it arranges the moves for capturing the attacking pieces on top | ||
*/ | ||
private function sortLegalMoves(&$legalMoveSquares, $attackingPieces) |
There was a problem hiding this comment.
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.
// $board = (new StrToBoard('2R3k1/1q5p/4P1pQ/5p2/8/8/1B2r1PP/1K6 b - - 1 1')) | ||
// ->create(); |
There was a problem hiding this comment.
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
public function getEval(): array | ||
{ | ||
return $this->eval; | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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 Happy learning! |
Hi, Happy Coding!! I'll be back to the work this weekend. |
@programarivm If the PR is closed, I would appreciate it if closed with a comment 🙂, efforts were put into it. Thanks. |
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:
Chess\Eval\OverloadingEval is already implemented. The next thing to do is to implement Please let me know if you'd want to be assigned #467 Happy coding and learning! |
closes #458
This PR implements TDD for DeflectionEval