Skip to content

Commit 41d50d7

Browse files
committed
Refactor tests to use a suite of common test problems
1 parent e20e684 commit 41d50d7

File tree

9 files changed

+566
-1693
lines changed

9 files changed

+566
-1693
lines changed

src/MultiObjectiveAlgorithms.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ function dominates(sense, a::SolutionPoint, b::SolutionPoint)
3434
end
3535
end
3636

37+
_sort!(solutions::Vector{SolutionPoint}) = sort!(solutions; by = x -> x.y)
38+
3739
function filter_nondominated(sense, solutions::Vector{SolutionPoint})
38-
solutions = sort(solutions; by = x -> x.y)
40+
_sort!(solutions)
3941
nondominated_solutions = SolutionPoint[]
4042
for candidate in solutions
4143
if any(test -> dominates(sense, test, candidate), solutions)
@@ -594,6 +596,7 @@ function MOI.optimize!(model::Optimizer)
594596
model.termination_status = status
595597
if solutions !== nothing
596598
model.solutions = solutions
599+
_sort!(model.solutions)
597600
end
598601
if MOI.get(model, ComputeIdealPoint())
599602
_compute_ideal_point(model, start_time)

test/algorithms/Chalmet.jl

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,17 @@ function test_knapsack_min()
5353
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
5454
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
5555
MOI.optimize!(model)
56-
X_E = Float64[
57-
0 0 1 1 1 0 1 1 1 1
58-
1 0 1 1 1 0 1 1 0 1
59-
0 1 1 1 1 0 1 0 1 1
56+
results = [
57+
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1] => [-3394, -3817],
58+
[0, 1, 1, 1, 1, 0, 1, 0, 1, 1] => [-3042, -4627],
59+
[0, 0, 1, 1, 1, 0, 1, 1, 1, 1] => [-2854, -4636],
6060
]
61-
Y_N = Float64[
62-
-2854 -4636
63-
-3394 -3817
64-
-3042 -4627
65-
]
66-
N = MOI.get(model, MOI.ResultCount())
67-
x_sol = hcat([MOI.get(model, MOI.VariablePrimal(i), x) for i in 1:N]...)
68-
@test isapprox(x_sol, X_E'; atol = 1e-6)
69-
y_sol = hcat([MOI.get(model, MOI.ObjectiveValue(i)) for i in 1:N]...)
70-
@test isapprox(y_sol, Y_N'; atol = 1e-6)
71-
@test MOI.get(model, MOI.ObjectiveBound()) vec(minimum(Y_N; dims = 1))
61+
@test MOI.get(model, MOI.ResultCount()) == length(results)
62+
for (i, (x_sol, y_sol)) in enumerate(results)
63+
@test (x_sol, MOI.get(model, MOI.VariablePrimal(i), x); atol = 1e-6)
64+
@test (y_sol, MOI.get(model, MOI.ObjectiveValue(i)); atol = 1e-6)
65+
end
66+
@test MOI.get(model, MOI.ObjectiveBound()) [-3394, -4636]
7267
return
7368
end
7469

@@ -103,22 +98,17 @@ function test_knapsack_max()
10398
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
10499
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
105100
MOI.optimize!(model)
106-
X_E = Float64[
107-
0 0 1 1 1 0 1 1 1 1
108-
1 0 1 1 1 0 1 1 0 1
109-
0 1 1 1 1 0 1 0 1 1
110-
]
111-
Y_N = Float64[
112-
2855 4636
113-
3395 3817
114-
3043 4627
101+
results = [
102+
[0, 0, 1, 1, 1, 0, 1, 1, 1, 1] => [2855, 4636],
103+
[0, 1, 1, 1, 1, 0, 1, 0, 1, 1] => [3043, 4627],
104+
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1] => [3395, 3817],
115105
]
116-
N = MOI.get(model, MOI.ResultCount())
117-
x_sol = hcat([MOI.get(model, MOI.VariablePrimal(i), x) for i in 1:N]...)
118-
@test isapprox(x_sol, X_E'; atol = 1e-6)
119-
y_sol = hcat([MOI.get(model, MOI.ObjectiveValue(i)) for i in 1:N]...)
120-
@test isapprox(y_sol, Y_N'; atol = 1e-6)
121-
@test MOI.get(model, MOI.ObjectiveBound()) vec(maximum(Y_N; dims = 1))
106+
@test MOI.get(model, MOI.ResultCount()) == length(results)
107+
for (i, (x_sol, y_sol)) in enumerate(results)
108+
@test (x_sol, MOI.get(model, MOI.VariablePrimal(i), x); atol = 1e-6)
109+
@test (y_sol, MOI.get(model, MOI.ObjectiveValue(i)); atol = 1e-6)
110+
end
111+
@test MOI.get(model, MOI.ObjectiveBound()) [3395, 4636]
122112
return
123113
end
124114

@@ -228,6 +218,6 @@ function test_too_many_objectives()
228218
return
229219
end
230220

231-
end
221+
end # module TestChalmet
232222

233223
TestChalmet.run_tests()

test/algorithms/Dichotomy.jl

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ function test_moi_bolp_1()
4545
MOI.Utilities.loadfromstring!(
4646
model,
4747
"""
48-
variables: x, y
49-
minobjective: [2 * x + y + 1, x + 3 * y]
50-
c1: x + y >= 1.0
51-
c2: 0.5 * x + y >= 0.75
52-
c3: x >= 0.0
53-
c4: y >= 0.25
54-
""",
48+
variables: x, y
49+
minobjective: [2 * x + y + 1, x + 3 * y]
50+
c1: x + y >= 1.0
51+
c2: 0.5 * x + y >= 0.75
52+
c3: x >= 0.0
53+
c4: y >= 0.25
54+
""",
5555
)
5656
x = MOI.get(model, MOI.VariableIndex, "x")
5757
y = MOI.get(model, MOI.VariableIndex, "y")
@@ -81,21 +81,21 @@ function test_moi_bolp_1_maximize()
8181
MOI.Utilities.loadfromstring!(
8282
model,
8383
"""
84-
variables: x, y
85-
maxobjective: [-2.0 * x + -1.0 * y, -1.0 * x + -3.0 * y + 0.5]
86-
c1: x + y >= 1.0
87-
c2: 0.5 * x + y >= 0.75
88-
c3: x >= 0.0
89-
c4: y >= 0.25
90-
""",
84+
variables: x, y
85+
maxobjective: [-2.0 * x + -1.0 * y, -1.0 * x + -3.0 * y + 0.5]
86+
c1: x + y >= 1.0
87+
c2: 0.5 * x + y >= 0.75
88+
c3: x >= 0.0
89+
c4: y >= 0.25
90+
""",
9191
)
9292
x = MOI.get(model, MOI.VariableIndex, "x")
9393
y = MOI.get(model, MOI.VariableIndex, "y")
9494
MOI.optimize!(model)
9595
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMAL
9696
@test MOI.get(model, MOI.ResultCount()) == 3
97-
X = [[0.0, 1.0], [0.5, 0.5], [1.0, 0.25]]
98-
Y = [-[1.0, 2.5], -[1.5, 1.5], -[2.25, 1.25]]
97+
X = [[1.0, 0.25], [0.5, 0.5], [0.0, 1.0]]
98+
Y = [[-2.25, -1.25], [-1.5, -1.5], [-1.0, -2.5]]
9999
for i in 1:3
100100
@test MOI.get(model, MOI.PrimalStatus(i)) == MOI.FEASIBLE_POINT
101101
@test MOI.get(model, MOI.DualStatus(i)) == MOI.NO_SOLUTION
@@ -117,13 +117,13 @@ function test_moi_bolp_1_reversed()
117117
MOI.Utilities.loadfromstring!(
118118
model,
119119
"""
120-
variables: x, y
121-
minobjective: [x + 3 * y, 2 * x + y]
122-
c1: x + y >= 1.0
123-
c2: 0.5 * x + y >= 0.75
124-
c3: x >= 0.0
125-
c4: y >= 0.25
126-
""",
120+
variables: x, y
121+
minobjective: [x + 3 * y, 2 * x + y]
122+
c1: x + y >= 1.0
123+
c2: 0.5 * x + y >= 0.75
124+
c3: x >= 0.0
125+
c4: y >= 0.25
126+
""",
127127
)
128128
x = MOI.get(model, MOI.VariableIndex, "x")
129129
y = MOI.get(model, MOI.VariableIndex, "y")
@@ -153,13 +153,13 @@ function test_moi_bolp_1_scalar()
153153
MOI.Utilities.loadfromstring!(
154154
model,
155155
"""
156-
variables: x, y
157-
minobjective: [2 * x + y, x + 3 * y]
158-
c1: x + y >= 1.0
159-
c2: 0.5 * x + y >= 0.75
160-
c3: x >= 0.0
161-
c4: y >= 0.25
162-
""",
156+
variables: x, y
157+
minobjective: [2 * x + y, x + 3 * y]
158+
c1: x + y >= 1.0
159+
c2: 0.5 * x + y >= 0.75
160+
c3: x >= 0.0
161+
c4: y >= 0.25
162+
""",
163163
)
164164
x = MOI.get(model, MOI.VariableIndex, "x")
165165
y = MOI.get(model, MOI.VariableIndex, "y")
@@ -219,17 +219,16 @@ function test_biobjective_knapsack()
219219
MOI.LessThan(900.0),
220220
)
221221
MOI.optimize!(model)
222-
results = Dict(
223-
[955.0, 906.0] => [2, 3, 5, 6, 9, 10, 11, 14, 15, 16, 17],
224-
[948.0, 939.0] => [1, 2, 3, 5, 6, 8, 10, 11, 15, 16, 17],
225-
[934.0, 971.0] => [2, 3, 5, 6, 8, 10, 11, 12, 15, 16, 17],
222+
results = [
226223
[918.0, 983.0] => [2, 3, 4, 5, 6, 8, 10, 11, 12, 16, 17],
227-
)
224+
[934.0, 971.0] => [2, 3, 5, 6, 8, 10, 11, 12, 15, 16, 17],
225+
[948.0, 939.0] => [1, 2, 3, 5, 6, 8, 10, 11, 15, 16, 17],
226+
[955.0, 906.0] => [2, 3, 5, 6, 9, 10, 11, 14, 15, 16, 17],
227+
]
228228
for i in 1:MOI.get(model, MOI.ResultCount())
229229
x_sol = MOI.get(model, MOI.VariablePrimal(i), x)
230-
X = findall(elt -> elt > 0.9, x_sol)
231-
Y = MOI.get(model, MOI.ObjectiveValue(i))
232-
@test results[Y] == X
230+
@test results[i][2] == findall(elt -> elt > 0.9, x_sol)
231+
@test results[i][1] MOI.get(model, MOI.ObjectiveValue(i))
233232
end
234233
return
235234
end
@@ -353,9 +352,9 @@ function test_three_objective()
353352
MOI.Utilities.loadfromstring!(
354353
model,
355354
"""
356-
variables: x
357-
maxobjective: [1.0 * x, -1.0 * x, 2.0 * x + 2.0]
358-
""",
355+
variables: x
356+
maxobjective: [1.0 * x, -1.0 * x, 2.0 * x + 2.0]
357+
""",
359358
)
360359
@test_throws(
361360
ErrorException("Only scalar or bi-objective problems supported."),
@@ -408,6 +407,6 @@ function test_vector_of_variables_objective()
408407
return
409408
end
410409

411-
end
410+
end # module TestDichotomy
412411

413412
TestDichotomy.run_tests()

0 commit comments

Comments
 (0)