Skip to content

Commit

Permalink
Fix handling of INVALID_MODEL (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Feb 20, 2022
1 parent a0d51ab commit 45f9ca7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Create a new Ipopt optimizer.
"""
mutable struct Optimizer <: MOI.AbstractOptimizer
inner::Union{Nothing,IpoptProblem}
invalid_model::Bool
variables::MOI.Utilities.VariablesContainer{Float64}
variable_primal_start::Vector{Union{Nothing,Float64}}
variable_lower_start::Vector{Union{Nothing,Float64}}
Expand Down Expand Up @@ -67,6 +68,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
function Optimizer()
return new(
nothing,
false,
MOI.Utilities.VariablesContainer{Float64}(),
Union{Nothing,Float64}[],
Union{Nothing,Float64}[],
Expand Down Expand Up @@ -123,6 +125,7 @@ MOI.eval_hessian_lagrangian(::_EmptyNLPEvaluator, H, x, σ, μ) = nothing

function MOI.empty!(model::Optimizer)
model.inner = nothing
model.invalid_model = false
MOI.empty!(model.variables)
empty!(model.variable_primal_start)
empty!(model.variable_lower_start)
Expand Down Expand Up @@ -1119,6 +1122,11 @@ function MOI.optimize!(model::Optimizer)
push!(g_U, bound.upper)
end
start_time = time()
if length(model.variables.lower) == 0
# Don't attempt to create a problem because Ipopt will error.
model.invalid_model = true
return
end
model.inner = CreateIpoptProblem(
length(model.variables.lower),
model.variables.lower,
Expand Down Expand Up @@ -1254,7 +1262,9 @@ end
### MOI.TerminationStatus

function MOI.get(model::Optimizer, ::MOI.TerminationStatus)
if model.inner === nothing
if model.invalid_model
return MOI.INVALID_MODEL
elseif model.inner === nothing
return MOI.OPTIMIZE_NOT_CALLED
end
status = _STATUS_CODES[model.inner.status]
Expand Down Expand Up @@ -1292,10 +1302,9 @@ function MOI.get(model::Optimizer, ::MOI.TerminationStatus)
return MOI.OTHER_ERROR
elseif status == :NonIpopt_Exception_Thrown
return MOI.OTHER_ERROR
elseif status == :Insufficient_Memory
return MOI.MEMORY_LIMIT
else
error("Unrecognized Ipopt status $status")
@assert status == :Insufficient_Memory
return MOI.MEMORY_LIMIT
end
end

Expand Down
10 changes: 4 additions & 6 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,10 @@ end

function test_empty_optimize()
model = Ipopt.Optimizer()
err = ErrorException(
"IPOPT: Failed to construct problem because there are 0 variables. " *
"If you intended to construct an empty problem, one work-around is " *
"to add a variable fixed to 0.",
)
@test_throws err MOI.optimize!(model)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INVALID_MODEL
@test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION
@test MOI.get(model, MOI.PrimalStatus()) == MOI.NO_SOLUTION
return
end

Expand Down

2 comments on commit 45f9ca7

@odow
Copy link
Member Author

@odow odow commented on 45f9ca7 Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/55066

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" 45f9ca77300a3a180aa82dbcd8938e4cd4524c94
git push origin v1.0.0

Please sign in to comment.