Skip to content

Commit

Permalink
Improve MOI interface (#233)
Browse files Browse the repository at this point in the history
* barrier iterations support

* a few more fixes

* removed comment
  • Loading branch information
sshin23 authored Oct 20, 2022
1 parent 6593459 commit be4f87e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Interfaces/MOI_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
silent::Bool
options::Dict{Symbol,Any}
solve_time::Float64
solve_iterations::Int
sense::MOI.OptimizationSense

variables::MOI.Utilities.VariablesContainer{Float64}
Expand Down Expand Up @@ -45,6 +46,7 @@ function Optimizer(; kwargs...)
false,
option_dict,
NaN,
0,
MOI.FEASIBILITY_SENSE,
MOI.Utilities.VariablesContainer{Float64}(),
Union{Nothing,Float64}[],
Expand Down Expand Up @@ -170,7 +172,7 @@ end
MOI.supports(::Optimizer, ::MOI.RawOptimizerAttribute) = true

function MOI.set(model::Optimizer, p::MOI.RawOptimizerAttribute, value)
model.options[p.name] = value
model.options[Symbol(p.name)] = value
# No need to reset model.solver because this gets handled in optimize!.
return
end
Expand Down Expand Up @@ -660,6 +662,7 @@ function MOIModel(model::Optimizer)
end
end


# TODO
model.options[:jacobian_constant], model.options[:hessian_constant] = false, false
model.options[:dual_initialized] = !iszero(y0)
Expand Down Expand Up @@ -689,6 +692,7 @@ function MOI.optimize!(model::Optimizer)
model.solver = MadNLPSolver(model.nlp; model.options...)
model.result = solve!(model.solver)
model.solve_time = model.solver.cnt.total_time
model.solve_iterations = model.solver.cnt.k
return
end

Expand Down Expand Up @@ -883,3 +887,6 @@ function MOI.get(model::Optimizer, attr::MOI.NLPBlockDual)
return s .* model.result.multipliers[(offset+1):end]
end

### MOI.BarrierIterations
MOI.get(model::Optimizer,::MOI.BarrierIterations) = model.solve_iterations

7 changes: 6 additions & 1 deletion test/MOI_interface_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ function test_MOI_Test()
return
end

function test_Name()
function test_extra()
model = MadNLP.Optimizer()
MOI.set(model, MOI.RawOptimizerAttribute("linear_solver"), UmfpackSolver)

@test MOI.supports(model, MOI.Name())
@test MOI.get(model, MOI.Name()) == ""
MOI.set(model, MOI.Name(), "Model")
@test MOI.get(model, MOI.Name()) == "Model"

@test MOI.get(model, MOI.BarrierIterations()) == 0

return
end

Expand Down

0 comments on commit be4f87e

Please sign in to comment.