Program Assignment 3

100 points
Due: 11/13

LP Airlines is a small, private airline company that flies routes between a number of small cities, including Roanoke, VA. Here is a sample of part of LPA's flight timetable and route map:

flight map

Flight No. From To Departs Arrives Days
lp1211Columbus Charlotte 11:1012:20Mon Tue Wed Fri Sun
lp1212Charlotte Columbus 13:2016:20Mon Tue Wed Fri Sun
lp1322Columbus Pittsburgh11:3012:40 Tue Thu
lp1323PittsburghColumbus 13:3014:40 Tue Thu
lp1458Roanoke Charlotte 09:1010:00Mon Tue Wed Thu Fri Sat Sun
lp1459Charlotte Roanoke 11:0013:50Mon Tue Wed Thu Fri Sat Sun
lp1472Columbus Charlotte 20:3021:30Mon Wed Thu Sat
lp1473Charlotte Columbus 16:3019:30Mon Wed Thu Sat
lp1510Charlotte Roanoke 08:3011:20Mon Tue Wed Thu Fri Sat Sun
lp1511Roanoke Charlotte 12:2013:10Mon Tue Wed Thu Fri Sat Sun
lp1613PittsburghCharlotte 09:0009:40Mon Tue Wed Thu Fri Sat
lp1614Charlotte Pittsburgh09:1011:45Mon Tue Wed Thu Fri Sat Sun
lp1620PittsburghRoanoke 07:5508:45Mon Tue Wed Thu Fri Sat Sun
lp1621Roanoke Pittsburgh09:2510:15Mon Tue Wed Thu Fri Sat Sun
lp1623Roanoke Pittsburgh12:4513:35Mon Tue Wed Thu Fri Sat Sun
lp1805Charlotte Pittsburgh14:4517:20Mon Tue Wed Thu Fri Sun
lp1806PittsburghCharlotte 16:1016:55Mon Tue Wed Thu Fri Sun
lp4732Charlotte Raleigh 09:4010:50Mon Tue Wed Thu Fri Sat Sun
lp4733Raleigh Charlotte 09:4010:50Mon Tue Wed Thu Fri Sat Sun
lp4752Charlotte Raleigh 11:4012:50Mon Tue Wed Thu Fri Sat Sun
lp4773Raleigh Charlotte 13:4014:50Mon Tue Wed Thu Fri Sat Sun
lp4822Charlotte Raleigh 18:4019:50Mon Tue Wed Thu Fri
lp4833Raleigh Charlotte 19:4020:50Mon Tue Wed Thu Fri

The goal of this assignment is to create a simple route planner that can be used in making flight reservations. Given constraints on a passenger's itinerary--such as a chosen starting point and ending point, for example--your program should find and display all possible routes meeting those constraints.

The constraints that may be specified by a passenger are:

All of these constraints are optional; the traveller is free to specify only those that are appropriate. Your route planner should find all possible flight plans that satisfy the constraints given. Further, the flight plans generated by your route planner should obey these common sense restrictions for connecting flights:

For this half of the assignment, the implementation language is Prolog. You will be programming in a logical programming style, as described in Chapter 16 of the text book. Since this program must be written in Prolog, appropriate use of logic language features is expected. In particular, you may not use any versions of assert, retract, record, erase, flag, or any of the other database features of Prolog.

As with prior programs, your solution must adhere to good programming style, including correct use of parameter passing, identifier naming, predicate length, commenting, headers for each predicate, etc.

As you work through this assignment, you may find the following resources useful:

In order to construct a solution, one must first devise a representation scheme for encoding flight data stored in the flight timetable. You will end up with a more readable (and easier to write) solution if you use a simple prolog structure to represent time information. If you place the following goal at the top of your program, you will introduce a new "operator symbol" that you can use later in your code:

:-  op( 50, xfy, :).

This is a goal rather tha a rule--the :- appearing without any lefthand side instructs the interpreter to execute the righthand side as it interprets the file, rather than to define a new rule (it means the same as ?-, which can also be used to include goals within a source file). This goal tells the interpreter that : (the colon) is a binary infix operator with a precedence level of 50. You can now use a colon between two data items in an argument position to create a two-field structure.

We can use our new operator in structuring the two parts of time values (the hours and the minutes) so that they can be extracted easily. Times constructed this way can be built or separated into their constituent elements through Prolog's pattern matching in predicate parameter positions. For example, consider this:

    write_time( Hours:Minutes ) :- /* Extract structure's parts using a pattern */
        write( Hours ),
        write( ' hours and ' ),
        write( Minutes ),
        write( ' minutes.' ),
        nl.

    /* It can also combine elements into a single structure */
    ?- write_time( 13:15 ).

We can then easily use this new operator to describe times in a convenient fashion in our timetable, which can be represented as a set of Prolog facts. Each "entry" in our timetable can be laid out as a 5-item list, consisting of the flight number, starting city, ending city, departure time, arrival time, and a list of days on which the flight flies:

    timetable( [lp1211, columbus, charlotte, 11:10, 12:20,
                               [ mon, tue, wed, fri, sun ] ] ).

It is then a simple matter to encode the flight timetable as a list of such facts.

You will notice that most of the data is represented in the form of atoms (identifiers that start with lower case letters). It is fine to convert any proper name in the assignment to all lower-case when representing it as an atom.

Write your program as a Prolog predicate called plan_routes that matches the following signature:

plan_routes( From_City, To_City, From_Time, To_Time, Day ) :-
    /* your definition goes here */.

This predicate could be called with specific values for any or all of the parameters, or "don't care" variables for unspecified constraints. If the departure time is specified, any flight plans beginning on or after that time should be included; if the arrival time is specified, any flight plans ending on or before that time should be included.

Your plan_routes predicate should print out a one-line header indicating the query, followed by a list of all possible flight plans matching the query in the format shown in the following examples:

?- plan_routes( pittsburgh, columbus, _From_Time, 15:00, tue ).
All flight plans from pittsburgh any-time to columbus 15:0 on mon:

    - Plan -------------
    lp1323 from pittsburgh 13:30 to columbus 14:40 tue
    --------------------

?- plan_routes( roanoke, columbus, _From_Time, _To_Time, mon ).

All flight plans from roanoke any-time to columbus any-time on mon:

    - Plan -------------
    lp1458 from roanoke 9:10 to charlotte 10:0 mon
    lp1212 from charlotte 13:20 to columbus 16:20 mon
    --------------------
    - Plan -------------
    lp1458 from roanoke 9:10 to charlotte 10:0 mon
    lp1473 from charlotte 16:30 to columbus 19:30 mon
    --------------------
    - Plan -------------
    lp1458 from roanoke 9:10 to charlotte 10:0 mon
    lp4752 from charlotte 11:40 to raleigh 12:50 mon
    lp4773 from raleigh 13:40 to charlotte 14:50 mon
    lp1473 from charlotte 16:30 to columbus 19:30 mon
    --------------------
    - Plan -------------
    lp1511 from roanoke 12:20 to charlotte 13:10 mon
    lp1473 from charlotte 16:30 to columbus 19:30 mon
    --------------------

?- plan_routes( roanoke, columbus, 15:00, _To_Time, _Day ).

All flight plans from roanoke 15:0 to columbus any-time on any-day:

    no routes available.

In printing out flight data, remember that write and nl are the basic output primitives. write can display any Prolog data structure, including times using our custom : operator. In the header for the list of flight plans, use any-city, any-time, or any-day to indicate unspecified constraints.

Your Prolog implementation is to be organized in a single file. All documentation and identifying information should be placed in comments within the single source file. The comments at the beginning of the file should identify the assignment and give your full name. Every Prolog predicate should be preceded by a comment block explaining its purpose, the meaning of each parameter, and the general strategy of its implementation if applicable.

Your program will be submitted electronically through the Curator for automatic grading against a randomly generated test suite. To submit your program, login to the Curator using your PID and password. Be sure to select the 91389-CS 3304 Comparative Languages section when logging in. Click on "Submit", choose 3-Prolog from the project drop-down list, click "Browse" to specify your solution, and then click "Upload."

In addition, you must also turn in a printed version of your program source in class according to the rules specified in the syllabus, which also describes the late policy. Your hardcopy printout must match the highest-scoring submission sent to the Curator earliest. In other words, you may not submit your program until it works, go back and add comments and documentation, and then make a final submission of the printable version of your program. Once you receive full credit from the Curator on a submission, that is the version you must print and hand in.