C SC 483 and AI: Computation and Cognition

Lecture 7 October 8th Checkpoint

• Task 3 Completion • Also have a notion • By now your of move history: program should: – handle – compute the usual • (K and R not moved) legal moves for all • condition: can’t move through attacked pieces squares – also handle – handle • (one chance only) Castling

• highlighted as a move – need to compute attacks on intervening squares as well as Attack Computation

• 4 bitboards maintained in • Example pseudo code: parallel proc all_moves_black {} { – bb: regular bitboard bb = allP_moves | rank_moves(R) | – rbb: 90 degree rotated bitboard rank_moves(Q) | k_moves(K) | – ldbb: leading diagonal bitboard n_moves(N) – tdbb: trailing diagonal bitboard rbb = file_moves(rR) | file_moves(rQ) – R: black bitboard ldbb = ld_moves(ldB] | ld_moves(ldQ) – rR: black root rotated bitboard tdbb = td_moves(tdB) | – Q, rQ, ldQ, tdQ: black td_moves(tdQ) regular, rotated, leading diagonal, return [list bb rbb ldbb tdbb] trailing diagonal bitboards } – etc. Castling

all_moves_black highlighted • intersect squares of interest with the bitboards returned by all_moves_black to see if castling is possible • Example • (king side castling for white) • can’t castle if any of the following are non-zero 1. 0x0E00000000000000 & bb 2. 0x1010100 & rbb 3. 0x0021040000000000 & ldbb 4. 0x5200000000000000 & tdbb

0x0E = 0 0 0 0 1 1 1 0 Task 4: Evaluation Function

• Task: • Due Date design and implement a position – 2 weeks (10/22) evaluation function • Presentation requirement – It should return a positive number when white is up and a negative – one (or 2) powerpoint slides number when black is up, zero if • your evaluation function equal – 2nd powerpoint slide – course gets really interesting here • table of your results for – (formal bitboard stuff done) Levon’s 16 boards – evaluation is where computation • (to be described later in and cognition meet: this lecture) – and we have expert analysis – email your slides to me from Levon Evaluation Function

• Computes • Example: – How good is this position? – piece_value(q) 9 – i.e. is it likely to lead to a win? – piece_value(Q) -9 • Desiderata – piece_value(r) 5 – cheap, easy to compute – piece_value(R) -5 – will be used over and over ... – accurate – piece_value(b) 3.5 • Simplest method: – piece_value(B) -3.5 – from lecture 1: value of pieces – piece_value(n) 3.5

– ∑k piece_value(k) – piece_value(N) -3.5 – over all pieces on the board – piece_value(p) 1 – + white is up/– black is up – tree search will inform us if a temporary – piece_value(P) -1 loss in material can be made up later – piece_value(k) 0 () – piece_value(K) 0 actually ∞ Other easily computable functions • Mobility • Bad a bishop which is hemmed in by the a measure of how many player's own pawns moves are open to you – computation – Simple idea: • diagonal bitboard • compute the possible • rank lookup for occupancy returns attack squares move bitboard • get a count of the # of 1s in • count the number of bits that byte (a lookup function) set – advantage this is computed anyway when we – Disadvantage: look up the possible legal • typically: lots of legal moves for a bishop moves available, most do not advance your cause References

• http://www.gamedev.net/ • computation reference/articles/article1 – we need to compute 208.asp squares attacked board control anyway – In chess, a side – instead of simply controls a square if it ORing attack bitboards has more pieces for each type of piece attacking it than the into a single attack opponent. bitboard for each color – we could sum attack bitboards References

• http://web.archive.org/web/200 – 41103012847/http://myweb.cab leone.net/christienolan/coach/e • A pawn that is behind valuating.htm the pawns of the same color on the adjacent • files and that cannot be – advanced with the support of another • rotated pawn bitboard pawn. (file) • scan rank bitboard – byte by byte • check pawn rotated • check if square in bitboard for > 1 occupancy by byte front of last pawn is attacked by opponent References

• http://web.archive.org/web • 7 keys /20041103012847/http://m yweb.cableone.net/christi – 1.Material enolan/coach/evaluating.h – 2.King Safety tm – 3.Piece Mobility – 4.Pawn Structure • principally designed for – 5.Space human evaluation – 6.The Center • not all keys are easy and – 7.Threats cheap to program References

• http://alumni.imsa.edu/~stendahl/comp/t • A 4 point penalty is also invoked xt/gnuchess.txt for each attack by the opponent to PAWNS a backward pawn and for a • The material value of a pawn is 100 backward pawn on a half-. points. • Pawn Advancement in the centre is • Isolated pawns get a penalty depending given a bonus of about 4 points per on which file they occupy: rank in the opening increasing to (12,14,16,20,20,16,14,12) for files (a..h). about 8 points per rank in the • Doubled pawns (which are not also ending. Advancement on the isolated) get a penalty of 12 points. edges is given a lower bonus. • Backward pawns (defined simply as not being defended by a pawn with the • Pawns on the e and d files and on square in front also not defended by a a the 2nd rank are given a 10 point pawn) are penalized 6 points. penalty. An additional penalty of 15 points is invoked if these pawns are also blocked. References

• Pawns within 2 squares of the king KNIGHTS are given a 10 point bonus. • The material value of a is 330 • Passed pawns are given a bonus points. for increasing rank which is a • The main heuristic for knights is a bonus function of stage of the game and for proximity to the centre. This varies of whether the opponent blocks or from 0 points in the corners to 30 points attacks one or more squares in in the centre. Knights are also given a front of the pawn or if the bonus for being within 2 squares of each opponents king is in the square of enemy piece. This bonus is a function of the pawn. This bonus ranges from the stage of the game, equalling 4 points about 15 points for a pawn on the in the end game. second rank up to about 300 points • A penalty of 1 point per square is given for a on the 7th rank for distance from either king. which can't be stopped from queening. • A bonus of up to 8 points (depends on stage) is given for knights which can't be driven away by enemy pawns. Tradeoffs

• Chess is time-limited • You only have so much CPU resources to work with • Complex evaluation function may take away time better spent on the tree search (depth) • Seek a good balance • Machine Learning issues: – learn the balance? – learn which factors are most important when – you could wire up two versions of your program to play against one another to figure out which is better EVALUATION

board positions for Task 4 expert analysis from Levon (1) WHITE WINS(Endgame): +- Reason: A King and a Queen can the opposing King even against the best possible defense: Black can either continue the play hoping for a or that White does not know how to do execute this elementary win or just resign. (2) WHITE should WIN (Middlegame): +- White has a winning position. Reason: He is up ( ahead ) by a Rook and 2 Pawns ( 7 pts ) and Black has no for it. Black has some reason to continue the play, as there are many pieces still left and White may miss some tactical shots or have a poor technique. (3) Black should Win ( Endgame ): -+ Reason: Black can surround White’s lonely pawn with the remaining 2 pieces and win it. The resulting position of King vs King+Rook is a theoretical Win for Black. White probably should not give up, as it is not super easy to checkmate with King+Rook (4) Black should Win ( Middlegame ): -+ Reason: Black has just announced a Royal and is winning a Queen by force. He will be up in material significantly. (5) White has a very good chance to win ( Endgame ): +/- Reason: White is up in points (2 pts) but there are many positions where white may not win or it may be a theoretical , especially if the pawns are traded. For example, it is nearly impossible to win with a King + Rook vs. King + Knight. (6) White has a huge positional advantage (Middlegame):+/- Reason: White has an attack on the Black King, the h7 pawn is very weak, the Nf6 is pinned and is weak, White has an on e5, more space and much better coordinated pieces. Black may survive or may not or if survives, may end up losing some material. (7) White has a large endgame advantage: +/- Reason: White has much more space, much better pieces, the Ng8 and Be8 have no real moves. Problem: How do we win this? All the pawns are protected, there are no passed pawns, only 3 pawns left and the Black king is relatively safe. (8) Black has Material Advantage in Endgame: -/+ Reason: Rook is much stronger than a Knight. Problem: There are no pawns left, and it is unclear how Black can win any of the knights, put white in or checkmate. (9) Black has a large Middlegame advantage: -/+ Reason: Black is a healthy pawn up, well-developed as well as has menacing bishops aimed at white’s King side.. White does not have much of compensation. Problem: The game is not over, white has many hidden chances and Black is only 1 pawn up. (10) White is slightly better:(Endgame) +/= Reason: White has 2 Bishops, each piece better than the black counterpart, Black Bishop is on the same color as its pawns. Problem: Black has all the weaknesses covered, all pawns protected and is ready for white’s advances. At some point the Knight might become a strong piece too and create problems for white. (11) White is slightly better( Middlegame): +/= Reason: Everything being close to equal ( Development, space, material_, white has more possibilities on the King side, e5 pawn is weaker and white has slightly better coordinated and active pieces. (12) Black is slightly better (Endgame) =/+ Reason: Black King and Rook are more active and hit and cover more squares than their counterparts. White also has some weak pawns. Problem: How do we win it? If Black pushes the King side pawns, we just trade pawns, and if Black goes with the King to the Queen side to win the a2 pawn, Black’s own king side gets weak. Black will either play a very long game here, trying to tire White out, or after few tries will offer a draw. (13) Black is slightly better in the Middlegame: =/+ Reason: Black is better developed, has more control of the center, and has some targets such as Nc3 and possible Qh4 attacks. (14) Equal Middlegame: = Reason: Symmetrical position and set-up.. Both sides have the same possibilities. People will either quickly agree to a draw or battle it out. It will most likely end up in a draw between equal players anyways. (15) Equal Endgame: = Reason: Even though it seems complicated, both sides will be able to promote their pawns to Queen, and be taken by the opposing Rooks. The result is a King vs. King = Draw (16) Unclear: Reason: It is chaotic, there is material imbalance, both sides are attacking the opponent’s King, Every piece is doing something and there are numerous tactical shots due to openness of the game. There are no exact in sight and the further calculation is extremely complicated.