Ejemplo n.º 1
0
        public static int Evaluate(BoardState board, bool enableCache, EvaluationStatistics statistics)
        {
            var openingPhase = board.GetPhaseRatio();
            var endingPhase  = BoardConstants.PhaseResolution - openingPhase;

            var result = MaterialEvaluator.Evaluate(board);

            result += enableCache ?
                      PawnStructureEvaluator.Evaluate(board, statistics, openingPhase, endingPhase) :
                      PawnStructureEvaluator.EvaluateWithoutCache(board, statistics, openingPhase, endingPhase);
            result += PositionEvaluator.Evaluate(board, openingPhase, endingPhase);

            if (endingPhase != BoardConstants.PhaseResolution)
            {
                var fieldsAttackedByWhite = 0ul;
                var fieldsAttackedByBlack = 0ul;

                result += MobilityEvaluator.Evaluate(board, openingPhase, endingPhase, ref fieldsAttackedByWhite, ref fieldsAttackedByBlack);
                result += KingSafetyEvaluator.Evaluate(board, openingPhase, endingPhase, fieldsAttackedByWhite, fieldsAttackedByBlack);
                result += RookEvaluator.Evaluate(board, openingPhase, endingPhase);
                result += BishopEvaluator.Evaluate(board, openingPhase, endingPhase);
            }

            return(board.ColorToMove == Color.White ? result : -result);
        }
Ejemplo n.º 2
0
        private void OnSearchUpdate(object sender, SearchStatistics stats)
        {
            var score = FormatScore(stats.Score);
            var principalVariation = FormatPrincipalVariation(stats.PrincipalVariation, stats.PrincipalVariationMovesCount);

            Send($"info depth {stats.Depth} seldepth {stats.SelectiveDepth} time {stats.SearchTime} " +
                 $"score {score} nodes {stats.TotalNodes} nps {stats.TotalNodesPerSecond} pv {principalVariation}");

            if (_debugMode && stats.PrincipalVariationMovesCount > 0 && stats.PrincipalVariation[0] != Move.Empty)
            {
                var sign = stats.Board.ColorToMove == Color.White ? 1 : -1;
                stats.Board.MakeMove(stats.PrincipalVariation[0]);

                var evaluationStatistics = new EvaluationStatistics();
                var openingPhase         = stats.Board.GetPhaseRatio();
                var endingPhase          = BoardConstants.PhaseResolution - openingPhase;

                var fieldsAttackedByWhite = 0ul;
                var fieldsAttackedByBlack = 0ul;

                var materialEvaluation      = sign * MaterialEvaluator.Evaluate(stats.Board);
                var positionEvaluation      = sign * PositionEvaluator.Evaluate(stats.Board, openingPhase, endingPhase);
                var pawnStructureEvaluation = sign * PawnStructureEvaluator.Evaluate(stats.Board, evaluationStatistics, openingPhase, endingPhase);
                var mobility   = sign * MobilityEvaluator.Evaluate(stats.Board, openingPhase, endingPhase, ref fieldsAttackedByWhite, ref fieldsAttackedByBlack);
                var kingSafety = sign * KingSafetyEvaluator.Evaluate(stats.Board, openingPhase, endingPhase, fieldsAttackedByWhite, fieldsAttackedByBlack);
                var rooks      = sign * RookEvaluator.Evaluate(stats.Board, openingPhase, endingPhase);
                var bishops    = sign * BishopEvaluator.Evaluate(stats.Board, openingPhase, endingPhase);

                var total = materialEvaluation + positionEvaluation + pawnStructureEvaluation +
                            mobility + kingSafety;

                Send($"info string evaluation {total} phase {openingPhase} material {materialEvaluation} " +
                     $"position {positionEvaluation} pawns {pawnStructureEvaluation} mobility {mobility} ksafety {kingSafety} " +
                     $"rooks {rooks} bishops {bishops} irrmoves {stats.Board.IrreversibleMovesCount}");

                stats.Board.UndoMove(stats.PrincipalVariation[0]);
            }
        }
Ejemplo n.º 3
0
        public void Run(params string[] parameters)
        {
            var fen                  = string.Join(' ', parameters);
            var boardState           = FenToBoard.Parse(fen, false);
            var evaluationStatistics = new EvaluationStatistics();

            var openingPhase = boardState.GetPhaseRatio();
            var endingPhase  = BoardConstants.PhaseResolution - openingPhase;

            var fieldsAttackedByWhite = 0ul;
            var fieldsAttackedByBlack = 0ul;

            var materialEvaluation      = MaterialEvaluator.Evaluate(boardState);
            var positionEvaluation      = PositionEvaluator.Evaluate(boardState, openingPhase, endingPhase);
            var pawnStructureEvaluation = PawnStructureEvaluator.EvaluateWithoutCache(boardState, evaluationStatistics, openingPhase, endingPhase);
            var mobility   = MobilityEvaluator.Evaluate(boardState, openingPhase, endingPhase, ref fieldsAttackedByWhite, ref fieldsAttackedByBlack);
            var kingSafety = KingSafetyEvaluator.Evaluate(boardState, openingPhase, endingPhase, fieldsAttackedByWhite, fieldsAttackedByBlack);
            var rooks      = RookEvaluator.Evaluate(boardState, openingPhase, endingPhase);
            var bishops    = BishopEvaluator.Evaluate(boardState, openingPhase, endingPhase);

            var total = materialEvaluation + positionEvaluation + pawnStructureEvaluation +
                        mobility + kingSafety;

            _interactiveConsole.WriteLine($"Evaluation for board with hash {boardState.Hash} (phase {openingPhase}, " +
                                          $"{boardState.IrreversibleMovesCount} irreversible moves)");

            _interactiveConsole.WriteLine($" = Material: {materialEvaluation}");
            _interactiveConsole.WriteLine($" = Position: {positionEvaluation}");
            _interactiveConsole.WriteLine($" = Pawns: {pawnStructureEvaluation}");
            _interactiveConsole.WriteLine($" = Mobility: {mobility}");
            _interactiveConsole.WriteLine($" = King safety: {kingSafety}");
            _interactiveConsole.WriteLine($" = Rooks: {rooks}");
            _interactiveConsole.WriteLine($" = Bishops: {bishops}");
            _interactiveConsole.WriteLine();
            _interactiveConsole.WriteLine($" = Total: {total}");
        }