The N-Queens Problem www.cs.colostate.edu/~cs115/Queens.ppt Search The N-Queens Problem www.cs.colostate.edu/~cs115/Queens.ppt Most slides from Milos Hauskrecht.

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

Heuristics, and what to do if you dont know what to do Carl Hultquist.
Local Search Algorithms
Informed search algorithms
Artificial Intelligence Presentation
Local Search Algorithms Chapter 4. Outline Hill-climbing search Simulated annealing search Local beam search Genetic algorithms Ant Colony Optimization.
BackTracking Algorithms
1 Transportation problem The transportation problem seeks the determination of a minimum cost transportation plan for a single commodity from a number.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Problem Solving Agents A problem solving agent is one which decides what actions and states to consider in completing a goal Examples: Finding the shortest.
September 26, 2012Introduction to Artificial Intelligence Lecture 7: Search in State Spaces I 1 After our “Haskell in a Nutshell” excursion, let us move.
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
Local search algorithms
1 Chapter 5 Advanced Search. 2 Chapter 5 Contents l Constraint satisfaction problems l Heuristic repair l The eight queens problem l Combinatorial optimization.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
Ryan Kinworthy 2/26/20031 Chapter 7- Local Search part 1 Ryan Kinworthy CSCE Advanced Constraint Processing.
1 Chapter 5 Advanced Search. 2 l
Backtracking.
Ch. 11: Optimization and Search Stephen Marsland, Machine Learning: An Algorithmic Perspective. CRC 2009 some slides from Stephen Marsland, some images.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Algorithms and their Applications CS2004 ( ) 13.1 Further Evolutionary Computation.
Artificial Intelligence for Games Online and local search
Local Search Algorithms
Local Search Pat Riddle 2012 Semester 2 Patricia J Riddle Adapted from slides by Stuart Russell,
Ricochet Robots Mitch Powell Daniel Tilgner. Abstract Ricochet robots is a board game created in Germany in A player is given 30 seconds to find.
Backtracking & Brute Force Optimization Intro2CS – weeks
Search in State Spaces Problem solving as search Search consists of –state space –operators –start state –goal states A Search Tree is an efficient way.
Program Development by Stepwise Refinement
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
N- Queens Solution with Genetic Algorithm By Mohammad A. Ismael.
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.
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
Local Search Algorithms CMPT 463. When: Tuesday, April 5 3:30PM Where: RLC 105 Team based: one, two or three people per team Languages: Python, C++ and.
Eight Queens Problem The problem is to place 8 queens on a chess board so that none of them can attack the other. A chess board can be considered a plain.
Constraints Satisfaction Edmondo Trentin, DIISM. Constraint Satisfaction Problems: Local Search In many optimization problems, the path to the goal is.
Local search algorithms In many optimization problems, the path to the goal is irrelevant; the goal state itself is the solution State space = set of "complete"
Section 1: Problem solving AQA Computing A2 © Nelson Thornes 2009 Examples of problems with different time complexities Section 1.2: Comparing algorithms.
Games: Expectimax MAX MIN MAX Prune if α ≥ β. Games: Expectimax MAX MIN MAX
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
CSG3F3/ Desain dan Analisis Algoritma
Recursion Data Structure Submitted By:- Dheeraj Kataria.
BackTracking CS255.
CSSE 230 Day 25 Skip Lists.
Introduction to Artificial Intelligence
Local Search Algorithms
Design and Analysis of Algorithm
Constraint Satisfaction Problem
Computer Science cpsc322, Lecture 14
Artificial Intelligence Problem solving by searching CSC 361
Artificial Intelligence (CS 370D)
Recursion Copyright (c) Pearson All rights reserved.
Applied Combinatorics, 4th Ed. Alan Tucker
adapted from Recursive Backtracking by Mike Scott, UT Austin
3. Brute Force Selection sort Brute-Force string matching
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
3. Brute Force Selection sort Brute-Force string matching
Backtracking and Branch-and-Bound
Artificial Intelligence CIS 342
Local Search Algorithms
Graphs.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Local Search Algorithms
Backtracking, Search, Heuristics
Solving Problems by Searching
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

The N-Queens Problem www.cs.colostate.edu/~cs115/Queens.ppt Search The N-Queens Problem www.cs.colostate.edu/~cs115/Queens.ppt Most slides from Milos Hauskrecht

N-queens problem Find a configuration of n queens not attacking each other

What is the maximum number of queens that can be placed on an n x n chessboard such that no two attack one another? The answer is n queens, which gives eight queens for the usual 8x8 board

12 Unique Solutions 8-Queens Problem

8-Queens Problem There are 92 distinct solutions There are 12 unique solutions discounting symmetrical answers (rotatations/reflections) How many solutions for 4-Queens? N-Queens? Wikipedia.org

Problems N < 4 N < 4 Cannot use N Queens 3 2 1

What is the minimum number of queens needed to occupy or attack all squares of an 8x8 board?

Search Solving problems by searching Some problems have a straightforward solution Just apply the formula, or follow a standardized procedure Example: solution of the quadratic equation Hardly a sign of intelligence More interesting problems require search: more than one possible alternative needs to be explored before the problem is solved the number of alternatives to search among can be very large, even infinite.

Search Problems is defined by: Search space: – The set of objects among which we search for the solution Example: objects = routes between cities, or N-queen configurations Goal condition – What are the characteristics of the object we want to find in the search space? – Examples: Path between cities A and B Path between A and B with the smallest number of links Path between A and B with the shortest distance Non-attacking n-queen configuration

N-Queens Problem Problem: -- How do we represent the search space?

N-Queens Problem Problem: -- How do we represent the search space? – We look for a target configuration, not a sequence of moves – No distinguished initial state, no operators (moves) -- Don’t use a graph

A Solution: Know your domain Since there are N rows and N queens, there must be a queen in each column (why?), Put a queen in each row, but also need to pick a row for each queen. Randomize the rows at the beginning, and then, in each iteration move one queen that reduces the number of threatened queens by a maximum. We do it in each iteration, until the number if threatened queens reach 0, or we get to a situation where we can’t improve any more, because we had a bad start - so we start over, randomizing locations again, and doing the whole thing again, until the queens a safe spot.

Solution: Backtracking place a queen in the top row, then note the column and diagonals it occupies.  then place a queen in the next row down, taking care not to place it in the same column or diagonal.  Keep track of the occupied columns and diagonals and move on to the next row.  If no position is open in the next row, we back track to the previous row and move the queen over to the next available spot in its row and the process starts over again. Demo: http://www.math.utah.edu/~alfeld/queens/queens.html

Find 1 Solution: Iterative (non-search) For N > 4 only N is even except N ≠ 6K+2 : Row 1 to N/2: Queen on 2*Row Row N/2+1 to N: Queen on 2*Row-N-1 N is even, N = 6K+2 Row 1 to N/2: Queen on (2*Row + N/2 - 3)mod N +1 Row N/2+1 to N: Queen on N - (2*(N-Row+1) + N/2 - 3)mod N N is odd: When N is even, no queen is placed on position (1,1). So this just places the first N-1 queens as on an N-1 (even) sized board, then places the last queen on the bottom right position (N,N). 8 6 7

Solution: Genetic Algorithms Maintain a list of potential solutions Modify potential solutions in parallel Crossover Randomly swap X number of Queen positions Mutation Randomly change a Queen’s position to a new random location

Problems

Time is an issue difficult for a search algorithm such as depth-first search with backtracking to find a solution for the N-queens problem in an acceptable amount of time. It took over 11 days to get the results for N = 20

Computational Considerations Computer solutions to the N-Queens problem are basically the same as the method you would use by hand. It is simply a brute force trial and error method. The amount of time required to find all solutions for any order “N” is roughly proportional to “N” Factorial. It took over 11 days to get the results for “N” = 20. If we increase “N” to 21, it would take about 4 months for the program to run. For higher orders of “N”, the problem has to be broken into parts with each part delegated to a separate computer. Thus, dozens and more likely, hundreds of computers are needed to solve problems with “N” in the low 20's. With present computing power, it is unlikely that the total number of solutions will be found when “N” equals 30 or higher.

Why is this important? In and of itself, the N queens problem is not important.   However, the problem works as an interesting optimization testbed there are many, many solutions to this problem on the web, but few of them are fast. Also, techniques used to speed up this code can be used in other areas, such as a chess-playing program or a unit path-finding algorithm for a game.

Search Problems What do we care about solving such a trivial toy problem? What other problems require search? Patents for circuits discovered by a software program!

Toy Problems Ideal for benchmarks, comparisons Is your program faster/more efficient than others on problem X? Solve Sudoku puzzle, size N Solve crossword puzzles Traveling Salesman Ant-colony optimization

Toy Problems in CS Toy Problems attempts to show examples of interesting systems and questions, which can be addressed well with a little programming, but which are not primarily about programming per se. Instead, Toy Problems is about the world around us. Examples are taken from basic physics, everyday phenomena, and basic (but interesting) math. What all of them have in common is that they lend themselves to short, easy programs, often with a little graphical output. (If a graph says more than a thousand words, a program producing graphics is a thousand times more interesting to write.) Another very important point Toy Problems tries to make is that the world around us can be understood.