PROLOG 8 QUEENS PROBLEM.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
PROLOG MORE PROBLEMS.
Polynomial Time Algorithms for the N-Queen Problem Rok sosic and Jun Gu.
Local Search Algorithms Chapter 4. Outline Hill-climbing search Simulated annealing search Local beam search Genetic algorithms Ant Colony Optimization.
8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
Modern Programming Languages
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.
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
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.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Search Problems Russell and Norvig: Chapter 3, Sections 3.1 – 3.3 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm by Prof. Jean-Claude.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Data Structures Using C++ 2E Chapter 6 Recursion.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
Data Structures Using C++ 2E Chapter 6 Recursion.
COSC2007 Data Structures II
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
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.
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Solving N-Queens in Clojure
Two Dimensional Arrays
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
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.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
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"?
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
Backtracking & Brute Force Optimization Intro2CS – weeks
© 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.
Chapter 13 Backtracking Introduction The 3-coloring problem
N-queens problem Original problem: How to place 8 queens on an 8x8 chessboard so that no two queens attack each other N-queen problem: Generalization for.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
An Analysis of the n- Queens problem Saleem Karamali.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Search Problems Russell and Norvig: Chapter 3, Sections 3.1 – 3.3 CS121 – Winter 2003.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Friend Function.
CSSE 230 Day 25 Skip Lists.
Intro to Computer Science II
Sit-In Lab 1 Ob-CHESS-ion
Constraint Satisfaction Problem
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"?
Data Structures Using Java
Recursion Copyright (c) Pearson All rights reserved.
Back Tracking.
Analysis and design of algorithm
Eight-QueenS Problem: Fuzzy Constraint Satisfaction Problem Using Soar
Solving Inequalities by Adding or Subtracting
Magic Squares   10   X.
Ch. 6 Recursion as a Problem Solving Technique
CS Software Studio Assignment 1
Exercise: Dice roll sum
Russell and Norvig: Chapter 3, Sections 3.1 – 3.3
The N-Queens Problem Search The N-Queens Problem Most slides from Milos Hauskrecht.
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"?
SYSTEMS.
1.6 Absolute Value Equations and Inequalities
Data Structures & Programming
Presentation transcript:

PROLOG 8 QUEENS PROBLEM

8 QUEENS Place 8 queens on an 8 X 8 chessboard so that: Queens do not attack each other

8 QUEENS Solution will be programmed as a unary predicate solution(Position) Will be true if Position represents a valid chessboard position for the 8 queens and no 2 queens can attack each other

8 QUEENS Representation of the chess board position Each queen will be represented by X and Y (row and column) We will define a new operator, : X:Y Potential solution: [X1:Y1, X2:Y2, X3:Y3, …, X8:Y8]

8 QUEENS Since every queen will have to be in a different row  We can hard code all the Xis to be 1 to 8 A potential solution can be templated as : [1:Y1, 2:Y2, 3:Y3, .., 8:Y8]

8 QUEENS Recursive formulation of the problem We will “think” about this problem in terms of placing N queens not just 8  recursive relation between N queens and (N – 1) queens Use this to solve when number of queens is 8

8 QUEENS Recursive formulation of the problem List of N queens Base case: list of queens is empty General case: list of queens looks like [X:Y | Others] and Others itself is a solution X and Y must be integers between 1 and 8 The X:Y queen must not attack any queen in Others

8 QUEENS solution([]). % no queen  good solution([X:Y | Others]) :- solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X:Y,Others). % need to define % X will be hard coded 1 to 8

8 QUEENS Need to define predicate noattack noattack takes 2 arguments: A queen A list of queens Base case: list of queens is empty The queen cannot attack the queens in the empty list (there is not any)

8 QUEENS Base case for noattack noattack(_,[]). A queen (1st argument) cannot be attacked by 0 queen

8 QUEENS General case If the list of queens is not empty, then it can be written [X1:Y1 | Others] where X1:Y1 represents the first queen (head) and Others the other queens (tail) noattack(X:Y,[X1:Y1 | Others]) :- ????

8 QUEENS noattack(X:Y,[X1:Y1 | Others]) :- ???? That will be true if: Queen X:Y cannot attack queen X1:Y1, and Queen X:Y cannot attack any of the queens in the list of queens Others

8 QUEENS One queen vs another (X:Y vs X1:Y1) X and X1 must be different but that is automatic (the Xis are hard coded) Y and Y1 must be different (column) Y1 – Y must be different from X1 – X (main diagonal) Y1 – Y must be different from X – X1 (other diagonal)

8 QUEENS One queen vs another (X:Y vs X1:Y1) Y1 and Y must be different X1 and X are hard coded and automatically different Y1 – Y different from X1 – X Y1 – Y different from X – X1 We will use the =\= operator , arithmetic not equal

8 QUEENS General case for noattack noattack(X:Y,[X1:Y1 | Others]) :- Y =\= Y1, Y1 – Y =\= X1 – X, Y1 – Y =\= X – X1, noattack(X:Y, Others). % X and X1 are hard coded different

8 QUEENS solution([]). % no queen solution([X:Y | Others]) :- solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X:Y,Others).

8 QUEENS noattack(_,[]). noattack(X:Y,[X1:Y1 | Others]) :- Y =\= Y1, Y1 – Y =\= X1 – X, Y1 – Y =\= X – X1, noattack(X:Y, Others).

8 QUEENS Hard code the Xis into 1 to 8 template([1:Y1, 2:Y2, 3:Y3, …, 8:Y8]). To run the program: template(S), solution(S).