-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement traveling salesman problem solver with using the random key…
… genetic algorithm
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module TravelingSalesman | ||
|
||
import ..RandomKeyGA: run_ga | ||
|
||
export TravelinSalesmenResult | ||
export travelingsalesman | ||
|
||
struct TravelinSalesmenResult | ||
route::Vector{Int} | ||
cost::Float64 | ||
end | ||
|
||
function travelingsalesman(distancematrix::Matrix{TType}; | ||
popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1):: TravelinSalesmenResult where {TType<:Float64} | ||
|
||
n, _ = size(distancematrix) | ||
|
||
function costfn(route::Vector{Int})::Float64 | ||
cost = 0.0 | ||
for i in 1:length(route)-1 | ||
cost += distancematrix[route[i], route[i+1]] | ||
end | ||
cost += distancematrix[route[end], route[1]] | ||
return cost | ||
end | ||
|
||
|
||
# popsize::Int, chsize::Int, costfn::F, ngen::Int, pcross::Float64, pmutate::Float64, nelites::Int | ||
garesult = run_ga(popsize, n, costfn, ngen, pcross, pmutate, nelites) | ||
|
||
best = garesult.chromosomes[1] | ||
cost = costfn(sortperm(best.values)) | ||
|
||
return TravelinSalesmenResult(sortperm(best.values), cost) | ||
end | ||
|
||
|
||
end # end of module TravelingSalesman |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
@testset "Traveling Salesman with Random Key GA" verbose = true begin | ||
|
||
@testset "Points in a rectangle" begin | ||
|
||
# | ||
# * * * * * * | ||
# * * | ||
# * * * * * * | ||
pts = Float64[ | ||
0 0; | ||
0 1; | ||
0 2; | ||
1 2; | ||
2 2; | ||
3 2; | ||
4 2; | ||
5 2; | ||
5 1; | ||
5 0; | ||
4 0; | ||
3 0; | ||
2 0; | ||
1 0; | ||
] | ||
|
||
n = size(pts, 1) | ||
|
||
distmat = zeros(n, n) | ||
|
||
# create the distance matrix | ||
for i in 1:n | ||
for j in 1:n | ||
distmat[i, j] = sqrt(sum((pts[i, :] .- pts[j, :]).^2)) | ||
end | ||
end | ||
|
||
bestval = Inf64 | ||
|
||
for i in 1:10 | ||
result = travelingsalesman(distmat, ngen = 1000, popsize = 100, pcross = 1.0, pmutate = 0.10) | ||
if result.cost < bestval | ||
bestval = result.cost | ||
bestresult = result | ||
end | ||
end | ||
|
||
@test bestval == 14.0 | ||
end | ||
|
||
|
||
end # end of whole @testset |