-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_utilities.jl
More file actions
133 lines (120 loc) · 4.75 KB
/
test_utilities.jl
File metadata and controls
133 lines (120 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2019, Oscar Dowson and contributors
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v.2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
module TestUtilities
using Test
import MultiObjectiveAlgorithms as MOA
import MultiObjectiveAlgorithms: MOI
function run_tests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$name" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
function test_filter_nondominated()
x = Dict{MOI.VariableIndex,Float64}()
solutions = [MOA.SolutionPoint(x, [0, 1]), MOA.SolutionPoint(x, [1, 0])]
@test MOA.filter_nondominated(MOI.MIN_SENSE, solutions) == solutions
@test MOA.filter_nondominated(MOI.MAX_SENSE, solutions) ==
reverse(solutions)
return
end
function test_filter_nondominated_sort_in_order()
x = Dict{MOI.VariableIndex,Float64}()
solutions = [MOA.SolutionPoint(x, [0, 1]), MOA.SolutionPoint(x, [1, 0])]
r_solutions = reverse(solutions)
@test MOA.filter_nondominated(MOI.MIN_SENSE, r_solutions) == solutions
@test MOA.filter_nondominated(MOI.MAX_SENSE, r_solutions) == r_solutions
return
end
function test_filter_nondominated_remove_duplicates()
x = Dict{MOI.VariableIndex,Float64}()
solutions = [MOA.SolutionPoint(x, [0, 1]), MOA.SolutionPoint(x, [1, 0])]
trial = solutions[[1, 1]]
@test MOA.filter_nondominated(MOI.MIN_SENSE, trial) == [solutions[1]]
@test MOA.filter_nondominated(MOI.MAX_SENSE, trial) == [solutions[1]]
return
end
function test_filter_nondominated_weakly_dominated()
x = Dict{MOI.VariableIndex,Float64}()
solutions = [
MOA.SolutionPoint(x, [0, 1]),
MOA.SolutionPoint(x, [0.5, 1]),
MOA.SolutionPoint(x, [1, 0]),
]
@test MOA.filter_nondominated(MOI.MIN_SENSE, solutions) == solutions[[1, 3]]
@test MOA.filter_nondominated(MOI.MAX_SENSE, solutions) == solutions[[3, 2]]
solutions = [
MOA.SolutionPoint(x, [0, 1]),
MOA.SolutionPoint(x, [0.5, 1]),
MOA.SolutionPoint(x, [0.75, 1]),
MOA.SolutionPoint(x, [0.8, 0.5]),
MOA.SolutionPoint(x, [0.9, 0.5]),
MOA.SolutionPoint(x, [1, 0]),
]
@test MOA.filter_nondominated(MOI.MIN_SENSE, solutions) ==
solutions[[1, 4, 6]]
@test MOA.filter_nondominated(MOI.MAX_SENSE, solutions) ==
solutions[[6, 5, 3]]
return
end
function test_filter_nondominated_knapsack()
x = Dict{MOI.VariableIndex,Float64}()
solutions = [
MOA.SolutionPoint(x, [0, 1, 1]),
MOA.SolutionPoint(x, [0, 1, 1]),
MOA.SolutionPoint(x, [1, 0, 1]),
MOA.SolutionPoint(x, [1, 1, 0]),
MOA.SolutionPoint(x, [1, 1, 0]),
]
result = solutions[[1, 3, 4]]
@test MOA.filter_nondominated(MOI.MIN_SENSE, solutions) == result
@test MOA.filter_nondominated(MOI.MAX_SENSE, solutions) == reverse(result)
return
end
function test_filter_nondominated_triple()
x = Dict{MOI.VariableIndex,Float64}()
for p in MOA.Combinatorics.permutations(1:3)
solutions = [
MOA.SolutionPoint(x, [0, 1, 1][p]),
MOA.SolutionPoint(x, [0, 2, 0][p]),
MOA.SolutionPoint(x, [1, 1, 1][p]),
]
# The permutation can change the ordering of the solutions that are
# returned, so we can't use `@test min_sol == solutions[1:2]`
min_sol = MOA.filter_nondominated(MOI.MIN_SENSE, copy(solutions))
@test solutions[1] in min_sol && solutions[2] in min_sol
@test length(min_sol) == 2
max_sol = MOA.filter_nondominated(MOI.MAX_SENSE, copy(solutions))
@test solutions[2] in max_sol && solutions[3] in max_sol
@test length(max_sol) == 2
end
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 == reverse(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()