-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLexicographic.jl
202 lines (187 loc) · 7.86 KB
/
Lexicographic.jl
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
# 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)
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 = Dict(
[0, 1, 1] => [0, 1, 0, 1],
[1, 0, 1] => [1, 0, 0, 1],
[1, 1, 0] => [1, 1, 0, 0],
)
@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[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)
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
end
TestLexicographic.run_tests()