Beispiel #1
0
 def test_pick_up_delivery_lp(self):
     self.G.nodes[2]["request"] = 5
     self.G.nodes[2]["demand"] = 10
     self.G.nodes[3]["demand"] = 10
     self.G.nodes[3]["request"] = 4
     self.G.nodes[4]["demand"] = -10
     self.G.nodes[5]["demand"] = -10
     self.G.add_edge(2, 5, cost=10)
     self.G.remove_node(1)
     prob = VehicleRoutingProblem(
         self.G,
         load_capacity=15,
         pickup_delivery=True,
     )
     prob.solve(pricing_strategy="Exact", cspy=False)
     assert prob.best_value == 65
Beispiel #2
0
class TestIssue110:
    def setup(self):
        G = DiGraph()
        G.add_edge("Source", 1, cost=[1, 2])
        G.add_edge("Source", 2, cost=[2, 4])
        G.add_edge(1, "Sink", cost=[0, 0])
        G.add_edge(2, "Sink", cost=[2, 4])
        G.add_edge(1, 2, cost=[1, 2])
        G.nodes[1]["demand"] = 13
        G.nodes[2]["demand"] = 13
        self.prob = VehicleRoutingProblem(G,
                                          mixed_fleet=True,
                                          load_capacity=[10, 15])

    def test_node_load(self):
        self.prob.solve()
        assert self.prob.best_routes_type == {1: 1, 2: 1}
Beispiel #3
0
 def test_periodic(self):
     self.G.nodes[2]["frequency"] = 2
     prob = VehicleRoutingProblem(self.G, num_stops=2, periodic=2)
     prob.solve()
     assert prob.best_value == 90
     frequency = 0
     for r in prob.best_routes:
         if 2 in prob.best_routes[r]:
             frequency += 1
     assert frequency == 2
     assert prob.schedule[0] in [[1], [1, 2]]
     prob = VehicleRoutingProblem(self.G,
                                  num_stops=2,
                                  periodic=2,
                                  num_vehicles=1)
     prob.solve()
     assert prob.schedule == {}
Beispiel #4
0
 def test_distribution_collection(self):
     self.G.nodes[1]["collect"] = 12
     self.G.nodes[2]["collect"] = 0
     self.G.nodes[3]["collect"] = 0
     self.G.nodes[4]["collect"] = 1
     self.G.nodes[5]["collect"] = 0
     self.G.nodes["Source"]["collect"] = 0
     self.G.nodes["Sink"]["collect"] = 0
     prob = VehicleRoutingProblem(
         self.G,
         load_capacity=15,
         distribution_collection=True,
     )
     prob.solve(cspy=False)
     lp_sol = prob.best_value
     prob.solve(cspy=True)
     cspy_sol = prob.best_value
     assert lp_sol == cspy_sol
     assert lp_sol == 80
Beispiel #5
0
class TestIssue79:
    def setup(self):
        G = DiGraph()
        G.add_edge("Source", 8, cost=0)
        G.add_edge("Source", 6, cost=1)
        G.add_edge("Source", 2, cost=1)
        G.add_edge("Source", 5, cost=1)
        G.add_edge(8, 6, cost=0)
        G.add_edge(6, 2, cost=0)
        G.add_edge(2, 5, cost=0)
        G.add_edge(5, "Sink", cost=0)
        G.add_edge(8, "Sink", cost=1)
        G.add_edge(6, "Sink", cost=1)
        G.add_edge(2, "Sink", cost=1)
        G.nodes[8]["demand"] = 8
        G.nodes[6]["demand"] = 4
        G.nodes[2]["demand"] = 1
        G.nodes[5]["demand"] = 2
        G.nodes[8]["collect"] = 1
        G.nodes[6]["collect"] = 1
        G.nodes[2]["collect"] = 1
        G.nodes[5]["collect"] = 2
        self.prob = VehicleRoutingProblem(G,
                                          load_capacity=15,
                                          distribution_collection=True)

    def test_node_load_cspy(self):
        self.prob.solve()
        assert self.prob.node_load[1][8] == 8
        assert self.prob.node_load[1][6] == 5
        assert self.prob.node_load[1][2] == 5
        assert self.prob.node_load[1][5] == 5

    def test_node_load_lp(self):
        self.prob.solve(cspy=False)
        assert self.prob.node_load[1][8] == 8
        assert self.prob.node_load[1][6] == 5
        assert self.prob.node_load[1][2] == 5
        assert self.prob.node_load[1][5] == 5
Beispiel #6
0
def test_check_arguments():
    G = DiGraph()
    G.add_edge("Source", "Sink", cost=1)
    with pytest.raises(TypeError):
        prob = VehicleRoutingProblem(G, num_stops=3.5)
        prob.solve()
    with pytest.raises(TypeError):
        prob = VehicleRoutingProblem(G, load_capacity=-10)
        prob.solve()
    with pytest.raises(TypeError):
        prob = VehicleRoutingProblem(G, duration=-1)
        prob.solve()
    with pytest.raises(ValueError):
        prob = VehicleRoutingProblem(G)
        prob.solve(pricing_strategy="Best")
Beispiel #7
0
def _run_single_problem(path_to_instance: Path, **kwargs):
    'Run single problem with solver arguments as in kwargs'
    instance_folder = path_to_instance.parent
    instance_type = path_to_instance.parent.stem
    instance_name = path_to_instance.name
    logger.info("Solving instance %s", instance_name)
    # Load data
    if instance_type == "cvrp":
        data = AugeratDataSet(path=instance_folder,
                              instance_name=instance_name)
    elif instance_type == "cvrptw":
        data = SolomonDataSet(path=instance_folder,
                              instance_name=instance_name)
    # Solve problem
    prob = VehicleRoutingProblem(data.G,
                                 load_capacity=data.max_load,
                                 time_windows=bool(instance_type == "cvrptw"))
    prob.solve(**kwargs)
    # Output results
    table = CsvTable(instance_name=instance_name,
                     comp_time=prob.comp_time,
                     best_known_solution=data.best_known_solution,
                     instance_type=instance_type)
    table.from_vrpy_instance(prob)
Beispiel #8
0
 def test_num_vehicles_use_all(self):
     prob = VehicleRoutingProblem(self.G,
                                  num_stops=3,
                                  num_vehicles=2,
                                  use_all_vehicles=True,
                                  drop_penalty=100)
     prob.solve()
     assert len(prob.best_routes) == 2
     prob.num_vehicles = 3
     prob.solve()
     assert len(prob.best_routes) == 3
     prob.num_vehicles = 4
     prob.solve()
     assert len(prob.best_routes) == 4
Beispiel #9
0
def test_consistency_parameters():
    """Checks if solving parameters are consistent."""
    G = DiGraph()
    G.add_edge("Source", "Sink", cost=1)
    prob = VehicleRoutingProblem(G, pickup_delivery=True)
    # pickup delivery requires cspy=False
    with pytest.raises(NotImplementedError):
        prob.solve()
    # pickup delivery requires pricing_strategy="Exact"
    with pytest.raises(ValueError):
        prob.solve(cspy=False, pricing_strategy="Stops")
    # pickup delivery expects at least one request
    with pytest.raises(KeyError):
        prob.solve(cspy=False, pricing_strategy="Exact")
Beispiel #10
0
from networkx import from_numpy_matrix, set_node_attributes, relabel_nodes, DiGraph
from numpy import matrix
from data import DISTANCES, DEMANDS, COLLECT
import sys

sys.path.append("../../")
sys.path.append("../../../cspy")
from vrpy import VehicleRoutingProblem

# Transform distance matrix to DiGraph
A = matrix(DISTANCES, dtype=[("cost", int)])
G = from_numpy_matrix(A, create_using=DiGraph())

# Set demand and collect volumes
set_node_attributes(G, values=DEMANDS, name="demand")
set_node_attributes(G, values=COLLECT, name="collect")

# Relabel depot
G = relabel_nodes(G, {0: "Source", 17: "Sink"})

if __name__ == "__main__":

    prob = VehicleRoutingProblem(G, load_capacity=15, distribution_collection=True,)
    prob.solve(pricing_strategy="PrunePaths")
    print(prob.best_value)
    print(prob.best_routes)
    print(prob.node_load)
Beispiel #11
0
 def test_extend_preassignment(self):
     routes = [[2, 3]]
     prob = VehicleRoutingProblem(self.G, num_stops=4)
     prob.solve(preassignments=routes)
     assert prob.best_value == 70
Beispiel #12
0
# Set demands and requests
for (u, v) in PICKUPS_DELIVERIES:
    G.nodes[u]["request"] = v
    G.nodes[u]["demand"] = PICKUPS_DELIVERIES[(u, v)]
    G.nodes[v]["demand"] = -PICKUPS_DELIVERIES[(u, v)]

# Relabel depot
G = relabel_nodes(G, {0: "Source", 17: "Sink"})

if __name__ == "__main__":

    prob = VehicleRoutingProblem(G,
                                 load_capacity=6,
                                 pickup_delivery=True,
                                 num_stops=6)
    prob.solve(cspy=False, pricing_strategy="Exact")
    print(prob.best_value)
    print(prob.best_routes)
    for (u, v) in PICKUPS_DELIVERIES:
        found = False
        for route in prob.best_routes.values():
            if u in route and v in route:
                found = True
                break
        if not found:
            print((u, v), "Not present")
            assert False

    print(prob.node_load)
    assert prob.best_value == 5980
Beispiel #13
0
class TestsAugerat:
    def setup(self):
        """
        Augerat instance P-n16-k8.vrp
        """
        data = AugeratDataSet(path="benchmarks/data/cvrp/",
                              instance_name="P-n16-k8.vrp")
        self.G = data.G
        self.prob = VehicleRoutingProblem(self.G, load_capacity=data.max_load)
        self.solver_args = {"pricing_strategy": "BestPaths"}

    def test_setup_instance_name(self):
        assert self.G.graph["name"] == "P-n16-k8"

    def test_setup_vehicle_capacity(self):
        assert self.G.graph["vehicle_capacity"] == 35

    def test_setup_nodes(self):
        # extra node for the Sink
        assert len(self.G.nodes()) == 16 + 1

    def test_setup_edges(self):
        assert len(self.G.edges()) == 16 * (16 - 1) + 1

    def test_subproblem_lp(self):
        self.prob.solve(**self.solver_args, cspy=False)
        assert round(self.prob.best_value, -1) in [450, 460]

    def test_subproblem_lp_greedy(self):
        self.prob.solve(**self.solver_args, cspy=False, greedy=True)
        assert round(self.prob.best_value, -1) in [450, 460]

    def test_subproblem_cspy(self):
        self.prob.solve(**self.solver_args)
        assert round(self.prob.best_value, -1) in [450, 460]

    def test_subproblem_lp_with_initial_routes(self):
        # benchmark result
        # http://vrp.galgos.inf.puc-rio.br/index.php/en/
        r_1 = ["Source", 2, "Sink"]
        r_2 = ["Source", 6, "Sink"]
        r_3 = ["Source", 8, "Sink"]
        r_4 = ["Source", 15, 12, 10, "Sink"]
        r_5 = ["Source", 14, 5, "Sink"]
        r_6 = ["Source", 13, 9, 7, "Sink"]
        r_7 = ["Source", 11, 4, "Sink"]
        r_8 = ["Source", 3, 1, "Sink"]
        ini = [r_1, r_2, r_3, r_4, r_5, r_6, r_7, r_8]
        self.prob.solve(**self.solver_args, cspy=False, initial_routes=ini)
        assert int(self.prob.best_value) == 450

    def test_subproblem_cspy_with_initial_routes(self):
        # benchmark result
        # http://vrp.galgos.inf.puc-rio.br/index.php/en/
        r_1 = ["Source", 2, "Sink"]
        r_2 = ["Source", 6, "Sink"]
        r_3 = ["Source", 8, "Sink"]
        r_4 = ["Source", 15, 12, 10, "Sink"]
        r_5 = ["Source", 14, 5, "Sink"]
        r_6 = ["Source", 13, 9, 7, "Sink"]
        r_7 = ["Source", 11, 4, "Sink"]
        r_8 = ["Source", 3, 1, "Sink"]
        ini = [r_1, r_2, r_3, r_4, r_5, r_6, r_7, r_8]
        self.prob.solve(**self.solver_args, initial_routes=ini)
        assert int(self.prob.best_value) == 450
Beispiel #14
0
class TestsOrTools:
    def setup(self):
        # Transform distance matrix to DiGraph
        A = matrix(DISTANCES, dtype=[("cost", int)])
        G_d = from_numpy_matrix(A, create_using=DiGraph())
        # Transform time matrix to DiGraph
        A = matrix(TRAVEL_TIMES, dtype=[("time", int)])
        G_t = from_numpy_matrix(A, create_using=DiGraph())
        # Merge
        G = compose(G_d, G_t)
        # Set time windows
        set_node_attributes(G, values=TIME_WINDOWS_LOWER, name="lower")
        set_node_attributes(G, values=TIME_WINDOWS_UPPER, name="upper")
        # Set demand and collect volumes
        set_node_attributes(G, values=DEMANDS, name="demand")
        set_node_attributes(G, values=COLLECT, name="collect")
        # Relabel depot
        self.G = relabel_nodes(G, {0: "Source", 17: "Sink"})
        # Define VRP
        self.prob = VehicleRoutingProblem(self.G)

    def test_cvrp(self):
        self.prob.load_capacity = 15
        self.prob.solve(cspy=False, pricing_strategy="PrunePaths")
        sol_lp = self.prob.best_value
        self.prob.solve(pricing_strategy="PrunePaths")
        sol_cspy = self.prob.best_value
        assert int(sol_lp) == 6208
        assert int(sol_cspy) == 6208

    def test_vrptw(self):
        self.prob.time_windows = True
        self.prob.solve(cspy=False)
        sol_lp = self.prob.best_value
        self.prob.solve()
        sol_cspy = self.prob.best_value
        assert int(sol_lp) == 6528
        assert int(sol_cspy) == 6528

    def test_cvrpsdc(self):
        self.prob.load_capacity = 15
        self.prob.distribution_collection = True
        self.prob.solve(cspy=False, pricing_strategy="PrunePaths")
        sol_lp = self.prob.best_value
        self.prob.solve(pricing_strategy="PrunePaths")
        sol_cspy = self.prob.best_value
        assert int(sol_lp) == 6208
        assert int(sol_cspy) == 6208

    def test_pdp(self):
        # Set demands and requests
        for (u, v) in PICKUPS_DELIVERIES:
            self.G.nodes[u]["request"] = v
            self.G.nodes[u]["demand"] = PICKUPS_DELIVERIES[(u, v)]
            self.G.nodes[v]["demand"] = -PICKUPS_DELIVERIES[(u, v)]
        self.prob.pickup_delivery = True
        self.prob.load_capacity = 10
        self.prob.num_stops = 6
        self.prob.solve(cspy=False)
        sol_lp = self.prob.best_value
        assert int(sol_lp) == 5980
Beispiel #15
0
class TestIssue86:
    def setup(self):
        G = DiGraph()
        G.add_edge(1, 2, cost=77.67)
        G.add_edge(1, 3, cost=0.0)
        G.add_edge(1, 4, cost=96.61)
        G.add_edge(1, 5, cost=0.0)
        G.add_edge(1, 6, cost=59.03)
        G.add_edge(2, 1, cost=77.67)
        G.add_edge(2, 3, cost=77.67)
        G.add_edge(2, 4, cost=64.85)
        G.add_edge(2, 5, cost=77.67)
        G.add_edge(2, 6, cost=62.2)
        G.add_edge(3, 1, cost=0.0)
        G.add_edge(3, 2, cost=77.67)
        G.add_edge(3, 4, cost=96.61)
        G.add_edge(3, 5, cost=0.0)
        G.add_edge(3, 6, cost=59.03)
        G.add_edge(4, 1, cost=96.61)
        G.add_edge(4, 2, cost=64.85)
        G.add_edge(4, 3, cost=96.61)
        G.add_edge(4, 5, cost=96.61)
        G.add_edge(4, 6, cost=39.82)
        G.add_edge(5, 1, cost=0.0)
        G.add_edge(5, 2, cost=77.67)
        G.add_edge(5, 3, cost=0.0)
        G.add_edge(5, 4, cost=96.61)
        G.add_edge(5, 6, cost=59.03)
        G.add_edge(6, 1, cost=59.03)
        G.add_edge(6, 2, cost=62.2)
        G.add_edge(6, 3, cost=59.03)
        G.add_edge(6, 4, cost=39.82)
        G.add_edge(6, 5, cost=59.03)
        G.add_edge("Source", 1, cost=18.03)
        G.add_edge(1, "Sink", cost=18.93)
        G.add_edge("Source", 2, cost=61.29)
        G.add_edge(2, "Sink", cost=61.29)
        G.add_edge("Source", 3, cost=18.03)
        G.add_edge(3, "Sink", cost=18.03)
        G.add_edge("Source", 4, cost=79.92)
        G.add_edge(4, "Sink", cost=79.92)
        G.add_edge("Source", 5, cost=18.03)
        G.add_edge(5, "Sink", cost=18.03)
        G.add_edge("Source", 6, cost=44.38)
        G.add_edge(6, "Sink", cost=44.38)
        G.nodes[1]["request"] = 2
        G.nodes[1]["demand"] = 25000
        G.nodes[2]["demand"] = -25000
        G.nodes[3]["request"] = 4
        G.nodes[3]["demand"] = 25000
        G.nodes[4]["demand"] = -25000
        G.nodes[5]["request"] = 6
        G.nodes[5]["demand"] = 10000
        G.nodes[6]["demand"] = -10000
        self.prob = VehicleRoutingProblem(G,
                                          load_capacity=25000,
                                          pickup_delivery=True)

    def test_solve(self):
        self.prob.solve(cspy=False, solver="cbc")
        assert round(self.prob.best_value, 0) == 468
Beispiel #16
0
 def test_LP_stops_capacity(self):
     """Tests column generation procedure on toy graph"""
     prob = VehicleRoutingProblem(self.G, num_stops=3, load_capacity=10)
     prob.solve(cspy=False)
     assert prob.best_value == 80
Beispiel #17
0
from numpy import matrix
from data import DISTANCES, PICKUPS_DELIVERIES
import sys

sys.path.append("../../")
from vrpy import VehicleRoutingProblem

# Transform distance matrix to DiGraph
A = matrix(DISTANCES, dtype=[("cost", int)])
G = from_numpy_matrix(A, create_using=DiGraph())

# Set demands and requests
for (u, v) in PICKUPS_DELIVERIES:
    G.nodes[u]["request"] = v
    G.nodes[u]["demand"] = PICKUPS_DELIVERIES[(u, v)]
    G.nodes[v]["demand"] = -PICKUPS_DELIVERIES[(u, v)]

# Relabel depot
G = relabel_nodes(G, {0: "Source", 17: "Sink"})

if __name__ == "__main__":

    prob = VehicleRoutingProblem(G,
                                 load_capacity=6,
                                 pickup_delivery=True,
                                 num_stops=6)
    prob.solve(cspy=False)
    print(prob.best_value)
    print(prob.best_routes)
    print(prob.node_load)
Beispiel #18
0
 def test_time_limit(self):
     prob = VehicleRoutingProblem(self.G, num_stops=3)
     prob.solve(cspy=False, time_limit=0.01)
     assert prob.best_value == 70
Beispiel #19
0
class TestIssue99:
    def setup(self):

        distance_ = [
            [
                0,
                0.0,
                0.0,
                34.751182089808005,
                92.30245516008434,
                40.1681913442985,
                139.77829026093886,
                22.427641389383695,
                184.16196166082054,
                24.56323283561296,
                120.32361659641211,
                32.4378310152284,
                67.38909304866816,
                0.0,
            ],
            [
                0,
                0.0,
                0.0,
                34.751182089808005,
                92.30245516008434,
                40.1681913442985,
                139.77829026093886,
                22.427641389383695,
                184.16196166082054,
                24.56323283561296,
                120.32361659641211,
                32.4378310152284,
                67.38909304866816,
                0.0,
            ],
            [
                0,
                34.751182089808005,
                34.751182089808005,
                0.0,
                98.7079853042215,
                5.44379884433748,
                132.19619367955679,
                12.670008991256175,
                180.4339020413057,
                59.02280970986385,
                111.80230048333873,
                17.019169601216593,
                52.06928899775174,
                34.751182089808005,
            ],
            [
                0,
                92.30245516008434,
                92.30245516008434,
                98.7079853042215,
                0.0,
                100.22229866723093,
                61.488042505729055,
                92.72080465523338,
                95.63269969727976,
                90.58095214437495,
                49.80739772877824,
                111.78483468218113,
                60.09941865363839,
                92.30245516008434,
            ],
            [
                0,
                40.1681913442985,
                40.1681913442985,
                5.44379884433748,
                100.22229866723093,
                0.0,
                131.22379419294873,
                17.9495157302615,
                179.86968501771943,
                64.376780302526,
                110.81671081048268,
                20.17178955112936,
                50.836998290147,
                40.1681913442985,
            ],
            [
                0,
                139.77829026093886,
                139.77829026093886,
                132.19619367955679,
                61.488042505729055,
                131.22379419294873,
                0.0,
                131.60938240610648,
                49.833874922036394,
                145.2998753018322,
                20.40765785593238,
                148.57304976786813,
                80.3991613224116,
                139.77829026093886,
            ],
            [
                0,
                22.427641389383695,
                22.427641389383695,
                12.670008991256175,
                92.72080465523338,
                17.9495157302615,
                131.60938240610648,
                0.0,
                178.66012933702726,
                46.44000947768777,
                111.40463276346814,
                19.110204473855905,
                53.409038447894005,
                22.427641389383695,
            ],
            [
                0,
                184.16196166082054,
                184.16196166082054,
                180.4339020413057,
                95.63269969727976,
                179.86968501771943,
                49.833874922036394,
                178.66012933702726,
                0.0,
                185.80417023197256,
                69.739408771209,
                196.3770010004616,
                129.30466285486838,
                184.16196166082054,
            ],
            [
                0,
                24.56323283561296,
                24.56323283561296,
                59.02280970986385,
                90.58095214437495,
                64.376780302526,
                145.2998753018322,
                46.44000947768777,
                185.80417023197256,
                0.0,
                127.25510989846872,
                56.34697111358194,
                82.12357585699394,
                24.56323283561296,
            ],
            [
                0,
                120.32361659641211,
                120.32361659641211,
                111.80230048333873,
                49.80739772877824,
                110.81671081048268,
                20.40765785593238,
                111.40463276346814,
                69.739408771209,
                127.25510989846872,
                0.0,
                128.21818865240243,
                59.99584151749755,
                120.32361659641211,
            ],
            [
                0,
                32.4378310152284,
                32.4378310152284,
                17.019169601216593,
                111.78483468218113,
                20.17178955112936,
                148.57304976786813,
                19.110204473855905,
                196.3770010004616,
                56.34697111358194,
                128.21818865240243,
                0.0,
                68.79822623983333,
                32.4378310152284,
            ],
            [
                0,
                67.38909304866816,
                67.38909304866816,
                52.06928899775174,
                60.09941865363839,
                50.836998290147,
                80.3991613224116,
                53.409038447894005,
                129.30466285486838,
                82.12357585699394,
                59.99584151749755,
                68.79822623983333,
                0.0,
                67.38909304866816,
            ],
            [
                0,
                0.0,
                0.0,
                34.751182089808005,
                92.30245516008434,
                40.1681913442985,
                139.77829026093886,
                22.427641389383695,
                184.16196166082054,
                24.56323283561296,
                120.32361659641211,
                32.4378310152284,
                67.38909304866816,
                0.0,
            ],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        ]

        demands = {
            0: 0,
            1: 24,
            2: 12,
            3: 22,
            4: 63,
            5: 44,
            6: 68,
            7: 41,
            8: 27,
            9: 38,
            10: 15,
            11: 17,
            12: 0,
        }

        # Transform distance matrix to DiGraph
        A_ = array(distance_, dtype=[("cost", int)])
        G_ = from_numpy_matrix(A_, create_using=DiGraph())

        # Set demand
        set_node_attributes(G_, values=demands, name="demand")

        # Relabel depot
        G_ = relabel_nodes(G_, {0: "Source", 13: "Sink"})

        # Define VRP
        self.prob = VehicleRoutingProblem(G_, load_capacity=100)

    def test_lp(self):
        self.prob.solve(cspy=False)
        print(self.prob.best_routes)
        assert self.prob.best_value == 829

    def test_cspy(self):
        self.prob.solve(cspy=True)
        print(self.prob.best_routes)
        assert self.prob.best_value == 829
Beispiel #20
0
 def test_LP_stops(self):
     """Tests column generation procedure on toy graph with stop constraints"""
     prob = VehicleRoutingProblem(self.G, num_stops=3)
     prob.solve(cspy=False)
     assert prob.best_value == 70
Beispiel #21
0
 def test_partial_lock(self):
     routes = [["Source", 3]]
     prob = VehicleRoutingProblem(self.G, num_stops=4)
     prob.solve(preassignments=routes)
     assert prob.best_value == 75
Beispiel #22
0
G = from_numpy_matrix(A, create_using=DiGraph())

# Set demands
set_node_attributes(G, values=DEMANDS_DROP, name="demand")

# Relabel depot
G = relabel_nodes(G, {0: "Source", 17: "Sink"})

if __name__ == "__main__":

    prob = VehicleRoutingProblem(G,
                                 load_capacity=15,
                                 drop_penalty=1000,
                                 num_vehicles=4)
    prob.solve(
        preassignments=
        [  # locking these routes should yield prob.best_value == 7936
            # [9, 14, 16],
            # [12, 11, 4, 3, 1],
            # [7, 13],
            # [8, 10, 2, 5],
        ], )
    print(prob.best_value)
    print(prob.best_routes)
    print(prob.best_routes_cost)
    print(prob.best_routes_load)
    print(prob.node_load)
    assert prob.best_value == 8096

    # why doesn't vrpy find 7936 ?
Beispiel #23
0
)

from vrpy import VehicleRoutingProblem

# Transform distance matrix to DiGraph
A = array(DISTANCES, dtype=[("cost", int)])
G_d = from_numpy_matrix(A, create_using=DiGraph())

# Transform time matrix to DiGraph
A = array(TRAVEL_TIMES, dtype=[("time", int)])
G_t = from_numpy_matrix(A, create_using=DiGraph())

# Merge
G = compose(G_d, G_t)

# Set time windows
set_node_attributes(G, values=TIME_WINDOWS_LOWER, name="lower")
set_node_attributes(G, values=TIME_WINDOWS_UPPER, name="upper")

# Relabel depot
G = relabel_nodes(G, {0: "Source", 17: "Sink"})

if __name__ == "__main__":

    prob = VehicleRoutingProblem(G, time_windows=True)
    prob.solve()
    print(prob.best_value)
    print(prob.best_routes)
    print(prob.arrival_time)
    assert prob.best_value == 6528
Beispiel #24
0
 def test_fixed_cost(self):
     prob = VehicleRoutingProblem(self.G, num_stops=3, fixed_cost=100)
     prob.solve()
     assert prob.best_value == 70 + 200
     assert set(prob.best_routes_cost.values()) == {30 + 100, 40 + 100}