Skip to content

Commit

Permalink
Add Tests for CVRP
Browse files Browse the repository at this point in the history
  • Loading branch information
heatingma committed May 7, 2024
1 parent 48813cf commit 35ae023
Show file tree
Hide file tree
Showing 12 changed files with 460 additions and 29 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ coverage.xml
.coverage

# tests/solver_test
tests/solver_test/mis_test/**
tests/solver_test/tsp50_test_small/**
tests/solver_test/mis_test/**
tests/solver_test/cvrp50_test/**

# tests/draw_test
tests/draw_test/mis_example/preprocessed/**
tests/draw_test/mis_example/solve/**
tests/draw_test/mis_problem.png
tests/draw_test/mis_solution.png
tests/draw_test/eil101_solution.png
tests/draw_test/eil101_problem.png
tests/draw_test/eil101_problem.png
tests/draw_test/cvrp_solution.png
tests/draw_test/cvrp_problem.png
7 changes: 5 additions & 2 deletions ml4co_kit/generator/cvrp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tqdm import tqdm
from typing import Union
from multiprocessing import Pool
from ml4co_kit.solver.cvrp import CVRPSolver, CVRPPyVRPSolver
from ml4co_kit.solver.cvrp import CVRPSolver, CVRPPyVRPSolver, CVRPLKHSolver


class CVRPDataGenerator:
Expand Down Expand Up @@ -110,7 +110,10 @@ def check_solver(self):
# get solver
if type(self.solver) == str:
self.solver_type = self.solver
supported_solver_dict = {"pyvrp": CVRPPyVRPSolver}
supported_solver_dict = {
"pyvrp": CVRPPyVRPSolver,
"lkh": CVRPLKHSolver
}
supported_solver_type = supported_solver_dict.keys()
if self.solver_type not in supported_solver_type:
message = (
Expand Down
14 changes: 12 additions & 2 deletions ml4co_kit/solver/cvrp/lkh.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ def _solve(
log_save_path = f"{tmp_name}.log"

# prepare for solve
self.to_vrp(save_dir="./", filename=vrp_save_path)
self.tmp_solver.from_data(
depots=depot_coord,
points=nodes_coord,
demands=demands,
capacities=capacity
)
self.tmp_solver.to_vrp(
save_dir="./",
filename=vrp_save_path
)
self.write_parameter_file(
save_path=para_save_path,
vrp_file_path=real_vrp_save_path,
Expand Down Expand Up @@ -131,6 +140,7 @@ def solve(
) -> np.ndarray:
# prepare
self.from_data(depots, points, demands, capacities, norm, normalize)
self.tmp_solver = CVRPSolver()
start_time = time.time()

# solve
Expand All @@ -139,7 +149,7 @@ def solve(
num_points = p_shape[0]
if num_threads == 1:
if show_time:
for idx in tqdm(range(num_points), desc="Solving CVRP Using PyVRP"):
for idx in tqdm(range(num_points), desc="Solving CVRP Using LKH"):
tours.append(self._solve(
depot_coord=self.depots[idx],
nodes_coord=self.points[idx],
Expand Down
76 changes: 76 additions & 0 deletions tests/draw_test/A-n32-k5.vrp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
NAME : A-n32-k5
COMMENT : (Augerat et al, No of trucks: 5, Optimal value: 784)
TYPE : CVRP
DIMENSION : 32
EDGE_WEIGHT_TYPE : EUC_2D
CAPACITY : 100
NODE_COORD_SECTION
1 82 76
2 96 44
3 50 5
4 49 8
5 13 7
6 29 89
7 58 30
8 84 39
9 14 24
10 2 39
11 3 82
12 5 10
13 98 52
14 84 25
15 61 59
16 1 65
17 88 51
18 91 2
19 19 32
20 93 3
21 50 93
22 98 14
23 5 42
24 42 9
25 61 62
26 9 97
27 80 55
28 57 69
29 23 15
30 20 70
31 85 60
32 98 5
DEMAND_SECTION
1 0
2 19
3 21
4 6
5 19
6 7
7 12
8 16
9 6
10 16
11 8
12 14
13 21
14 16
15 3
16 22
17 18
18 19
19 1
20 24
21 8
22 12
23 4
24 8
25 24
26 24
27 2
28 20
29 15
30 2
31 14
32 9
DEPOT_SECTION
1
-1
EOF
10 changes: 10 additions & 0 deletions tests/solver_test/A-n55-k9.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Route #1: 4 7 42 31 20 46 26
Route #2: 36 11 15 51 2 17 14
Route #3: 37 3 34 33 21
Route #4: 1 45 6 8
Route #5: 25 41 29
Route #6: 23 52 24 44 50 48 18
Route #7: 32 38 16 40 53 5 10 12
Route #8: 30 22 19 27 13 54 28
Route #9: 47 39 49 9 35 43
cost 1073
122 changes: 122 additions & 0 deletions tests/solver_test/A-n55-k9.vrp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
NAME : A-n55-k9
COMMENT : (Augerat et al, No of trucks: 9, Optimal value: 1073)
TYPE : CVRP
DIMENSION : 55
EDGE_WEIGHT_TYPE : EUC_2D
CAPACITY : 100
NODE_COORD_SECTION
1 36 64
2 94 47
3 10 23
4 16 46
5 25 79
6 41 30
7 81 45
8 14 79
9 42 56
10 90 17
11 41 39
12 21 14
13 41 46
14 65 96
15 13 49
16 21 14
17 57 2
18 14 42
19 66 62
20 58 96
21 5 51
22 41 50
23 50 99
24 84 85
25 97 90
26 47 76
27 11 54
28 60 97
29 60 89
30 58 68
31 30 93
32 9 60
33 47 44
34 19 40
35 15 40
36 88 21
37 33 58
38 21 51
39 57 7
40 81 6
41 49 6
42 51 78
43 9 62
44 84 36
45 95 76
46 89 44
47 10 49
48 69 16
49 75 66
50 97 11
51 74 69
52 1 14
53 96 91
54 46 22
55 74 92
DEMAND_SECTION
1 0
2 3
3 12
4 25
5 4
6 11
7 20
8 21
9 10
10 20
11 13
12 14
13 16
14 17
15 11
16 36
17 6
18 7
19 21
20 11
21 17
22 22
23 10
24 19
25 21
26 23
27 19
28 15
29 22
30 7
31 11
32 15
33 22
34 12
35 24
36 25
37 2
38 15
39 18
40 13
41 3
42 20
43 14
44 10
45 10
46 66
47 10
48 7
49 12
50 24
51 5
52 18
53 7
54 11
55 12
DEPOT_SECTION
1
-1
EOF
10 changes: 10 additions & 0 deletions tests/solver_test/cvrp50_test.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/solver_test/cvrp50_test_small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
depots 0.501598060035068 0.5498974493818155 points 0.11304211469010916 0.3992104921106189 0.8543301758946301 0.6680426729220328 0.6684074580260562 0.6610042549414241 0.9291481012289046 0.03963379415136281 0.577893430682808 0.9741123948967905 0.9254043937203746 0.6852604249650516 0.4192002407472739 0.16968284582496107 0.6330816505673114 0.8191967412341001 0.9437566383627888 0.44608124603161126 0.24926817141906388 0.25608966423561697 0.0702122125672493 0.6373161425524806 0.8031124167881284 0.3223531001235268 0.2139721795608015 0.1949689028025262 0.4479264651014683 0.5168148955467924 0.2662948520821572 0.48698120401901435 0.2343649523336474 0.46814103815540253 0.33793948876599766 0.4264070096760526 0.0667452568504644 0.49139523888032877 0.16092630873258185 0.18917045825953582 0.7622513731696926 0.26436298482392273 0.4167481052631158 0.009570937843020322 0.8977652795293365 0.4426413270275974 0.0006947721062070533 0.015360395005902472 0.8392742134551229 0.9325537526304675 0.9619507201933537 0.5881811349957976 0.22907309495887784 0.1720308295334002 0.30979794986409637 0.5822960198933747 0.905312793526502 0.0012351432467750945 0.223097741905672 0.8227695993599383 0.3555768004813611 0.7552497844656109 0.40276563636983165 0.038385625229090614 0.6320352879435833 0.20649733365636957 0.5865746357816728 0.33870318493440743 0.0952604017485923 0.559657430027712 0.5983510693157378 0.7738305271946897 0.39776058480549514 0.4610728444572503 0.7355421367274163 0.4344463350536104 0.83847116397056 0.9032690889516504 0.8737974455951626 0.5353763180686822 0.40398664577701127 0.5094795102260636 0.374549826179722 0.30604759186468655 0.6688214262327677 0.7657545560485289 0.9863419374575921 0.24303759178877404 0.5516564470472992 0.6307334273594052 0.3946355370816592 0.3113068626372393 0.6912626592742003 0.5870587796163824 0.036123094753925944 0.7504936601515089 0.7616425717679123 0.22493013263163253 0.7107899514975534 0.9641083443327164 0.6751804284704886 0.7994001075121416 demands 2.0 8.0 2.0 7.0 3.0 9.0 6.0 2.0 7.0 5.0 1.0 4.0 9.0 6.0 6.0 5.0 7.0 2.0 2.0 5.0 4.0 2.0 5.0 5.0 5.0 5.0 4.0 1.0 3.0 6.0 4.0 7.0 9.0 9.0 2.0 8.0 5.0 3.0 4.0 4.0 1.0 3.0 6.0 2.0 3.0 7.0 1.0 9.0 1.0 6.0 capacity 40.0 output 0 7 31 21 23 19 13 26 10 0 33 45 41 17 36 40 14 0 12 43 4 28 48 20 32 0 2 6 25 39 9 22 37 0 44 35 8 5 49 24 38 50 42 3 46 0 27 15 16 1 18 34 11 47 29 30 0
depots 0.4714729068178677 0.20689363635140767 points 0.6682740010463054 0.1368705020526474 0.959209783181899 0.8685708579583493 0.5899341943607497 0.33492161604221715 0.3722659793844889 0.626396046034317 0.43671464585076136 0.7987825014465118 0.786328501583814 0.729416560874742 0.4095595389478194 0.42846239225143434 0.5260266907701404 0.5015385059043186 0.2348490534800446 0.7400928993395417 0.42257474332274025 0.854222874454145 0.1669013978747742 0.8277704866084222 0.084824602658505 0.2490493897089262 0.646697975427824 0.8232193151279 0.08445746016746414 0.6492445854022443 0.03771536332930203 0.5713420930864881 0.20542882295973552 0.0117687425250661 0.2581609351727234 0.9344162665235103 0.3800557161362572 0.07544110319804831 0.4855363140413823 0.7589671777584606 0.9247837554176086 0.7986276221241904 0.9526088828409918 0.7200971411259609 0.5755865722282824 0.1966731612107223 0.4726279002987124 0.44193228193356027 0.7511391757947087 0.5850966279643861 0.10579551226635708 0.8546965981416499 0.04197577890354054 0.5827646783664523 0.3888392301215329 0.4411037409000841 0.09457476220262018 0.7854784154583713 0.9909847609736818 0.08878914474256983 0.3950020494291896 0.49345010903904907 0.9127438546438186 0.8314856209150885 0.949585765475134 0.5457708752634866 0.27714180389946264 0.5336496075891558 0.6754937365942821 0.4412529493176144 0.11323995206660542 0.914973190453434 0.6694287035560543 0.9153789664886837 0.5495361164628769 0.49077515936231375 0.9669170059667724 0.359919145549099 0.007337275269860943 0.3034923565468354 0.1569570128911617 0.9949102296097929 0.4340234200476555 0.9631880869779804 0.3538839697887778 0.7994155274521151 0.9773166500053615 0.1769633858202816 0.3811252432969252 0.4520814111219176 0.41923161110589924 0.011139483456309773 0.32450709987653714 0.03762181210789839 0.5969888927521074 0.5318264725370081 0.580480904365413 0.2730541513369419 0.37470408629847984 0.5268102460098356 0.9170921506552436 0.5552874274729183 demands 2.0 3.0 4.0 7.0 5.0 5.0 8.0 2.0 2.0 3.0 6.0 3.0 5.0 2.0 1.0 2.0 4.0 3.0 2.0 7.0 9.0 4.0 8.0 6.0 1.0 7.0 5.0 2.0 9.0 3.0 2.0 6.0 9.0 9.0 3.0 9.0 8.0 2.0 2.0 8.0 7.0 6.0 2.0 9.0 9.0 9.0 7.0 4.0 8.0 2.0 capacity 40.0 output 0 30 4 42 17 41 10 5 19 8 0 16 12 39 15 26 14 28 25 35 40 11 9 0 45 46 18 0 22 48 3 47 37 23 0 1 29 43 38 32 50 24 34 0 13 36 2 31 20 21 6 0 7 27 44 49 33 0
38 changes: 33 additions & 5 deletions tests/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
sys.path.append(root_folder)
from ml4co_kit.draw.tsp import draw_tsp_solution, draw_tsp_problem
from ml4co_kit.draw.mis import draw_mis_solution, draw_mis_problem

from ml4co_kit.solver import TSPConcordeSolver, KaMISSolver
from ml4co_kit.draw.cvrp import draw_cvrp_solution, draw_cvrp_problem
from ml4co_kit.solver import TSPConcordeSolver, KaMISSolver, CVRPPyVRPSolver


def test_draw_tsp():
# use TSPConcordeSolver to solve the problem
solver = TSPConcordeSolver(scale=100)
solver.from_tsp("tests/draw_test/eil101.tsp")
solver.solve()

# draw
draw_tsp_problem(
save_path="tests/draw_test/eil101_problem.png",
points=solver.ori_points,
Expand All @@ -24,7 +27,7 @@ def test_draw_tsp():
)


def test_mis_tsp():
def test_draw_mis():
# use KaMISSolver to solve the problem
mis_solver = KaMISSolver()
mis_solver.solve(src="tests/draw_test/mis_example")
Expand All @@ -39,8 +42,33 @@ def test_mis_tsp():
gpickle_path="tests/draw_test/mis_example/mis_example.gpickle",
result_path="tests/draw_test/mis_example/solve/mis_example_unweighted.result"
)



def test_draw_cvrp():
# use KaMISSolver to solve the problem
solver = CVRPPyVRPSolver(
depots_scale=1,
points_scale=1,
time_limit=1
)
solver.from_vrp("tests/draw_test/A-n32-k5.vrp")
solver.solve()

# draw
draw_cvrp_problem(
save_path="tests/draw_test/cvrp_problem.png",
depots=solver.depots,
points=solver.points
)
draw_cvrp_solution(
save_path="tests/draw_test/cvrp_solution.png",
depots=solver.depots,
points=solver.points,
tour=solver.tours
)


if __name__ == "__main__":
test_draw_tsp()
test_mis_tsp()
test_draw_mis()
test_draw_cvrp()
7 changes: 6 additions & 1 deletion tests/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

root_folder = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root_folder)
from ml4co_kit.data import SATLIBOriDataset
from ml4co_kit.data import SATLIBOriDataset, VRPLIBOriDataset
from ml4co_kit.evaluate import TSPLIBOriEvaluator, TSPUniformEvaluator, SATLIBEvaluator
from ml4co_kit.solver import TSPConcordeSolver, KaMISSolver

Expand Down Expand Up @@ -72,7 +72,12 @@ def test_satlib_original_eval():
raise ValueError(message)


def test_vrplib_original_eval():
VRPLIBOriDataset()


if __name__ == "__main__":
test_tsplib_original_eval()
test_tsp_uniform_eval()
test_satlib_original_eval()
test_vrplib_original_eval()
Loading

0 comments on commit 35ae023

Please sign in to comment.