예제 #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);
        }
예제 #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);
        }
예제 #3
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));
        }