Download Lec11

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Artificial intelligence in video games wikipedia , lookup

Minimax wikipedia , lookup

Computer Go wikipedia , lookup

Transcript
Today’s
Lab:
Intelligent CS 5 ?
M-Z
Chess is the Drosophila of
artificial intelligence.
- Alexander Kronrod
The computer that
defeated Garry Kasparov
Games: computers vs. humans…
• HW 11 (1 problem !)
Recitation for HW11 -- Friday 11/12, 8:00 am
1997
due Sunday, 11/14 at midnight
due Monday, 11/15 at midnight
M/T sections
W/Th sections
• 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
beginner
early programs ~ 1960’s
500
amateur
1200
MacHack (1100) ~ 1967 MIT
world ranked
2000
Slate (2070) ~ 1970’s Northwestern
Deep Thought ~ 1989 Carnegie Mellon
Deep Blue ~ 1996
2800
IBM
world champion
Deep Blue rematch ~ 1997 IBM
Computers’ strategy…
• Strategic thinking was considered essential for intelligence
• Computer Chess has a long history:
Ranking
beginner
early programs ~ 1960’s
500
100’s of moves/sec
amateur
1200
MacHack (1100) ~ 1967 MIT
10,000’s of moves/sec
100,000,000 moves/sec
200,000,000 moves/sec
world ranked
2000
Slate (2070) ~ 1970’s Northwestern
Deep Thought ~ 1989 Carnegie Mellon
how far ahead is this?
Deep Blue ~ 1996
2800
IBM
world champion
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
game tree
for C4
1 Ply
beginner
early programs ~ 1960’s
500
2 Ply
amateur
1200
MacHack (1100) ~ 1967 MIT
Branching Factor Estimates
for different two-player games
world ranked
2000
Slate (2070) ~ 1970’s Northwestern
Deep Thought ~ 1989 Carnegie Mellon
Deep Blue ~ 1996
2800
IBM
world champion
Deep Blue rematch ~ 1997 IBM
Tic-tac-toe
4
Connect Four
7
Checkers
10
Othello
30
Chess
40
Go
300
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
beginner
early programs ~ 1960’s
500
2 Ply
amateur
1200
MacHack (1100) ~ 1967 MIT
Branching Factor Estimates
for different two-player games
world ranked
2000
Slate (2070) ~ 1970’s Northwestern
Deep Thought ~ 1989 Carnegie Mellon
Deep Blue ~ 1996
2800
IBM
world champion
Deep Blue rematch ~ 1997 IBM
Tic-tac-toe
4
Connect Four
7
Checkers
10
Othello
30
Chess
40
Go
300
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
beginner
early programs ~ 1960’s
500
2 Ply
amateur
1200
MacHack (1100) ~ 1967 MIT
Branching Factor Estimates
for different two-player games
world ranked
2000
Slate (2070) ~ 1970’s Northwestern
Deep Thought ~ 1989 Carnegie Mellon
Deep Blue ~ 1996
2800
IBM
world champion
Deep Blue rematch ~ 1997 IBM
“solved” games
computer-dominated
human-dominated
Tic-tac-toe
4
Connect Four
7
Checkers
10
Othello
30
Chess
40
Go
300
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
b.winsFor(‘X’)
1
2
3
4
5
6
b.addMove(3,‘X’)
capabilities of b
b.removeMove(3)
b.isOver()
b.clear()
(the last 3 are new for this week)
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…
1
Ask what kind of players
should play for X and O
2
Create two objects of
class Player with
appropriate inputs
3
Ask each of these objects to
findScores for X and O
and then breakties.
4
Details
Player
playerForX
(data and methods)
Details
Player
playerForO
(data and methods)
See who wins!
demo…
what details are needed?
Player
Picture of a Player object
Player
playerForX
char
checker
int
lookahead
int
tiebreakType
Player(char ch, int lk, int tbk)
double[] findScores(Board b)
double[] plyHuman(Board b)
double[] ply0(Board b)
methods
double[] ply1,2,3,4,N(Board b)
double evaluate(Board b)
void printScores(double[] s)
Imagine if Board
weren’t a Class… !
int breaktie(double[] s)
class Player
{
private char checker;
private int lookahead;
private int tiebreakType;
Player code
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…
1
Ask what kind of players
should play for X and O
2
Create two objects of
class Player with
appropriate inputs
3
Ask each of these objects to
findScores for X and O
and then breakties.
4
Details
Player
playerForX
(data and methods)
Details
Player
playerForO
(data and methods)
See who wins!
demo…
what details are needed?
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
Choosing a move
1) Find scores at appropriate lookahead…
ply0: 0 ply of lookahead
ply1: 1 ply of lookahead
ply2,3,4: 2,3,4 ply of lookahead
plyHuman: ask the user
findScores
chooses one of these
methods to run
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)
{
char
checker
int
lookahead
this
b
ply1
int
tiebreakType
class Player
{
// returns scores with 1ply lookahead
//
public double[] ply1(Board b)
{
char
checker
int
lookahead
this
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…
b
Evaluating a board
Assigns a score to any Board b
100.0 for a win
50.0 for a “tie”
0.0 for a loss
-1.0 for an invalid move
not possible in evaluate
Score for X
Score for X
Score for X
Score for O
Score for O
Score for O
evaluate
100.0 for a win
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
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
0-ply scores for X:
no moves at all!
1-ply scores for X:
X moves
2-ply scores for X:
X moves
O moves
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’
‘O’
new‘X’
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
b
‘X’ to move
‘X’
‘O’
new‘X’
Looking ahead 1 ply…
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’
‘O’
new‘X’
Looking ahead 1 ply…
b
(1) For each possible move
(2) Add the column’s move
‘X’ to move
(3) Evaluate the boards
Col 6
Col 0
Col 5
Col 1
Col 2
Col 3
Col 4
NONE
‘X’
‘O’
new‘X’
Looking ahead 1 ply…
b
(1) For each possible move
(2) Add the column’s move
‘X’ to move
(3) Evaluate the boards
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’
‘O’
new‘X’
Looking ahead 1 ply…
b
(1) For each possible move
(2) Add the column’s move
‘X’ to move
(3) Evaluate the boards
(4) Choose one of the best
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
public double[] ply1(Board b)
{
ply1
?
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
this week…
(extra credit)
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 …
0 ply:
random (but legal) choice of move !
1 ply:
X’s move
2 ply:
X’s move
3 ply:
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
public double[] ply2(Board b)
{
ply2
depends on ply1 !
Lab this week
Last Names
• Problem 1: A Connect Four Player…
You’ll need to write (and use)
M-Z
Player(char ch, int lk, int tbk)
char getChecker()
char me()
void printScores()
char opp()
int go(Board b)
double evaluate(Board b)
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
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
0-ply scores for X:
no moves at all!
1-ply scores for X:
X moves
2-ply scores for X:
X moves
O moves
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’
‘O’
new‘X’
Looking ahead 1 ply…
b
(1) For each possible move
(2) Add the column’s move
‘X’ to move
(3) Evaluate the boards
(4) Choose one of the best
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 at
http://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()
?
:
if
else
and
is shorthand for if … else …, but
only for deciding between values
class Player
{
private char checker;
public char opp()
{
}
?:
// data member
// returns opponent’s checker
addMove
‘X’
‘O’
Class: Board
Object: b
b.addMove(3,‘X’)
changes b by adding
checker ‘X’ into row 3
b before
b after
new‘X’
Adding a move without changing b !
b before
b after
a new Board with
the move added
Board nextb = b.newAddMove(3,‘X’);