示例#1
0
def test_pack_updates_route_if_same_destination() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(5, 186, "Markham", "Oshawa")
    p2 = Parcel(6, 13, "Copenhagen", "Oshawa")
    t.pack(p)
    t.pack(p2)
    assert t.route == ["Toronto", "Oshawa"]
示例#2
0
def test_pack_updates_cargo() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(5, 186, "Markham", "Toronto")
    p2 = Parcel(6, 13, "Copenhagen", "Barrie")
    t.pack(p)
    t.pack(p2)
    assert t.cargo == [p, p2]
示例#3
0
def test_pack_updates_route() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(5, 186, "Markham", "Montreal")
    p2 = Parcel(6, 13, "Copenhagen", "Barrie")
    t.pack(p)
    t.pack(p2)
    assert t.route == ["Toronto", "Montreal", "Barrie"]
示例#4
0
def test_pack_same_dest() -> None:
    """Test if pack appends route iff the last city on route is different
    than the new parcel added."""
    t = Truck(1, 20, 'Depot')
    p = Parcel(1, 5, 'City n', 'City A')
    p2 = Parcel(2, 15, 'City n', 'City A')
    t.pack(p)
    t.pack(p2)
    assert t.route[1] == 'City A'
    assert len(t.route) == 2
示例#5
0
def test_parcel_allocations_doctest() -> None:
    """Test the doctest provided for Fleet.parcel_allocations"""
    f = Fleet()
    t1 = Truck(1423, 10, 'Toronto')
    p1 = Parcel(27, 5, 'Toronto', 'Hamilton')
    p2 = Parcel(12, 5, 'Toronto', 'Hamilton')
    assert t1.pack(p1) is True
    assert t1.pack(p2) is True
    t2 = Truck(1333, 10, 'Toronto')
    p3 = Parcel(28, 5, 'Toronto', 'Hamilton')
    assert t2.pack(p3) is True
    f.add_truck(t1)
    f.add_truck(t2)
    assert f.parcel_allocations() == {1423: [27, 12], 1333: [28]}
示例#6
0
def test_average_distance_travelled_doctest() -> None:
    """Test the doctest provided for Fleet.average_distance_travelled"""
    f = Fleet()
    t1 = Truck(1423, 10, 'Toronto')
    p1 = Parcel(1, 5, 'Toronto', 'Hamilton')
    assert t1.pack(p1) is True

    t2 = Truck(1333, 10, 'Toronto')
    p2 = Parcel(2, 5, 'Toronto', 'Hamilton')
    assert t2.pack(p2) is True

    m = DistanceMap()
    m.add_distance('Toronto', 'Hamilton', 9)
    f.add_truck(t1)
    f.add_truck(t2)
    assert f.average_distance_travelled(m) == 18.0
示例#7
0
def test_average_fullness_doctest() -> None:
    """Test the doctest provided for Fleet.average_fullness"""
    f = Fleet()
    t = Truck(1423, 10, 'Toronto')
    p = Parcel(1, 5, 'Buffalo', 'Hamilton')
    assert t.pack(p) is True

    f.add_truck(t)
    assert f.average_fullness() == 50.0
示例#8
0
def test_total_unused_space_doctest() -> None:
    """Test the doctest provided for Fleet.total_unused_space"""
    f = Fleet()
    assert f.total_unused_space() == 0

    t = Truck(1423, 1000, 'Toronto')
    p = Parcel(1, 5, 'Buffalo', 'Hamilton')
    assert t.pack(p) is True

    f.add_truck(t)
    assert f.total_unused_space() == 995
示例#9
0
def test_num_nonempty_trucks_doctest() -> None:
    """Test the doctest provided for Fleet.num_nonempty_trucks"""
    f = Fleet()

    t1 = Truck(1423, 10, 'Toronto')
    f.add_truck(t1)
    p1 = Parcel(1, 5, 'Buffalo', 'Hamilton')
    assert t1.pack(p1) is True

    p2 = Parcel(2, 4, 'Toronto', 'Montreal')
    assert t1.pack(p2) is True
    assert t1.fullness() == 90.0

    t2 = Truck(5912, 20, 'Toronto')
    f.add_truck(t2)
    p3 = Parcel(3, 2, 'New York', 'Windsor')
    assert t2.pack(p3) is True
    assert t2.fullness() == 10.0

    t3 = Truck(1111, 50, 'Toronto')
    f.add_truck(t3)
    assert f.num_nonempty_trucks() == 2
示例#10
0
def read_parcels(parcel_file):
    """Read parcel data from <parcel_file> and returns a list of parcels.

    === Parameter and Return Types ===

    @type parcel_file: str
        The name of a file containing parcel data in the form specified in
        Assignment 1.
    @rtype: [Parcel]
        Returns a list of parcels

    === Local Variables ===

    type parcel_list: [Parcel]
        A list that stores parcels
    type file: _io.TextIOWrapper
    type line: str
        A single line from the data file
    type tokens: [str]
        A list that separates strings by commasand stores useful data. The
        phrase 'David, Toronto' may be stored in tokens as ['David', 'Toronto']
    type pid: int
        Parcel identification number.
    type source: int
        Parcel source.
    type destination: Truck
        Parcel destination
    type volume
        Parcel volume
    type one_parcel
        A parcel that will be added to <parcel_list>

    === Representation Invariants ===

    volume >= 0
        Negative volume does not make sense.
    """
    parcel_list = []

    with open(parcel_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            pid = int(tokens[0].strip())
            source = tokens[1].strip()
            destination = tokens[2].strip()
            volume = int(tokens[3].strip())
            one_parcel = Parcel(pid, source, destination, volume)

            parcel_list.append(one_parcel)

    return parcel_list
示例#11
0
def read_parcels(parcel_file: str) -> List[Parcel]:
    """Read parcel data from <parcel_file> and return.

    Precondition: <parcel_file> is the path to a file containing parcel data in
                  the form specified in Assignment 1.
    """
    parcels = []
    with open(parcel_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            pid = int(tokens[0].strip())
            source = tokens[1].strip()
            destination = tokens[2].strip()
            volume = int(tokens[3].strip())
            parcel = Parcel(pid, volume, source, destination)
            parcels.append(parcel)
    return parcels
示例#12
0
def test_greedy_scheduler_example() -> None:
    """Test GreedyScheduler on the example provided."""
    p17 = Parcel(17, 25, 'York', 'Toronto')
    p21 = Parcel(21, 10, 'York', 'London')
    p13 = Parcel(13, 8, 'York', 'London')
    p42 = Parcel(42, 20, 'York', 'Toronto')
    p25 = Parcel(25, 15, 'York', 'Toronto')
    p61 = Parcel(61, 15, 'York', 'Hamilton')
    p76 = Parcel(76, 20, 'York', 'London')

    t1 = Truck(1, 40, 'York')
    t2 = Truck(2, 40, 'York')
    t3 = Truck(3, 25, 'York')

    f = Fleet()
    f.add_truck(t1)
    f.add_truck(t2)
    f.add_truck(t3)

    # We've left parcel_file, truck_file, and map_file empty in the config
    # dictionary below because you should *not* use these in your
    # GreedyScheduler. It is not responsible for reading data from these files.
    config = {
        'depot_location': 'York',
        'parcel_file': '',
        'truck_file': '',
        'map_file': '',
        'algorithm': 'greedy',
        'parcel_priority': 'destination',
        'parcel_order': 'non-increasing',
        'truck_order': 'non-increasing',
        'verbose': 'false'
    }

    scheduler = GreedyScheduler(config)
    unscheduled = scheduler.schedule([p17, p21, p13, p42, p25, p61, p76],
                                     [t1, t2, t3])

    assert unscheduled == [p76]

    truck_parcels = f.parcel_allocations()
    assert truck_parcels[1] == [17, 61]
    assert truck_parcels[2] == [42, 25]
    assert truck_parcels[3] == [21, 13]
示例#13
0
def test_priority_truck_non_decreasing_destination() -> None:
    """Test the doctest provided for PriorityQueue.add and
    PriorityQueue.remove"""
    t1 = Truck(1, 25, 'York')
    t2 = Truck(2, 10, 'York')
    t3 = Truck(3, 8, 'York')
    t4 = Truck(4, 20, 'York')
    t5 = Truck(5, 15, 'York')
    t6 = Truck(6, 15, 'York')
    t7 = Truck(7, 20, 'York')
    p1 = Parcel(1, 15, 'York', 'Toronto')
    trucks = [t1, t2, t3, t4, t5, t6, t7]

    et = _capable_trucks(p1, trucks)
    assert len(et) == 5
    pq = PriorityQueue(_less_truck_space)
    for t in et:
        pq.add(trucks[t])
    truck = pq.remove()
    assert truck.id == 5
示例#14
0
def test_priority_queue_non_decreasing_volume() -> None:
    """Test the doctest provided for PriorityQueue.add and
    PriorityQueue.remove"""
    p1 = Parcel(1, 25, 'York', 'Toronto')
    p2 = Parcel(2, 10, 'York', 'London')
    p3 = Parcel(3, 8, 'York', 'London')
    p4 = Parcel(4, 20, 'York', 'Toronto')
    p5 = Parcel(5, 15, 'York', 'Toronto')
    p6 = Parcel(6, 15, 'York', 'Hamilton')
    p7 = Parcel(7, 20, 'York', 'London')
    parcels = [p1, p2, p3, p4, p5, p6, p7]
    pq = PriorityQueue(_smaller_volume)
    for parcel in parcels:
        pq.add(parcel)
    assert pq.remove().volume == 8
    assert pq.remove().volume == 10
    assert pq.remove().volume == 15
    assert pq.remove().volume == 15
    assert pq.remove().volume == 20
    assert pq.remove().volume == 20
    assert pq.remove().volume == 25
示例#15
0
def test_priority_queue_non_increasing_destination() -> None:
    """Test the doctest provided for PriorityQueue.add and
    PriorityQueue.remove"""
    p1 = Parcel(1, 25, 'York', 'a')
    p2 = Parcel(2, 10, 'York', 'aa')
    p3 = Parcel(3, 8, 'York', 'aaa')
    p4 = Parcel(4, 20, 'York', 'a')
    p5 = Parcel(5, 15, 'York', 'aaaa')
    p6 = Parcel(6, 15, 'York', 'aa')
    p7 = Parcel(7, 20, 'York', 'aaaaaa')
    parcels = [p1, p2, p3, p4, p5, p6, p7]
    pq = PriorityQueue(_smaller_city)
    for parcel in parcels:
        pq.add(parcel)
    assert pq.remove().id == 7
    assert pq.remove().id == 5
    assert pq.remove().id == 3
    assert pq.remove().id == 2
    assert pq.remove().id == 6
    assert pq.remove().id == 1
    assert pq.remove().id == 4
示例#16
0
def read_parcels(parcel_file):
    """Read parcel data from <parcel_file> and return XXXX

    @type parcel_file: str
        The name of a file containing parcel data in the form specified in
        Assignment 1.
    @rtype: XXXX

    TODO: Complete this docstring.
    """
    # TODO: Initialize some helpful variables.
    parcellist = []

    with open(parcel_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            pid = int(tokens[0].strip())
            source = tokens[1].strip()
            destination = tokens[2].strip()
            volume = int(tokens[3].strip())
            parcel = Parcel(pid, source, destination, volume)
            parcellist.append(parcel)
    return parcellist
示例#17
0
def read_parcels(parcel_file):
    """Read parcel data from <parcel_file> and return XXXX

    @type parcel_file: str
        The name of a file containing parcel data in the form specified in
        Assignment 1.
    @rtype: list

    >>>print(read_parcels("data"))
    >>>'[list of things in that data file]'
    """
    parcellist = []

    with open(parcel_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            pid = int(tokens[0].strip())
            source = tokens[1].strip()
            destination = tokens[2].strip()
            volume = int(tokens[3].strip())
            parcel = Parcel(pid,source,destination,volume)
            parcellist.append(parcel)
    return parcellist
示例#18
0
def read_parcels(parcel_file):
    """Read parcel data from <parcel_file> and return a list of parcel objects
    storing information regarding to parcel_id, source, destination and volume.

    @type parcel_file: str
        The name of a file containing parcel data in the form specified in
        Assignment 1.
    @rtype: list[object]
        The list containing objects storing the information
    of pacels in the parcel_file.
    """
    parcels = []
    with open(parcel_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            pid = int(tokens[0].strip())
            source = tokens[1].strip()
            destination = tokens[2].strip()
            volume = int(tokens[3].strip())
            temp = Parcel(pid, source, destination, volume)
            parcels.append(temp)

    return parcels
示例#19
0
def test_average_distance_travelled() -> None:  # (...)
    p17 = Parcel(17, 25, 'York', 'Toronto')
    p21 = Parcel(21, 10, 'York', 'London')
    p13 = Parcel(13, 8, 'York', 'London')
    p42 = Parcel(42, 20, 'York', 'Toronto')
    p25 = Parcel(25, 15, 'York', 'Toronto')
    p61 = Parcel(61, 15, 'York', 'Hamilton')
    p76 = Parcel(76, 20, 'York', 'London')

    t1 = Truck(1, 40, 'York')
    t2 = Truck(2, 40, 'York')
    t3 = Truck(3, 25, 'York')

    f = Fleet()
    f.add_truck(t1)
    f.add_truck(t2)
    f.add_truck(t3)

    dmap = DistanceMap()
    dmap.add_distance('York', 'Toronto', 1)
    dmap.add_distance('York', 'London', 2)
    dmap.add_distance('York', 'Hamilton', 3)
algm = []
source = 'New York'
for i in parcel_algm:
    for j in truck_algm:
        algm.append([i, j])
# print(algm)
# for each in algm:
#     if each[0] == 'nondecreasing_volume':
#         print(each[0], each[1])
for each in algm:
    parcel_normal = [['1', 'A', 40], ['2', 'B', 15], ['3', 'C', 20],
                     ['4', 'A', 5], ['5', 'A', 5], ['6', 'B', 10],
                     ['20', 'A', 200]]
    truck_normal = [['20', 100], ['21', 25], ['22', 60]]
    parcels = []
    trucks = []
    for i in parcel_normal:
        temp = Parcel(i[0], source, i[1], i[2])
        parcels.append(temp)
    for j in truck_normal:
        temp = Truck(j[0], j[1])
        trucks.append(temp)
    g = GreedyScheduler(each[0], each[1])
    v = g.schedule(parcels, trucks)
    for m in v:
        print(m.get_parcel()[0])
    print(each)
    for k in trucks:
        print(k.get_volume(), k.get_destination(), k.get_parcel())
    print('===============================')
示例#21
0
def test_absolute_fullness() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(1, 60, "Cape Town", "Barrie")
    t.pack(p)
    assert t.capacity - t.absolute_fullness() == 360
示例#22
0
def test_fullness() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(4, 42, "Cologne", "Barrie")
    t.pack(p)
    assert t.fullness() == (42 / 420) * 100
示例#23
0
def test_pack_underpack() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(3, 419, "Anchorage", "Barrie")
    assert t.pack(p) == True
示例#24
0
def test_pack_overpack() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(2, 430, "Rome", "Barrie")
    assert t.pack(p) == False
示例#25
0
def test_rational_float() -> None:
    """Test if fullness round to one decimal place."""
    t = Truck(1, 3, 'Depot')
    p = Parcel(1, 1, 'City n', 'aCity A')
    t.pack(p)
    assert t.fullness() == 33.3