Skip to content

Commit 4ec76fd

Browse files
odowsandyspiers
andauthored
Add MOA.SilentInner attribute (#181)
Co-authored-by: Sandy Spiers <86579677+sandyspiers@users.noreply.github.com>
1 parent 8b446ec commit 4ec76fd

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ the solution process.
8888
* `MOA.ObjectivePriority(index::Int)`
8989
* `MOA.ObjectiveRelativeTolerance(index::Int)`
9090
* `MOA.ObjectiveWeight(index::Int)`
91+
* `MOA.SilentInner()`
9192
* `MOA.SolutionLimit()`
9293
* `MOI.TimeLimitSec()`
9394

src/MultiObjectiveAlgorithms.jl

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ MOI.instantiate(optimizer_factory; with_cache_type = Float64)
178178
## Example
179179
180180
```julia
181-
import MultiObjectiveAlgorithms as MOA
182-
import HiGHS
183-
optimizer = () -> MOA.Optimizer(HiGHS.Optimizer)
181+
julia> using JuMP
182+
183+
julia> import MultiObjectiveAlgorithms as MOA
184+
185+
julia> import HiGHS
186+
187+
julia> model = Model(() -> MOA.Optimizer(HiGHS.Optimizer))
184188
```
185189
"""
186190
mutable struct Optimizer <: MOI.AbstractOptimizer
@@ -202,6 +206,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
202206
function Optimizer(optimizer_factory)
203207
inner = MOI.instantiate(optimizer_factory; with_cache_type = Float64)
204208
if MOI.supports(inner, MOI.Silent())
209+
# Make the default for `SilentInner` true.
205210
MOI.set(inner, MOI.Silent(), true)
206211
end
207212
return new(
@@ -261,6 +266,33 @@ function MOI.set(model::Optimizer, ::MOI.Silent, value::Bool)
261266
return
262267
end
263268

269+
### SilentInner
270+
271+
"""
272+
SilentInner() <: MOI.AbstractOptimizerAttribute
273+
274+
An optimizer attribute that controls whether the inner optimizer's `MOI.Silent`
275+
attribute.
276+
277+
By default, this attribute is set to `true`. Set it to `false` to enable logging
278+
of the inner solver. In most cases, this will result in a large amount of output
279+
that is hard to interpret, but it may be helpful when debugging failed solves.
280+
"""
281+
struct SilentInner <: MOI.AbstractOptimizerAttribute end
282+
283+
function MOI.supports(model::Optimizer, ::SilentInner)
284+
return MOI.supports(model.inner, MOI.Silent())
285+
end
286+
287+
function MOI.get(model::Optimizer, ::SilentInner)
288+
return MOI.get(model.inner, MOI.Silent())
289+
end
290+
291+
function MOI.set(model::Optimizer, ::SilentInner, value::Bool)
292+
MOI.set(model.inner, MOI.Silent(), value)
293+
return
294+
end
295+
264296
### TimeLimitSec
265297

266298
function MOI.supports(model::Optimizer, attr::MOI.TimeLimitSec)

test/test_model.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,32 @@ function test_printing()
271271
return
272272
end
273273

274+
function test_printing_silent_inner()
275+
model = MOA.Optimizer(HiGHS.Optimizer)
276+
@test MOI.supports(model, MOA.SilentInner())
277+
@test MOI.get(model, MOA.SilentInner()) == true
278+
MOI.set(model, MOA.SilentInner(), false)
279+
@test MOI.get(model, MOA.SilentInner()) == false
280+
MOI.set(model, MOI.Silent(), true)
281+
MOI.set(model, MOA.Algorithm(), MOA.KirlikSayin())
282+
x = MOI.add_variables(model, 2)
283+
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
284+
MOI.add_constraint(model, x[2], MOI.LessThan(3.0))
285+
MOI.add_constraint(model, 3.0 * x[1] - 1.0 * x[2], MOI.LessThan(6.0))
286+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
287+
f = MOI.Utilities.vectorize([3.0 * x[1] + x[2], -1.0 * x[1] - 2.0 * x[2]])
288+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
289+
dir = mktempdir()
290+
open(joinpath(dir, "log.txt"), "w") do io
291+
redirect_stdout(() -> MOI.optimize!(model), io)
292+
return
293+
end
294+
contents = read(joinpath(dir, "log.txt"), String)
295+
@test occursin("HiGHS", contents)
296+
@test !occursin("MultiObjectiveAlgorithms.jl", contents)
297+
return
298+
end
299+
274300
end # module
275301

276302
TestModel.run_tests()

0 commit comments

Comments
 (0)