From 1e16279492a4e99ffb8b3abbc6799788bffb4b6c Mon Sep 17 00:00:00 2001 From: "mhsatman@gmail.com" Date: Wed, 22 May 2024 21:56:23 +0300 Subject: [PATCH] less constraints in p-median problem --- src/pmedian.jl | 6 +++--- test/testpmedian.jl | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/pmedian.jl b/src/pmedian.jl index e280be6..8e60bbe 100644 --- a/src/pmedian.jl +++ b/src/pmedian.jl @@ -117,6 +117,8 @@ function pmedian_with_distances(distancematrix::Matrix, ncenters::Int)::PMedianR distances = distancematrix + M = 100000.0 * n + model = Model(HiGHS.Optimizer) MOI.set(model, MOI.Silent(), true) @@ -127,9 +129,7 @@ function pmedian_with_distances(distancematrix::Matrix, ncenters::Int)::PMedianR @constraint(model, sum(y) == ncenters) for i = 1:n - for j = 1:n - @constraint(model, z[i, j] .<= y[j]) - end + @constraint(model, sum(z[:, i]) <= M * y[i]) end for i = 1:n diff --git a/test/testpmedian.jl b/test/testpmedian.jl index ae737b0..d22a32c 100644 --- a/test/testpmedian.jl +++ b/test/testpmedian.jl @@ -73,7 +73,8 @@ result2 = pmedian_with_distances(distance_matrix, 3) result3 = pmedian_with_distances(distance_matrix, 4) - @test sort(result1.centers) == [3, 4] + + @test (sort(result1.centers) == [3, 4]) || (sort(result1.centers) == [1, 2]) @test result1.objective == 22.0 @test sort(result2.centers) == [3, 4, 5]