Skip to content

Fix comparison for non-dominated to account for tolerances #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/MultiObjectiveAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,41 @@ end
Base.:(==)(a::SolutionPoint, b::SolutionPoint) = a.y == b.y

"""
dominates(sense, a::SolutionPoint, b::SolutionPoint)
dominates(sense, a::SolutionPoint, b::SolutionPoint; atol::Float64)

Returns `true` if point `a` dominates point `b`.
"""
function dominates(sense, a::SolutionPoint, b::SolutionPoint)
if a.y == b.y
return false
elseif sense == MOI.MIN_SENSE
return all(a.y .<= b.y)
function dominates(
sense::MOI.OptimizationSense,
a::SolutionPoint,
b::SolutionPoint;
atol::Float64 = 1e-6,
)
l, u = extrema(a.y - b.y)
if sense == MOI.MIN_SENSE
# At least one element must be strictly better => l < -atol
# No element can be structly worse => u <= atol
return l < -atol && u <= atol
else
return all(a.y .>= b.y)
# At least one element must be strictly better => u > atol
# No element can be structly worse => l >= -atol
return u > atol && l >= -atol
end
end

_sort!(solutions::Vector{SolutionPoint}) = sort!(solutions; by = x -> x.y)

function filter_nondominated(sense, solutions::Vector{SolutionPoint})
function filter_nondominated(
sense,
solutions::Vector{SolutionPoint};
atol::Float64 = 1e-6,
)
_sort!(solutions)
nondominated_solutions = SolutionPoint[]
for candidate in solutions
if any(test -> dominates(sense, test, candidate), solutions)
if any(test -> dominates(sense, test, candidate; atol), solutions)
# Point is dominated. Don't add
elseif any(test -> test.ycandidate.y, nondominated_solutions)
elseif any(test -> ≈(test.y, candidate.y; atol), nondominated_solutions)
# Point already added to nondominated solutions. Don't add
else
push!(nondominated_solutions, candidate)
Expand Down
21 changes: 21 additions & 0 deletions test/test_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ function test_filter_nondominated_triple()
return
end

function test_filter_epsilon()
x = Dict{MOI.VariableIndex,Float64}()
solutions =
[MOA.SolutionPoint(x, [1, 1 + 1e-6]), MOA.SolutionPoint(x, [2, 1])]
new_solutions = MOA.filter_nondominated(MOI.MAX_SENSE, copy(solutions))
@test new_solutions == solutions[2:2]
solutions =
[MOA.SolutionPoint(x, [1, 1 + 9e-5]), MOA.SolutionPoint(x, [2, 1])]
new_solutions = MOA.filter_nondominated(MOI.MAX_SENSE, copy(solutions))
@test new_solutions == solutions
solutions =
[MOA.SolutionPoint(x, [-1, -1 - 1e-6]), MOA.SolutionPoint(x, [-2, -1])]
new_solutions = MOA.filter_nondominated(MOI.MIN_SENSE, copy(solutions))
@test new_solutions == solutions[2:2]
solutions =
[MOA.SolutionPoint(x, [-1, -1 - 9e-5]), MOA.SolutionPoint(x, [-2, -1])]
new_solutions = MOA.filter_nondominated(MOI.MIN_SENSE, copy(solutions))
@test new_solutions == reverse(solutions)
return
end

end

TestUtilities.run_tests()
Loading