Download presentation
Presentation is loading. Please wait.
Published byClement Mosley Modified over 9 years ago
1
Intelligent CS 5 ? HW 11 (1 problem !) M/T sections W/Th sections due Sunday, 11/14 at midnight due Monday, 11/15 at midnight Recitation for HW11 -- Friday 11/12, 8:00 am 2nd midterm exam -- this Friday, 11/12 Take-home, 2.0 hours, closed-book exam. Practice problems are online… exemption: > 95% HW Exam will be available this Friday; it’s due Sunday evening by 5:00 pm. Chess is the Drosophila of artificial intelligence. The computer that defeated Garry Kasparov 1997 www.cs.hmc.edu/~dodds/cs5 (top link) Today’s Lab: M-Z - Alexander Kronrod Games: computers vs. humans…
2
500 1200 2000 2800 Two-player games early programs ~ 1960’s Strategic thinking was considered essential for intelligence Computer Chess has a long history: Ranking beginner amateur world ranked world champion MacHack ( 1100 ) ~ 1967 MIT Deep Thought ~ 1989 Carnegie Mellon Slate ( 2070 ) ~ 1970’s Northwestern Deep Blue ~ 1996 IBM Deep Blue rematch ~ 1997 IBM
3
500 1200 2000 2800 Computers’ strategy… early programs ~ 1960’s Strategic thinking was considered essential for intelligence Computer Chess has a long history: Ranking beginner amateur world ranked world champion MacHack ( 1100 ) ~ 1967 MIT Deep Thought ~ 1989 Carnegie Mellon Slate ( 2070 ) ~ 1970’s Northwestern Deep Blue ~ 1996 IBM Deep Blue rematch ~ 1997 IBM 100’s of moves/sec 10,000’s of moves/sec 100,000,000 moves/sec 200,000,000 moves/sec how far ahead is this?
4
500 1200 2000 2800 Games’ Branching Factors early programs ~ 1960’s Branching Factor Estimates for different two-player games Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 On average, there are about 40 possible moves that a chess player can make from any board configuration… Ranking beginner amateur world ranked world champion MacHack ( 1100 ) ~ 1967 MIT Deep Thought ~ 1989 Carnegie Mellon Slate ( 2070 ) ~ 1970’s Northwestern Deep Blue ~ 1996 IBM Deep Blue rematch ~ 1997 IBM 0 Ply 1 Ply 2 Ply game tree for C4
5
500 1200 2000 2800 Games’ Branching Factors early programs ~ 1960’s Branching Factor Estimates for different two-player games Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 On average, there are about 40 possible moves that a chess player can make from any board configuration… Ranking beginner amateur world ranked world champion MacHack ( 1100 ) ~ 1967 MIT Deep Thought ~ 1989 Carnegie Mellon Slate ( 2070 ) ~ 1970’s Northwestern Deep Blue ~ 1996 IBM Deep Blue rematch ~ 1997 IBM 0 Ply 1 Ply 2 Ply
6
500 1200 2000 2800 Games’ Branching Factors early programs ~ 1960’s Branching Factor Estimates for different two-player games Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 On average, there are about 40 possible moves that a chess player can make from any board configuration… Ranking beginner amateur world ranked world champion MacHack ( 1100 ) ~ 1967 MIT Deep Thought ~ 1989 Carnegie Mellon Slate ( 2070 ) ~ 1970’s Northwestern Deep Blue ~ 1996 IBM Deep Blue rematch ~ 1997 IBM “solved” games computer-dominated human-dominated 0 Ply 1 Ply 2 Ply
7
Winning: Details public boolean winsFor(char ch) { for (int r=0 ; r<this.nrows-3 ; ++r) { for (int c=0 ; c<this.ncols-3 ; ++c) { if (this.data[r+0][c+0] == ch && this.data[r+1][c+1] == ch && this.data[r+2][c+2] == ch && this.data[r+3][c+3] == ch) { return true; } … same idea for vert., horiz., other diag. … return false; } which diagonals? which board piece? which curly braces?
8
public boolean winsFor(char ch) { for (int r=0 ; r<this.nRows-3 ; ++r) for (int c=0 ; c<this.nCols-3 ; ++c) if (this.data[r+0][c+0] == ch && this.data[r+1][c+1] == ch && this.data[r+2][c+2] == ch && this.data[r+3][c+3] == ch) return true; … same idea for vert., horiz., other diag. … return false; } Winning: Details (compact version)
9
Objects hide details! Class: Board Object: b b.winsFor(‘X’) b.addMove(3,‘X’) ‘X’ ‘O’ capabilities of b so that important things aren’t lost in the shuffle… b.removeMove(3) b.isOver() (the last 3 are new for this week) 0123456 b.clear()
10
Objects hide details! class CS5App { public static void main(String[] args) { H.pl("Hi! Welcome to Connect 4..."); H.pl("How many rows/columns ? (4-15)"); int R = H.ni(); int C = H.ni(); Board b = new Board(R,C); char player = 'X'; while (true) { b.print(); int c = H.ni(); // gets next move b.addMove(c,player); if (b.winsFor(player)) break; if (player == 'X') player = '0'; else player = 'X'; } // end of while } Hw10Hw11
11
Where we’re headed… Player playerForX Details (data and methods) Player playerForO Details (data and methods) 1 2 3 4 Ask what kind of players should play for X and O Create two objects of class Player with appropriate inputs Ask each of these objects to findScores for X and O and then breaktie s. See who wins! demo… what details are needed?
12
char checker Picture of a Player object Player int lookahead Player playerForX Player(char ch, int lk, int tbk) int tiebreakType void printScores(double[] s) double evaluate(Board b) double[] plyHuman(Board b) double[] ply0(Board b) int breaktie(double[] s) double[] findScores(Board b) Imagine if Board weren’t a Class… ! double[] ply1,2,3,4,N(Board b) methods
13
Player code private char checker; private int lookahead; private int tiebreakType; public Player(char ch, int la, int tbk) // constructor { } public char getChecker() // accessor “getter” method { } public char me() // short for getChecker() { } public char opp() // returns the opponent’s checker { } class Player {
14
Where we’re headed… Player playerForX Details (data and methods) Player playerForO Details (data and methods) 1 2 3 4 Ask what kind of players should play for X and O Create two objects of class Player with appropriate inputs Ask each of these objects to findScores for X and O and then breaktie s. See who wins! demo… what details are needed?
15
Hw10 class CS5App { public static void main(String[] args) { H.pl("Hi! Welcome to Connect 4..."); int R = H.ni(); int C = H.ni(); Board b = new Board(R,C); char player = 'X'; while (true) { b.print(); int uc = H.ni(); // user’s column b.addMove(uc,player); if (b.winsFor(player)) break; if (player == 'X') player = '0'; else player = 'X'; } // end of while } Hw11
16
Choosing a move 1) Find scores at appropriate lookahead… 2) Print the scores. 3) Break ties to determine the next move. ply0 : 0 ply of lookahead ply1 : 1 ply of lookahead ply2,3,4 : 2,3,4 ply of lookahead plyHuman : ask the user printScores : prints the scores to each column breaktie : chooses ONE maximum score findScores chooses one of these methods to run
17
ply0 class Player { // returns scores with no lookahead // public double[] ply0(Board b) { char checker int lookahead int tiebreakType this b
18
ply1 class Player { // returns scores with 1ply lookahead // public double[] ply1(Board b) { char checker int lookahead int tiebreakType this b Which column should have the best score? Lookahead 1 move Evaluate the results for each column Later, we’ll choose the best column to move…
19
Evaluating a board Assigns a score to any Board b Score for X Score for O Score for X Score for O Score for X Score for O 100.0 for a win -1.0 for an invalid move 0.0 for a loss 50.0 for a “tie” not possible in evaluate
20
evaluate class Player { // returns the appropriate score for b // remember: all of Player’s methods are available public double evaluate(Board b) { 100.0 for a win -1.0 for an invalid move 0.0 for a loss 50.0 for a “tie” Improvements? Write tournamentEvaluate for Ex. Cr.!
21
b 0-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 1-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 2-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 3-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 “Quiz” It is X’s move.. Compute the score that X would find for each column for each of these lookaheads: 6012345 no moves at all! X moves O moves X moves O moves X moves
22
class Player { private int tiebreakType; private int lookahead; private char checker; public int breaktie(double[] s) { double maxScore = getMax(s); /* assume getMax is already written */ if (this.tiebreakType == 2) /* random tie breaker is tiebreakType == 2 */ { } Write breaktie to return a randomly chosen best score (max score) from an array of scores named s.
23
‘X’ ‘O’ new‘X’ b Looking ahead 1 ply… (1) For each possible move (2) Add the column’s move (3) Evaluate the boards (4) Choose one of the best ‘X’ to move
24
‘X’ ‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Looking ahead 1 ply… ‘X’ to move (1) For each possible move (2) Add the column’s move
25
‘X’ ‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Looking ahead 1 ply… (1) For each possible move (2) Add the column’s move (3) Evaluate the boards ‘X’ to move NONE
26
‘X’ ‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Looking ahead 1 ply… 100.0 50.0 100.0 50.0 100.0 (1) For each possible move (2) Add the column’s move (3) Evaluate the boards ‘X’ to move NONE
27
‘X’ ‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Looking ahead 1 ply… 100.0 50.0 100.0 50.0 100.0 (1) For each possible move (2) Add the column’s move (3) Evaluate the boards (4) Choose one of the best ‘X’ to move NONE
28
ply1 public double[] ply1(Board b) {
29
Two-player games have been a key focus of AI as long as computers have been around… Strategic thinking = intelligence ? Humans and computers have different relative strengths in these games:
30
Two-player games have been a key focus of AI as long as computers have been around… Strategic thinking = intelligence ? computers good at looking ahead in the game to find winning combinations of moves Humans and computers have different relative strengths in these games: this week…
31
Two-player games have been a key focus of AI as long as computers have been around… Strategic thinking = intelligence ? humanscomputers good at evaulating the strength of a board for a player good at looking ahead in the game to find winning combinations of moves Humans and computers have different relative strengths in these games: this week… (extra credit)
32
How humans play games… - experts could reconstruct these perfectly - novice players did far worse… An experiment (by deGroot) was performed in which chess positions were shown to novice and expert players…
33
How humans play games… - experts could reconstruct these perfectly - novice players did far worse… Random chess positions (not legal ones) were then shown to the two groups - experts and novices did just as badly at reconstructing them! An experiment (by deGroot) was performed in which chess positions were shown to novice and expert players…
34
Looking further ahead … 0 ply: 2 ply:3 ply: random (but legal) choice of move ! (1) player will win (2) player will avoid losing (3) player will set up a win by forcing the opponent to avoid losing 1 ply: X ’s move
35
ply2 public double[] ply2(Board b) { depends on ply1 !
36
Extra Credit: the plyN method ! Lab this week You’ll need to write (and use) Last Names Problem 1: A Connect Four Player … Extra Credit: tournamentEvaluate & a C4 round-robin 2, 4, 6, and 8-ply lookahead for O will all produce different scores! Player(char ch, int lk, int tbk) char getChecker() char me() char opp() void printScores() double evaluate(Board b) double[] plyHuman(Board b) double[] ply0(Board b) int breaktie(double[] s) int go(Board b) (and the others listed on the HW) M-Z double[] findScores(Board b) http://www.cs.hmc.edu/~ccecka/C4/ O to move
37
b 0-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 1-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 2-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 3-ply scores for X: col 0col 1col 2col 3col 4col 5col 6 “Quiz” It is X’s move.. Compute the score that X would find for each column for each of these lookaheads: 6012345 no moves at all! X moves O moves X moves O moves X moves
38
class Player { private int tiebreakType; private int lookahead; private char checker; public int breaktie(double[] s) { double maxScore = getMax(s); /* assume getMax is already written */ if (this.tiebreakType == 2) /* random tie breaker is tiebreakType == 2 */ { } Write breaktie to return a randomly chosen best score (max score) from an array of scores named s.
39
‘X’ ‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Looking ahead 1 ply… 100.0 50.0 100.0 50.0 100.0 (1) For each possible move (2) Add the column’s move (3) Evaluate the boards (4) Choose one of the best ‘X’ to move NONE
40
Winning -- details public boolean winsFor(char ox) { for (int r=0 ; r<this.nRows-3 ; ++r) { for (int c=0 ; c<this.nCols-3 ; ++c) { if (this.data[r+0][c+0] == ox && this.data[r+1][c+1] == ox && this.data[r+2][c+2] == ox && this.data[r+3][c+3] == ox) { return true; } … same idea for vert., horiz., SW-NE diag. … return false; } | | | | | |X| | | | | | | |O|X|O| | | | | |O|X|X| |O|O| |X|O|O|X|X|O|X| --------------- 0 1 2 3 4 5 6. finds this diagonal: complete HW10PR2 solutions at http://www.cs.hmc.edu/courses/2002/fall/cs5/week_10/sols.html
41
static static methods belong to a class, not an object H.pl(“I’m a static method”); // lots double av = averageArray(stocks); // HW 7 int syl = numSyllables(word); // HW 6 double d = Math.sqrt(343.0); If the static method is in another class, the class name is needed!
42
opp() and ?: class Player { private char checker; // data member public char opp() // returns opponent’s checker { } ? : is shorthand for if … else …, but only for deciding between values if else
43
addMove Class: Board Object: b b.addMove(3,‘X’) ‘X’ ‘O’ changes b by adding checker ‘X’ into row 3 new‘X’ b before b after
44
Adding a move without changing b ! b before b after Board nextb = b.newAddMove(3,‘X’); a new Board with the move added
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.