Using Structures: Example Programs - PowerPoint PPT Presentation

About This Presentation
Title:

Using Structures: Example Programs

Description:

Title: PowerPoint Presentation Last modified by: mgv Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show Other titles – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 21
Provided by: cseScEdu76
Learn more at: https://cse.sc.edu
Category:

less

Transcript and Presenter's Notes

Title: Using Structures: Example Programs


1
Using Structures Example Programs
  • Notes for Ch.4 of Bratko
  • For CSCE 580 Sp03
  • Marco Valtorta

2
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,WC,I),
    length(H,WC,N), I / N lt 10000.

3
Datalog and Relational Operations
  • See UseDefK2.ppt

4
Doing Data Abstraction
  • Selectors can be used to hide details of
    representation.

5
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

6
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.

7
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.

8
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,
  • 940 / 1050 / ba4733 / alldays,
  • 1940 / 2050 / ba4833 / mo,tu,we,th,fr,su
    ).

9
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

10
Auxiliary Predicates
  • flight( Place1,Place2,Day,FlightNum,DepTime,ArrTim
    e).
  • deptime( Route, Time).
  • transfer( Time1, Time2).
  • Note the similarity between the NFA simulation
    and the route finding problem
  • states lt-gt cities
  • transitions lt-gt flights
  • path between initial and final state lt-gt route
    between start and end city

11
Route Relation
  • Direct connection
  • route( Place1, Place2, Day, Place1/Place2/Fnum/De
    p) - 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).

12
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

13
More Infinite Looping Trouble
  • ?- route( ljubljana, edinburgh, th, R).
  • R ljubljana/zurich/jp322/1130,
    zurich/london/sr806/1610, london/edinburgh/ba48
    22/1840
  • Action (h for help) ? abort
  • Execution Aborted
  • ?- conc( R,_,_,_,_,_), route( ljubljana,
    edinburgh, th, R).
  • R ljubljana/zurich/jp322/1130,
    zurich/london/sr806/1610,
    london/edinburgh/ba4822/1840
  • No

14
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

15
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

16
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

17
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

18
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

19
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

20
Eight Queens Efficiency
  • The second program is the least efficient
  • ?- time( setof( S, solution(S), L)).
  • 1,139,743 inferences in 1.10 seconds (1034640
    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 (776387
    Lips)
  • Third program
  • 1 ?- time( setof( S, solution(S), L)).
  • 120,544 inferences in 0.15 seconds (802471 Lips)
Write a Comment
User Comments (0)
About PowerShow.com