Intelligent CS 5 ?
• HW 11 (1 problem !) M/T sections
W/Th sections
due Sunday, 11/14 at midnight
due Monday, 11/15 at midnightRecitation for HW11 -- Friday 11/12, 8:00 am
• 2nd midterm exam -- this Friday, 11/12Take-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…
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
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?
500
1200
2000
2800
Games’ Branching Factors
early programs ~ 1960’s
Branching Factor Estimatesfor 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
500
1200
2000
2800
Games’ Branching Factors
early programs ~ 1960’s
Branching Factor Estimatesfor 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
500
1200
2000
2800
Games’ Branching Factors
early programs ~ 1960’s
Branching Factor Estimatesfor 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
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?
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)
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)
0 1 2 3 4 5 6
b.clear()
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 }}
Hw10 Hw11
Where we’re headed…
Player
PlayerplayerForX
Details(data and methods)
PlayerplayerForO
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 breakties.
See who wins!
demo…what details are
needed?
charchecker
Picture of a Player object
Player
intlookaheadPlayer
playerForXPlayer(char ch, int lk, int tbk)
inttiebreakType
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
Player codeprivate 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{
Where we’re headed…
Player
PlayerplayerForX
Details(data and methods)
PlayerplayerForO
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 breakties.
See who wins!
demo…what details are
needed?
Hw10class 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
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
findScoreschooses one of these
methods to run
ply0class Player{ // returns scores with no lookahead // public double[] ply0(Board b) {
charchecker
intlookahead
inttiebreakType
this
b
ply1class Player{ // returns scores with 1ply lookahead // public double[] ply1(Board b) {
charchecker
intlookahead
inttiebreakType
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…
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
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.!
b
0-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
1-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
2-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
3-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
“Quiz”It is X’s move. .
Compute the score that X would find for each
column for each of these lookaheads:
60 1 2 3 4 5
no moves at all!
X moves
X movesO moves
X movesO movesX moves
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.
‘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
‘X’‘O’new‘X’
Col 6
Col 5
Col 4Col 3Col 2
Col 1
Col 0
b
Looking ahead 1 ply…
‘X’ to move
(1) For each possible move
(2) Add the column’s move
‘X’‘O’new‘X’
Col 6
Col 5
Col 4Col 3Col 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
‘X’‘O’new‘X’
Col 6
Col 5
Col 4Col 3Col 2
Col 1
Col 0
b
Looking ahead 1 ply…
100.0
50.0
100.0100.0
50.0
100.0
(1) For each possible move
(2) Add the column’s move
(3) Evaluate the boards ‘X’ to move
NONE-1.0
‘X’‘O’new‘X’
Col 6
Col 5
Col 4Col 3Col 2
Col 1
Col 0
b
Looking ahead 1 ply…
100.0
50.0
100.0100.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-1.0
ply1public double[] ply1(Board b){
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:
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…
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)
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…
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…
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 X’s move X’s move
ply2public double[] ply2(Board b){
depends on ply1 !
•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
b
0-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
1-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
2-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
3-ply scores for X:col 0 col 1 col 2 col 3 col 4 col 5 col 6
“Quiz”It is X’s move. .
Compute the score that X would find for each
column for each of these lookaheads:
60 1 2 3 4 5
no moves at all!
X moves
X movesO moves
X movesO movesX moves
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.
‘X’‘O’new‘X’
Col 6
Col 5
Col 4Col 3Col 2
Col 1
Col 0
b
Looking ahead 1 ply…
100.0
50.0
-1.0
100.0100.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
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
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 ?:
class Player{ private char checker; // data member
public char opp() // returns opponent’s checker {
}
? : is shorthand for if … else …, but only for deciding between
valuesif else
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
Adding a move without changing b !
b before
b after
Board nextb = b.newAddMove(3,‘X’);
a new Board with the move added
Top Related