Skip to content

Commit

Permalink
add documentation for traveling salesman
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed Aug 2, 2024
1 parent 95e3da7 commit 887f9d7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ OperationsResearchModels.johnsons_ga
```@docs
OperationsResearchModels.makespan
```


## Traveling Salesman
```@docs
OperationsResearchModels.travelingsalesman
```
52 changes: 52 additions & 0 deletions src/travelingsalesman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,58 @@ struct TravelinSalesmenResult
cost::Float64
end



"""
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
```julia
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)
```
"""
function travelingsalesman(distancematrix::Matrix{TType};
popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1):: TravelinSalesmenResult where {TType<:Float64}

Expand Down

0 comments on commit 887f9d7

Please sign in to comment.