1 / 65

IS313 Tomorrow…

IS313 Tomorrow…. Wednesday, Nov. 11 - Classes + Objects, part 2. Thursday, Nov. 12 - Date class due. Wednesday, Nov. 18 - Classes + Objects, part 3. Thursday, Nov. 19 - Board class due & proposal. Wednesday, Dec. 2 - Networks and their analysis.

angeni
Télécharger la présentation

IS313 Tomorrow…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IS313 Tomorrow… Wednesday, Nov. 11 - Classes + Objects, part 2 Thursday, Nov. 12 - Date class due Wednesday, Nov. 18 - Classes + Objects, part 3 Thursday, Nov. 19 - Board class due & proposal Wednesday, Dec. 2 - Networks and their analysis Thursday, Dec. 3 - progress report due – with code Wednesday, Dec. 9 - In-class project presentations IS 313 Schedule Thursday, Dec. 10 - progress report due – with code Wednesday, Dec. 16 - no class meeting Thursday, Dec. 17 - Final project due! HMC news… Wednesday, Dec. 23 - Player class due (Hw #9)

  2. IS313 today: Intelligent software An object is structured data that is alive, responsible, and intelligent. Sound too friendly? This week’s objects and classes will be just the opposite ... import antigravity! | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X|O| | --------------- 0 1 2 3 4 5 6 X to move. Is there a way to win?

  3. Aargh! Python has no Connect-four datatype… | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X|O| | --------------- 0 1 2 3 4 5 6 … but we can correct that! Can I see a demo?

  4. Designing classes 1) What data? (Data Members) Not limited to 7x6! 2) What are the key capabilities? (Methods)

  5. int int Connect Four: the objectb 4 str str str str list width Board b data 3 str str str str ? height str str str str Starting from b, how would you examine the string marked by the ? What is the name of the method that will construct this data?

  6. 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 self.data = [ [' ']*self.width ] * self.height Doesn't work! Not much of a game…

  7. 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( self.width ): boardRow = [] for col in range( self.height ): boardRow += [' '] # add a space to this row self.data += [boardRow] Better! Same idea as in Life

  8. 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 self.data = [ [' ']*self.width for row inrange(self.height) ] What was this called again… ? Even shorter!

  9. int int Connect Four: the objectb 6 7 … height width … list Board b data … X str str str str Which rows and columns are these? … X X str str str str … X O O O str str str str | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| --------------- 0 1 2 3 4 5 6 printed version which is row 0 ?

  10. int int The grim task of printing … 7 … width … 6 X list Board b data str str str str height … X X str str str str … X O O O Which rows and columns are these? str str str str | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| --------------- 0 1 2 3 4 5 6 What is the name of the method that will print this data?

  11. Connect Four: __repr__ 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 inrange( self.width ): s += self.data[row][col] + '|' s += '\n' return s What else?

  12. Connect Four: set_board | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --------------- 0 1 2 3 4 5 6 >>> b = Board( 7, 6 ) >>> print b >>> LoS = [ ' ', ' ', 'OXO ', 'XXOO ', 'XOXXOX ', 'XOOXXOO' ] >>> b.set_board( LoS ) >>> print b What is this a list of? | | | | | | | | | | | | | | | | |O|X|O| | | | | |X|X|O|O| | | | |X|O|X|X|O|X| | |X|O|O|X|X|O|O| --------------- 0 1 2 3 4 5 6

  13. Connect Four: set_board defset_board(self, LoS): """ this method sets the board to the list_of_strings that is input """ for row in range( ): for col in range( ): self.data[row][col] = LoS[row][col] What goes in the blanks?

  14. Try it! | | | | | | | | | | | | | | | | |O|X|O| | | | | |X|X|O|O| | | | |X|O|X|X|O|X| | |X|O|O|X|X|O|O| --------------- 0 1 2 3 4 5 6 Step through this addMove method. How does it work? What's wrong? Try to fix it… ! defaddMove(self, col, ox): row = self.height-1 while True: if self.data[row][col] == ' ': self.data[row][col] = ox row -= 1 a C4 board col # 'O' or 'X'

  15. Try it! defallowsMove(self, col): a C4 board col # allowsMove should return True if col has enough space to allow a move; it should return False otherwise. | |X| | | | | | | |O| | | | | | |O|X|O| | | | | |X|X|O|O| | | | |X|O|X|X|O|X| | |X|O|O|X|X|O|O| --------------- 0 1 2 3 4 5 6 b True b.allowsMove(0) False b.allowsMove(1)

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

  17. Checking wins… ? self X O Thoughts? corner cases?

  18. ? Strategic thinking == intelligence Two-player games have been a key focus of AI as long as computers have been around… 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

  19. ? Strategic thinking == intelligence Two-player games have been a key focus of AI as long as computers have been around… humans computers good at evaluating 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.

  20. How humans play games… An experiment (by A. deGroot) was performed in which chess positions were shown to novice and expert players… - experts could reconstruct these perfectly - novice players did far worse…

  21. How humans play games… An experiment (by A. 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 equally well (badly) at reconstructing them!

  22. ? Strategic thinking == intelligence Two-player games have been a key focus of AI as long as computers have been around… humans computers good at evaluating the strength of a board for a player good at looking ahead in the game to find winning combinations of moves hw9 pr1 building an AI chess player … humans and computers have different relative strengths in these games.

  23. Player class What data does an AI player need? DATA MEMBERS 0 'LEFT' 'X' int ply Player pForX string tbt string ox checker, O or X tiebreakType moves to look ahead surprisingly little! Let's see a demo! How about knowledge about its opponent?

  24. Board and Player methods Board's data also includes data! Board All of Player's data __init__( self, width, height ) Player allowsMove( self, col ) addMove( self, col, ox ) __init__(self, ox, tbt, ply) delMove( self, col ) __repr__(self) __repr__( self ) oppCh(self) set_board( self, LoS ) scoreBoard(self, b) isFull( self ) scoresFor(self, b) winsFor( self, ox ) tiebreakMove(self, scores) hostGame( self ) nextMove(self, b)

  25. scoreBoard ‘X’ ‘O’ Assigns a score to any board, b A simple system: 100.0 50.0 0.0 for a win for anything else for a loss Score for Score for Score for Score for

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

  27. Looking further ahead… scoreBoard looks ahead 0 moves The "Zen" approach -- we are excellent at this! 0-ply If you look one move ahead, how many possibilities are there to consider? A 1-ply lookahead player will "see" an impending victory. to move… special case 1-ply scores A score for each column…? p42.scoresFor( b42 )

  28. Looking further ahead… scoreBoard looks ahead 0 moves The "Zen" approach -- we are excellent at this! 0-ply If you look one move ahead, how many possibilities are there to consider? A 2-ply lookahead player will also "see" an opponent's impending victory. to move… 1-ply score 2-ply What about 3-ply? score p43.scoresFor( b42 ) and p44

  29. Looking further ahead… scoreBoard looks ahead 0 moves The "Zen" approach -- we are excellent at this! 0-ply If you look one move ahead, how many possibilities are there to consider? 1-ply 2-ply scoresFor( self, b ) returns a LIST of scores, one for each column you can choose to move next…

  30. self ‘X’ Score! ‘O’ col 0 col 1 col 2 col 3 col 4 col 5 col 6 0-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 1-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 2-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 3-ply scores for O:

  31. Example 1-ply and 2-ply lookahead scores http://www.stanford.edu/~ccecka/research/C4.html It is O’s move. What scores does a 1-ply lookahead for O assign to each move? |O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| --------------- 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 Which change at 2-ply? It is X’s move. What scores does a 2-ply lookahead for X assign to each move? | | | | | | |O| | | | | | | |O| | | | | | | |X| |X| |X|O| | |O| |X|O|O|X| |X|X| |X|O|O|O| |O|X| --------------- 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 Which change at 3-ply?

  32. Answers to example lookahead scores http://www.stanford.edu/~ccecka/research/C4.html It is O’s move. What scores does a 1-ply lookahead for O assign to each move? |O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| --------------- 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 -1 100 50 100 50 100 50 0 0 Which change at 2-ply? | | | | | | |O| | | | | | | |O| | | | | | | |X| |X| |X|O| | |O| |X|O|O|X| |X|X| |X|O|O|O| |O|X| --------------- 0 1 2 3 4 5 6 It is X’s move. What scores does a 2-ply lookahead for X assign to each move? be careful! col 0 col 1 col 2 col 3 col 4 col 5 col 6 100 0 0 0 0 50 -1 Which change at 3-ply? 0

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

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

  35. ‘X’ scoresFor each column ‘O’ new‘X’ self (1) For each possible move (2) Add it to the board (3) Ask OPPONENT to score each board (4) Which score will the opponent choose? What, then, should assign for your score? (self's score) 0.0 50.0 Col 6 Col 0 Col 5 Col 1 Col 2 Col 3 Col 4 50.0 50.0 0.0 0.0 0.0

  36. Computer Chess Computers cut their teeth playing chess… Ranking first paper: 1950 beginner early programs ~ 1960’s 500 100’s of moves/sec amateur 1200 10,000’s of moves/sec MacHack (1100) ~ 1967 MIT 1,000,000’s moves/sec world ranked Slate (2070) ~ 1970’s Northwestern 2000 Deep Thought ~ 1989 Carnegie Mellon 3,500,000 moves/sec Deep Blue ~ 1996 IBM Deep Fritz: 2002 X3D Fritz: 2003 Hydra: 2006 What is Hydra's chess rating? world champion 2800 Deep Blue rematch ~ 1997 IBM 200,000,000 moves/sec

  37. Games’ Branching Factors • 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 Branching Factor Estimates for different two-player games Tic-tac-toe 4 Connect Four 7 Checkers 10 Othello 30 Chess 40 Go 300 Hydra at home in the United Arab Emirates… Hydra looks ahead 18 ply !

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

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

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

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

  42. Hwks and Projects due 11/19/09 • Homework 8: A Connect Four Board… due 12/23/09 ! • Homework 9: A Connect Four Player… by the end of the term…

  43. Hwks and Projects due 11/19/09 • Homework 8: A Connect Four Board… due 12/23/09 ! • Homework 9: A Connect Four Player… by the end of the term… • Homework 8, part 2: final project proposal Pre-planned projects Ideas Open-ended 1) Web-based Text Clouds Flash Cards ? Image processing pySQL ? using pyGame: "snake" 2) 3d simulation game: vPool 3) implementing Picobot! others welcome! 4) robot navigation: pyRobot

  44. Project Deliverables https://www.cs.hmc.edu/twiki/bin/view/CS5/ISProjectsPage • Proposal due 11/19/09 one-page (proposal.txt) that includes your project choice and a first description of the project-specific ideas - also, what libraries you'll use • Preliminary milestone due 12/3/09 Working code (preliminary.zip) that uses your libraries and shows access to the data you need (sound, graphics, etc.) Also, a plan for the next steps to take -- this may include classes you'll build. • Presentation / intermediate milestone due 12/9-10/09 The presentation is ~10 minutes of your vision, the technical details, and a demo of what you have so far. The intermediate.zip milestone should have the presentation, demo, and a final plan, including classes. • Project due 12/17/09 Your final project (project.zip), with documentation and "future work."

  45. Examples?! Date, due Date(11,11,09).tomorrow()

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

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

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

  49. ‘X’ Choosing the best move ‘O’ new‘X’ b (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! 100.0 50.0 Col 6 Col 0 Col 5 Col 1 Col 2 Col 3 Col 4 50.0 50.0 100.0 100.0 100.0

More Related