Traveling Salesman

OperationsResearchModels.TravelingSalesman.travelingsalesmanFunction
travelingsalesman(distancematrix::Matrix; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::TravelinSalesmenResult

Given a matrix of distances, returns a TravelinSalesmenResult with the best route and its cost.

Arguments

  • distancematrix::Matrix: a matrix of distances
  • popsize::Int: the population size. Default is 100
  • ngen::Int: the number of generations. Default is 1000
  • pcross::Float64: the crossover probability. Default is 0.8
  • pmutate::Float64: the mutation probability. Default is 0.01
  • nelites::Int: the number of elites. Default is 1

Returns

  • TravelinSalesmenResult: a custom data type that holds the best route and its cost

Example

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)

for i in 1:n
    for j in 1:n
        distmat[i, j] = sqrt(sum((pts[i, :] .- pts[j, :]).^2))
    end 
end

result = travelingsalesman(distmat, ngen = 1000, popsize = 100, pcross = 1.0, pmutate = 0.10)
source