Skip to content

Commit 9fe66f9

Browse files
committed
Update
1 parent 5d1afa5 commit 9fe66f9

File tree

4 files changed

+93
-29
lines changed

4 files changed

+93
-29
lines changed

test/algorithms/Chalmet.jl

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import HiGHS
1111
import MultiObjectiveAlgorithms as MOA
1212
import MultiObjectiveAlgorithms: MOI
1313

14+
include(joinpath(dirname(@__DIR__), "mock_optimizer.jl"))
15+
1416
function run_tests()
1517
for name in names(@__MODULE__; all = true)
1618
if startswith("$name", "test_")
@@ -253,34 +255,6 @@ function test_single_point()
253255
return
254256
end
255257

256-
function _solve_mock(mock)
257-
highs = HiGHS.Optimizer()
258-
MOI.set(highs, MOI.Silent(), true)
259-
index_map = MOI.copy_to(highs, mock)
260-
MOI.optimize!(highs)
261-
x = [index_map[xi] for xi in MOI.get(mock, MOI.ListOfVariableIndices())]
262-
MOI.Utilities.mock_optimize!(
263-
mock,
264-
MOI.get(highs, MOI.TerminationStatus()),
265-
MOI.get(highs, MOI.VariablePrimal(), x),
266-
)
267-
obj = MOI.get(highs, MOI.ObjectiveValue())
268-
MOI.set(mock, MOI.ObjectiveValue(), obj)
269-
return
270-
end
271-
272-
function mock_optimizer(fail_after::Int)
273-
return () -> begin
274-
model = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
275-
MOI.Utilities.set_mock_optimize!(
276-
model,
277-
ntuple(i -> _solve_mock, fail_after)...,
278-
mock -> MOI.Utilities.mock_optimize!(mock, MOI.NUMERICAL_ERROR),
279-
)
280-
return model
281-
end
282-
end
283-
284258
function test_solve_failures()
285259
m, n = 2, 10
286260
p1 = [5.0 1 10 8 3 5 3 3 7 2; 10 6 1 6 8 3 2 10 6 1]
@@ -292,7 +266,6 @@ function test_solve_failures()
292266
MOI.set(model, MOA.Algorithm(), MOA.Chalmet())
293267
x_ = MOI.add_variables(model, m * n)
294268
x = reshape(x_, m, n)
295-
# MOI.add_constraint.(model, x, MOI.ZeroOne())
296269
MOI.add_constraint.(model, x, MOI.Interval(0.0, 1.0))
297270
f = MOI.Utilities.operate(vcat, Float64, sum(p1 .* x), sum(p2 .* x))
298271
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

test/algorithms/Dichotomy.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Ipopt
1212
import MultiObjectiveAlgorithms as MOA
1313
import MultiObjectiveAlgorithms: MOI
1414

15+
include(joinpath(dirname(@__DIR__), "mock_optimizer.jl"))
16+
1517
function run_tests()
1618
for name in names(@__MODULE__; all = true)
1719
if startswith("$name", "test_")
@@ -407,6 +409,34 @@ function test_vector_of_variables_objective()
407409
return
408410
end
409411

412+
function test_solve_failures()
413+
m, n = 2, 10
414+
p1 = [5.0 1 10 8 3 5 3 3 7 2; 10 6 1 6 8 3 2 10 6 1]
415+
p2 = [4.0 6 4 3 1 6 8 2 9 7; 8 8 8 2 4 8 8 1 10 1]
416+
w = [5.0 9 3 5 10 5 7 10 7 8; 4 8 8 6 10 8 10 7 5 1]
417+
b = [34.0, 33.0]
418+
for fail_after in 0:3
419+
model = MOA.Optimizer(mock_optimizer(fail_after))
420+
MOI.set(model, MOA.Algorithm(), MOA.Dichotomy())
421+
x_ = MOI.add_variables(model, m * n)
422+
x = reshape(x_, m, n)
423+
MOI.add_constraint.(model, x, MOI.Interval(0.0, 1.0))
424+
f = MOI.Utilities.operate(vcat, Float64, sum(p1 .* x), sum(p2 .* x))
425+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
426+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
427+
for i in 1:m
428+
f_i = sum(w[i, j] * x[i, j] for j in 1:n)
429+
MOI.add_constraint(model, f_i, MOI.LessThan(b[i]))
430+
end
431+
for j in 1:n
432+
MOI.add_constraint(model, sum(1.0 .* x[:, j]), MOI.EqualTo(1.0))
433+
end
434+
MOI.optimize!(model)
435+
@test MOI.get(model, MOI.TerminationStatus()) == MOI.NUMERICAL_ERROR
436+
end
437+
return
438+
end
439+
410440
end # module TestDichotomy
411441

412442
TestDichotomy.run_tests()

test/algorithms/EpsilonConstraint.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Ipopt
1212
import MultiObjectiveAlgorithms as MOA
1313
import MultiObjectiveAlgorithms: MOI
1414

15+
include(joinpath(dirname(@__DIR__), "mock_optimizer.jl"))
1516
include(joinpath(dirname(@__DIR__), "vOptLib.jl"))
1617

1718
function run_tests()
@@ -502,6 +503,34 @@ function test_too_many_objectives()
502503
return
503504
end
504505

506+
function test_solve_failures()
507+
m, n = 2, 10
508+
p1 = [5.0 1 10 8 3 5 3 3 7 2; 10 6 1 6 8 3 2 10 6 1]
509+
p2 = [4.0 6 4 3 1 6 8 2 9 7; 8 8 8 2 4 8 8 1 10 1]
510+
w = [5.0 9 3 5 10 5 7 10 7 8; 4 8 8 6 10 8 10 7 5 1]
511+
b = [34.0, 33.0]
512+
for fail_after in 0:3
513+
model = MOA.Optimizer(mock_optimizer(fail_after))
514+
MOI.set(model, MOA.Algorithm(), MOA.EpsilonConstraint())
515+
x_ = MOI.add_variables(model, m * n)
516+
x = reshape(x_, m, n)
517+
MOI.add_constraint.(model, x, MOI.Interval(0.0, 1.0))
518+
f = MOI.Utilities.operate(vcat, Float64, sum(p1 .* x), sum(p2 .* x))
519+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
520+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
521+
for i in 1:m
522+
f_i = sum(w[i, j] * x[i, j] for j in 1:n)
523+
MOI.add_constraint(model, f_i, MOI.LessThan(b[i]))
524+
end
525+
for j in 1:n
526+
MOI.add_constraint(model, sum(1.0 .* x[:, j]), MOI.EqualTo(1.0))
527+
end
528+
MOI.optimize!(model)
529+
@test MOI.get(model, MOI.TerminationStatus()) == MOI.NUMERICAL_ERROR
530+
end
531+
return
532+
end
533+
505534
end # module TestEpsilonConstraint
506535

507536
TestEpsilonConstraint.run_tests()

test/mock_optimizer.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2019, Oscar Dowson and contributors
2+
# This Source Code Form is subject to the terms of the Mozilla Public License,
3+
# v.2.0. If a copy of the MPL was not distributed with this file, You can
4+
# obtain one at http://mozilla.org/MPL/2.0/.
5+
6+
function _solve_mock(mock)
7+
highs = HiGHS.Optimizer()
8+
MOI.set(highs, MOI.Silent(), true)
9+
index_map = MOI.copy_to(highs, mock)
10+
MOI.optimize!(highs)
11+
x = [index_map[xi] for xi in MOI.get(mock, MOI.ListOfVariableIndices())]
12+
MOI.Utilities.mock_optimize!(
13+
mock,
14+
MOI.get(highs, MOI.TerminationStatus()),
15+
MOI.get(highs, MOI.VariablePrimal(), x),
16+
)
17+
obj = MOI.get(highs, MOI.ObjectiveValue())
18+
MOI.set(mock, MOI.ObjectiveValue(), obj)
19+
return
20+
end
21+
22+
function mock_optimizer(fail_after::Int)
23+
return () -> begin
24+
model = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
25+
MOI.Utilities.set_mock_optimize!(
26+
model,
27+
ntuple(i -> _solve_mock, fail_after)...,
28+
mock -> MOI.Utilities.mock_optimize!(mock, MOI.NUMERICAL_ERROR),
29+
)
30+
return model
31+
end
32+
end

0 commit comments

Comments
 (0)