UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.

Slides:



Advertisements
Similar presentations
Recognising Languages We will tackle the problem of defining languages by considering how we could recognise them. Problem: Is there a method of recognising.
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
4. Using Structures: Example Programs. Contents l Retrieving structured information from a database l Doing data abstraction l Simulating a non-deterministic.
4b Lexical analysis Finite Automata
Lecture 24 MAS 714 Hartmut Klauck
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.
PROLOG 8 QUEENS PROBLEM.
1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
PROBLEM SOLVING AND SEARCH
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Problem Solving by Searching Copyright, 1996 © Dale Carnegie & Associates, Inc. Chapter 3 Spring 2007.
Problem Solving and Search in AI Part I Search and Intelligence Search is one of the most powerful approaches to problem solving in AI Search is a universal.
Cs774 (Prasad)L5ArithStruc1 Arithmetic ; Structures
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Blind State-Space Search Notes for Ch.11 of Bratko For CSCE 580 Sp03 Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Search I Tuomas Sandholm Carnegie Mellon University Computer Science Department [Read Russell & Norvig Chapter 3]
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Pushdown Automaton (PDA)
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
CS 503 Spring semester 2015 WPI Most of the slides below are due to Jeffrey Ullman (one of our textbok’s authors). They are slides from a course he gave.
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
Theory of Computing Lecture 22 MAS 714 Hartmut Klauck.
1 Structures and Strategies for State Space Search 3 3.0Introduction 3.1Graph Theory 3.2Strategies for State Space Search 3.3Using the State Space to Represent.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Heuristic Search Notes for Ch.12 of Bratko and Ch.2 of Shoham For CSCE 580.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco.
1 Non-Deterministic Finite Automata. 2 Alphabet = Nondeterministic Finite Automaton (NFA)
Finite State Machines Data Structures and Algorithms for Information Processing 1.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Monkey and Bananas Exercise Notes on Exercise 3.10 of Bratko For CSCE 580 Sp03.
4.1 Retrieving structured information from a database
Solving problems by searching This Lecture Read Chapters 3.1 to 3.4 Next Lecture Read Chapter 3.5 to 3.7 (Please read lecture topic material before and.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
FATIH UNIVERSITY Department of Computer Engineering Programming Style and Technique Notes for Ch.8 of Bratko For CENG421&553 Fall03.
FATIH UNIVERSITY Department of Computer Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CENG 421 Fall03 Zeynep Orhan.
1 Solving problems by searching This Lecture Chapters 3.1 to 3.4 Next Lecture Chapter 3.5 to 3.7 (Please read lecture topic material before and after each.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
1 Solving problems by searching 171, Class 2 Chapter 3.
Lecture # 12. Nondeterministic Finite Automaton (NFA) Definition: An NFA is a TG with a unique start state and a property of having single letter as label.
NFA defined. NFA A Non-deterministic Finite-state Automata (NFA) is a language recognizing system similar to a DFA. It supports a level of non-determinism.
Strings Basic data type in computational biology A string is an ordered succession of characters or symbols from a finite set called an alphabet Sequence.
Introduction to State Space Search
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.
Chapter 13 Backtracking Introduction The 3-coloring problem
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
1 Example: Graph of a DFA Start 1 0 ACB 1 0 0,1 Previous string OK, does not end in 1. Previous String OK, ends in a single 1. Consecutive 1’s have been.
  An alphabet is any finite set of symbols.  Examples: ASCII, Unicode, {0,1} ( binary alphabet ), {a,b,c}. Alphabets.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Lexical analysis Finite Automata
Non Deterministic Automata
Principles of Computing – UFCFA3-30-1
4. Properties of Regular Languages
CSE322 Definition and description of finite Automata
Introduction to Finite Automata
Finite Automata.
4b Lexical analysis Finite Automata
4b Lexical analysis Finite Automata
Solving problems by searching
Finite-State Machines with No Output
AUTOMATA THEORY.
Non Deterministic Automata
Solving problems by searching
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Retrieving Structured Information From a Database Each family has three components: husband, wife, and children, e.g.: family( person( tom, fox, date( 7, may, 1960), works( bbc, 15200)), person( ann, fox, date( 9, may, 1961), unemployed), [ person( pat, fox, date( 5, may, 1983), unemployed), person( jim, fox, data( 5, may, 1983), unemployed) ] ). Database and relations in ch4_1.pl 3 ?- family( H,W,C), total( [H,W|C],I), length([H,W|C],N), I / N <

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Datalog and Relational Operations See UseDefK2.ppt

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Doing Data Abstraction Selectors can be used to hide details of representation.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Simulating a Non-deterministic Automaton Non-deterministic finite automaton (NFA) –accepts or rejects a string of symbols –has states –has transitions (possibly silent) –represented by a directed graph with self loops with distinguished final states and initial state (e.g. fig. 4.3) –string is accepted if there is a transition path s.t. it starts with the initial state it ends with a final state the arc labels along the path correspond to the complete input string

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Simulation of NFA (II) To simulate an NFA in Prolog, we need: final/1, which defines the finals states trans/3, s.t. trans(S1,X,S2) means that a transition from S1 to S2 is possible when X is read silent/2 s.t. silent( S1,S2) means that a silent move is possible from S1 to S2.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering nfa.pl 6 ?- length(Str,7), accepts(S,Str). Str = [a, a, a, a, a, a, b] S = s1 ; 5 ?- accepts(S, String), length(String, 7). ERROR: Out of local stack 12 ?- accepts(s1,S). ERROR: Out of local stack Why? –If the length of the input string is not limited, the recursion will never hit the empty list: the first clause will be missed. When the input string is limited, after exhausting all possible transitions consistent with it, the input will become empty and the first clause will be used.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Travel Agent Program to give advice about air travel timetable(Place1,Place2,ListOfFlights) ListOfFlights holds terms of the form DepartureTime/ArrivalTime/FlightNumber/ListOfDays, where / is an infix operator, e.g., timetable( london, edinburgh, [9:40 / 10:50 / ba4733 / alldays, 19:40 / 20:50 / ba4833 / [mo,tu,we,th,fr,su] ] ).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Finding Routes route( Place1, Place2, Day, Route), where Route is a sequence of flights that satisfy: the start point of the route is Place1 the end point is Place2 all the flights are on the same Day of the week all the flights in Route are in the timetable relation there is enough time for transfer between flights

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Auxiliary Predicates flight( Place1,Place2,Day,FlightNum,DepTime,ArrTime). deptime( Route, Time). transfer( Time1, Time2). Note the similarity between the NFA simulation and the route finding problem: –states cities –transitions flights –path between initial and final state route between start and end city

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Route Relation Direct connection: route( Place1, Place2, Day, [Place1/Place2/Fnum/Dep]) :- flight( Place1, Place2, Day, Fnum, Dep, Arr). Indirect connection with sufficient transfer time: route( P1,P2,Day,[P1/P3/Fnum1/Dep1 | RestRoute]) :- route( P3,P2,Day,RestRoute), flight( P1,P3,Day,Fnum1,Dep1,Arr1), deptime( RestRoute, Dep2), transfer( Arr1,Dep2).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Full Program fig4_5.pl As for the NFA simulation program, to avoid infinite loops, make sure to limit the length of the route. E.g., 3 ?- route( rome,edinburgh,mo,R). gives an infinite loop, while the following does not: 6 ?- conc(R,_,[_,_,_,_]), route(rome, edinburgh,mo,R). No conc generates routes in order of increasing length, so that shorter routes are tried first

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Infinite Looping Trouble ?- route( ljubljana, edinburgh, th, R). R = [ljubljana/zurich/jp322/11:30, zurich/london/sr806/16:10, london/edinburgh/ba4822/18:40] ; Action (h for help) ? abort % Execution Aborted ?- conc( R,_,[_,_,_,_]), route( ljubljana, edinburgh, th, R). R = [ljubljana/zurich/jp322/11:30, zurich/london/sr806/16:10, london/edinburgh/ba4822/18:40] ; No

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Eight Queens: Program 1 How to place 8 queens on a cheesboard, so that they do not attack each other solution(Pos) if Pos is a solution to the problem Positions are represented by a list of squares where the queen is sitting, e.g: [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1] (fig.4.6, a solution) We generalize to square boards of any size, so that we can use induction

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Program 1, ctd. solution( [ ]). solution( [X/Y | Others]) :- solution( Others), member( Y, [1,2,3,4,5,6,7,8]), noattack( X/Y, Others). This shows how to add a queen to extend a partial solution

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Program 1, ctd. noattack( _,[ ]). noattack( X/Y, [X1/Y1 | Others] ) :- Y =\= Y1, % Different Y-coordinates Y1-Y =\= X1-X, % Different diagonals Y1-Y =\= X-X1, noattack( X/Y, Others). The full program is in fig4_7.pl Finds all (92) solutions upon backtracking and stops

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Eight Queens: Program 2 Represent X queen position by their position in the position list: –[1/Y1, 2/Y2, …., 8/Y8] is replaced by –[Y1, Y2, …., Y8] Generate an ordering of the Y positions, then test that position: solution( S) :- permutation( [1,2,3,4,5,6,7,8], S), %generate safe(S). %test

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Program 2, ctd. noattack/2 is generalized to noattack/3, where the third argument represents the X distance (column distance) of two queens Full program in fig4_9.pl To get all solutions, do: ?- setof( S, solution(S), L), length( L,N). S = _G405 L = [[1, 5, 8, 6, 3, 7, 2, 4], [1, 6, 8, 3, 7, 4, 2|...], [1, 7, 4, 6, 8, 2|...], [1, 7, 5, 8, 2|...], [2, 4, 6, 8|...], [2, 5, 7|...], [2, 5|...], [2|...], [...|...]|...] N = 92

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Eight Queens: Program 3 Program 3 uses a redundant representation of the board, with –columns, x, 1 through 8 –rows, y, 1 through 8 –upward diagonals, u = x – y, -7 through 7 –downward diagonals, v = x + y, 2 through 16 When a queen is placed, its column, row, and diagonals are removed from consideration pl4_11.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Eight Queens: Efficiency The second program is the least efficient: ?- time( setof( S, solution(S), L)). % 1,139,743 inferences in 1.10 seconds ( Lips) In general, generate-and-test is inefficient: it is best to introduce constraints as early as possible in the solution design process First program: ?- time( setof( S, solution1(S), L)). % 171,051 inferences in 0.22 seconds ( Lips) Third program: 1 ?- time( setof( S, solution(S), L)). % 120,544 inferences in 0.15 seconds ( Lips)