-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLexicographic.jl
More file actions
275 lines (258 loc) · 11 KB
/
Lexicographic.jl
File metadata and controls
275 lines (258 loc) · 11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# 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 TestLexicographic
using Test
import HiGHS
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_knapsack()
P = Float64[1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
@test MOI.supports(model, MOA.LexicographicAllPermutations())
@test MOI.supports(model, MOA.ObjectiveRelativeTolerance(1))
MOI.set(model, MOA.LexicographicAllPermutations(), false)
MOI.set(model, MOA.ObjectiveRelativeTolerance(1), 0.1)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(vcat, Float64, P * x...)
f.constants[4] = 1_000.0
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
MOI.optimize!(model)
@test MOI.get(model, MOI.ResultCount()) == 1
x_sol = MOI.get(model, MOI.VariablePrimal(), x)
@test ≈(x_sol, [0.9, 1, 0, 0.1]; atol = 1e-3)
y_sol = MOI.get(model, MOI.ObjectiveValue())
@test ≈(y_sol, P * x_sol .+ [0.0, 0.0, 0.0, 1_000.0]; atol = 1e-4)
@test MOI.get(model, MOA.SubproblemCount()) == 8
return
end
function test_caching_optimizer_knapsack()
model = MOI.instantiate(
() -> MOA.Optimizer(HiGHS.Optimizer);
with_cache_type = Float64,
)
P = Float64[1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
@test MOI.supports(model, MOA.LexicographicAllPermutations())
@test MOI.supports(model, MOA.ObjectiveRelativeTolerance(1))
MOI.set(model, MOA.LexicographicAllPermutations(), false)
MOI.set(model, MOA.ObjectiveRelativeTolerance(1), 0.1)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(vcat, Float64, P * x...)
f.constants[4] = 1_000.0
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
MOI.optimize!(model)
@test MOI.get(model, MOI.ResultCount()) == 1
x_sol = MOI.get(model, MOI.VariablePrimal(), x)
@test ≈(x_sol, [0.9, 1, 0, 0.1]; atol = 1e-3)
return
end
function test_knapsack_default()
P = Float64[1 0 0 0; 0 1 0 0; 0 0 0 1]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(vcat, Float64, P * x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
MOI.optimize!(model)
results = [
[0, 1, 1] => [0, 1, 0, 1],
[1, 0, 1] => [1, 0, 0, 1],
[1, 1, 0] => [1, 1, 0, 0],
]
reverse!(results)
@test MOI.get(model, MOI.ResultCount()) == 3
for i in 1:MOI.get(model, MOI.ResultCount())
X = round.(Int, MOI.get(model, MOI.VariablePrimal(i), x))
Y = round.(Int, MOI.get(model, MOI.ObjectiveValue(i)))
@test results[i] == (Y => X)
end
return
end
function test_knapsack_min()
P = Float64[1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOA.LexicographicAllPermutations(), false)
MOI.set(model, MOA.ObjectiveRelativeTolerance(1), 0.1)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = MOI.Utilities.operate(vcat, Float64, -P * x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
MOI.optimize!(model)
x_sol = MOI.get(model, MOI.VariablePrimal(), x)
@test ≈(x_sol, [0.9, 1, 0, 0.1]; atol = 1e-3)
return
end
function test_knapsack_one_solution()
P = Float64[1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOA.LexicographicAllPermutations(), false)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(vcat, Float64, P * x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
MOI.optimize!(model)
x_sol = MOI.get(model, MOI.VariablePrimal(), x)
@test ≈(x_sol, [1, 1, 0, 0]; atol = 1e-3)
@test MOI.get(model, MOI.RawStatusString()) ==
"Solve complete. Found 1 solution(s)"
return
end
function test_infeasible()
for flag in (true, false)
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOA.LexicographicAllPermutations(), flag)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 2)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint(model, 1.0 * x[1] + 1.0 * x[2], MOI.LessThan(-1.0))
f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INFEASIBLE
@test MOI.get(model, MOI.PrimalStatus()) == MOI.NO_SOLUTION
@test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION
end
return
end
function test_unbounded()
for flag in (true, false)
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOA.LexicographicAllPermutations(), flag)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 2)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.DUAL_INFEASIBLE
end
return
end
function test_vector_of_variables_objective()
model = MOI.instantiate(; with_bridge_type = Float64) do
return MOA.Optimizer(HiGHS.Optimizer)
end
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 2)
MOI.add_constraint.(model, x, MOI.ZeroOne())
f = MOI.VectorOfVariables(x)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * xi for xi in x), MOI.GreaterThan(1.0))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMAL
return
end
function test_warn_all_permutations()
@test_logs (:warn,) MOA.Lexicographic(; all_permutations = true)
@test_logs (:warn,) MOA.Lexicographic(; all_permutations = false)
@test_logs MOA.Lexicographic()
return
end
function test_knapsack_time_limit()
P = Float64[1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOA.LexicographicAllPermutations(), false)
MOI.set(model, MOA.ObjectiveRelativeTolerance(1), 0.1)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(vcat, Float64, P * x...)
f.constants[4] = 1_000.0
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
MOI.set(model, MOI.TimeLimitSec(), 0.0)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.TIME_LIMIT
return
end
function test_knapsack_5_objectives()
P = Float64[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1; 1 1 1 1]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Lexicographic())
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 4)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(vcat, Float64, P * x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, sum(1.0 * x[i] for i in 1:4), MOI.LessThan(2.0))
@test_logs (:warn,) MOI.optimize!(model)
@test MOI.get(model, MOI.ResultCount()) == 6
results = [
[0, 0, 1, 1, 2] => [0, 0, 1, 1],
[0, 1, 0, 1, 2] => [0, 1, 0, 1],
[0, 1, 1, 0, 2] => [0, 1, 1, 0],
[1, 0, 0, 1, 2] => [1, 0, 0, 1],
[1, 0, 1, 0, 2] => [1, 0, 1, 0],
[1, 1, 0, 0, 2] => [1, 1, 0, 0],
]
reverse!(results)
for i in 1:MOI.get(model, MOI.ResultCount())
X = round.(Int, MOI.get(model, MOI.VariablePrimal(i), x))
Y = round.(Int, MOI.get(model, MOI.ObjectiveValue(i)))
@test results[i] == (Y => X)
end
MOI.set(model, MOA.LexicographicAllPermutations(), true)
@test_nowarn MOI.optimize!(model)
for i in 1:MOI.get(model, MOI.ResultCount())
X = round.(Int, MOI.get(model, MOI.VariablePrimal(i), x))
Y = round.(Int, MOI.get(model, MOI.ObjectiveValue(i)))
@test results[i] == (Y => X)
end
MOI.set(model, MOA.LexicographicAllPermutations(), false)
@test_nowarn MOI.optimize!(model)
@test MOI.get(model, MOI.ResultCount()) == 1
X = round.(Int, MOI.get(model, MOI.VariablePrimal(1), x))
Y = round.(Int, MOI.get(model, MOI.ObjectiveValue(1)))
@test ([1, 1, 0, 0, 2] => [1, 1, 0, 0]) == (Y => X)
return
end
end # module TestLexicographic
TestLexicographic.run_tests()