ChessUSA.com TalkChess.com
Hosted by Your Move Chess & Games
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

evaluate loose pieces
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions Flat
View previous topic :: View next topic  
Author Message
Eelco de Groot



Joined: 12 Mar 2006
Posts: 2591
Location: Groningen

PostPost subject: Re: evaluate loose pieces    Posted: Mon Feb 27, 2012 9:45 pm Reply to topic Reply with quote

mcostalba wrote:
Eelco de Groot wrote:

I think the change you propose is not really what Lucas means with "loose pieces" the usual term,


I have looked at his code, not how it is called.

Eelco de Groot wrote:

My own version of that code in Stockfish is a bit experimental and not very streamlined code


Your code is too complicated to work Wink

You are attacking 3 concepts (loose pieces, passed pawns and king attacks) in a single piece of code: it doesn't work like this. I'd suggest a more orthogonal approach, one different function for each concept. A possible implementation of your idea that seems an acceptable compromise between speed and accuracy could be:

Code:

  template<Color Us>
  Score evaluate_threats(const Position& pos, EvalInfo& ei) {

    const Color Them = (Us == WHITE ? BLACK : WHITE);

    Bitboard b, bb;
    Score score = SCORE_ZERO;

    // Enemy pieces not defended by a pawn and under our attack
    Bitboard weakEnemies =  pos.pieces(Them)
                          & ~ei.attackedBy[Them][PAWN]
                          & ei.attackedBy[Us][0];
    if (!weakEnemies)
        return SCORE_ZERO;

    Bitboard undefended = weakEnemies & ~ei.attackedBy[Them][0];

    // Add bonus according to type of attacked enemy piece and to the
    // type of attacking piece, from knights to queens. Kings are not
    // considered because are already handled in king evaluation.
    for (PieceType pt1 = KNIGHT; pt1 < KING; pt1++)
    {
        b = ei.attackedBy[Us][pt1] & weakEnemies;
        if (b)
            for (PieceType pt2 = PAWN; pt2 < KING; pt2++)
                if (bb = b & pos.pieces(pt2), bb)
                    score += bb & undefended ? 2 * ThreatBonus[pt1][pt2]
                                             : 1 * ThreatBonus[pt1][pt2];
    }
    return score;
  }


Okay I accept that challenge; I have actually put in more code Marco Razz Not that I don't think you guys are right; that this is difficult to test and in particular there is no way to see if any elements are actually making things worse if you don't test all of it separately. But there is an idea to it all that I try to get across, sometimes there are features in a chess position that you can't do so much about. They may have happened "by accident" or your opponent got them with a superior search or superior knowledge implemented. Of for instance they came with the opening. And maybe when you detect it, it is already too late. Now you can include all the knowledge you know in your evaluation function, but here you actually have the chance to do something about some of the elements! The piece is attacked and there is no defending pawn. The piece does not actually have to hang but you do know for sure you can attack it and you even know with what piece you do this. Maybe then it pays off, to see how good your target really is. A parallel with king safety: you start evaluating that fully only if you have more than one attacker in place.

For instance another idea is you could put the results in a separate evalmargin that includes attacker and attacked piece, and specifically use that knowledge in extending moves of the attacking and attacked piece. Just to name an idea what probably is just within limits of my own programming possibilities Smile I am maintaining the copyright to my codechanges Razz and it now looks like this:

Code:


  template<Color Us>
  Score evaluate_threats(const Position& pos, EvalInfo& ei) {

    const Color Them = (Us == WHITE ? BLACK : WHITE);

    Bitboard b;
    Score score = SCORE_ZERO;

    // Enemy pieces not defended by a pawn and under our attack
    Bitboard weakEnemies =  pos.pieces(Them)
                          & ~ei.attackedBy[Them][PAWN]
                          & ei.attackedBy[Us][0];
    if (!weakEnemies)
        return SCORE_ZERO;

    // Add bonus according to type of attacked enemy piece [pt2] and to the
    // type of attacking piece [pt1], from knights to queens. Kings are not
    // considered under pt2 because that is already handled in king evaluation.
    for (PieceType pt1 = KNIGHT; pt1 <= KING; pt1++)
    {
        b = ei.attackedBy[Us][pt1] & weakEnemies;
        if (b)
            for (PieceType pt2 = PAWN; pt2 < KING; pt2++)
            {
                Bitboard bb = b & pos.pieces(pt2);
                if (bb)
                {
                   score += ThreatBonus[pt1][pt2];
                   do { // Is the piece undefended?
                         bool undefended = false;
                         Square s = pop_1st_bit(&bb);
                         if (!(ei.attackedBy[Them][0] & s))
                         {
                            undefended = true;
                            score += ThreatBonus[pt1][pt2];
                            if (pt2 == PAWN && (!pos.square_is_empty(s + pawn_push(Them)) || (ei.attackedBy[Us][0] & s + pawn_push(Them))))
                            {
                               score += make_score(5, 10);
                               // Nearing a pawn ending and the pawn may become a candidate pawn or a passed pawn?
                               File f = file_of(s);
                               if (ei.pi->file_is_half_open(Us, f) && !pos.non_pawn_material(Them))
                                   score += make_score(0, 2*relative_rank(Them, s));
                               if (ei.pi->chainbase_pawns(Us) & s)
                                   score += make_score(20, 20);
                            }
                          }
                          // Does the piece attack our king?
                          if ((ei.kingAttackersBB[Us] & s))
                              score += make_score(30 + undefended * 20, 0);
                   } while (bb);
                }
             }
    }
    return score;
  }

_________________
Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as possible, you
are, by definition, not smart enough to debug it.
-- Brian W. Kernighan
Back to top
View user's profile Send private message
Display posts from previous:   
Subject Author Date/Time
evaluate loose pieces Lucas Braesch Sun Feb 26, 2012 5:20 am
      Re: evaluate loose pieces Larry Kaufman Sun Feb 26, 2012 3:57 pm
            Re: evaluate loose pieces Lucas Braesch Sun Feb 26, 2012 4:11 pm
                  Re: evaluate loose pieces Eelco de Groot Sun Feb 26, 2012 5:57 pm
                        Re: evaluate loose pieces Michael Hoffmann Sun Feb 26, 2012 6:53 pm
                        Re: evaluate loose pieces Marco Costalba Sun Feb 26, 2012 7:04 pm
                              Re: evaluate loose pieces Eelco de Groot Mon Feb 27, 2012 12:46 am
                                    Re: evaluate loose pieces Marco Costalba Mon Feb 27, 2012 7:17 am
                                          Re: evaluate loose pieces Lucas Braesch Mon Feb 27, 2012 10:20 am
                                                Re: evaluate loose pieces Sam Hamilton Mon Feb 27, 2012 10:23 am
                                          Re: evaluate loose pieces Lucas Braesch Mon Feb 27, 2012 1:40 pm
                                                Re: evaluate loose pieces Evert Glebbeek Mon Feb 27, 2012 2:34 pm
                                          Re: evaluate loose pieces Marek Kwiatkowski Mon Feb 27, 2012 2:58 pm
                                          Re: evaluate loose pieces Eelco de Groot Mon Feb 27, 2012 9:45 pm
                                          Re: evaluate loose pieces Gary Tue Feb 28, 2012 1:35 pm
                                                Re: evaluate loose pieces Marek Kwiatkowski Tue Feb 28, 2012 2:35 pm
                                                      Re: evaluate loose pieces Joona Kiiski Tue Feb 28, 2012 4:04 pm
                                                            Re: evaluate loose pieces Marek Kwiatkowski Tue Feb 28, 2012 5:57 pm
                                                            Re: evaluate loose pieces Eelco de Groot Fri May 25, 2012 11:36 pm
                                                      Re: evaluate loose pieces Gary Tue Feb 28, 2012 4:04 pm
                              Re: evaluate loose pieces Lucas Braesch Mon Feb 27, 2012 3:57 am
                        Re: evaluate loose pieces Sam Hamilton Sun Feb 26, 2012 7:12 pm
            Re: evaluate loose pieces Miguel A. Ballicora Sun Feb 26, 2012 5:11 pm
                  Re: evaluate loose pieces Larry Kaufman Sun Feb 26, 2012 5:20 pm
                        Re: evaluate loose pieces Sam Hamilton Sun Feb 26, 2012 6:50 pm
                              Re: evaluate loose pieces Lucas Braesch Mon Feb 27, 2012 3:59 am
                                    Re: evaluate loose pieces Sam Hamilton Mon Feb 27, 2012 4:28 am
                  Re: evaluate loose pieces Ed Schroder Mon Feb 27, 2012 11:42 am
            Re: evaluate loose pieces Gerd Isenberg Sun Feb 26, 2012 7:56 pm
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
Enhanced with Moby Threads