MacTech Network:   MacTech Forums  |  MacForge.net  |  Computer Memory  |  Register Domains  |  Cables  |  iPod Deals  | Mac Deals  | Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  Get a copy of MacTech RISK FREE  
MacTech Only Search:
Community Search:
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  
 Get Netflix

March 1998 Programmer's Challenge

 Help Peter Get Home

Mail solutions to: progchallenge@mactech.com
Due Date: 11:59pm ET, Sunday, 1 March 1998

Last summer, while he was on his way back to Perth from MacHack, Peter Lewis suggested a Challenge based on airline schedules. His idea was to select the quickest route from an origin to a destination, given a schedule of flights between pairs of cities. This month's Challenge is based on Peter's suggestion, but embellished to capture the "probabilistic" nature of airline travel these days.

Your job is to write a routine FindQuickestRoute that will route Peter from his departureAirport to his arrivalAirport in as few simulated flight hours as possible. Peter's trip begins on startDay at startTime. He has a list of airports to which he can fly, and an airlineSchedule of flights between pairs of those airports. The airlineSchedule consists of a flightNumber that will take him from a fromAirport to a toAirport, nominally departing at a scheduledDepartureTime, and nominally taking nominalFlightDuration seconds to get there. The scheduledDepartureTime is expressed in the local time zone of the fromAirport, which is provided as a timeOffset from Universal Time associated with each of the airports. Some of the flights in the airlineSchedule operate only on particular days, described in the operatingDays associated with each flightNumber.

Each of the airports has a minConnectTime, which is the minimum amount of time following one's arrival at the airport before one can catch a connecting flight. This applies to the initial flight, which must be scheduled to leave at least minConnectTime after the startTime of Peter's adventure. Each subsequent connecting leg must also be scheduled to leave at least minConnectTime after the actual arrival time of the preceding leg.

Sounds simple enough so far, right? Except that airplanes seldom depart or arrive on time. So we have introduced a little randomness in our simulated airline operations. The actual departure time of each flight is modeled as a random variable with an exponential distribution, governed by the parameter lambdaDeparture. The duration of each flight is also modeled as an exponential distribution, governed by lambdaDuration. When you decide to take a flight, the callback routine myGetFlightTime will roll the exponential dice for you and return the number of minutes the flight actually took, measured from the scheduledDepartureTime, taking into account the departure delay and the duration delay. A few facts for those whose probability theory is a little rusty: an exponential distribution with parameter t has a probability density function of t*exp(-tx), an expected value of (1/t), and a variance of 1/t^2.

The prototype for the code you should write is:

#if defined(__cplusplus)
extern "C" {
#endif

typedef char FlightNum[8],AirportName[64];

typedef enum {
Sunday=0, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday} DayOfWeek;

typedef struct MyTime {
long hour; /* 0-23, 0==midnight */
long min; /* 0-59 */
} MyTime;

typedef struct {
AirportName name; /* airport name */
MyTime timeOffset; /* local time offset relative to Universal Time */
/* 0 == Greenwich, {-5,0}==Eastern Time, etc. */
MyTime minConnectTime; /* minimum time to make a connection at this airport */
} Airport;

typedef struct {
FlightNum flightNumber;
AirportName fromAirport;
AirportName toAirport;
MyTime scheduledDepartureTime; /* local departure time from fromAirport */
MyTime nominalFlightDuration; /* nominal flight duration */
float lambdaDeparture; /* parameter for actual flight departure (mins) */
float lambdaDuration; /* parameter for actual flight duration (mins) */
Boolean operatingDays[7]; /* if operatingDays[d], flight valid on day d */
} Flight;

typedef long (*GetFlightTime) (
/* returns actual flight duration from scheduledDepartureTime in minutes */
FlightNum flightNumber /* flight number taken */
);

long FindQuickestRoute( /* return travel time in seconds */
AirportName departureAirport, /* origin airport */
AirportName arrivalAirport, /* destination airport */
DayOfWeek startDay, /* day the adventure begins (local time) */
MyTime startTime, /* time the adventure begins (local time) */
Airport airports[], /* places to fly from/to */
long numAirports, /* number of entries in airports[] */
Flight airlineSchedule[], /* flights to choose from */
long numFlights, /* number of entries in airlineSchedule[] */
GetFlightTime myGetFlightTime /* callback that provides actual flight duration */
);

#if defined(__cplusplus)
}
#endif

My test code will keep track of the flights you choose in the myGetFlightTime routine. It will ensure that operatingDays and minConnectTime constraints are met, that the fromAirport for travel leg n+1 is the same as the toAirport for travel leg n, etc. Violations of these constraints will result in a 24 hour simulated time penalty.

Test cases will include several trials for each departure/arrival pair to average out randomness. The winner will be the solution that routes Peter to his destinations in the least amount of time, where time is calculated as cumulative simulated travel time plus a penalty of 1 simulated hour for every second of execution time required by your FindQuickestRoute solution.

This will be a native PowerPC Challenge, using the latest CodeWarrior environment. Solutions may be coded in C, C++, or Pascal. Thanks to Peter Lewis for inspiring this Challenge - he wins two Challenge points for the suggestion.





Generate a short URL for this page:


Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!

Today's Deal


Apple Special

Order
Snow Leopard,
Mac Box Set, Family Pack,
and Snow Leopard Server
at a discount.
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on long distance * Upgrade your Computer. See local info about Westlake Village
appleexpo.com | bathjot.com | bathroomjot.com | bettersupplies.com | comclothing.com | computerlunatics.com | dotcomclothing.com | explainit.com | exposinternational.com | homeismycastle.com | hoodcards.com | intlexpo.com | keyinfocard.com | kosheru.com | learnmorsels.com | localdealcards.com | lvschools.com | macjobsearch.com | mactechjobs.com | mactechmonitor.com | mactechsupplies.com | macwishbook.com | movie-depot.com | netprofessional.com | nibblelearning.com | notesintheshower.com | officejot.com | onlinebigbox.com | palmosdepot.com | peopleslineitemveto.com | showerjot.com | snapestore.com | snapishop.com | snapistore.com | snaptradingpost.com | stimulusmap.com | stimulusroadmap.com | triunfoguides.com | video-depot.com
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.