440 likes | 542 Views
Today’s Lab:. Intelligent CS 5 ?. M-Z. Chess is the Drosophila of artificial intelligence. - Alexander Kronrod. The computer that defeated Garry Kasparov 1997. Games: computers vs. humans…. due Sunday, 11/14 at midnight. M/T sections. HW 11 ( 1 problem ! ).
E N D
Today’s Lab: Intelligent CS 5 ? M-Z Chess is the Drosophila of artificial intelligence. - Alexander Kronrod The computer that defeated Garry Kasparov 1997 Games: computers vs. humans… due Sunday, 11/14 at midnight M/T sections • HW 11 (1 problem !) due Monday, 11/15 at midnight W/Th sections Recitation for HW11 -- Friday 11/12, 8:00 am • 2nd midterm exam -- this Friday, 11/12 exemption: > 95% HW Take-home, 2.0 hours, closed-book exam. Practice problems are online… www.cs.hmc.edu/~dodds/cs5 (top link) Exam will be available this Friday; it’s due Sunday evening by 5:00 pm.
Two-player games • Strategic thinking was considered essential for intelligence • Computer Chess has a long history: Ranking early programs ~ 1960’s beginner 500 amateur 1200 MacHack (1100) ~ 1967 MIT world ranked Slate (2070) ~ 1970’s Northwestern 2000 Deep Thought ~ 1989 Carnegie Mellon Deep Blue ~ 1996 IBM world champion 2800 Deep Blue rematch ~ 1997 IBM
Computers’ strategy… • Strategic thinking was considered essential for intelligence • Computer Chess has a long history: Ranking early programs ~ 1960’s beginner 500 100’s of moves/sec amateur 1200 10,000’s of moves/sec MacHack (1100) ~ 1967 MIT 100,000,000 moves/sec 200,000,000 moves/sec world ranked Slate (2070) ~ 1970’s Northwestern 2000 Deep Thought ~ 1989 Carnegie Mellon how far ahead is this? Deep Blue ~ 1996 IBM world champion 2800 Deep Blue rematch ~ 1997 IBM
Games’ Branching Factors • On average, there are about 40 possible moves that a chess player can make from any board configuration… game tree for C4 0 Ply Ranking 1 Ply early programs ~ 1960’s beginner 500 2 Ply amateur 1200 MacHack (1100) ~ 1967 MIT Branching Factor Estimates for different two-player games world ranked Slate (2070) ~ 1970’s Northwestern Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 2000 Deep Thought ~ 1989 Carnegie Mellon Deep Blue ~ 1996 IBM world champion 2800 Deep Blue rematch ~ 1997 IBM
Games’ Branching Factors • On average, there are about 40 possible moves that a chess player can make from any board configuration… 0 Ply Ranking 1 Ply early programs ~ 1960’s beginner 500 2 Ply amateur 1200 MacHack (1100) ~ 1967 MIT Branching Factor Estimates for different two-player games world ranked Slate (2070) ~ 1970’s Northwestern Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 2000 Deep Thought ~ 1989 Carnegie Mellon Deep Blue ~ 1996 IBM world champion 2800 Deep Blue rematch ~ 1997 IBM
Games’ Branching Factors • On average, there are about 40 possible moves that a chess player can make from any board configuration… 0 Ply Ranking 1 Ply early programs ~ 1960’s beginner 500 2 Ply amateur 1200 MacHack (1100) ~ 1967 MIT Branching Factor Estimates for different two-player games world ranked Slate (2070) ~ 1970’s Northwestern Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 2000 “solved” games Deep Thought ~ 1989 Carnegie Mellon computer-dominated Deep Blue ~ 1996 IBM world champion 2800 human-dominated Deep Blue rematch ~ 1997 IBM
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?
Winning: Details (compact version) 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; }
Objects hide details! so that important things aren’t lost in the shuffle… ‘X’ ‘O’ Class: Board Object: b 0 1 2 3 4 5 6 b.addMove(3,‘X’) b.winsFor(‘X’) capabilities of b b.removeMove(3) b.clear() (the last 3 are new for this week) b.isOver()
Hw10 Hw11 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 } } Objects hide details!
Player Where we’re headed… Ask what kind of players should play for X and O 1 Details Player playerForX (data and methods) Create two objects of class Player with appropriate inputs 2 Ask each of these objects to findScores for X and O and then breakties. Details 3 Player playerForO (data and methods) 4 See who wins! demo… what details are needed?
Player Picture of a Player object int lookahead int tiebreakType char checker Player playerForX Player(char ch, int lk, int tbk) double[] findScores(Board b) double[] plyHuman(Board b) methods double[] ply0(Board b) double[] ply1,2,3,4,N(Board b) double evaluate(Board b) void printScores(double[] s) int breaktie(double[] s) Imagine if Board weren’t a Class… !
class Player { 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 { }
Player Where we’re headed… Ask what kind of players should play for X and O 1 Details Player playerForX (data and methods) Create two objects of class Player with appropriate inputs 2 Ask each of these objects to findScores for X and O and then breakties. Details 3 Player playerForO (data and methods) 4 See who wins! demo… what details are needed?
Hw10 Hw11 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 } }
Choosing a move 1) Find scores at appropriate lookahead… ply0: 0 ply of lookahead ply1: 1 ply of lookahead findScores ply2,3,4: 2,3,4 ply of lookahead chooses one of these methods to run plyHuman: ask the user 2) Print the scores. printScores: prints the scores to each column 3) Break ties to determine the next move. breaktie: chooses ONE maximum score
ply0 int tiebreakType class Player { // returns scores with no lookahead // public double[] ply0(Board b) { int lookahead char checker this b
ply1 int tiebreakType class Player { // returns scores with 1ply lookahead // public double[] ply1(Board b) { int lookahead char checker this Which column should have the best score? • Lookahead 1 move • Evaluate the results for each column b • Later, we’ll choose the best column to move…
Evaluating a board 100.0 for a win 50.0 for a “tie” 0.0 for a loss -1.0 for an invalid move Assigns a score to any Board b not possible in evaluate Score for X Score for X Score for X Score for O Score for O Score for O
100.0 for a win evaluate 50.0 for a “tie” 0.0 for a loss -1.0 for an invalid move class Player { // returns the appropriate score for b // remember: all of Player’s methods are available public double evaluate(Board b) { Improvements? Write tournamentEvaluate for Ex. Cr.!
b “Quiz” It is X’s move. . Compute the score that X would find for each column for each of these lookaheads: 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 0-ply scores for X: no moves at all! col 0 col 1 col 2 col 3 col 4 col 5 col 6 1-ply scores for X: X moves col 0 col 1 col 2 col 3 col 4 col 5 col 6 2-ply scores for X: X moves O moves col 0 col 1 col 2 col 3 col 4 col 5 col 6 3-ply scores for X: X moves O moves X moves
Write breaktie to return a randomly chosen best score (max score) from an array of scores named s. 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 */ { } } }
‘X’ Looking ahead 1 ply… ‘O’ new‘X’ b (1) For each possible move (2) Add the column’s move (3) Evaluate the boards (4) Choose one of the best ‘X’ to move
‘X’ Looking ahead 1 ply… ‘O’ new‘X’ b (1) For each possible move (2) Add the column’s move ‘X’ to move Col 6 Col 0 Col 5 Col 1 Col 2 Col 3 Col 4
‘X’ Looking ahead 1 ply… ‘O’ new‘X’ b (1) For each possible move (2) Add the column’s move (3) Evaluate the boards ‘X’ to move Col 6 Col 0 NONE Col 5 Col 1 Col 2 Col 3 Col 4
‘X’ Looking ahead 1 ply… ‘O’ new‘X’ b (1) For each possible move (2) Add the column’s move (3) Evaluate the boards ‘X’ to move 100.0 Col 6 Col 0 NONE -1.0 Col 5 Col 1 Col 2 Col 3 Col 4 50.0 50.0 100.0 100.0 100.0
‘X’ Looking ahead 1 ply… ‘O’ new‘X’ b (1) For each possible move (2) Add the column’s move (3) Evaluate the boards (4) Choose one of the best ‘X’ to move 100.0 Col 6 Col 0 NONE -1.0 Col 5 Col 1 Col 2 Col 3 Col 4 50.0 50.0 100.0 100.0 100.0
ply1 public double[] ply1(Board b) {
? Strategic thinking = intelligence Two-player games have been a key focus of AI as long as computers have been around… Humans and computers have different relative strengths in these games:
? Strategic thinking = intelligence Two-player games have been a key focus of AI as long as computers have been around… Humans and computers have different relative strengths in these games: computers good at looking ahead in the game to find winning combinations of moves this week…
? Strategic thinking = intelligence Two-player games have been a key focus of AI as long as computers have been around… Humans and computers have different relative strengths in these games: computers humans good at looking ahead in the game to find winning combinations of moves good at evaulating the strength of a board for a player (extra credit) this week…
How humans play games… An experiment (by deGroot) was performed in which chess positions were shown to novice and expert players… - experts could reconstruct these perfectly - novice players did far worse…
How humans play games… An experiment (by deGroot) was performed in which chess positions were shown to novice and expert players… - 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!
Looking further ahead … random (but legal) choice of move ! 0 ply: 1 ply: 2 ply: 3 ply: X’s move X’s move X’s move (1)player will win (2)player will avoid losing (3)player will set up a win by forcing the opponent to avoid losing
ply2 public double[] ply2(Board b) { depends on ply1 !
Lab this week M-Z Last Names • Problem 1: A Connect Four Player… Player(char ch, int lk, int tbk) char getChecker() char me() void printScores() char opp() int go(Board b) double evaluate(Board b) You’ll need to write (and use) double[] plyHuman(Board b) double[] ply0(Board b) int breaktie(double[] s) double[] findScores(Board b) (and the others listed on the HW) • Extra Credit: tournamentEvaluate & a C4 round-robin http://www.cs.hmc.edu/~ccecka/C4/ O to move 2, 4, 6, and 8-ply lookahead for O will all produce different scores! • Extra Credit: the plyN method !
b “Quiz” It is X’s move. . Compute the score that X would find for each column for each of these lookaheads: 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 0-ply scores for X: no moves at all! col 0 col 1 col 2 col 3 col 4 col 5 col 6 1-ply scores for X: X moves col 0 col 1 col 2 col 3 col 4 col 5 col 6 2-ply scores for X: X moves O moves col 0 col 1 col 2 col 3 col 4 col 5 col 6 3-ply scores for X: X moves O moves X moves
Write breaktie to return a randomly chosen best score (max score) from an array of scores named s. 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 */ { } } }
‘X’ Looking ahead 1 ply… ‘O’ new‘X’ b (1) For each possible move (2) Add the column’s move (3) Evaluate the boards (4) Choose one of the best ‘X’ to move 100.0 Col 6 Col 0 NONE -1.0 Col 5 Col 1 Col 2 Col 3 Col 4 50.0 50.0 100.0 100.0 100.0
Winning -- details complete HW10PR2 solutions athttp://www.cs.hmc.edu/courses/2002/fall/cs5/week_10/sols.html 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; } finds this diagonal: | | | | | | | | | | | | | | | | | |X| | | | | | | |O|X|O| | | | | |O|X|X| |O|O| |X|O|O|X|X|O|X| --------------- 0 1 2 3 4 5 6.
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!
opp() and ?: ? : is shorthand for if … else …, but only for deciding between values else if class Player { private char checker; // data member public char opp() // returns opponent’s checker { }
addMove ‘X’ new‘X’ ‘O’ Class: Board Object: b changes b by adding checker ‘X’ into row 3 b.addMove(3,‘X’) b before b after
Adding a move without changing b ! b before b after a new Board with the move added Board nextb = b.newAddMove(3,‘X’);