4. Using Structures: Example Programs. Contents l Retrieving structured information from a database l Doing data abstraction l Simulating a non-deterministic.

Slides:



Advertisements
Similar presentations
Methodological Innovations & Key Findings Jean Martin Department of Sociology UNIVERSITY of OXFORD From the Women and Employment Survey.
Advertisements

Conflict management By Alfred A. Jarrett. Conflict management You are married to a Man with 6 wives and you are the 7 th – the one with the only male.
AGES By Clay Tattersall And Paul Podlas. What? For our project we had chosen to take a look at married couples ages, for the husband and women, and compare.
1. An Overview of Prolog.
1 COMP313A Programming Languages Logic Programming (6)
Famous Victorian People By Michael Aberdeen. Contents Queen Victoria her life Queen Victoria her life Queen Victoria changes she made Queen Victoria changes.
Zuzana Karasová 7.ročník. She is 35. Her husband is Nicolas. Jennifer has 3 children : Justin, Susan & Matt. She married 12 years ago. She was born in.
What do you know about The Duke of Edinburgh’s Award? Year 7, unit 2, lesson 6.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Employee database: Conceptual Schema in ERD Chapter 3, page 62.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
4.1 Retrieving structured information from a database
By Anna Rogers. Coretta Scott King was born on April 27, 1927 in Heiberger, Alabama. She died in 2006 on January 30 th by ovarian cancer. She is best.
Reynolds Farley The University of Michigan Population Studies Center Institute for Social Research 426 Thompson Ann Arbor, Michigan August 1,
Early Childhood Education Need for quality child care.
BY ___________ My Big Biography Project. Childhood Date of Birth Their family Where they grew up or lived Nicknames Important childhood events.
FATIH UNIVERSITY Department of Computer Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CENG 421 Fall03 Zeynep Orhan.
Contents Distribution of Recipients Grounds for Income Support Conditions for Eligibility Current Processes Payments Population.
CHAPTER 5 SECTION 3 Sexual Abuse and Violence. Sexual Abuse Sexual abuse- is any sexual act without consent  Kissing  Fondling  Forced intercourse.
© Jalal Kawash Database Queries Peeking into Computer Science.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
Type your person’s name Copy and paste a picture of your person from the Internet.
Type your president’s name Copy and paste a picture of your president from the Internet.
– A ‘Golden Age’?. The Upper Classes The wealthiest owned several large houses The wives did not work Families would be looked after by dozens.
1 Household Interaction Impact on Married Female Labor Supply Zvi Eckstein and Osnat Lifshitz.
Queen Elizabeth II 1926 Elizabeth is Born 1947 Princess Elizabeth marries prince philip 1947 Princess Elizabeth marries prince philip 1953 the Coronation.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
The Night at the Museum PowerPoint Slide 1: Title Page 1.Name of Person 2.Picture of Person 3.Your name.
James Madison By:. Beginning to End Birth And Death James Madison was born in Port Conway Virginia on March 16, James Madison died at Montpelier.
wolfes wolves wolfs TRY AGAIN! wolfes wolves wolfs wolfes wolves.
China’s Lifestyle Family, Dating, Marriage, Children Policy.
Women's Rights in Afghanistan Project By: Julia Huynh and Joanna McElfish.
Miss Andrade's Kindergarten Family Journals What is a Family Journal? Weekly journal entry for you and your child Work together as a family Safe place.
For Friday No reading Prolog Handout 2. Homework.
by The McGraw-Hill Group of Companies Inc. All rights Reserved. McGraw-Hill/Irwin W ORK AND H OME CHAPTER 11.
The Family A group of people related by blood, marriage or other connection such as adoption © PDST Home Economics.
Queen Victoria ( ) 1837 Victoria became queen at the age of 18. Her reign lasted 64 years. She was an example to the people of her country, in.
Family Tree Project and Vocabulary
Families and Household. 23% on apparel, 48% entertainment, and 12% on fast food Driver- age of child, not rather than income 60% influence of children.
By Minhthu Date: 11/10/2015. Background Author: Nujood Ali with Delphine Minoui Location: Yemen is an Arab country in Southwest Asia Population :24.41.
AP Statistics Chapter 5 Section 3. Chance Toss coin 10 times. What is the likelihood of a run of 3 or more consecutive heads or tails? A couple plans.
Family ELL Warm Up Read the sentence and decide whether or not you agree or disagree with the statement. Afterwards, check with your group members.
Lady Macbeth’s Relationship
Trends in American Family Life
AN AVERAGE FINNISH LIFE
Ch 13 sec 3.
Copyright © American Speech-Language-Hearing Association
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
Prolog programming Introduction to Prolog
Main Affiliation (1) Minor Affiliation (2); Minor Affiliation (3)
Licensed Databases vs. Google Scholar: Which is more Scholarly?
Prolog programming Introduction to Prolog (part2)
Database.
Chapter 11 & 12-Living in a family
Loving Our Families Pastor Mike Pratt January 22, 2017.
Things Fall Apart: Gender Roles
Prolog programming Introduction to Prolog
The Godly Family.
CLICK the MOUSE TO PLAY! what is the plural of...?
The Changing American Family and Implications for Family Well-Being
Reynolds Farley The University of Michigan Population Studies Center
USA Learns Citizenship
Income Eligibility and Calculations
Prolog programming Introduction to Prolog (part4)
British Royal Family..
Your Name Here Class Here
Marriage and the Family
Cynthia Moriel, Adriana Hernandez, Jose Castaneda
Database SQL.
For the husband-wife, parent-child, and employer-employee relationships to thrive and succeed:
Presentation transcript:

4. Using Structures: Example Programs

Contents l Retrieving structured information from a database l Doing data abstraction l Simulating a non-deterministic automaton l Travel planning l The eight queens problem

Retrieving Structured Information from a DB l This exercise develops the skill of representing and manipulating structured data objects. l It also illustrates Prolog as a natural database query language. l A database can be represented in Prolog as a set of facts.

Retrieving Structured Information from a DB l A database about families: l We can refer to all Armstrong families by: family(person(_,armstrong,_,_),_,_) l Refer to all families with three children: family(_,_,[_,_,_]) family( person(tom,fox,date(7,may,1950),works(bbc,15200)), person(ann,fox,date(9,may,1951),unemployed), [person(pat,fox,date(5,may,1973),unemployed), person(jim,fox,date(5,may,1973),unemployed)]).

Retrieving Structured Information from a DB l To find all married women that have at least three children: ?-family(_,person(Name,Surname,_,_), [_,_,_|_]).

Retrieving Structured Information from a DB l Provide a set of procedures that can serve as a utility to make the intersection with the database more comfortable. husband(X):- family(X,_,_). wife(X):- family(_,X,_). child(X):- family(_,_,Children), member(X,Children). exist(X):- husband(X); wife(X); child(X). dateofbirth( person(_,_,Date,_),Date)). salary( person(_,_,_Works(_,S),S)). salary( person(_,_,_,unemployed),0).

Retrieving Structured Information from a DB Find the names of all the people in the DB: ?-exists(person(Name,Surname,_,_)). Find all children born in 1981: ?-child(X),dateofbirth(X,date(_,_,1981)). Find all employed wives: ?-wife(person(Name,Surname,_,works(_,_))). Find the names of unemployed people who were born before 1963: ?-exists(person,N,S,date(_,_,Y),unemployed)), Y<1963. Find people born before 1950 whose salary is less than 8000: ?-exists(P),dateofbirth(P,date(_,_,Year)), Year<1950,salary(P,S),S<8000.

Retrieving Structured Information from a DB l Total income of a family: total([],0). total([Person|List],Sum):- salary(Person,S0, total(List,Rest), Sum is S+Rest.

Simulating an NDA s1s2 s4s3 b a a b b null final(s3). trans(s1,a,a1). trans(s1,a,s2). trans(s1,b,s1). trans(s2,b,s3). trans(s3,b,s4). silent(s2,s4). silent(s3,s1).

Simulating an NDA accepts(State,[]):- final(State). accepts(State,[X|Rest]):- trans(State,X,State1), accepts(State1,Rest). accepts(State,String):- silent(State,State1), accepts(State1,String). ?-accepts(s1,[a,a,a,b]). yes ?-accepts(S,[a,b]). S=s1; S=s3. ?-accepts(s1,[X1,X2,X3]). X1=a X2=a X3=b; X1=b X2=a X3=b; no ?-String=[_,_,_], accepts(s1,String). String=[a,a,b]; String=[b,a,b]; no

Travel Planning l Develop an advisor program that will be able to answer useful questions, such as: –What days of the week is there a direct flight from London to Ljubljana? –How can I get from Ljubljana to Edinburgh on Thursday? –I have to visit Milan, Ljubljana and Zurich, starting from London on Tuesday and returning to London on Friday. In what sequence should I visit these cities so that I have no more than one flight each day of the tour?

Travel Planning l The program will be centered around a DB holding the flight information. timetable(Place1,Place2,List_of_flight) where List_of_flight is of the form: Departure_time/Arrival_time/Flight_number/List_of_days timetable(london,edinburgh, [9:40/10:50/ba4733/alldays, 17:40/20:50/ba4833/[mo,tu,we,th,fr,su]]).

Travel Planning l To find exact routes between two given cities on a given day of the week. route(Place1,Place2,Day,Route) Here Route is a sequence of flight satisfying the following criteria: –the start point of the route is Place1; –the end of the route is Place2; –all the flights are one the same day of the week Day; –all the flight in Route are in the timetable relation; –there is enough time for transfer between flights.

Travel Planning l The route is represented as a list of structured objects of the form: From-to:Flight_Number:departure_time l Some auxiliary predicates: –flight(Place1,Place2,Day,Flight_num,Dep_time,Arr_time) –deptime(Route,Time) –transfer(time1,Time2) There is at least 40 minutes between Time1 and Time2.

Travel Planning l The problem of finding a route is similar to the simulation of the NDA –States of NDA  cities –Transition between two states  a flight between two cities –transition  timetable –Finding a path  finding a route.

Travel Planning l Defining the route relation: –Direct flight route(Place1,Place2,Day,[Place1-Place2:Fnum:Dep]):- flight(Place,Place2,Day,Fnum,Dep,Arr). –Indirect flight route(P1,P2,Day,[P1-P3:Fnum1:Dep1|Route]):- route(P3,P2,Day,Route), flight(P1,P3,Fnum1,Dep1,Arr1), deptime(Route,Dep2), transfer(Arr1,Dep2). See Fig. 4.5, pp for the complete program.

Travel Planning l Some example questions: –What days of the week is tehre a direct flight from London to Ljubljana? ?-flight(london,ljubljana,Day,_,_,_). Day=fr; day=su; no –How can I get from Ljubljana to Edinburgh on Thursday? ?-route(ljubljana,edinburgh,th,R). R=[ljubljana-zurich:yu322:11:30, zurich-london:sr806:16:10, london-edinburgh:ba4822:18:40]

The Eight Queens Problem:Program 1 l The problem here is to place eight queens on the empty chessboard in such a way that no queen attacks any other queen

The Eight Queens Problem: Program 1 l The problem is to find such as list [X1/Y1,X2/Y2,X3/Y3,X4/Y4,X5/Y5,X6/Y6,X7/Y7,X8/Y8] l To make the search task easier, we fix the X-coordinates: [1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]

The Eight Queens Problem: Program 1 l Case 1: The list of the queen is empty: the empty list id certainly a solution because there is no attack. Case 2: The list of queen is non-empty: it looks like [X/Y|Others]. Then –There must be no attack between the queens in the list Others; i.e., Others must be a solution. –X and Y must be integers between 1 and 8. –A queen at square X/Y must not attack any of the queens in the list Others.

The Eight Queens Problem: Program 1 solution([]). solution([X/Y|Others]):- solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X/Y,Others). noattack(_,[]). noattack(X/Y,[X1/Y1|Others]):- Y=\=Y1,Y1-Y=\=X1-X,Y1-Y=\=X-X1, noattack(X/Y,Others). template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5, 6/Y6,7/Y7,8/Y8]). ?-template(S),solution(S).

The Eight Queens Problem: Program 2 l X-coordinates can be omitted, retaining only Y- coordinates: [Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8]. l To prevent the horizontal attack, no two queens can be in the same row. l Each solution is therefore represented by a permutation of the list: [1,2,3,4,5,6,7,8]. l Such a permutation, S, is a solution if all queens are safe.

The Eight Queens Problem: Program 2 solution(S):- permutation([1,2,3,4,5,6,7,8],S), safe(S). safe([]). safe([Queen|Others]):- sate(Others), noattack(Queen,Others,1). noattack(_,[],_). noattack(Y,[Y1|Ylist],Xdist):- Y1-Y=\=Xdist,Y-Y1=\=Xdist, Dist1 is Xdist+1, noattack(Y,Ylist,Disy1).

The Eight Queens Problem: Program 2 Queen Others Xdist=1 Xdist=3