lolamontes69

Ch5 Ex1-Programming Collective Intelligence

Jul 6th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. """ Chapter 5 Exercise 1: Group travel cost function.
  2.  
  3.    Add total flight time as a cost equal to $0.50 per minute on the plane. Next try adding a penalty of $20 for making anyone get to the airport before 8 a.m.
  4.  
  5.    The function was altered to include the costs mentioned above.
  6. """
  7. def schedulecost(sol):
  8.     totalprice=0
  9.     latestarrival=0
  10.     earliestdep=24*60
  11.     for d in range(len(sol)/2):
  12.         # Get the inbound and outbound flights
  13.         origin = people[d][1]
  14.         outbound = flights[(origin,destination)][int(sol[2*d])]    # Corrected for iterating wrongly
  15.         returnf = flights[(destination,origin)][int(sol[(2*d)+1])] # Corrected for iterating wrongly
  16.  
  17.         # Total price is the price of all outbound and return flights
  18.         totalprice+=outbound[2]
  19.         totalprice+=returnf[2]
  20.  
  21.         # Add $.0.50 for each minute spent on the plane there and back         # Chapter 5 Exercise 1
  22.         totalprice+=((getminutes(outbound[1]))-(getminutes(outbound[0])))*0.5
  23.         totalprice+=((getminutes(returnf[1]))-(getminutes(returnf[0])))*0.5
  24.  
  25.         # Add $20 if person arrives at the airport before 8:00 a.m             # Chapter 5 Exercise 1
  26.         if outbound[0]<'08:00': totalprice += 20
  27.         if returnf[0]<'08:00': totalprice += 20
  28.  
  29.         # Track the latest arrival and earliest departure
  30.         if latestarrival<getminutes(outbound[1]): latestarrival=getminutes(outbound[1])
  31.         if earliestdep>getminutes(returnf[0]): earliestdep=getminutes(returnf[0])
  32.  
  33.     # Every person must wait at the airport until the latest person arrives.
  34.     # They must also arrive at the same time and wait for their flights
  35.     totalwait=0
  36.     for d in range(len(sol)/2):
  37.         origin=people[d][1]
  38.         outbound=flights[(origin,destination)][int(sol[2*d])]    # Corrected for iterating wrongly
  39.         returnf=flights[(destination,origin)][int(sol[(2*d)+1])] # Corrected for iterating wrongly
  40.         totalwait+=latestarrival-getminutes(outbound[1])
  41.         totalwait+=getminutes(returnf[0])-earliestdep
  42.  
  43.  
  44.  
  45.     # Does this solution require an extra day of car rental? That'll be $50
  46.     if latestarrival<earliestdep: totalprice+=50  # Corrected
  47.  
  48.     return totalprice+totalwait
Advertisement
Add Comment
Please, Sign In to add comment