Skip to content

Commit

Permalink
update p-median for predefined distance matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed Apr 1, 2024
1 parent 6ac59f9 commit 2f75859
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/src/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ julia> result.flow
OperationsResearchModels.pmedian
```

```@docs
OperationsResearchModels.pmedian_with_distances
```

## Minimum Spanning Tree
```@docs
OperationsResearchModels.mst
Expand Down
4 changes: 2 additions & 2 deletions src/OperationsResearchModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import .MaximumFlow: MaximumFlowResult
import .Assignment: AssignmentProblem, AssignmentResult
import .Game: game, GameResult
import .MinimumSpanningTree: hasloop, mst, MstResult
import .PMedian: pmedian
import .PMedian: pmedian, pmedian_with_distances
import .CPM: cpm, CpmActivity, earliestfinishtime, longestactivity, CpmResult
import .CPM: pert, PertActivity, PertResult
import .Latex: latex
Expand All @@ -51,7 +51,7 @@ export ShortestPathProblem, MaximumFlowProblem
export AssignmentProblem, AssignmentResult
export game, GameResult
export hasloop, mst, MstResult
export pmedian
export pmedian, pmedian_with_distances
export cpm, CpmActivity, earliestfinishtime, longestactivity, CpmResult
export pert, PertActivity, PertResult
export Simplex
Expand Down
26 changes: 26 additions & 0 deletions src/pmedian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ function pmedian(data::Matrix, ncenters::Int)
end
end

return pmedian_with_distances(distances, ncenters)
end



"""
pmedian_with_distances(distancematrix, ncenters)
# Arguments
- `distancematrix::Matrix`: n x n matrix of distances
- `ncenters::Int`: Number of centers
# Descriptions
`ncenters` locations are selected that minimizes the total distances to the nearest rows.
# Output
- `Dict{String, Any}`: The dictionary object that holds the results.
"""
function pmedian_with_distances(distancematrix::Matrix, ncenters::Int)

n, _ = size(distancematrix)

distances = distancematrix


model = Model(HiGHS.Optimizer)
MOI.set(model, MOI.Silent(), true)

Expand Down
12 changes: 12 additions & 0 deletions test/testpmedian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@
@test iszero(y[5])
@test isone(y[6])
end

@testset "p-median with distances" begin
distance_matrix = Float64[0 6 3 2; 6 0 4 7; 3 4 0 10; 2 7 10 0]

number_of_depots = 2

result = pmedian_with_distances(distance_matrix, number_of_depots)

@test sort(result["centers"]) == [1, 2]
@test result["objective"] == 5.0
@test result["z"] [1 0 0 0; 0 1 0 0; 1 0 0 0; 1 0 0 0]
end
end

0 comments on commit 2f75859

Please sign in to comment.