Exemplo n.º 1
0
def read_trucks(truck_file, depot_location):
    """Read truck data from <truck_file> and return a list of truck objects
    storing information regarding to truck_id, and volume.

    @type truck_file: str
        The name of a file containing truck data in the form specified in
        Assignment 1.
    @type depot_location: str
        The city where all the trucks (and packages) are at the start of the
        experiment.
    @rtype: list[object]
        Return a list of truck objects storing information
        regarding to truck_id, and volume.
    """
    trucks = []

    with open(truck_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            tid = int(tokens[0])
            capacity = int(tokens[1])
            temp = Truck(tid, capacity)
            temp.add_depot(depot_location)
            trucks.append(temp)

    return trucks
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
0
def read_trucks(truck_file, depot_location):
    """Read truck data from <truck_file> and return XXXX

    @type truck_file: str
        The name of a file containing truck data in the form specified in
        Assignment 1.
    @type depot_location: str
        The city where all the trucks (and packages) are at the start of the
        experiment.
    @rtype: XXXX

    TODO: Complete this docstring.
    """
    # TODO: Initialize some helpful variables.
    trucklist = []
    with open(truck_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            tid = int(tokens[0])
            capacity = int(tokens[1])
            # TODO: Do something with tid and capacity.
            truck = Truck(tid, capacity)
            trucklist.append(truck)
    # TODO: Do something with

    return trucklist
Exemplo n.º 5
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]
Exemplo n.º 6
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"]
Exemplo n.º 7
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"]
Exemplo n.º 8
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]
Exemplo n.º 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
Exemplo n.º 10
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)
Exemplo n.º 11
0
def read_trucks(truck_file, depot_location):
    """Read truck data from <truck_file>

    Data from a .txt document is converted into truck objects. The returned
    value is a list of truck objects.

    === Parameter and Return Types ===

    @type truck_file: str
        The name of a file containing truck data in the form specified in
        Assignment 1.
    @type depot_location: str
        The city where all the trucks (and packages) are at the start of the
        experiment.
    @rtype: [Truck]
        Returns a list of trucks.

    === Local Variables ===

    type trucks: [Truck]
        Accumulating list of trucks
    type file: _io.TextIOWrapper
    type line: str
        A single line from the data file
    type tokens: [str]
        A list that separates commas and stores useful data. The phrase
        'Allan, Toronto' may be stored in tokens as ['Allan', 'Toronto']
    type tid: int
        Truck id
    type capacity: int
        Truck capacity
    type one_truck: Truck
        A truck that will be added to <trucks>

    === Representation Invariants ===

    capacity >= 0
        Negative capacity does not make sense.
    """
    trucks = []

    with open(truck_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            tid = int(tokens[0])
            capacity = int(tokens[1])
            one_truck = Truck(tid, capacity, depot_location)
            trucks.append(one_truck)
    return trucks
Exemplo n.º 12
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
Exemplo n.º 13
0
def read_trucks(truck_file: str, depot_location: str) -> Fleet:
    """Read truck data from <truck_file> and return a Fleet containing these
    trucks, with each truck starting at the <depot_location>.

    Precondition: <truck_file> is a path to a file containing truck data in the
                  form specified in Assignment 1.
    """
    f = Fleet()
    with open(truck_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            tid = int(tokens[0])
            capacity = int(tokens[1])
            t = Truck(tid, capacity, depot_location)
            f.add_truck(t)
    return f
Exemplo n.º 14
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]}
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
0
def test_num_trucks_doctest() -> None:
    """Test the doctest provided for Fleet.num_trucks"""
    f = Fleet()
    t1 = Truck(1423, 10, 'Toronto')
    f.add_truck(t1)
    assert f.num_trucks() == 1
Exemplo n.º 19
0
def test_pack_underpack() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(3, 419, "Anchorage", "Barrie")
    assert t.pack(p) == True
Exemplo n.º 20
0
def _truck_vol_least(existing: Truck, new: Truck) -> bool:
    """ Return True if available volume of truck <existing> is greater than
    available volume of truck <new>. This will produce a list with
    priority for trucks with more available space"""
    return existing.unused_space() < new.unused_space()
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('===============================')
Exemplo n.º 22
0
def test_length_routes() -> None:
    """Test that every trucks route is >= 1."""
    t = Truck(1, 10, 'Depot')
    assert len(t.route) == 1
Exemplo n.º 23
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
Exemplo n.º 24
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
Exemplo n.º 25
0
def test_pack_overpack() -> None:
    t = Truck(69, 420, "Toronto")
    p = Parcel(2, 430, "Rome", "Barrie")
    assert t.pack(p) == False
Exemplo n.º 26
0
def _less_truck_space(t1: Truck, t2: Truck) -> bool:
    """
    Returns True if Truck <t1> has less truck space. False otherwise.
    """
    return (t1.capacity - t1.absolute_fullness()) < \
           (t2.capacity - t2.absolute_fullness())