def get_truck_1():
    truck_1 = get_truck_loads()[0]
    truck_1_optimized = []
    previous_package = ''
    shortest_distance = 15
    truck_1_optimized_current_index = 0
    for pkg in truck_1:
        if '9' in pkg[5]:
            truck_1_optimized.append(pkg)
            truck_1.remove(pkg)
            previous_package = pkg
            truck_1_optimized_current_index += 1

    while len(truck_1) != 0:
        for pkg in truck_1:
            if float(calc_distance(previous_package, pkg)) < shortest_distance:
                try:
                    truck_1_optimized.remove(truck_1_optimized[truck_1_optimized_current_index])
                except IndexError:
                    pass
                truck_1_optimized.insert(truck_1_optimized_current_index, pkg)
                shortest_distance = float(calc_distance(previous_package, pkg))

        previous_package = truck_1_optimized[truck_1_optimized_current_index]
        truck_1.remove(previous_package)
        truck_1_optimized_current_index += 1
        shortest_distance = 15

    return truck_1_optimized
def get_truck_2():
    truck_2 = get_truck_loads()[1]
    truck_2_pre_sort = []
    for pkg in truck_2:
        if 'EOD' not in pkg[5]:
            truck_2_pre_sort.insert(0, pkg)
        else:
            truck_2_pre_sort.append(pkg)

    truck_2_optimized = []
    previous_package = truck_2_pre_sort[0]
    shortest_distance = 15
    truck_2_optimized_current_index = 0

    while len(truck_2_pre_sort) != 0:
        for pkg in truck_2_pre_sort:
            if float(calc_distance(previous_package, pkg)) < shortest_distance:
                try:
                    truck_2_optimized.remove(truck_2_optimized[truck_2_optimized_current_index])
                except IndexError:
                    pass
                truck_2_optimized.insert(truck_2_optimized_current_index, pkg)
                shortest_distance = float(calc_distance(previous_package, pkg))

        previous_package = truck_2_optimized[truck_2_optimized_current_index]
        truck_2_pre_sort.remove(previous_package)
        truck_2_optimized_current_index += 1
        shortest_distance = 15

    return truck_2_optimized
def get_truck_3():
    truck_3 = get_truck_loads()[2]
    truck_3_optimized = []
    previous_package = 0
    shortest_distance = 15
    truck_3_optimized_current_index = 0

    while len(truck_3) != 0:
        for pkg in truck_3:
            if float(calc_distance(previous_package, pkg)) < shortest_distance:
                try:
                    truck_3_optimized.remove(truck_3_optimized[truck_3_optimized_current_index])
                except IndexError:
                    pass
                truck_3_optimized.insert(truck_3_optimized_current_index, pkg)
                shortest_distance = float(calc_distance(previous_package, pkg))

        previous_package = truck_3_optimized[truck_3_optimized_current_index]
        truck_3.remove(previous_package)
        truck_3_optimized_current_index += 1
        shortest_distance = 15

    return truck_3_optimized
Example #4
0
def get_package_status(package_id, time_param):
    packages = get_package_table()
    truck_1 = get_truck_1()
    truck_2 = get_truck_2()
    truck_3 = get_truck_3()
    current_time_truck_1 = datetime(year=2020, month=12, day=25, hour=8, minute=0)  # Tracks current time for truck 1
    current_time_truck_2 = datetime(year=2020, month=12, day=25, hour=9, minute=5)  # Tracks current time for truck 2
    current_time_truck_3 = datetime(year=2020, month=12, day=25, hour=10, minute=20)  # Tracks current time for truck 3
    current_location_truck_1 = 0  # Current location of package (initializes to 0 which refers to the index of hub)
    current_location_name_truck_1 = 'Western Governors University'  # Current name of location where package resides
    current_location_truck_2 = 0  # Current location of package (initializes to 0 which refers to the index of hub)
    current_location_name_truck_2 = 'Western Governors University'  # Current name of location where package resides
    current_location_truck_3 = 0  # Current location of package (initializes to 0 which refers to the index of hub)
    current_location_name_truck_3 = 'Western Governors University'  # Current name of location where package resides
    final_destination = 0  # Index of hub (for returning trucks to hub)
    dest_distance = 0  # Distance between each destination on route
    dest_transit_time = 0  # Transit time between each destination on route
    delivery_time = ''  # Time package was delivered
    initial_time_param = time_param
    time_param = datetime.strptime(time_param, '%H:%M')
    converted_time_param = datetime(year=2020, month=12, day=25, hour=time_param.hour, minute=time_param.minute)
    truck_1_hub_arrival_time = ''  # Needed for dispatching truck 3. Driver must return to hub before truck 3 can depart

    # Sets the initial status of all packages to 'Out for Delivery'
    # Space-time complexity O(N)
    for pkg in truck_1:
        if current_time_truck_1 > converted_time_param + timedelta(seconds=1):
            pkg[8] = 'N/A'
            pkg[10] = 'N/A'
            packages.update(int(pkg[0]), pkg)
            continue
        else:
            pkg[8] = str(current_time_truck_1)
            pkg[10] = 'N/A'
            pkg[11] = 'Out for Delivery'
            packages.update(int(pkg[0]), pkg)

    # Delivers the packages, updates package status and delivery time
    # Space-time complexity O(N)
    for pkg in truck_1:
        dest_distance = calc_distance(current_location_truck_1, pkg)
        dest_transit_time = calc_dest_transit_time(dest_distance)
        delivery_time = str(calc_delivery_time(current_time_truck_1, dest_transit_time))
        current_time_truck_1 = calc_delivery_time(current_time_truck_1, dest_transit_time)
        if current_time_truck_1 >= converted_time_param:
            pkg[9] = current_location_name_truck_1
            continue
        else:
            current_location_truck_1 = pkg
            current_location_name_truck_1 = get_dest_name(pkg)
            pkg[9] = current_location_name_truck_1
            pkg[10] = delivery_time
            pkg[11] = 'Delivered'
            packages.update(int(pkg[0]), pkg)

    # Returns truck 1 to the hub
    dest_distance = calc_distance(current_location_truck_1, final_destination)
    dest_transit_time = calc_dest_transit_time(dest_distance)
    current_location_name_truck_1 = get_dest_name(final_destination)
    truck_1_hub_arrival_time = calc_delivery_time(current_time_truck_1, dest_transit_time)

    # Sets the initial status of all packages to 'Out for Delivery'
    # Space-time complexity O(N)
    for pkg in truck_2:
        if current_time_truck_2 > converted_time_param + timedelta(seconds=1):
            pkg[8] = 'N/A'
            pkg[10] = 'N/A'
            packages.update(int(pkg[0]), pkg)
            continue
        else:
            pkg[8] = str(current_time_truck_2)
            pkg[10] = 'N/A'
            pkg[11] = 'Out for Delivery'
            packages.update(int(pkg[0]), pkg)

    # Delivers the packages, updates package status and delivery time
    # Space-time complexity O(N)
    for pkg in truck_2:
        dest_distance = calc_distance(current_location_truck_2, pkg)
        dest_transit_time = calc_dest_transit_time(dest_distance)
        delivery_time = str(calc_delivery_time(current_time_truck_2, dest_transit_time))
        current_time_truck_2 = calc_delivery_time(current_time_truck_2, dest_transit_time)
        if current_time_truck_2 >= converted_time_param:
            pkg[9] = current_location_name_truck_2
            continue
        else:
            current_location_truck_2 = pkg
            current_location_name_truck_2 = get_dest_name(pkg)
            pkg[9] = current_location_name_truck_2
            pkg[10] = delivery_time
            pkg[11] = 'Delivered'
            packages.update(int(pkg[0]), pkg)

    # Sets initial departure time of truck 3 based on the time that truck 1 returns to the hub
    current_time_truck_3 = current_time_truck_3 if current_time_truck_3 > truck_1_hub_arrival_time \
        else truck_1_hub_arrival_time

    # Sets the initial status of all packages to 'Out for Delivery'
    # Space-time complexity O(N)
    for pkg in truck_3:
        if current_time_truck_3 > converted_time_param + timedelta(seconds=1):
            pkg[8] = 'N/A'
            pkg[10] = 'N/A'
            packages.update(int(pkg[0]), pkg)
            continue
        else:
            pkg[8] = str(current_time_truck_3)
            pkg[10] = 'N/A'
            pkg[11] = 'Out for Delivery'
            packages.update(int(pkg[0]), pkg)

    # Delivers the packages, updates package status and delivery time
    # Space-time complexity O(N)
    for pkg in truck_3:
        dest_distance = calc_distance(current_location_truck_3, pkg)
        dest_transit_time = calc_dest_transit_time(dest_distance)
        delivery_time = str(calc_delivery_time(current_time_truck_3, dest_transit_time))
        current_time_truck_3 = calc_delivery_time(current_time_truck_3, dest_transit_time)
        if current_time_truck_3 >= converted_time_param:
            pkg[9] = current_location_name_truck_3
            continue
        else:
            current_location_truck_3 = pkg
            current_location_name_truck_3 = get_dest_name(pkg)
            pkg[9] = current_location_name_truck_3
            pkg[10] = delivery_time
            pkg[11] = 'Delivered'
            packages.update(int(pkg[0]), pkg)

    # Prints the statuses for an individual package at a specific time based on a provided package ID parameter and
    # a provided time parameter
    # Space-time complexity O(N)
    pkg = packages.read(package_id)
    print('\nDetails for package ' + pkg[0] + ' as of ' + initial_time_param + ' --  Status: ' + pkg[11] +
          ' || Left Hub At: ' + pkg[8] + ' || Current Location: ' + pkg[9] + ' || Delivery Deadline: ' + pkg[5] +
          ' || Delivery Time: ' + pkg[10])

    return ''
Example #5
0
def exec_truck_routes():
    packages = get_package_table()
    truck_1 = get_truck_1()
    truck_2 = get_truck_2()
    truck_3 = get_truck_3()
    current_time_truck_1 = datetime(
        year=2020, month=12, day=25, hour=8,
        minute=0)  # Tracks current time for truck 1
    current_time_truck_2 = datetime(
        year=2020, month=12, day=25, hour=9,
        minute=5)  # Tracks current time for truck 2
    current_time_truck_3 = datetime(
        year=2020, month=12, day=25, hour=10,
        minute=20)  # Tracks current time for truck 3
    current_location_truck_1 = 0  # Current location of package (initializes to 0 which refers to the index of hub)
    current_location_name_truck_1 = ''  # Current name of location where package resides
    current_location_truck_2 = 0  # Current location of package (initializes to 0 which refers to the index of hub)
    current_location_name_truck_2 = ''  # Current name of location where package resides
    current_location_truck_3 = 0  # Current location of package (initializes to 0 which refers to the index of hub)
    current_location_name_truck_3 = ''  # Current name of location where package resides
    final_destination = 0  # Index of hub (for returning trucks to hub)
    dest_distance = 0  # Distance between each destination on route
    dest_transit_time = 0  # Transit time between each destination on route
    delivery_time = ''  # Time package was delivered
    total_mileage_truck_1 = 0
    total_mileage_truck_2 = 0
    total_mileage_truck_3 = 0
    total_transit_time_truck_1 = 0
    total_transit_time_truck_2 = 0
    total_transit_time_truck_3 = 0
    total_mileage = 0
    total_transit_time = 0
    truck_1_hub_arrival_time = ''  # Needed for dispatching truck 3. Driver must return to hub before truck 3 can depart

    # Sets the initial status of all packages to 'Out for Delivery'
    # Space-time complexity O(N)
    truck_1_departure_time = current_time_truck_1
    for pkg in truck_1:
        pkg[8] = str(current_time_truck_1)
        pkg[9] = 'Western Governors University'
        pkg[10] = 'N/A'
        pkg[11] = 'Out for Delivery'
        packages.update(int(pkg[0]), pkg)

    # Delivers the packages, updates package status and delivery time
    # Space-time complexity O(N)
    for pkg in truck_1:
        dest_distance = calc_distance(current_location_truck_1, pkg)
        dest_transit_time = calc_dest_transit_time(dest_distance)
        delivery_time = str(
            calc_delivery_time(current_time_truck_1, dest_transit_time))
        current_time_truck_1 = calc_delivery_time(current_time_truck_1,
                                                  dest_transit_time)
        current_location_truck_1 = pkg
        current_location_name_truck_1 = get_dest_name(pkg)
        pkg[9] = current_location_name_truck_1
        pkg[10] = delivery_time
        pkg[11] = 'Delivered'
        packages.update(int(pkg[0]), pkg)
        total_transit_time += int(dest_transit_time)
        total_transit_time_truck_1 += int(dest_transit_time)
        total_mileage += float(dest_distance)
        total_mileage_truck_1 += float(dest_distance)

    # Returns truck 1 to the hub
    dest_distance = calc_distance(current_location_truck_1, final_destination)
    dest_transit_time = calc_dest_transit_time(dest_distance)
    current_location_name_truck_1 = get_dest_name(final_destination)
    total_transit_time += int(dest_transit_time)
    total_transit_time_truck_1 += int(dest_transit_time)
    total_mileage += float(dest_distance)
    total_mileage_truck_1 += float(dest_distance)
    truck_1_hub_arrival_time = calc_delivery_time(current_time_truck_1,
                                                  dest_transit_time)

    # Sets the initial status of all packages to 'Out for Delivery'
    # Space-time complexity O(N)
    truck_2_departure_time = current_time_truck_2
    for pkg in truck_2:
        pkg[8] = str(current_time_truck_2)
        pkg[9] = 'Western Governors University'
        pkg[10] = 'N/A'
        pkg[11] = 'Out for Delivery'
        packages.update(int(pkg[0]), pkg)

    # Delivers the packages, updates package status and delivery time
    # Space-time complexity O(N)
    for pkg in truck_2:
        dest_distance = calc_distance(current_location_truck_2, pkg)
        dest_transit_time = calc_dest_transit_time(dest_distance)
        delivery_time = str(
            calc_delivery_time(current_time_truck_2, dest_transit_time))
        current_time_truck_2 = calc_delivery_time(current_time_truck_2,
                                                  dest_transit_time)
        current_location_truck_2 = pkg
        current_location_name_truck_2 = get_dest_name(pkg)
        pkg[9] = current_location_name_truck_2
        pkg[10] = delivery_time
        pkg[11] = 'Delivered'
        packages.update(int(pkg[0]), pkg)
        total_transit_time += int(dest_transit_time)
        total_transit_time_truck_2 += int(dest_transit_time)
        total_mileage += float(dest_distance)
        total_mileage_truck_2 += float(dest_distance)

    # Sets initial departure time of truck 3 based on the time that truck 1 returns to the hub
    current_time_truck_3 = current_time_truck_3 if current_time_truck_3 >= truck_1_hub_arrival_time \
        else truck_1_hub_arrival_time

    # Sets the initial status of all packages to 'Out for Delivery'
    # Space-time complexity O(N)
    truck_3_departure_time = current_time_truck_3
    for pkg in truck_3:
        pkg[8] = str(current_time_truck_3)
        pkg[9] = 'Western Governors University'
        pkg[10] = 'N/A'
        pkg[11] = 'Out for Delivery'
        packages.update(int(pkg[0]), pkg)

    # Delivers the packages, updates package status and delivery time
    # Space-time complexity O(N)
    for pkg in truck_3:
        dest_distance = calc_distance(current_location_truck_3, pkg)
        dest_transit_time = calc_dest_transit_time(dest_distance)
        delivery_time = str(
            calc_delivery_time(current_time_truck_3, dest_transit_time))
        current_time_truck_3 = calc_delivery_time(current_time_truck_3,
                                                  dest_transit_time)
        current_location_truck_3 = pkg
        current_location_name_truck_3 = get_dest_name(pkg)
        pkg[9] = current_location_name_truck_3
        pkg[10] = delivery_time
        pkg[11] = 'Delivered'
        packages.update(int(pkg[0]), pkg)
        total_transit_time += int(dest_transit_time)
        total_transit_time_truck_3 += int(dest_transit_time)
        total_mileage += float(dest_distance)
        total_mileage_truck_3 += float(dest_distance)

    # Returns values for use elsewhere
    return [
        total_transit_time, total_transit_time_truck_1,
        total_transit_time_truck_2, total_transit_time_truck_3, total_mileage,
        total_mileage_truck_1, total_mileage_truck_2, total_mileage_truck_3,
        truck_1_departure_time, truck_1_hub_arrival_time,
        truck_2_departure_time, truck_3_departure_time
    ]