1
\$\begingroup\$

I have a CVRP problem that I solved in Python. There are 6 planes (T1-T6) and 3 airports (AUS,DAL,HOU). The airports have 1,2,3 gates respectively. Each airport has a minimum ground time of 25, 30, 35 minutes respectively- that each plane must wait after arrival before departing again. The planes can only fly between 0600 and 2200 (military time). The number of flights landing at any given airport at one time is constrained by the number of gates. I have a list called flight_schedule that has stored the initial 6 flights taking off at 0600 from each airport.

I have created a program that correctly outputs the flight schedule, however, I would like to condense it and make it "prettier" or more "pythonic" any suggestions would be greatly appreciated

Heres the code:

def military_time(minutes):
    if minutes < 600:
        h = minutes // 60 
        m = minutes % 60
        return("{0:02d}{1:02d}".format(h, m))
    elif 600 < minutes < 720:
        h = minutes // 60
        m = minutes % 60
        return("{0:02d}{1:02d}".format(h, m))
    elif minutes == 720:
        h = 1200
        return("1200")
    else:
        h = ((minutes-720) // 60) + 12
        m = (minutes) % 60
        return("{0:02d}{1:02d}".format(h, m))

start = 360
end = 1320

# Flight times
AD = 50
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

#Initialize the list
flights = []

#T1 AUS to DAL and back
while start < end:
    ArrD = DepA + AD 
    flights.append(['T1', 'AUS', 'DAL', military_time(DepA), military_time(ArrD)])
    DepD = ArrD + DW + 20
    ArrA = DepD + AD 
    flights.append(['T1', 'DAL', 'AUS', military_time(DepD), military_time(ArrA)])
    DepA = ArrA + AW + 20 
    start = DepA + AD + DW + 20 + AD    

#T2 DAL to HOU and back
start = 360
end = 1320

# Flight times
AD = 50
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

while start < end:
    ArrH = DepD + DH
    flights.append(['T2', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
    DepH = ArrH + HW
    ArrD= DepH + DH
    flights.append(['T2', 'HOU', 'DAL', military_time(DepH), military_time(ArrD)])
    DepD = ArrD + DW
    start = DepD + DH + HW + DH

#T3 HOU to AUS and back
start = 360
end = 1320

# Flight times
AD = 50
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

while start < end:
    ArrA = DepH + AH 
    flights.append(['T3', 'HOU', 'AUS', military_time(DepH), military_time(ArrA)])
    DepA = ArrA + AW + 30
    ArrH= DepA + AH  
    flights.append(['T3', 'AUS', 'HOU', military_time(DepA), military_time(ArrH)])
    DepH = ArrH + HW + 15
    start = DepH + AH + HW + AH

#T4
start = 360
end = 1320

# Flight times
AD = 50
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

for i in range(10):
    if start < end:
        ArrH = DepD + DH
        flights.append(['T4', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
        DepH = ArrH + HW + 90
        start = DepH + DH + DW
    if start < end:
        ArrD = DepH + DH
        flights.append(['T4', 'HOU', 'DAL', military_time(DepH), military_time(ArrD)])
        DepD = ArrD + DW
        start = DepD + DH + HW

#T5
start = 360
end = 1320

# Flight times
AD = 50
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

for i in range(10):
    if start < end:
        ArrD = DepH + DH
        flights.append(['T5', 'HOU', 'DAL', military_time(DepH), military_time(ArrD)])
        DepD = ArrD + DW
        start = DepD + DH + HW
    if start < end:
        ArrH = DepD + DH
        flights.append(['T5', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
        DepH = ArrH + HW + 90
        start = DepH + DH + DW

#T6
start = 360
end = 1320

# Flight times
AD = 50
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360
T6DH = 455

for i in range(10):
    if start < end:
        ArrD = T6DH + DH
        flights.append(['T6', 'HOU', 'DAL', military_time(T6DH), military_time(ArrD)])
        DepD = ArrD + DW
        start = DepD + DH+ HW
    if start < end:
        ArrH = DepD + DH
        flights.append(['T6', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
        T6DH = ArrH + HW + 90
        start = T6DH + DH + DW

#sort the flight schedule according to tail number and departure time
flight_schedule = sorted(flights, key = lambda x: x[0] + x[3])

print(flight_schedule)

csv_header = 'tail_number,origin,destination,departure_time,arrival_time' 
file_name = 'flight_schedule.csv'

def print_flight_schedule(file_name, csv_header, flight_schedule): 
    with open(file_name,'wt') as f:
        print(csv_header, file=f) 
        for s in flight_schedule:
            print(','.join(s), file=f)

print_flight_schedule(file_name, csv_header, flight_schedule)
\$\endgroup\$
4
\$\begingroup\$
  1. If you're allowed to use other Python packages, the first thing I would suggest is using the Python datetime module in your military_time() function.

  2. Try to name the variables into something useful, understandable, and unambiguous. Pretend you're writing code that you will read again in 6 months. AD is not a very useful variable name. Use something like time_AUS_DAL or flight_time_AUS_DAL. Don't sacrifice readability for brevity in code.

  3. In any code, you should try to follow DRY (Don't Repeat Yourself) as much as possible. You have 6 loops, of which the first three are very similar and the last three are very similar. They're all essentially doing the same thing but with different parameters. You can thus refactor them into a function.

    def get_entry(plane, airport1, airport2, start, end, flight_time1, flight_time2, ground_time1, ground_time2, ...):
        # ... your code here ...
        return [plane, airport1, airport2, ....]
    
  4. Instead of redeclaring this chunk 6 times,

    start = 360
    end = 1320
    
    # Flight times
    AD = 50
    AH = 45
    DH = 65
    
    #Ground times
    AW = 25
    DW = 30
    HW = 35
    
    #Start times
    DepA = 360
    DepD = 360
    DepH = 360
    ArrA = 360
    ArrD = 360
    ArrH = 360
    

do those calculations in local variables inside your refactored function. There's no need to redeclare DepA, DepD, etc, since you overwrite them in the while loop anyway.

  1. You can also use lists/dictionaries to store this that chunk. You can do something like the following:

    flight_times = {
        'AD': 50, 'AH': 45, 'DH': 65
    }
    ....
    

This allows you to iterate through it to generate your flight schedule.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your helpful suggestions @omgimanerd. As far as using datetime module, not allowed. \$\endgroup\$ – zsad512 Jul 7 '17 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.