@@ -2,24 +2,54 @@ module TravelingSalesman
22
33import .. RandomKeyGA: run_ga
44
5- export TravelinSalesmenResult
6- export travelingsalesman
5+ import .. OperationsResearchModels: solve
76
8- struct TravelinSalesmenResult
7+
8+ export TravelingSalesmanProblem
9+ export TravelingSalesmanResult
10+
11+
12+ """
13+ TravelingSalesmanProblem
14+
15+ # Description
16+ A data structure to hold the problem definition of the traveling salesman problem.
17+
18+ # Fields
19+ - `distancematrix::Matrix{Real}`: The distance matrix representing the distances between cities.
20+ """
21+ struct TravelingSalesmanProblem
22+ distancematrix:: Matrix{Real}
23+ end
24+
25+
26+
27+
28+ """
29+ TravelingSalesmanResult
30+
31+ # Description
32+ A data structure to hold the result of the traveling salesman problem.
33+
34+ # Fields
35+ - `route::Vector{Int}`: The best route found.
36+ - `cost::Float64`: The cost of the best route.
37+ """
38+ struct TravelingSalesmanResult
939 route:: Vector{Int}
1040 cost:: Float64
1141end
1242
1343
1444
1545"""
16- travelingsalesman(distancematrix::Matrix ; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::TravelinSalesmenResult
46+ solve(problem::TravelingSalesmanProblem ; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::TravelingSalesmanResult
1747
18- Given a matrix of distances, returns a TravelinSalesmenResult with the best route and its cost.
48+ Given a matrix of distances wrapped in a TravelingSalesmanProblem , returns a TravelingSalesmanResult with the best route and its cost.
1949
2050# Arguments
2151
22- - `distancematrix::Matrix `: a matrix of distances
52+ - `problem::TravelingSalesmanProblem `: a TravelingSalesmanProblem instance containing the distance matrix
2353- `popsize::Int`: the population size. Default is 100
2454- `ngen::Int`: the number of generations. Default is 1000
2555- `pcross::Float64`: the crossover probability. Default is 0.8
@@ -28,7 +58,7 @@ Given a matrix of distances, returns a TravelinSalesmenResult with the best rout
2858
2959# Returns
3060
31- - `TravelinSalesmenResult `: a custom data type that holds the best route and its cost
61+ - `TravelingSalesmanResult `: a custom data type that holds the best route and its cost
3262
3363# Example
3464
@@ -59,20 +89,20 @@ for i in 1:n
5989 end
6090end
6191
62- result = travelingsalesman( distmat, ngen = 1000, popsize = 100, pcross = 1.0, pmutate = 0.10)
92+ result = solve(TravelingSalesmanProblem( distmat) , ngen = 1000, popsize = 100, pcross = 1.0, pmutate = 0.10)
6393```
6494"""
65- function travelingsalesman (distancematrix :: Matrix{TType} ;
66- popsize = 100 , ngen = 1000 , pcross = 0.8 , pmutate = 0.01 , nelites = 1 ):: TravelinSalesmenResult where {TType <: Float64 }
95+ function solve (problem :: TravelingSalesmanProblem ;
96+ popsize = 100 , ngen = 1000 , pcross = 0.8 , pmutate = 0.01 , nelites = 1 ):: TravelingSalesmanResult
6797
68- n, _ = size (distancematrix)
98+ n, _ = size (problem . distancematrix)
6999
70100 function costfn (route:: Vector{Int} ):: Float64
71101 cost = 0.0
72102 for i in 1 : length (route)- 1
73- cost += distancematrix[route[i], route[i+ 1 ]]
103+ cost += problem . distancematrix[route[i], route[i+ 1 ]]
74104 end
75- cost += distancematrix[route[end ], route[1 ]]
105+ cost += problem . distancematrix[route[end ], route[1 ]]
76106 return cost
77107 end
78108
@@ -83,8 +113,8 @@ function travelingsalesman(distancematrix::Matrix{TType};
83113 best = garesult. chromosomes[1 ]
84114 cost = costfn (sortperm (best. values))
85115
86- return TravelinSalesmenResult (sortperm (best. values), cost)
87- end
116+ return TravelingSalesmanResult (sortperm (best. values), cost)
117+ end
88118
89119
90120end # end of module TravelingSalesman
0 commit comments