Solving sudoku: work in progress Jeanine Meyer Purchase College/SUNY Math/CS Senior Seminar.

Slides:



Advertisements
Similar presentations
Heuristic Search techniques
Advertisements

Adversarial Search Reference: “Artificial Intelligence: A Modern Approach, 3 rd ed” (Russell and Norvig)
Playing Fair at Sudoku Joshua Cooper USC Department of Mathematics.
CS 4700: Foundations of Artificial Intelligence Bart Selman Reinforcement Learning R&N – Chapter 21 Note: in the next two parts of RL, some of the figure/section.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Mastering Chess An overview of common chess AI Adam Veres.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
Best-First Search: Agendas
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Problem Solving Using Search Reduce a problem to one of searching a graph. View problem solving as a process of moving through a sequence of problem states.
CPSC 322 Introduction to Artificial Intelligence October 25, 2004.
Data Parallel Algorithms Presented By: M.Mohsin Butt
MAE 552 – Heuristic Optimization Lecture 28 April 5, 2002 Topic:Chess Programs Utilizing Tree Searches.
What is AI  An attempt to imitate human reactions by scripting reactions to happen when a certain cause is brought about.
How computers play games with you CS161, Spring ‘03 Nathan Sturtevant.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)
SUDOKU Via Relaxation Labeling
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.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
Announcements.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Artificial Intelligence
Development of a Machine-Learning-Based AI For Go By Justin Park.
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.
Mark Dunlop, Computer and Information Sciences, Strathclyde University 1 Algorithms & Complexity 5 Games Mark D Dunlop.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
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.
Lacey Johnson Summer 2013 Student Research Project – James Madison University Sudoku can be described as a system of polynomials which can then be solved.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
Spring, 2009Purchase College/SUNY The Set Game Jeanine Meyer Mathematics/Computer Science Senior Seminar.
Microsoft ® Office Excel 2003 Training Using XML in Excel SynAppSys Educational Services presents:
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
CSCI 4310 Lecture 6: Adversarial Tree Search. Book Winston Chapter 6.
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.
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.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
Problem Reduction So far we have considered search strategies for OR graph. In OR graph, several arcs indicate a variety of ways in which the original.
Course Overview  What is AI?  What are the Major Challenges?  What are the Main Techniques?  Where are we failing, and why?  Step back and look at.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
Backtracking & Brute Force Optimization Intro2CS – weeks
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
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.
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 -
Optimized SAT Encoding For Sudoku Puzzles Will Klieber and Gi-Hwon Kwon Sept 27, 2007 rev. 2 Changes from v1: Added a new slide after “A Better Encoding.
Teaching Computers to Think:
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.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSG3F3/ Desain dan Analisis Algoritma
Done Done Course Overview What is AI? What are the Major Challenges?
CS Fall 2016 (Shavlik©), Lecture 11, Week 6
Done Done Course Overview What is AI? What are the Major Challenges?
Constraint Satisfaction Problems vs. Finite State Problems
Using a Stack Chapter 6 introduces the stack data type.
Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9:
Using a Stack Chapter 6 introduces the stack data type.
CSE (c) S. Tanimoto, 2001 Search-Introduction
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Search.
Search.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Solving sudoku: work in progress Jeanine Meyer Purchase College/SUNY Math/CS Senior Seminar

Outline sudoku game general comments on A.I. Flash application to help player –does not produce new games Flash application to solve: in progress Note: a computer talk rather than a mathematics talk Will repeat this!

sudoku game originated in Japan appears in many newspapers –The Journal News built on 'magic squares' category of problem: –produce result satisfying set of constraints –more general problem is: produce a best result (maximize or minimize an objective function) given set of constraints For example, integer programming Mostly in class NP-complete many heuristic techniques available

Artificial Intelligence … applying computers to real-world, perhaps fairly open problems, including games Chess International Master David Levy's challenge inspired much work in AI and all computing –1968 bet no computer could beat him in 10 years. He won (played and beat various computer programs. There was at least one draw.) –1989 Deep Thought beat Levy –1997 Deep Blue beat the world champion (Gary Kasparov) after losing in 1996 Some controversy

AI The best computer algorithm may not be the human approach. –Will describe chess history Heuristic is term for methods that applies some special technique and may advance the process Brute force is term for less subtle process: any method that just generates possibilities My approach to sudoku –mixture –may not be the best –still working on it

Backtracking General AI technique Prune, as best as possible, the tree of possible choices/moves Make a choice Develop/extend this choice If it leads to a problem –backtrack: restore position before making choice –remove this choice from 'tree' Repeat

Does backtracking work? Assuming bad choices are removed to stop being used again, yes! –finite number of possible moves (though this number could be very large) unpopulated board in sudoku has 9 to the 81. How big is that???? However, size of game should restrict this to moves that do not violate rules…. How big is that?

Machine learning Build program to play many games Play any way….and see what works Sometimes used with neural nets: graph based way to encode moves/changes of state

Chess history Chess is a game against an opponent, not a puzzle to solve! First programs attempted to characterize positions using descriptions given by chess players –Did not do well Second generation: just use brute force to look ahead and ‘count material’ –Did better! –Some chess experts did try to adapt their game for this sort of play [Final] Deep Blue chess program based on [then] special hardware using parallel processors –uses 'book' of openings (as do most expert chess players) –middle game is mainly 'look ahead and count material' –end game uses chess knowledge (more added for 1997 match)

How do I play sudoku Examine board and see if there are any immediate fits –only one number is possible in a position (only-possible spot) –(more subtle) only one position is available for a particular number in a row, column or 3x3 (only-position) –(much more subtle) A pair of numbers that are jointly possible in a pair of positions in a row, column or 3x3 must occupy those positions so nothing else is possible –(much much more subtle) Same thing for 3 numbers… –(related) If number can just occur in single column or row of 3x3, it can’t occur in that column or row in another 3x3 –???? Put in any such numbers and repeat. Paper gets messy! Some descriptions of beautiful sudoku claim that guessing should not be required.

You? Other methods

My program Developed as helper/solver NOT Generate games –This is a topic in its own right! –Need to generate a partial board that only has one completion AND –[beautiful sudoku] can be solved using defined methods.

About implementation Staged Flash pushbuttons easy way to test out individual features Modularization important Needed to create situations that would test each heuristic (more on this later)

Program Much use of arrays –arrays of arrays –array of arrays of arrays Some conversions between numbers 1 to 9 and strings "1", "2", … stack for save and restore (push and pop) ‘okay so far’ coding

General procedure Generate and then maintain the set values. cset is two-dimensional array of strings, each string just 1 character long Generate the possibles: cposs is two- dimensional array of strings, each string may be 1 to 9 (rare) characters long. Do each heuristic Repeat Note: indices of arrays start with zero, but I ignore that position and just use 1 to 9.

set = ""; for (cx=1;cx<=9;cx++){ set=set+cset[r][cx]; } // holds set nums for (n=1;n<=9;n++) { sn = String(n); if (set.indexOf(sn)==-1) // n not set {count = 0; for (i=1;i<=9;i++) { //count if possible if (cposs[r][i].indexOf(sn)>-1) {fpos = i; count++; } } if (count==1) { cset[r][fpos]=sn; namec = "("+r+","+fpos+")"; _root[namec].cellvalue = sn; cposs[r][fpos] = ""; } } //ends n not assigned } //ends looping through numbers n 1 to 9 Code for only-position check for any number n in row r.

Development technique I added a button (labeled test) that calls a function that takes what has been written in the workspace area (little letters) and then calls… a new function to be tried. I used this to test the new heuristics directly rather than work to create a sample game. See the source code in sudoku.fla

Features of first program: helper enter initial board print option check save and restore –Stack (LIFO): last in/first out

Solver: sudoku1 (Done in Flash to build on first. Does use Flash input/output) Compute possibles Compute and set from only possibles Compute and set from only position Use buttons (can see small type) mes/sudoku1.fla

Solver sudoku2 (build on last) try button keeps track of rounds Each round –computes possibles –compute and set from only possible –(re-) compute possibles –compute and set from only position –checks if done doku2.fla

solver: sudoku3 (build on others) Add guessing –keep track of badguesses –guess bad if leads to bad check AND leads to a situation in which there are no more guesses Add automatic save on guessing and restore on bad guess Not fully tested… doku3.fla

Comments on sudoku3 save and restoring the badguess information is/was a challenge –do the restore (including restoring badguesses array) and THEN if (guesses.length>0) { badguesses.push(guesses.pop()); } check for no guesses is/was a challenge.. need a method to handle player guesses

“Group of 2” heuristic If a pair of numbers occurs as possibles in just two positions in a row, column or 3x3, then those numbers will occupy those two positions and so these 2 numbers must be removed from all other possibilities –A row (col) has a pair occupying two positions within a 3x3, say 12 and 12, then Remove 1 or 2 if it occurs in that row (col) outside the 3x3 Remove the 1 or 2 outside the row(col) in that 3x3 –Calculation done on possibles, so must be followed by other heuristics that produce new settings! The addition of this heuristic seems to improve success considerably.

Saving to local disk To support development (and playing), added the following Name and save a setting using Flash SharedObject –Like cookies –Player/user can disable or limit

Where?

To use SharedObject function savetodisk() { gamename = playersname.text; var so:SharedObject = SharedObject.getLocal(gamename); for (r=1; r<=nr; r++) { for (c=1; c<=nc; c++) { namec = "("+r+","+c+")"; cset[r][c] = _root[namec].cellvalue; } so.data.settings = cset; so.flush(); }

Count Added the feature to indicate the number of filled cells, in order to see if progress made.

Latest program Go to newmedia.purchase.edu/~Jeanine and take links to Flash examples and scroll down OR go directly to mes/sudoku7a.html

5 star challenge

Conclusion… Some difficulties may arise from desire to combine human/player system with automatic guessing (with backtracking) The latest set of heuristics solves many puzzles very quickly. The development task was motivation for several ‘working’ features Process (working in stages, using buttons to test individual functions) is good model.

Senior project More work (better?) on solver More work (better?) on helper Generating valid partial Sudoku also possibility –Valid means: 1 and only 1 completion –Beautiful Sudoku means that you don’t have to guess (may be other heuristics)