12.07.2015 Views

CS 345 Project 3: A Prolog Travel Agent

CS 345 Project 3: A Prolog Travel Agent

CS 345 Project 3: A Prolog Travel Agent

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>CS</strong> <strong>345</strong> <strong>Project</strong> 3: A <strong>Prolog</strong> <strong>Travel</strong> <strong>Agent</strong>Assignment date: Wednesday, November 20, 2002Due date: Monday, December 9, 20021 Introduction<strong>Prolog</strong>’s strengths lie in the fact that it allows you to easily encode facts aboutthe world, and it includes an automated inference engine that can deduce theconsequences of these facts. In this assignment, we’ll take advantage of thesestrengths to build a simple expert system that will help us plan air travel routes.2 Representing FlightsFirst, let’s look at how we’ll represent a flight between two cities. Flights canleave at several times during the day, and might not fly every day.A single flight has a departure time, an arrival time, a flight number, andone or more days that it flies on. We’ll represent this as:departure / arrival / flight_number / list_of_daysfor example:9:40 / 12:10 / nw600 / [mon, tue, wed, thu, fri]The ’/’ is used only to divide fields here, not as an arithmetic operator.We’ll call all the flights between two cities a timetable. A timetable consistsof a departure city, a destination city, and a list of flights. For example:timetable(new_york, san_francisco,[12:10 / 5:00 / nw610 / [mon, tue, wed, thu, fri],5:30 / 10:15 / united440 / [mon , tue, fri] ]).The sample code contains a flight database with a number of flights in it.However, not every flight is in the database. . For your first task, add additionalflights to the database so that, if there is a flight from city A to city B, there isalso a flight from city B to city A. You can specify whatever dates, times andflight numbers you wish.1


Next, notice that some of the flights use the term alldays, rather than listingeach day of the week. The sample code contains a clause called flyday thatwill be used to determine whether there is a flight on a given day. Currently, itworks for lists of cities and for the term alldays. Add an additional clause so thatwe can also use ’weekdays’ to determine when a flight happens. A flight flieson weekdays if it flies on Monday, Tuesday, Wednesday, Thursday and Friday.Update your flight database to use weekdays where appropriate.3 Finding a RouteThe code included under the heading Route Finding tells <strong>Prolog</strong> how to constructa route between any two cities. Essentially, the algorithm is: There isa route between A and B if there’s a flight from A to B, or if there’s a flightfrom A to C and one from C to B and there’s enough time to transfer betweenflights.Currently, the code says that we can construct a route if the traveler has 40minutes to transfer between flights. We would like to change this to reflect thefact that transfers take longer at larger airports.First, let’s assume that there are three types of airports: large, medium andsmall. New York, Chicago and Los Angeles are large airports. San Francisco,Dallas and Miami are medium airports. Everything else is a small airport. Addfacts to your knowledge base to reflect this. Note: there is an easy way and ahard way to do this. You should use the easy way; that is, the way that requiresmaking the fewest changes to your database. To do this, think about what youwant the default airport size to be.Now, we’ll modify transfer to be a three-place relation, where the third argumentis the city where we’re transferring. Rewrite trasfer so that it’s possibleto transfer if we have 40 minutes in a small airport, 60 minutes in a mediumairport, and 90 minutes in a large airport.You should now be able to ask queries like:• How can I get from New York to Los Angeles?• What days of the week can I fly directly from San Francisco to Seattle?• Where and when is flight nw323?Try querying the database in different ways.4 Consecutive CitiesOne thing we can’t yet do is specify a tour. Perhaps I’d like to visit New York,Chicago and Dallas on consecutive days, or San Francisco, Los Angeles andSeattle all on the same day.To do this, we need to be able to generate a permutation of a list. We’ll dothis recursively. The provided code contains clauses for deleting an item from2


a list, and then defines insert in terms of delete. think about this one a bit -what this says is that, in order to get a list L that has item X added to it, finda list such that if we deleted X from it, we’d wind up with L.This code also has a definition of permutation that uses delete. It recursivelydeletes an element from the input list. When the recursion bottoms out, theseitems are then prepended to the output list.First, write a version of permute that uses insert instead of delete. The logicgoes like this: The empty list is its own permutation. If a list is not empty,it has a head and a tail. Permute the tail, and then insert the head into thepermuted list.Now, use your version of permute to create a clause called tour. Let’s startsimple: assume that I want to visit three cities on the same day, but I don’tcare about the order. I can do this if there’s flights from the first to the secondand the second to the third. Write a clause called tour that does this. Tourshould look like this:tour([san_francisco,dallas,new_york],List).Your code should fill in List with a list of each of the flight numbers on thetour.OK, now that we’ve got this, generalize tour to work on any number of cities.And finally, let’s make our tour more interesting. Instead of flying to differentcities all in the same day, we’d like to spend an evening in each city. So, thismeans that we’ll need to change tour to find flights on consecutive days. You’llwant to add some knowledge to your database indicating that, for example,Tuesday comes after Monday.5 Limiting the length of the tripSo far, we’ve been able to call route like this and get an answer:route(new_york, chicago, mon, R).This works fine if there is a route from New York to Chicago, but it fails ifthere isn’t. For example,route(oakland, chicago, mon, R).will run forever.In this task, we’ll fix the length of the route to be a maximum of 4. Thisway, if we can’t find a route of length 4 or less, we’ll fail immediately.To do this, we need to make a concatenation operator. This is the same asthe append operator we’ve discussed in class. Write a clause (or find it in theclass notes) that concatenates two lists and stores the result in a third list.We can use concatenate in a rather clever way to limit the length of R, thetotal route. Recall that if we leave one of the ’inputs’ to concatenate unbound,we can ask <strong>Prolog</strong> to find a list that satisfies the concatenation. For example:3


?- conc(X,[d,e,f],[a,b,c,d,e,f]).X = [a, b, c]Now, think about how you could ask <strong>Prolog</strong> to find a value for X that hadthree elements in it, but you didn’t care what those elements were.You’re almost there. Now, you’ll want to use conc to control the length ofthe route generated by route. So, write a ’wrapper’ clause called four route thatfirst calls conc and fixes the length of the route to be four or less, and then callsroute.6 User InterfaceFinally, we need to build a user interface for our route planner. No one wantsto sit and enter <strong>Prolog</strong> queries all day to find out what flights to take.I’ve provided a toplevel shell for you called ui. When you type ui into theinterpreter, it’ll print a list of options. The user can then type in a number(followed by a period). Your task is to implement the process choice clausefor the three different possibilities (route, tour or exit). Feel free to make theinterface as elaborate as you’d like. You can either let users enter cities, orchoose them from a list. You should display the results in a nice format - don’tjust dump out the variable bindings. Print the flight information nicely, so thatit’s easy for the user to read.4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!