-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathChalmet.jl
228 lines (215 loc) · 7.19 KB
/
Chalmet.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
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
# 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 TestChalmet
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_min()
n = 10
W = 2137.0
C = Float64[
566 611 506 180 817 184 585 423 26 317
62 84 977 979 874 54 269 93 881 563
]
w = Float64[557, 898, 148, 63, 78, 964, 246, 662, 386, 272]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Chalmet())
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, n)
MOI.add_constraint.(model, x, MOI.ZeroOne())
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(
[MOI.ScalarAffineTerm(w[j], x[j]) for j in 1:n],
0.0,
),
MOI.LessThan(W),
)
f = MOI.VectorAffineFunction(
[
MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(-C[i, j], x[j]))
for i in 1:2 for j in 1:n
],
[0.0, 0.0],
)
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
]
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)
return
end
function test_knapsack_max()
n = 10
W = 2137.0
C = Float64[
566 611 506 180 817 184 585 423 26 317
62 84 977 979 874 54 269 93 881 563
]
w = Float64[557, 898, 148, 63, 78, 964, 246, 662, 386, 272]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Chalmet())
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, n)
MOI.add_constraint.(model, x, MOI.ZeroOne())
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(
[MOI.ScalarAffineTerm(w[j], x[j]) for j in 1:n],
0.0,
),
MOI.LessThan(W),
)
f = MOI.VectorAffineFunction(
[
MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(C[i, j], x[j])) for
i in 1:2 for j in 1:n
],
[1.0, 0.0],
)
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
]
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)
return
end
function test_time_limit()
n = 10
W = 2137.0
C = Float64[
566 611 506 180 817 184 585 423 26 317
62 84 977 979 874 54 269 93 881 563
]
w = Float64[557, 898, 148, 63, 78, 964, 246, 662, 386, 272]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Chalmet())
MOI.set(model, MOI.Silent(), true)
MOI.set(model, MOI.TimeLimitSec(), 0.0)
x = MOI.add_variables(model, n)
MOI.add_constraint.(model, x, MOI.ZeroOne())
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(
[MOI.ScalarAffineTerm(w[j], x[j]) for j in 1:n],
0.0,
),
MOI.LessThan(W),
)
f = MOI.VectorAffineFunction(
[
MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(C[i, j], x[j])) for
i in 1:2 for j in 1:n
],
[0.0, 0.0],
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.TIME_LIMIT
@test MOI.get(model, MOI.ResultCount()) > 0
return
end
function test_unbounded()
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Chalmet())
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
@test MOI.get(model, MOI.PrimalStatus()) == MOI.NO_SOLUTION
@test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION
return
end
function test_infeasible()
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.Chalmet())
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
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.Chalmet())
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_too_many_objectives()
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.Chalmet())
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)
@test_throws(
ErrorException("Chalmet requires exactly two objectives"),
MOI.optimize!(model),
)
return
end
end
TestChalmet.run_tests()