C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should...

31
C SC 483 Chess and AI: Computation and Cognition Lecture 7 October 8th

Transcript of C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should...

Page 1: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

C SC 483Chess and AI: Computation

and CognitionLecture 7

October 8th

Page 2: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Checkpoint

• Task 3 Completion• By now your

program should:– compute the usual

legal moves for allpieces

– also handle pawnpromotion

• Also have a notionof move history:– handle castling

• (K and R not moved)• condition: can’t move

through attackedsquares

– handle en passant• (one chance only)

Page 3: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Castling• highlighted as a king move

– need to compute attacks on intervening squares as well as check

Page 4: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Attack Computation• 4 bitboards maintained in

parallel– bb: regular bitboard– rbb: 90 degree rotated bitboard– ldbb: leading diagonal bitboard– tdbb: trailing diagonal bitboard– R: black rook bitboard– rR: black root rotated bitboard– Q, rQ, ldQ, tdQ: black queen

regular, rotated, leading diagonal,trailing diagonal bitboards

– etc.

• Example pseudo code:proc all_moves_black {} {

bb = allP_moves | rank_moves(R) |rank_moves(Q) | k_moves(K) |n_moves(N)

rbb = file_moves(rR) | file_moves(rQ)ldbb = ld_moves(ldB] | ld_moves(ldQ)tdbb = td_moves(tdB) |

td_moves(tdQ) return [list bb rbb ldbb tdbb]}

Page 5: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Castling• intersect squares of interest

with the bitboards returned byall_moves_black to see ifcastling is possible

• Example• (king side castling for white)• can’t castle if any of the

following are non-zero1. 0x0E00000000000000 & bb2. 0x1010100 & rbb3. 0x0021040000000000 & ldbb4. 0x5200000000000000 & tdbb

0x0E = 0 0 0 0 1 1 1 0

all_moves_black highlighted

Page 6: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Task 4: Evaluation Function• Task:

design and implement a positionevaluation function– It should return a positive number

when white is up and a negativenumber when black is up, zero ifequal

– course gets really interesting here– (formal bitboard stuff done)– evaluation is where computation

and cognition meet:– and we have expert analysis

from Levon

• Due Date– 2 weeks (10/22)

• Presentation requirement– one (or 2) powerpoint slides

• your evaluation function– 2nd powerpoint slide

• table of your results forLevon’s 16 boards

• (to be described later inthis lecture)

– email your slides to me

Page 7: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Evaluation Function• Computes

– How good is this position?– i.e. is it likely to lead to a win?

• Desiderata– cheap, easy to compute– will be used over and over ...– accurate

• Simplest method:– from lecture 1: value of pieces– ∑k piece_value(k)– over all pieces on the board– + white is up/– black is up– tree search will inform us if a temporary

loss in material can be made up later(gambit)

• Example:– piece_value(q) 9– piece_value(Q) -9– piece_value(r) 5– piece_value(R) -5– piece_value(b) 3.5– piece_value(B) -3.5– piece_value(n) 3.5– piece_value(N) -3.5– piece_value(p) 1– piece_value(P) -1– piece_value(k) 0– piece_value(K) 0 actually ∞

Page 8: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

Other easily computablefunctions

• Mobilitya measure of how many

moves are open to you– Simple idea:

• compute the possiblemove bitboard

• count the number of bitsset

– Disadvantage:• typically: lots of legal

moves available, mostdo not advance yourcause

• Bad Bishopa bishop which is hemmed in by the

player's own pawns– computation

• diagonal bitboard• rank lookup for occupancy

returns attack squares• get a count of the # of 1s in

that byte (a lookup function)– advantage

this is computed anyway when welook up the possible legalmoves for a bishop

Page 9: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

References

• http://www.gamedev.net/reference/articles/article1208.aspboard control– In chess, a side

controls a square if ithas more piecesattacking it than theopponent.

• computation– we need to compute

squares attackedanyway

– instead of simplyORing attack bitboardsfor each type of pieceinto a single attackbitboard for each color

– we could sum attackbitboards

Page 10: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

References• http://web.archive.org/web/200

41103012847/http://myweb.cableone.net/christienolan/coach/evaluating.htm

• pawn structure– isolated pawn

• rotated pawn bitboard(file)

– doubled pawns• check pawn rotated

bitboard for > 1occupancy by byte

– backward pawn• A pawn that is behind

the pawns of the samecolor on the adjacentfiles and that cannot beadvanced with thesupport of anotherpawn.

• scan rank bitboardbyte by byte

• check if square infront of last pawn isattacked by opponent

Page 11: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

References• http://web.archive.org/web

/20041103012847/http://myweb.cableone.net/christienolan/coach/evaluating.htm

• principally designed forhuman evaluation

• not all keys are easy andcheap to program

• 7 keys– 1.Material– 2.King Safety– 3.Piece Mobility– 4.Pawn Structure– 5.Space– 6.The Center– 7.Threats

Page 12: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

References• http://alumni.imsa.edu/~stendahl/comp/t

xt/gnuchess.txtPAWNS• The material value of a pawn is 100

points.• Isolated pawns get a penalty depending

on which file they occupy:(12,14,16,20,20,16,14,12) for files (a..h).

• Doubled pawns (which are not alsoisolated) get a penalty of 12 points.

• Backward pawns (defined simply as notbeing defended by a pawn with thesquare in front also not defended by a apawn) are penalized 6 points.

• A 4 point penalty is also invokedfor each attack by the opponent toa backward pawn and for abackward pawn on a half-open file.

• Pawn Advancement in the centre isgiven a bonus of about 4 points perrank in the opening increasing toabout 8 points per rank in theending. Advancement on theedges is given a lower bonus.

• Pawns on the e and d files and onthe 2nd rank are given a 10 pointpenalty. An additional penalty of 15points is invoked if these pawnsare also blocked.

Page 13: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

References• Pawns within 2 squares of the king

are given a 10 point bonus.• Passed pawns are given a bonus

for increasing rank which is afunction of stage of the game andof whether the opponent blocks orattacks one or more squares infront of the pawn or if theopponents king is in the square ofthe pawn. This bonus ranges fromabout 15 points for a pawn on thesecond rank up to about 300 pointsfor a passed pawn on the 7th rankwhich can't be stopped fromqueening.

KNIGHTS• The material value of a knight is 330

points.• The main heuristic for knights is a bonus

for proximity to the centre. This variesfrom 0 points in the corners to 30 pointsin the centre. Knights are also given abonus for being within 2 squares of eachenemy piece. This bonus is a function ofthe stage of the game, equalling 4 pointsin the end game.

• A penalty of 1 point per square is givenfor distance from either king.

• A bonus of up to 8 points (depends onstage) is given for knights which can't bedriven away by enemy pawns.

Page 14: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

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

Page 15: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

EVALUATIONboard positions for Task 4expert analysis from Levon

Page 16: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(1) WHITE WINS(Endgame): +-Reason: A King and a Queen can checkmate the opposing King even against the

best possible defense:Black can either continue the play hoping for a stalemate or that White does not

know how to do execute this elementary win or just resign.

Page 17: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 Compensationfor it.

Black has some reason to continue the play, as there are many pieces still left and White maymiss some tactical shots or have a poor technique.

Page 18: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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

Page 19: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(4) Black should Win ( Middlegame ): -+Reason: Black has just announced a Royal Fork and is winning a Queen by force. He will

be up in material significantly.

Page 20: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 notwin or it may be a theoretical draw, especially if the pawns are traded. For example, it is

nearly impossible to win with a King + Rook vs. King + Knight.

Page 21: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 pinnedand is weak, White has an outpost on e5, more space and much better coordinated pieces.

Black may survive or may not or if survives, may end up losing some material.

Page 22: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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.

Page 23: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 zugzwang or checkmate.

Page 24: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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.

Page 25: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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’sadvances. At some point the Knight might become a strong piece too and create problems for

white.

Page 26: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 andactive pieces.

Page 27: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 ifBlack 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.

Page 28: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(13) Black is slightly better in the Middlegame:=/+

Reason: Black is better developed, has more control of the center, and has sometargets such as Nc3 and possible Qh4 attacks.

Page 29: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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.

Page 30: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(15) Equal Endgame: =Reason: Even though it seems complicated, both sides will be able to promote theirpawns to Queen, and be taken by the opposing Rooks. The result is a King vs. King

= Draw

Page 31: C SC 483 Chess and AI: Computation and Cognitionsandiway/CSC483/lecture7.pdf · (2) WHITE should WIN (Middlegame): +-White has a winning position. Reason: He is up ( ahead ) by a

(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 tacticalshots due to openness of the game. There are no exact checkmates in sight and the

further calculation is extremely complicated.