Beispiel #1
0
        // weight_option() computes the value of an evaluation weight, by combining
        // two UCI-configurable weights (midgame and endgame) with an internal weight.
        public static WeightS weight_option(string mgOpt, string egOpt, Score internalWeight)
        {
            WeightS w = new WeightS(Engine.Options[mgOpt].getInt() * Types.mg_value(internalWeight) / 100,
                                    Engine.Options[egOpt].getInt() * Types.eg_value(internalWeight) / 100);

            return(w);
        }
Beispiel #2
0
        // do_evaluate() is the evaluation entry point, called directly from evaluate()
        public static Value do_evaluate(Position pos, bool Trace)
        {
            Debug.Assert(0 == pos.checkers());

            EvalInfo ei = new EvalInfo();
            Score    score;

            Score[] mobility   = new Score[] { ScoreS.SCORE_ZERO, ScoreS.SCORE_ZERO };
            Thread  thisThread = pos.this_thread();

            // Initialize score by reading the incrementally updated scores included
            // in the position object (material + piece square tables) and adding a
            // Tempo bonus. Score is computed from the point of view of white.
            score = pos.psq_score() + (pos.side_to_move() == ColorS.WHITE ? Tempo : -Tempo);

            // Probe the material hash table
            ei.mi  = Material.probe(pos, thisThread.materialTable, thisThread.endgames);
            score += ei.mi.material_value();

            // If we have a specialized evaluation function for the current material
            // configuration, call it and return.
            if (ei.mi.specialized_eval_exists())
            {
                return(ei.mi.evaluate(pos));
            }

            // Probe the pawn hash table
            ei.pi  = Pawns.probe(pos, thisThread.pawnsTable);
            score += apply_weight(ei.pi.pawns_value(), Weights[EvalWeightS.PawnStructure]);

            // Initialize attack and king safety bitboards
            init_eval_info(pos, ei, ColorS.WHITE);
            init_eval_info(pos, ei, ColorS.BLACK);

            ei.attackedBy[ColorS.WHITE][PieceTypeS.ALL_PIECES] |= ei.attackedBy[ColorS.WHITE][PieceTypeS.KING];
            ei.attackedBy[ColorS.BLACK][PieceTypeS.ALL_PIECES] |= ei.attackedBy[ColorS.BLACK][PieceTypeS.KING];

            // Do not include in mobility squares protected by enemy pawns or occupied by our pawns or king
            Bitboard[] mobilityArea = new Bitboard[] { ~(ei.attackedBy[ColorS.BLACK][PieceTypeS.PAWN] | pos.pieces_color_piecetype(ColorS.WHITE, PieceTypeS.PAWN, PieceTypeS.KING)),
                                                       ~(ei.attackedBy[ColorS.WHITE][PieceTypeS.PAWN] | pos.pieces_color_piecetype(ColorS.BLACK, PieceTypeS.PAWN, PieceTypeS.KING)) };
            // Evaluate pieces and mobility
            score += evaluate_pieces(pos, ei, mobility, mobilityArea, PieceTypeS.KNIGHT, ColorS.WHITE, Trace);
            score += Eval.apply_weight(mobility[ColorS.WHITE] - mobility[ColorS.BLACK], Weights[EvalWeightS.Mobility]);

            // Evaluate kings after all other pieces because we need complete attack
            // information when computing the king safety evaluation.
            score += evaluate_king(pos, ei, ColorS.WHITE, Trace)
                     - evaluate_king(pos, ei, ColorS.BLACK, Trace);

            // Evaluate tactical threats, we need full attack information including king
            score += evaluate_threats(pos, ei, ColorS.WHITE, Trace)
                     - evaluate_threats(pos, ei, ColorS.BLACK, Trace);

            // Evaluate passed pawns, we need full attack information including king
            score += evaluate_passed_pawns(pos, ei, ColorS.WHITE, Trace)
                     - evaluate_passed_pawns(pos, ei, ColorS.BLACK, Trace);

            // If one side has only a king, check whether exists any unstoppable passed pawn
            if (0 == pos.non_pawn_material(ColorS.WHITE) || 0 == pos.non_pawn_material(ColorS.BLACK))
            {
                score += evaluate_unstoppable_pawns(pos, ColorS.WHITE, ei)
                         - evaluate_unstoppable_pawns(pos, ColorS.BLACK, ei);
            }

            // Evaluate space for both sides, only in middle-game.
            if (ei.mi.space_weight() != 0)
            {
                int s = evaluate_space(pos, ei, ColorS.WHITE) - evaluate_space(pos, ei, ColorS.BLACK);
                score += Eval.apply_weight(s * ei.mi.space_weight(), Weights[EvalWeightS.Space]);
            }

            // Scale winning side if position is more drawish that what it appears
            ScaleFactor sf = Types.eg_value(score) > ValueS.VALUE_DRAW ? ei.mi.scale_factor(pos, ColorS.WHITE)
                                                                       : ei.mi.scale_factor(pos, ColorS.BLACK);

            // If we don't already have an unusual scale factor, check for opposite
            // colored bishop endgames, and use a lower scale for those.
            if (ei.mi.game_phase() < PhaseS.PHASE_MIDGAME &&
                pos.opposite_bishops() &&
                (sf == ScaleFactorS.SCALE_FACTOR_NORMAL || sf == ScaleFactorS.SCALE_FACTOR_ONEPAWN))
            {
                // Ignoring any pawns, do both sides only have a single bishop and no
                // other pieces?
                if (pos.non_pawn_material(ColorS.WHITE) == ValueS.BishopValueMg &&
                    pos.non_pawn_material(ColorS.BLACK) == ValueS.BishopValueMg)
                {
                    // Check for KBP vs KB with only a single pawn that is almost
                    // certainly a draw or at least two pawns.
                    bool one_pawn = (pos.count(ColorS.WHITE, PieceTypeS.PAWN) + pos.count(ColorS.BLACK, PieceTypeS.PAWN) == 1);
                    sf = one_pawn ? (8) : (32);
                }
                else
                {
                    // Endgame with opposite-colored bishops, but also other pieces. Still
                    // a bit drawish, but not as drawish as with only the two bishops.
                    sf = (50 * sf / ScaleFactorS.SCALE_FACTOR_NORMAL);
                }
            }

            // Interpolate between a middlegame and a (scaled by 'sf') endgame score
            Value v = Types.mg_value(score) * (ei.mi.game_phase())
                      + Types.eg_value(score) * (PhaseS.PHASE_MIDGAME - ei.mi.game_phase()) * sf / ScaleFactorS.SCALE_FACTOR_NORMAL;

            v /= (PhaseS.PHASE_MIDGAME);

            // In case of tracing add all single evaluation contributions for both white and black
            if (Trace)
            {
                //Tracing.add_term(Tracing.PST, pos.psq_score());
                //Tracing.add_term(Tracing.IMBALANCE, ei.mi.material_value());
                //Tracing.add_term(PAWN, ei.pi.pawns_value());
                //Tracing.add_term(Tracing.MOBILITY, apply_weight(mobility[WHITE], Weights[Mobility])
                //                                   , apply_weight(mobility[BLACK], Weights[Mobility]));
                //Score w = ei.mi->space_weight() * evaluate_space<WHITE>(pos, ei);
                //Score b = ei.mi->space_weight() * evaluate_space<BLACK>(pos, ei);
                //Tracing.add_term(Tracing.SPACE, apply_weight(w, Weights[Space]), apply_weight(b, Weights[Space]));
                //Tracing.add_term(Tracing.TOTAL, score);
                //Tracing.ei = ei;
                //Tracing.sf = sf;
            }

            return(pos.side_to_move() == ColorS.WHITE ? v : -v);
        }
Beispiel #3
0
        // evaluate_king() assigns bonuses and penalties to a king of a given color
        public static Score evaluate_king(Position pos, EvalInfo ei, Color Us, bool Trace)
        {
            Color Them = (Us == ColorS.WHITE ? ColorS.BLACK : ColorS.WHITE);

            Bitboard undefended, b, b1, b2, safe;
            int      attackUnits;
            Square   ksq = pos.king_square(Us);

            // King shelter and enemy pawns storm
            Score score = ei.pi.king_safety(pos, ksq, Us);

            // Main king safety evaluation
            if (ei.kingAttackersCount[Them] != 0)
            {
                // Find the attacked squares around the king which have no defenders
                // apart from the king itself
                undefended = ei.attackedBy[Them][PieceTypeS.ALL_PIECES]
                             & ei.attackedBy[Us][PieceTypeS.KING]
                             & ~(ei.attackedBy[Us][PieceTypeS.PAWN] | ei.attackedBy[Us][PieceTypeS.KNIGHT]
                                 | ei.attackedBy[Us][PieceTypeS.BISHOP] | ei.attackedBy[Us][PieceTypeS.ROOK]
                                 | ei.attackedBy[Us][PieceTypeS.QUEEN]);

                // Initialize the 'attackUnits' variable, which is used later on as an
                // index to the KingDanger[] array. The initial value is based on the
                // number and types of the enemy's attacking pieces, the number of
                // attacked and undefended squares around our king and the quality of
                // the pawn shelter (current 'score' value).
                attackUnits = Math.Min(20, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
                              + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + Bitcount.popcount_Max15(undefended))
                              + 2 * (ei.pinnedPieces[Us] != 0 ? 1 : 0)
                              - Types.mg_value(score) / 32;

                // Analyse the enemy's safe queen contact checks. Firstly, find the
                // undefended squares around the king that are attacked by the enemy's
                // queen...
                b = undefended & ei.attackedBy[Them][PieceTypeS.QUEEN] & ~pos.pieces_color(Them);
                if (b != 0)
                {
                    // ...and then remove squares not supported by another enemy piece
                    b &= (ei.attackedBy[Them][PieceTypeS.PAWN] | ei.attackedBy[Them][PieceTypeS.KNIGHT]
                          | ei.attackedBy[Them][PieceTypeS.BISHOP] | ei.attackedBy[Them][PieceTypeS.ROOK]);
                    if (b != 0)
                    {
                        attackUnits += QueenContactCheck
                                       * Bitcount.popcount_Max15(b)
                                       * (Them == pos.side_to_move() ? 2 : 1);
                    }
                }

                // Analyse the enemy's safe rook contact checks. Firstly, find the
                // undefended squares around the king that are attacked by the enemy's
                // rooks...
                b = undefended & ei.attackedBy[Them][PieceTypeS.ROOK] & ~pos.pieces_color(Them);

                // Consider only squares where the enemy rook gives check
                b &= BitBoard.PseudoAttacks[PieceTypeS.ROOK][ksq];

                if (b != 0)
                {
                    // ...and then remove squares not supported by another enemy piece
                    b &= (ei.attackedBy[Them][PieceTypeS.PAWN] | ei.attackedBy[Them][PieceTypeS.KNIGHT]
                          | ei.attackedBy[Them][PieceTypeS.BISHOP] | ei.attackedBy[Them][PieceTypeS.QUEEN]);

                    if (b != 0)
                    {
                        attackUnits += RookContactCheck
                                       * Bitcount.popcount_Max15(b)
                                       * (Them == pos.side_to_move() ? 2 : 1);
                    }
                }

                // Analyse enemy's safe distance checks for sliders and knights
                safe = ~(pos.pieces_color(Them) | ei.attackedBy[Us][PieceTypeS.ALL_PIECES]);

                b1 = pos.attacks_from_square_piecetype(ksq, PieceTypeS.ROOK) & safe;
                b2 = pos.attacks_from_square_piecetype(ksq, PieceTypeS.BISHOP) & safe;

                // Enemy queen safe checks
                b = (b1 | b2) & ei.attackedBy[Them][PieceTypeS.QUEEN];
                if (b != 0)
                {
                    attackUnits += QueenCheck * Bitcount.popcount_Max15(b);
                }

                // Enemy rooks safe checks
                b = b1 & ei.attackedBy[Them][PieceTypeS.ROOK];
                if (b != 0)
                {
                    attackUnits += RookCheck * Bitcount.popcount_Max15(b);
                }

                // Enemy bishops safe checks
                b = b2 & ei.attackedBy[Them][PieceTypeS.BISHOP];
                if (b != 0)
                {
                    attackUnits += BishopCheck * Bitcount.popcount_Max15(b);
                }

                // Enemy knights safe checks
                b = pos.attacks_from_square_piecetype(ksq, PieceTypeS.KNIGHT) & ei.attackedBy[Them][PieceTypeS.KNIGHT] & safe;
                if (b != 0)
                {
                    attackUnits += KnightCheck * Bitcount.popcount_Max15(b);
                }

                // To index KingDanger[] attackUnits must be in [0, 99] range
                attackUnits = Math.Min(99, Math.Max(0, attackUnits));

                // Finally, extract the king danger score from the KingDanger[]
                // array and subtract the score from evaluation.
                score -= KingDanger[Us == Search.RootColor ? 1 : 0][attackUnits];
            }

            if (Trace)
            {
                Tracing.terms[Us][PieceTypeS.KING] = score;
            }

            return(score);
        }
Beispiel #4
0
        public static Score[][] KingDanger = new Score[ColorS.COLOR_NB][] { new Score[128], new Score[128] }; // 2, 128

        // apply_weight() weighs score 'v' by weight 'w' trying to prevent overflow
        public static Score apply_weight(Score v, WeightS w)
        {
            return(Types.make_score(Types.mg_value(v) * w.mg / 256, Types.eg_value(v) * w.eg / 256));
        }