Re: Simplifying code
Posted: Fri May 15, 2020 2:41 pm
Weiss excluding the Fathom code is 2.5k lines of code
Code: Select all
ISearchResult SearchMoves(IChessPosition position, IMovePrioQ prioQ, IVariation initialVariation, int depth,
int plyCount, int lb, int ub)
{
if (!(TimeManagement.SearchExpired(Level))
&& (initialVariation == null || initialVariation.Value < ub)
&& prioQ.Count() > 0)
{
var mv = prioQ.Top();
var firstVariation = SearchMove(position, mv, initialVariation, depth, plyCount, lb, ub);
if (firstVariation == null)
{
return null;
}
else
{
var nextLb = Max(lb, firstVariation.Value);
var remPrioQ = prioQ.Pop();
var remResult = SearchMoves(position, remPrioQ, firstVariation, depth, plyCount, nextLb, ub);
if (firstVariation != initialVariation)
{
return ResultBuilder.BuildResult(firstVariation, remResult);
}
else
{
return remResult;
}
}
}
return null;
}
What about reordering it like that:Henk wrote: ↑Fri Jun 12, 2020 2:01 pm Now I have this for alpha beta search. Don't like it. Too many if statements.Code: Select all
ISearchResult SearchMoves(IChessPosition position, IMovePrioQ prioQ, IVariation initialVariation, int depth, int plyCount, int lb, int ub) { if (!(TimeManagement.SearchExpired(Level)) && (initialVariation == null || initialVariation.Value < ub) && prioQ.Count() > 0) { var mv = prioQ.Top(); var firstVariation = SearchMove(position, mv, initialVariation, depth, plyCount, lb, ub); if (firstVariation == null) { return null; } else { var nextLb = Max(lb, firstVariation.Value); var remPrioQ = prioQ.Pop(); var remResult = SearchMoves(position, remPrioQ, firstVariation, depth, plyCount, nextLb, ub); if (firstVariation != initialVariation) { return ResultBuilder.BuildResult(firstVariation, remResult); } else { return remResult; } } } return null; }
Code: Select all
ISearchResult SearchMoves(IChessPosition position, IMovePrioQ prioQ, IVariation initialVariation, int depth, int plyCount, int lb, int ub)
{
if (TimeManagement.SearchExpired(Level)
|| (initialVariation && initialVariation.Value >= ub)
|| prioQ.Count() <= 0)
{
return null;
}
var mv = prioQ.Top();
var firstVariation = SearchMove(position, mv, initialVariation, depth, plyCount, lb, ub);
if (firstVariation == null)
{
return null;
}
var nextLb = Max(lb, firstVariation.Value);
var remPrioQ = prioQ.Pop();
var remResult = SearchMoves(position, remPrioQ, firstVariation, depth, plyCount, nextLb, ub);
if (firstVariation != initialVariation)
{
return ResultBuilder.BuildResult(firstVariation, remResult);
}
return remResult;
}
Good to hear that you think a lot about theory ... But what is your goal? To have a working chess engine that plays decent chess and has source code without bugs that can be maintained and changed quite easily (which is somehow related to "being simple" and "you like it"!)? Or to have an engine that matches common formal theories?Henk wrote: ↑Fri Jun 12, 2020 2:33 pm Yes looks better. But I saw a video about monads recently. Although i am not sure I understand it.
Don't know anything about category theory.
So maybe I could use something like a monad to hide some obvious recurring tests.
Monad is a functor plus a flatten operation I understood.
Already unfamiliar with functors.
I hate bugs and code i can't understand or change easily.Sven wrote: ↑Fri Jun 12, 2020 2:48 pmGood to hear that you think a lot about theory ... But what is your goal? To have a working chess engine that plays decent chess and has source code without bugs that can be maintained and changed quite easily (which is somehow related to "being simple" and "you like it"!)? Or to have an engine that matches common formal theories?Henk wrote: ↑Fri Jun 12, 2020 2:33 pm Yes looks better. But I saw a video about monads recently. Although i am not sure I understand it.
Don't know anything about category theory.
So maybe I could use something like a monad to hide some obvious recurring tests.
Monad is a functor plus a flatten operation I understood.
Already unfamiliar with functors.
I would have proposed almost the same, for the same reasons. It would improve readability. However, it would not reduce the number of if's. Addressing that would require to know a bit more background.mhouppin wrote: ↑Fri Jun 12, 2020 2:17 pmWhat about reordering it like that:Henk wrote: ↑Fri Jun 12, 2020 2:01 pm Now I have this for alpha beta search. Don't like it. Too many if statements.Code: Select all
ISearchResult SearchMoves(IChessPosition position, IMovePrioQ prioQ, IVariation initialVariation, int depth, int plyCount, int lb, int ub) { if (!(TimeManagement.SearchExpired(Level)) && (initialVariation == null || initialVariation.Value < ub) && prioQ.Count() > 0) { var mv = prioQ.Top(); var firstVariation = SearchMove(position, mv, initialVariation, depth, plyCount, lb, ub); if (firstVariation == null) { return null; } else { var nextLb = Max(lb, firstVariation.Value); var remPrioQ = prioQ.Pop(); var remResult = SearchMoves(position, remPrioQ, firstVariation, depth, plyCount, nextLb, ub); if (firstVariation != initialVariation) { return ResultBuilder.BuildResult(firstVariation, remResult); } else { return remResult; } } } return null; }
Less indentations and if-else blocks by exploiting the "return" statements.Code: Select all
ISearchResult SearchMoves(IChessPosition position, IMovePrioQ prioQ, IVariation initialVariation, int depth, int plyCount, int lb, int ub) { if (TimeManagement.SearchExpired(Level) || (initialVariation && initialVariation.Value >= ub) || prioQ.Count() <= 0) { return null; } var mv = prioQ.Top(); var firstVariation = SearchMove(position, mv, initialVariation, depth, plyCount, lb, ub); if (firstVariation == null) { return null; } var nextLb = Max(lb, firstVariation.Value); var remPrioQ = prioQ.Pop(); var remResult = SearchMoves(position, remPrioQ, firstVariation, depth, plyCount, nextLb, ub); if (firstVariation != initialVariation) { return ResultBuilder.BuildResult(firstVariation, remResult); } return remResult; }
Good ... then I'd suggest to forget about category theory, monads, and functors for a while ...Henk wrote: ↑Fri Jun 12, 2020 3:00 pmI hate bugs and code i can't understand or change easily.Sven wrote: ↑Fri Jun 12, 2020 2:48 pmGood to hear that you think a lot about theory ... But what is your goal? To have a working chess engine that plays decent chess and has source code without bugs that can be maintained and changed quite easily (which is somehow related to "being simple" and "you like it"!)? Or to have an engine that matches common formal theories?Henk wrote: ↑Fri Jun 12, 2020 2:33 pm Yes looks better. But I saw a video about monads recently. Although i am not sure I understand it.
Don't know anything about category theory.
So maybe I could use something like a monad to hide some obvious recurring tests.
Monad is a functor plus a flatten operation I understood.
Already unfamiliar with functors.
Today and previous week I had to undo my changes after repairing 1000 compile time errors. Much misery.
Almost impossible to replace bitboards by something else in my source code.
So code should be written such that it easy to make changes. That is most important.