Def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in.

Slides:



Advertisements
Similar presentations
Adversarial Search Chapter 6 Sections 1 – 4. Outline Optimal decisions α-β pruning Imperfect, real-time decisions.
Advertisements

Adversarial Search Chapter 6 Section 1 – 4. Types of Games.
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
"Intelligent" CS 5 An object is structured data that is alive, responsible, and intelligent. Sound too friendly? This week’s objects and classes will be.
AI for Connect-4 (or other 2-player games) Minds and Machines.
CS 484 – Artificial Intelligence
Adversarial Search Chapter 6 Section 1 – 4.
Presented by : Ashin Ara Bithi Roll : 09 Iffat Ara Roll : 22 12th Batch Department of Computer Science & Engineering University of Dhaka.
Adversarial Search Chapter 5.
Adversarial Search Chapter 6. History Much of the work in this area has been motivated by playing chess, which has always been known as a "thinking person's.
Hoe schaakt een computer? Arnold Meijster. Why study games? Fun Historically major subject in AI Interesting subject of study because they are hard Games.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Artificial Intelligence in Game Design
HW 6: Problems 2&3 Simulating Connect 4.
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.
This time: Outline Game playing The minimax algorithm
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 580 Artificial Intelligence Ch.6: Adversarial Search Fall 2008 Marco Valtorta.
1 DCP 1172 Introduction to Artificial Intelligence Lecture notes for Chap. 6 [AIMA] Chang-Sheng Chen.
CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html.
PSU CS 370 – Introduction to Artificial Intelligence Game MinMax Alpha-Beta.
Lecture 5 Note: Some slides and/or pictures are adapted from Lecture slides / Books of Dr Zafar Alvi. Text Book - Aritificial Intelligence Illuminated.
Minimax.
Adversarial Search CS30 David Kauchak Spring 2015 Some material borrowed from : Sara Owsley Sood and others.
Game Playing.
A whole new class of programming CS 5 today HW10: Due Sun, Nov 15 Pr0: Ariane 5 Reading Pr1/Lab:the Date class Pr2 Connect4Board Pr3 Connect4Player (extra.
IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where.
Adversarial Search CS311 David Kauchak Spring 2013 Some material borrowed from : Sara Owsley Sood and others.
Adversarial Search Chapter 6 Section 1 – 4. Outline Optimal decisions α-β pruning Imperfect, real-time decisions.
IS313 Today: two I's AIUI hw9pr1.py an arbitrarily-good Connect 4 player! Player class Hw 10 (project) I wish there were three i's! Hw 9 is due Wed., 12/23.
Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Game Playing. Towards Intelligence? Many researchers attacked “intelligent behavior” by looking to strategy games involving deep thought. Many researchers.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Games 1 Alpha-Beta Example [-∞, +∞] Range of possible values Do DF-search until first leaf.
Code Design Using Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
IS313 … Monday, Nov. 8 - Classes + Objects, part 2 Monday, Nov Project strategies & example pres. Tuesday, Nov. 9 - Date class due Tuesday, Nov.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Cilk Pousse James Process CS534. Overview Introduction to Pousse Searching Evaluation Function Move Ordering Conclusion.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
GAME PLAYING 1. There were two reasons that games appeared to be a good domain in which to explore machine intelligence: 1.They provide a structured task.
HW 6: Problems 2 & 3 Simulating Connect 4. HW 6: Overview Connect 4: Variation of Tic-Tac-Toe – Board: Vertical 7x6 – Two players take alternating move.
Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'
Today’s Topics Playing Deterministic (no Dice, etc) Games –Mini-max –  -  pruning –ML and games? 1997: Computer Chess Player (IBM’s Deep Blue) Beat Human.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Tic tac toe XX PC: X YOU: O The PC-player with mark X goes first. After the PC, you place the mark O at the position of a green circle. If you succeed.
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
February 25, 2016Introduction to Artificial Intelligence Lecture 10: Two-Player Games II 1 The Alpha-Beta Procedure Can we estimate the efficiency benefit.
Explorations in Artificial Intelligence Prof. Carla P. Gomes Module 5 Adversarial Search (Thanks Meinolf Sellman!)
Artificial Intelligence in Game Design Board Games and the MinMax Algorithm.
Homework 10 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/10 Mon., 11/11 Problems
Adversarial Search Chapter 5 Sections 1 – 4. AI & Expert Systems© Dr. Khalid Kaabneh, AAU Outline Optimal decisions α-β pruning Imperfect, real-time decisions.
ADVERSARIAL SEARCH Chapter 6 Section 1 – 4. OUTLINE Optimal decisions α-β pruning Imperfect, real-time decisions.
IS313 Tomorrow… Wednesday, Nov Classes + Objects, part 2 Wednesday, Nov Classes + Objects, part 3 Thursday, Nov Date class due Thursday,
EECS 110: Lec 14: Classes and Objects
EECS 110: Lec 15: Classes and Objects (2)
Adversarial Environments/ Game Playing
CS this week 4 Building classes vs. using the library hw10pr2
CS Fall 2016 (Shavlik©), Lecture 11, Week 6
Introduction to Object-Oriented Programming (OOP)
Intelligent CS 5 ? This week's objects will be more adversarial…
Introduction to Object-Oriented Programming (OOP)

EECS 110: Lec 14: Classes and Objects
HW 6: Problems 2 & 3 Simulating Connect 4.
HW 6: Problems 2 & 3 Simulating Connect 4.
EECS 110: Lec 15: Classes and Objects (2)
Introduction to Object-Oriented Programming (OOP)
CS51A David Kauchak Spring 2019
CS51A David Kauchak Spring 2019
Presentation transcript:

def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in [1,3,5,7,8,10] and self.day == 31: self.day = 0 self.month += 1 elif self.month in [4,6,9,11] and self.day == 30: self.day = 0 self.month += 1 elif self.month == 2: if self.isLeapYear() and self.day == 29: self.day = 0 self.month += 1 elif (self.isLeapYear() == False) and self.day == 28: self.day = 0 self.month += 1 elif self.month == 12 and self.day == 31: self.day = 0 self.month = 1 self.year += 1 self.day += 1 Style Pointers

def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. "”” self.day += 1 if self.isLeapYear() == True: DIM = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.day > DIM[self.month]: self.day = 1 self.month += 1 if self.month > 12: self.month = 1 self.year += 1 else: DIM = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.day > DIM[self.month]: self.day = 1 self.month += 1 if self.month > 12: self.month = 1 self.year += 1 Style Pointers

def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ DIM = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.isLeapYear(): DIM[2] = 29 self.day += 1 if self.day > DIM[self.month]: self.day = 1 self.month += 1 if self.month > 12: self.month = 1 self.year += 1 A More Elegant Solution

Style Pointers def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False def isAfter(self, d2): """ Returns true if self is after d2 """ if self.year > d2.year: return True if self.month > d2.month and self.year == d2.year: return True if self.day > d2.day and d2.month == self.month and \ self.year == d2.year: return True return False

A More Elegant Solution def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False def isAfter(self, d2): """ Returns true if self is after d2 """ return d2.isBefore(self)

Style Pointers def diff( self, d2 ): """ Returns the number of days between self and d2 """ dcopy = self.copy() difference = 0 if dcopy.isBefore(d2) == True: while dcopy.isBefore(d2) == True: dcopy.tomorrow() difference -= 1 else: while dcopy.isAfter(d2): dcopy.yesterday() difference += 1 return difference

An Elegant Solution def diff( self, d2 ): """ Returns the number of days between self and d2 """ dcopy = self.copy() difference = 0 while dcopy.isBefore(d2): dcopy.tomorrow() difference -= 1 while dcopy.isAfter(d2): dcopy.yesterday() difference += 1 return difference

CS 121 Today Connect Four X to move. Is there a way to win? | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X|O| |

Aargh! Python has no Connect-four datatype… | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| … but we can correct that!

Designing classes 1) What data? (Data Members) 2) What are objects' crucial capabilities? (Methods) Not limited to 7x6!

Designing classes 1)What data? (Data Members) - height, width - Where the chips are - Whose turn it is - Winning condition - End condition (number of chips played) 2) What are objects' crucial capabilities? (Methods) Not limited to 7x6!

Designing classes 1)What data? (Data Members) - height, width - Where the chips are - Whose turn it is - Winning condition - End condition (number of chips played) 2) What are objects' crucial capabilities? (Methods) - create a new object - check for win - print - get the next move/switch turn -check for full board Not limited to 7x6!

Connect Four: the object b Board b int width str data list str data int height What is the name of the method that will construct this data?

Connect Four: the object b Board b int width str data list str data int height What is the name of the method that will construct this data? __init__(…)

Connect Four: constructor class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height self.data = [] # this will be the board for row in range( height ): # 6 boardRow = [] for col in range( width ): # 7 boardRow += [' '] # add a space to this row self.data += [boardRow]

Connect Four: the object b Board b int width str data list str int height | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| What is the name of the method that will print this data?

def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( self.height ): s += '|' for col in range( self.width ): s += self.data[row][col] + '|' s += '\n' return s Connect Four: __repr__ To remove? To add? which row is row 0, row 1, and so on?

def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( self.height ): s += '|' for col in range( self.width ): s += self.data[row][col] + '|' s += '\n‘ s += '--'*self.width + '-\n‘ for col in range( self.width ): s += ' ' + str(col % 10) s += '\n' return s Connect Four: __repr__ which row is row 0, row 1, and so on?

class Board def mystery(self, col, ox): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[self.height-1][col] = ox def allowsMove(self, col): Step through this mystery method. What is each line doing? What's going wrong? a C4 board col # 'X' or 'O' Write allowsMove to return True if col is a valid move; False otherwise.

class Board { def mystery(self, col, ox): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[self.height-1][col] = ox # Adds ox at the top of a column # But at the same time overwrites the existing # elements in the column… # Does not check if it is possible to write in a column… def allowsMove(self, col): } Step through this mystery method. What is each line doing? What's going wrong? a C4 board col # 'X' or 'O' Write allowsMove to return True if col is a valid move; False otherwise.

class Board def mystery1(self, col, ox): if allowsMove(col): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox return self.data[self.height-1][col] = ox def allowsMove(self, col): Step through this mystery method. What is each line doing? What's going wrong? a C4 board col # 'X' or 'O' Write allowsMove to return True if col is a valid move; False otherwise.

class Board def mystery1(self, col, ox): if allowsMove(col): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox return self.data[self.height-1][col] = ox def allowsMove(self, col): if 0 <= col < self.width: return self.data[0][col] == ' ' Step through this mystery method. What is each line doing? What's going wrong? a C4 board col # 'X' or 'O' Write allowsMove to return True if col is a valid move; False otherwise.

C4 Board class: methods __init__( self, width, height ) allowsMove( self, col ) __repr__( self ) addMove( self, col, ox ) isFull( self ) winsFor( self, ox ) the “constructor” checks if allowed places a checker outputs a string checks if any space is left checks if a player has won hostGame( self ) play! delMove( self, col ) removes a checker Which of these will require the most thought?

winsFor( self, ox ) Thoughts? X O b b.winsFor( 'X' ) or 'O'

winsFor( self, ox ) Thoughts? X O b def winsFor(self, ox): # check for horizontal wins for row in range(0,self.height): for col in range(0,self.width-3): if self.data[row][col] == ox and \ self.data[row][col+1] == ox and \ self.data[row][col+2] == ox and \ self.data[row][col+3] == ox: return True # check for vertical wins

Two-player games have been a key focus of AI as long as computers have been around… Strategic thinking == intelligence ? In 1945, Alan Turing predicted that computers would be better chess players than people in ~ 50 years… and thus would have achieved intelligence. Alan Turing memorial Manchester, England

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. humans computers good at evaluating the strength of a board for a player Particularly good at recognizing patterns good at looking ahead in the game to find winning combinations of moves

How humans play games… - experts could reconstruct these perfectly - novice players did far worse… An experiment (by A. 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 equally well (badly) at reconstructing them! An experiment (by A. deGroot) was performed in which chess positions were shown to novice and expert players…

The Player class Player pForX Details (data and methods) What data and methods are needed to construct and implement a Player object?

string ox Picture of a Player object Player pForX __init__(self, ox, tbt, ply) string tbt __repr__(self) scoreBoard(self, b) scoresFor(self, b) tiebreakMove(self, scores) nextMove(self, b) oppCh(self) 'X' 'LEFT' int ply 3 DATA METHODS tiebreakType checker, O or X

scoreBoard ‘X’‘X’ ‘O’‘O’ Assigns a score to any board, b A simple system: for a win for a lossfor anything else Score for

scoreBoard ‘X’‘X’ ‘O’‘O’ Assigns a score to any board, b A simple system: for a win for a lossfor anything else Score for

scoreBoard ‘X’‘X’ ‘O’‘O’ Assigns a score to any board, b A simple system: for a win for a lossfor anything else Score for

scoreBoard Assigns a score to any board, b A simple system: for a win for a lossfor anything else scoreBoard(self, b) Implementation ideas… What methods that already exist will come in handy? This doesn't seem to be looking very far ahead ! How can there be no 'X' or 'O' input? What class is this method in?

Looking further ahead… scoreBoard looks ahead 0 moves If you look one move ahead, how many possibilities are there to consider? 0-ply 1-ply A 1-ply lookahead player will "see" an impending victory. to move… score

Looking further ahead… scoreBoard looks ahead 0 moves If you look one move ahead, how many possibilities are there to consider? 0-ply 1-ply A 1-ply lookahead player will "see" an impending victory. to move… score

Looking further ahead… scoreBoard looks ahead 0 moves If you look one move ahead, how many possibilities are there to consider? 0-ply A 2-ply lookahead player will also "see" an opponent's impending victory. to move… 2-ply score

Looking further ahead… scoreBoard looks ahead 0 moves If you look one move ahead, how many possibilities are there to consider? 0-ply 1-ply scoresFor( self, b ) returns a LIST of scores, one for each column you can choose to move next… 2-ply

|O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| It is O’s move. What scores does a 1-ply lookahead for O assign to each move? col 0col 1col 2col 3col 4col 5col 6 Which change at 2-ply? Example 1-ply and 2-ply lookahead scores

|O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| It is O’s move. What scores does a 1-ply lookahead for O assign to each move? col 0col 1col 2col 3col 4col 5col 6 Which change at 2-ply? Example 1-ply and 2-ply lookahead scores

|O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| It is O’s move. What scores does a 1-ply lookahead for O assign to each move? col 0col 1col 2col 3col 4col 5col 6 Example 1-ply and 2-ply lookahead scores col 0col 1col 2col 3col 4col 5col ply 2-ply

| | | | | | |O| | | | | | | |X| |X| |X|O| | |O| |X|O|O|X| |X|X| |X|O|O|O| |O|X| It is X’s move. What scores does a 2-ply lookahead for X assign to each move? col 0col 1col 2col 3col 4col 5col 6 Example 1-ply and 2-ply lookahead scores

| | | | | | |O| | | | | | | |X| |X| |X|O| | |O| |X|O|O|X| |X|X| |X|O|O|O| |O|X| It is X’s move. What scores does a 2-ply lookahead for X assign to each move? col 0col 1col 2col 3col 4col 5col 6 Example 1-ply and 2-ply lookahead scores

b 0-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 1-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 2-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 3-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 Practice ‘X’‘X’ ‘O’‘O’

0-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 1-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 2-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 3-ply scores for O: col 0col 1col 2col 3col 4col 5col 6 Solutions b ‘X’‘X’ ‘O’‘O’

‘X’‘X’ ‘O’‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b scoresFor each column (1) For each possible move (2) Add it to the board

‘X’‘X’ ‘O’‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b (1) For each possible move (2) Add it to the board (3) Ask OPPONENT to score each board At what ply? scoresFor each column

‘X’‘X’ ‘O’‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b (1) For each possible move (2) Add it to the board (3) Ask OPPONENT to score each board (4) Take the opponent's MAX What to assign for a score? scoresFor each column 50.0

scoresFor def scoresFor(self, b): (1) For each possible move (2) Add it to the board (3) Ask OPPONENT to score each board - at ? ply (4) the score is 100-max

Write tiebreakMove to return the leftmost best score inside the list scores def tiebreakMove(self, scores): if self.tbt == 'LEFT': How would 'RANDOM' and 'RIGHT' work differently?

Computer Chess early programs ~ 1960’s Computers cut their teeth playing chess… 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 1,000,000’s moves/sec 3,500,000 moves/sec Deep Fritz: 2002 X3D Fritz: 2003 Hydra: ,000,000 moves/sec first paper: 1950 What is Hydra's chess rating?

Games’ Branching Factors 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 fewer than 40 possible moves that a chess player can make from any board configuration… 0 Ply 1 Ply 2 Ply Hydra at home in the United Arab Emirates… Hydra looks ahead 18 ply !

Games’ Branching Factors Branching Factor Estimates for different two-player games Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go Ply 2 Ply Boundaries for qualitatively different games… 0 Ply

Games’ Branching Factors Branching Factor Estimates for different two-player games Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 “solved” games computer-dominated human-dominated 1 Ply 2 Ply 0 Ply Progress

Looking further ahead … 0 ply: 2 ply:3 ply: Zen choice of move: here and now | | | | |O| | | | | | | |X| | | | | | | |X|O|O| | |X| | |O|X|X|O|X|O| | | | | | | | | | |X| | | | | | | |O|O| | | |X|X| |X|O| | |O|X|O| |O|X| | (1) Player will win (2) Player will avoid losing (3) Player will set up a win by forcing the opponent to avoid losing X ’s move X ‘s move 1 ply: | | | | | |O|X| | | | | |O|X|X|X| |O|O| X ’s move

‘X’‘X’ ‘O’‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Choosing the best move (1) For each possible move (2) Add it to the board (3) Ask OPPONENT to score each board - ply? (4) Reverse the scores

‘X’‘X’ ‘O’‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Choosing the best move (1) For each possible move (2) Add it to the board (3) Ask OPPONENT to score each board - ply? (4) Reverse the scores (5) Find one max - that's it!