Skip to content
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
5 changes: 4 additions & 1 deletion src/MultiObjectiveAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ function dominates(sense, a::SolutionPoint, b::SolutionPoint)
end
end

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

function filter_nondominated(sense, solutions::Vector{SolutionPoint})
solutions = sort(solutions; by = x -> x.y)
_sort!(solutions)
nondominated_solutions = SolutionPoint[]
for candidate in solutions
if any(test -> dominates(sense, test, candidate), solutions)
Expand Down Expand Up @@ -594,6 +596,7 @@ function MOI.optimize!(model::Optimizer)
model.termination_status = status
if solutions !== nothing
model.solutions = solutions
_sort!(model.solutions)
end
if MOI.get(model, ComputeIdealPoint())
_compute_ideal_point(model, start_time)
Expand Down
52 changes: 21 additions & 31 deletions test/algorithms/Chalmet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,17 @@ function test_knapsack_min()
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.optimize!(model)
X_E = Float64[
0 0 1 1 1 0 1 1 1 1
1 0 1 1 1 0 1 1 0 1
0 1 1 1 1 0 1 0 1 1
results = [
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1] => [-3394, -3817],
[0, 1, 1, 1, 1, 0, 1, 0, 1, 1] => [-3042, -4627],
[0, 0, 1, 1, 1, 0, 1, 1, 1, 1] => [-2854, -4636],
]
Y_N = Float64[
-2854 -4636
-3394 -3817
-3042 -4627
]
N = MOI.get(model, MOI.ResultCount())
x_sol = hcat([MOI.get(model, MOI.VariablePrimal(i), x) for i in 1:N]...)
@test isapprox(x_sol, X_E'; atol = 1e-6)
y_sol = hcat([MOI.get(model, MOI.ObjectiveValue(i)) for i in 1:N]...)
@test isapprox(y_sol, Y_N'; atol = 1e-6)
@test MOI.get(model, MOI.ObjectiveBound()) ≈ vec(minimum(Y_N; dims = 1))
@test MOI.get(model, MOI.ResultCount()) == length(results)
for (i, (x_sol, y_sol)) in enumerate(results)
@test ≈(x_sol, MOI.get(model, MOI.VariablePrimal(i), x); atol = 1e-6)
@test ≈(y_sol, MOI.get(model, MOI.ObjectiveValue(i)); atol = 1e-6)
end
@test MOI.get(model, MOI.ObjectiveBound()) ≈ [-3394, -4636]
return
end

Expand Down Expand Up @@ -103,22 +98,17 @@ function test_knapsack_max()
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.optimize!(model)
X_E = Float64[
0 0 1 1 1 0 1 1 1 1
1 0 1 1 1 0 1 1 0 1
0 1 1 1 1 0 1 0 1 1
]
Y_N = Float64[
2855 4636
3395 3817
3043 4627
results = [
[0, 0, 1, 1, 1, 0, 1, 1, 1, 1] => [2855, 4636],
[0, 1, 1, 1, 1, 0, 1, 0, 1, 1] => [3043, 4627],
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1] => [3395, 3817],
]
N = MOI.get(model, MOI.ResultCount())
x_sol = hcat([MOI.get(model, MOI.VariablePrimal(i), x) for i in 1:N]...)
@test isapprox(x_sol, X_E'; atol = 1e-6)
y_sol = hcat([MOI.get(model, MOI.ObjectiveValue(i)) for i in 1:N]...)
@test isapprox(y_sol, Y_N'; atol = 1e-6)
@test MOI.get(model, MOI.ObjectiveBound()) ≈ vec(maximum(Y_N; dims = 1))
@test MOI.get(model, MOI.ResultCount()) == length(results)
for (i, (x_sol, y_sol)) in enumerate(results)
@test (x_sol, MOI.get(model, MOI.VariablePrimal(i), x); atol = 1e-6)
@test ≈(y_sol, MOI.get(model, MOI.ObjectiveValue(i)); atol = 1e-6)
end
@test MOI.get(model, MOI.ObjectiveBound()) ≈ [3395, 4636]
return
end

Expand Down Expand Up @@ -228,6 +218,6 @@ function test_too_many_objectives()
return
end

end
end # module TestChalmet

TestChalmet.run_tests()
83 changes: 41 additions & 42 deletions test/algorithms/Dichotomy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ function test_moi_bolp_1()
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y
minobjective: [2 * x + y + 1, x + 3 * y]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
variables: x, y
minobjective: [2 * x + y + 1, x + 3 * y]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
)
x = MOI.get(model, MOI.VariableIndex, "x")
y = MOI.get(model, MOI.VariableIndex, "y")
Expand Down Expand Up @@ -81,21 +81,21 @@ function test_moi_bolp_1_maximize()
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y
maxobjective: [-2.0 * x + -1.0 * y, -1.0 * x + -3.0 * y + 0.5]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
variables: x, y
maxobjective: [-2.0 * x + -1.0 * y, -1.0 * x + -3.0 * y + 0.5]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
)
x = MOI.get(model, MOI.VariableIndex, "x")
y = MOI.get(model, MOI.VariableIndex, "y")
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMAL
@test MOI.get(model, MOI.ResultCount()) == 3
X = [[0.0, 1.0], [0.5, 0.5], [1.0, 0.25]]
Y = [-[1.0, 2.5], -[1.5, 1.5], -[2.25, 1.25]]
X = [[1.0, 0.25], [0.5, 0.5], [0.0, 1.0]]
Y = [[-2.25, -1.25], [-1.5, -1.5], [-1.0, -2.5]]
for i in 1:3
@test MOI.get(model, MOI.PrimalStatus(i)) == MOI.FEASIBLE_POINT
@test MOI.get(model, MOI.DualStatus(i)) == MOI.NO_SOLUTION
Expand All @@ -117,13 +117,13 @@ function test_moi_bolp_1_reversed()
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y
minobjective: [x + 3 * y, 2 * x + y]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
variables: x, y
minobjective: [x + 3 * y, 2 * x + y]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
)
x = MOI.get(model, MOI.VariableIndex, "x")
y = MOI.get(model, MOI.VariableIndex, "y")
Expand Down Expand Up @@ -153,13 +153,13 @@ function test_moi_bolp_1_scalar()
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y
minobjective: [2 * x + y, x + 3 * y]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
variables: x, y
minobjective: [2 * x + y, x + 3 * y]
c1: x + y >= 1.0
c2: 0.5 * x + y >= 0.75
c3: x >= 0.0
c4: y >= 0.25
""",
)
x = MOI.get(model, MOI.VariableIndex, "x")
y = MOI.get(model, MOI.VariableIndex, "y")
Expand Down Expand Up @@ -219,17 +219,16 @@ function test_biobjective_knapsack()
MOI.LessThan(900.0),
)
MOI.optimize!(model)
results = Dict(
[955.0, 906.0] => [2, 3, 5, 6, 9, 10, 11, 14, 15, 16, 17],
[948.0, 939.0] => [1, 2, 3, 5, 6, 8, 10, 11, 15, 16, 17],
[934.0, 971.0] => [2, 3, 5, 6, 8, 10, 11, 12, 15, 16, 17],
results = [
[918.0, 983.0] => [2, 3, 4, 5, 6, 8, 10, 11, 12, 16, 17],
)
[934.0, 971.0] => [2, 3, 5, 6, 8, 10, 11, 12, 15, 16, 17],
[948.0, 939.0] => [1, 2, 3, 5, 6, 8, 10, 11, 15, 16, 17],
[955.0, 906.0] => [2, 3, 5, 6, 9, 10, 11, 14, 15, 16, 17],
]
for i in 1:MOI.get(model, MOI.ResultCount())
x_sol = MOI.get(model, MOI.VariablePrimal(i), x)
X = findall(elt -> elt > 0.9, x_sol)
Y = MOI.get(model, MOI.ObjectiveValue(i))
@test results[Y] == X
@test results[i][2] == findall(elt -> elt > 0.9, x_sol)
@test results[i][1] ≈ MOI.get(model, MOI.ObjectiveValue(i))
end
return
end
Expand Down Expand Up @@ -353,9 +352,9 @@ function test_three_objective()
MOI.Utilities.loadfromstring!(
model,
"""
variables: x
maxobjective: [1.0 * x, -1.0 * x, 2.0 * x + 2.0]
""",
variables: x
maxobjective: [1.0 * x, -1.0 * x, 2.0 * x + 2.0]
""",
)
@test_throws(
ErrorException("Only scalar or bi-objective problems supported."),
Expand Down Expand Up @@ -408,6 +407,6 @@ function test_vector_of_variables_objective()
return
end

end
end # module TestDichotomy

TestDichotomy.run_tests()
Loading
Loading