Skip to content

Commit 66b32ca

Browse files
committed
Add MOA.SilentInner attribute
1 parent 2f32152 commit 66b32ca

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

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
@@ -160,9 +160,13 @@ MOI.instantiate(optimizer_factory; with_cache_type = Float64)
160160
## Example
161161
162162
```julia
163-
import MultiObjectiveAlgorithms as MOA
164-
import HiGHS
165-
optimizer = () -> MOA.Optimizer(HiGHS.Optimizer)
163+
julia> using JuMP
164+
165+
julia> import MultiObjectiveAlgorithms as MOA
166+
167+
julia> import HiGHS
168+
169+
julia> model = Model(() -> MOA.Optimizer(HiGHS.Optimizer))
166170
```
167171
"""
168172
mutable struct Optimizer <: MOI.AbstractOptimizer
@@ -184,6 +188,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
184188
function Optimizer(optimizer_factory)
185189
inner = MOI.instantiate(optimizer_factory; with_cache_type = Float64)
186190
if MOI.supports(inner, MOI.Silent())
191+
# Make the default for `SilentInner` true.
187192
MOI.set(inner, MOI.Silent(), true)
188193
end
189194
return new(
@@ -243,6 +248,33 @@ function MOI.set(model::Optimizer, ::MOI.Silent, value::Bool)
243248
return
244249
end
245250

251+
### SilentInner
252+
253+
"""
254+
SilentInner() <: MOI.AbstractOptimizerAttribute
255+
256+
An optimizer attribute that controls whether the inner optimizer's `MOI.Silent`
257+
attribute.
258+
259+
By default, this attribute is set to `false`. Set it to `true` to enable logging
260+
of the inner solver. In most cases, this will result in a large amount of output
261+
that is hard to interpret, but it may be helpful when debugging failed solves.
262+
"""
263+
struct SilentInner <: MOI.AbstractOptimizerAttribute end
264+
265+
function MOI.supports(model::Optimizer, ::SilentInner)
266+
return MOI.supports(model.inner, MOI.Silent())
267+
end
268+
269+
function MOI.get(model::Optimizer, ::SilentInner)
270+
return MOI.get(model.inner, MOI.Silent())
271+
end
272+
273+
function MOI.set(model::Optimizer, ::SilentInner, value::Bool)
274+
MOI.set(model.inner, MOI.Silent(), value)
275+
return
276+
end
277+
246278
### TimeLimitSec
247279

248280
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)