CPS 100, Fall 2011 10.1 PFTN2Wks l Making progress on Boggle assignment  Many, many classes, each designed to do one thing  Some related by inheritance,

Slides:



Advertisements
Similar presentations
Computers playing games. One-player games Puzzle: Place 8 queens on a chess board so that no two queens attack each other (i.e. on the same row, same.
Advertisements

Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
CPS 100, Fall Backtracking by image search.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
CPS 100, Fall Algorithms and Idioms l How do you avoid calculating the same thing over and over and over and over and …  Does it matter?  Recalculate.
1 CSC 421: Algorithm Design & Analysis Spring 2013 See online syllabus: (also on BlueLine2) Course.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
CIS 310: Visual Programming, Spring 2006 Western State College 310: Visual Programming Othello.
Backtracking.
1 Adversary Search Ref: Chapter 5. 2 Games & A.I. Easy to measure success Easy to represent states Small number of operators Comparison against humans.
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 2 Adapted from slides of Yoonsuck.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 See online syllabus (also available through BlueLine): Course goals:
Game Playing Chapter 5. Game playing §Search applied to a problem against an adversary l some actions are not under the control of the problem-solver.
Game Playing.
Game Playing Chapter 5. Game playing §Search applied to a problem against an adversary l some actions are not under the control of the problem-solver.
CPS 100, Spring Trees: no data structure lovelier?
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
CPS 100e, Spring Day, Week, Month Overview l Backtracking p/review  Boggle  APT l Assignment review  Boggle  APT l PQ Overview.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
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.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Software Design 8.1 Compsci 108 l Object oriented design and programming of medium-sized projects in groups using modern tools and practices in meaningful.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Software Design 18.1 CPS 108 l Object oriented design and programming of medium-sized projects in groups using modern tools and practices in meaningful.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 See online syllabus (also available through Blackboard): Course goals:
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.
Chess Strategies Component Skills Strategies Prototype Josh Waters, Ty Fenn, Tianyu Chen.
CompSci 100 Prog Design and Analysis II Oct 26, 2010 Prof. Rodger CPS 100, Fall
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
CompSci Inheritance and Interfaces  Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape,
CompSci Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Graph Search II GAM 376 Robin Burke. Outline Homework #3 Graph search review DFS, BFS A* search Iterative beam search IA* search Search in turn-based.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
CompSci Problem Solving: Sudoku  Rules of the Game Sudoku is played with a 9 by 9 "board" consisting of nine 3 by 3 sub-boards. The symbols 1 -
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
CPS 100, Spring Search, Trees, Games, Backtracking l Trees help with search  Set, map: binary search tree, balanced and otherwise  Quadtree.
CPS 100, Spring Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write.
Understanding AI of 2 Player Games. Motivation Not much experience in AI (first AI project) and no specific interests/passion that I wanted to explore.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
TWTG What work is still to come APT, Huff, APT
CSC 421: Algorithm Design & Analysis
Game playing Types of games Deterministic vs. chance
TWTG What work is still to come APT, Huff, APT
Backtracking, Search, Heuristics
CSCI 104 Backtracking Search
CSC 421: Algorithm Design & Analysis
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSC 421: Algorithm Design & Analysis
Back Tracking.
Analysis and design of algorithm
adapted from Recursive Backtracking by Mike Scott, UT Austin
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Backtracking, Search, Heuristics
Minimax strategies, alpha beta pruning
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Owen Astrachan November 9, 2018
Presentation transcript:

CPS 100, Fall PFTN2Wks l Making progress on Boggle assignment  Many, many classes, each designed to do one thing  Some related by inheritance, object-oriented concept  Many single purpose classes --- can be hard to grok  Recursive backtracking, also hard to grok l Toward trees and recurrences  More self-referential structures  Recursive algorithms  Self-referential run-time analysis (kind of)

CPS 100, Fall OO Aside: Interfaces, Abstract Classes l Interface is a contract  X implements Y: X provides method implementations  No code sharing among implementing classes  Characteristics of interface: signatures only  Why create an interface? l Abstract class leverages common code  Often implements interface with common code  Consider Map/AbstractMap, List/AbstractList,…  One method labeled abstract, requires implementation!

CPS 100, Fall Object-oriented [programming|design] l Classes encapsulate state and behavior  Methods are behavior, ivars/fields are state  Ideally class models/deals with one concept, not many  State is typically 'hidden' from client code, methods used l In bigger programs inheritance very useful  Avoid code duplication  Allow for open/close: classes open to extension, closed to modification  Hollywood principle: don't call us, we'll call you You can make your own List class, sort it! How?

CPS 100, Fall Interfaces and Abstraction in Boggle General idea: create IThing for interfaces  IPlayer, IAutoPlayer, ILexicon, IBoardMaker  Facilitates new implementations if IThing doesn't change Hollywood principle for rest of code/Boggle classes We've designed the interfaces, they don’t change l AbstractPlayer and AbstractAutoPlayer  Factor out common code, differentiate in subclasses  Game play by Gui/Controller/View, back-and-forth plays  Be careful, be wary of ignoring/not knowing parent code How do you update score in a BoardFirstAutoPlayer l How can you create different boards?

CPS 100, Fall Inheritance concepts l Parent/super class can be extended by subclassing  Possible to use methods from parent class, subs have them!  Possible to override parent methods, change behavior  Possible to do both! Call super.doThis(); l Often you don't have access to parent.java file  Still can subclass, use methods, extend/override them  Do NOT have access to private data fields  DO have access to protected data fields l Hard to do OO design, leave for later courses  But get an idea now as to power and utility

CPS 100, Fall Backtracking by image search

CPS 100, Fall Searching with no guarantees l Search for best move in automated game play  Can we explore every move?  Are there candidate moves ranked by “goodness”?  Can we explore entire tree of possible moves? l Search with partial information  Predictive texting with T9 or iTap or …  Finding words on a Boggle board  What numbers fit in Sudoku suare l Try something, if at first you don’t succeed ….

CPS 100, Fall Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write that program?  l How does Bing/Googlemap find routes from one place to another?  Shortest path algorithms  Longest path algorithms l Optimal algorithms and heuristic algorithms  When is close good enough? How do measure “closeness”?  When is optimality important, how much does it cost?

CPS 100, Fall Exhaustive Search/Heuristics l We can probably explore entire game tree for tic- tac-toe, but not for chess  How many tic-tac-toe boards are there?  How many chess boards are there? l What do we do when the search space is huge?  Brute-force/exhaustive won't work, need heuristics ?  What about google-maps/Garmin finding routes? l Backtracking can use both concepts  Game tree pruning a good idea most of the time

CPS 100, Fall Classic problem: N queens l Can queens be placed on a chess board so that no queens attack each other?  Easily place two queens  What about 8 queens? l Make the board NxN, this is the N queens problem  Place one queen/column  Horiz/Vert/Diag attacks l Backtracking  Tentative placement  Recurse, if ok done!  If fail, undo tentative, retry l wikipedia-n-queens wikipedia-n-queens

CPS 100, Fall Backtracking idea with N queens l For each column C, tentatively place a queen  Try first row in column C, if ok, move onto next column Typically “move on” is recursive  If solved, done, otherwise try next row in column C Must unplace queen when failing/unwind recursion l Each column C “knows” what row R it's on  If first time, that’s row zero, but might be an attack  Unwind recursion/backtrack, try “next” location l Backtracking: record an attempt go forward  Move must be “undoable” on backtracking/unwinding

CPS 100, Fall N queens backtracking: Queens.java public boolean solve(int col){ if (col == mySize) return true; // try each row until all are tried for(int r=0; r < mySize; r++){ if (myBoard.safeToPlace(r,col)){ myBoard.setQueen(r,col,true); if (solve(col+1)){ return true; } myBoard.setQueen(r,col,false); } return false; }

CPS 100, Fall Basic ideas in backtracking search l Enumerate all possible choices/moves  We try these choices in order, committing to a choice  If the choice doesn’t pan out we must undo the choice Backtracking step, choices must be undoable l Inherently recursive, when to stop searching?  When all columns tried in N queens  When we have found the exit in a maze  When every possible moved tried in Tic-tac-toe or chess? Is there a difference between these games? l Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything

CPS 100, Fall Queens Details l How do we know when it’s safe to place a queen?  No queen in same row, or diagonal  For each column, store the row that a queen is in  See QBoard.java for details l Advanced OO: For GUI version, we use a decorator  The QBoardGUI is an IQueenState class and it has an IQueenState object in it  Appears as an IQueenState to client, but uses an existing one to help do its work  One of many object oriented design patterns, seen in Huff in the BitInputStream class

CPS 100, Fall Boggle Program

CPS 100, Fall Backtracking Practice l GridGame APT: how many winning moves?  It's my turn and there are no places to go: return 0, why?  Try to place an 'X', count opponent wins. If 0 then … After placing 'X' and counting, unplace the 'X', why? l What about looking for a word on Boggle Board?  If we've already made 'EA' and we look at b[3][5] Can we make 'EAT'? Can we make 'EACH'? Trying for 'EAT', if b[3][5] is 'T' then … Trying for 'EACH', if b[3][5] is 'A' then … Trying for 'EACH', if b[3][5] is 'C' then …

CPS 100, Fall Boggle Search for Word l Starting at board location (row,col): find a string S  We want to keep track of where we are in the string  Also track what board locations used for S search l How do we know when we're done?  Base case of recursive, backtracking call  Where we are in the string? l How do we keep track of used locations?  Store in array list: tentatively use current one, recurse  If we don’t succeed, take off the last one stored!

CPS 100, Fall Using Howto Hints l Helper method in GoodWordOnBoardFinder  Needed: board, row, col, word, word-index, list  Search for "skunk" at (0,0) at (0,1) at …  Call help(board,row,col,"skunk",0,list)  If we find the 's', where do we look next and for what?  If we don't find the 's' what does that mean?  If (row,col) isn't legal position then …  If we've found the last 'k' what does that mean? l Do we first check for …  Does order of checking make a difference?

CPS 100, Fall Lots and lots of classes – sensible? l Javadoc can help understand methods/classes  We are using an 'un-named' package in Boggle  Generate javadoc from within Eclipse, Project menu  Useful for browsing classes/files l Java-isms  Class Foo is in Foo.java  We use IThing for an Interface named Thing  You can document private stuff too, useful in development l Experience is a good teacher

CPS 100, Fall Daphne Koller l 2004, Macarthur l 2008, first ACM/Infosys l “The world is noisy and messy …You need to deal with the noise and uncertainty.” “I find it distressing that the view of the field is that you sit in your office by yourself surrounded by old pizza boxes and cans of Coke, hacking away at the bowels of the Windows operating system,” she said. “I spend most of my time thinking about things like how does a cell work or how do we understand images in the world around us?”

CPS 100, Fall Computer v. Human in Games l Computers can explore a large search space of moves quickly  How many moves possible in chess, for example? l Computers cannot explore every move (why) so must use heuristics  Rules of thumb about position, strategy, board evaluation  Try a move, undo it and try another, track the best move l What do humans do well in these games? What about computers?  What about at Duke?

CPS 100, Fall Games at Duke l Alan Biermann  Natural language processing  Compsci 1: Great Ideas  Duchess, checkers, chess l Tom Truscott  Duke undergraduate working with/for Biermann  Usenet: online community l Second EFF Pioneer Award (with Vint Cerf!) Second EFF Pioneer Award (with Vint Cerf!)

CPS 100, Fall Heuristics l A heuristic is a rule of thumb, doesn’t always work, isn’t guaranteed to work, but useful in many/most cases  Search problems that are “big” often can be approximated or solved with the right heuristics l What heuristic is good for Sudoku?  Is there always a no-reasoning move, e.g., 5 goes here?  What about “if I put a 5 here, then…”  Do something else? l What other optimizations/improvements can we make?  For chess, checkers: good heuristics, good data structures

CPS 100, Fall Barbara Liskov l First woman to earn PhD from compsci dept  Stanford l Turing award in 2008 l OO, SE, PL “It's much better to go for the thing that's exciting. But the question of how you know what's worth working on and what's not separates someone who's going to be really good at research and someone who's not. There's no prescription. It comes from your own intuition and judgment.”