Skip to content

Commit

Permalink
Rebuild IpoptProblem only if modified
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jul 18, 2022
1 parent fdb557e commit d6055e2
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ MOI.supports(::Optimizer, ::MOI.RawOptimizerAttribute) = true

function MOI.set(model::Optimizer, p::MOI.RawOptimizerAttribute, value)
model.options[p.name] = value
# No need to reset model.inner because this gets handled in optimize!.
return
end

Expand All @@ -191,6 +192,7 @@ function MOI.add_variable(model::Optimizer)
push!(model.variable_primal_start, nothing)
push!(model.mult_x_L, nothing)
push!(model.mult_x_U, nothing)
model.inner = nothing
return MOI.add_variable(model.variables)
end

Expand Down Expand Up @@ -231,7 +233,10 @@ function MOI.get(
end

function MOI.add_constraint(model::Optimizer, x::MOI.VariableIndex, set::_SETS)
return MOI.add_constraint(model.variables, x, set)
index = MOI.add_constraint(model.variables, x, set)
# TODO(odow): we could update model.inner in-place here.
model.inner = nothing
return index
end

function MOI.set(
Expand All @@ -241,6 +246,8 @@ function MOI.set(
set::S,
) where {S<:_SETS}
MOI.set(model.variables, MOI.ConstraintSet(), ci, set)
# TODO(odow): we could update model.inner in-place here.
model.inner = nothing
return
end

Expand All @@ -249,6 +256,7 @@ function MOI.delete(
ci::MOI.ConstraintIndex{MOI.VariableIndex,<:_SETS},
)
MOI.delete(model.variables, ci)
model.inner = nothing
return
end

Expand All @@ -262,7 +270,9 @@ function MOI.is_valid(
end

function MOI.add_constraint(model::Optimizer, func::_FUNCTIONS, set::_SETS)
return MOI.add_constraint(model.qp_data, func, set)
index = MOI.add_constraint(model.qp_data, func, set)
model.inner = nothing
return index
end

function MOI.get(
Expand Down Expand Up @@ -300,6 +310,7 @@ function MOI.set(
) where {F<:_FUNCTIONS,S<:_SETS}
MOI.throw_if_not_valid(model, ci)
MOI.set(model.qp_data, attr, ci, value)
# No need to reset model.inner, because this gets handled in optimize!.
return
end

Expand All @@ -321,6 +332,7 @@ function MOI.set(
)
MOI.throw_if_not_valid(model, vi)
model.variable_primal_start[column(vi)] = value
# No need to reset model.inner, because this gets handled in optimize!.
return
end

Expand Down Expand Up @@ -348,6 +360,7 @@ function MOI.set(
)
MOI.throw_if_not_valid(model, ci)
model.mult_x_L[ci.value] = value
# No need to reset model.inner, because this gets handled in optimize!.
return
end

Expand All @@ -368,6 +381,7 @@ function MOI.set(
)
MOI.throw_if_not_valid(model, ci)
model.mult_x_U[ci.value] = value
# No need to reset model.inner, because this gets handled in optimize!.
return
end

Expand Down Expand Up @@ -397,6 +411,7 @@ function MOI.set(
model.mult_x_L[ci.value] = 0.0
model.mult_x_U[ci.value] = value
end
# No need to reset model.inner, because this gets handled in optimize!.
return
end

Expand All @@ -421,6 +436,7 @@ function MOI.set(
values::Union{Nothing,Vector},
)
model.nlp_dual_start = values
# No need to reset model.inner, because this gets handled in optimize!.
return
end

Expand All @@ -432,6 +448,7 @@ MOI.supports(::Optimizer, ::MOI.NLPBlock) = true

function MOI.set(model::Optimizer, ::MOI.NLPBlock, nlp_data::MOI.NLPBlockData)
model.nlp_data = nlp_data
model.inner = nothing
return
end

Expand All @@ -445,6 +462,8 @@ function MOI.set(
sense::MOI.OptimizationSense,
)
model.sense = sense
# TODO(odow): we could update model.inner in-place here.
model.inner = nothing
return
end

Expand Down Expand Up @@ -472,6 +491,7 @@ function MOI.set(
func::F,
) where {F<:Union{MOI.VariableIndex,<:_FUNCTIONS}}
MOI.set(model.qp_data, attr, func)
model.inner = nothing
return
end

Expand Down Expand Up @@ -648,7 +668,9 @@ end

function MOI.optimize!(model::Optimizer)
start_time = time()
_setup_model(model)
if model.inner === nothing
_setup_model(model)
end
if model.invalid_model
return
end
Expand Down

0 comments on commit d6055e2

Please sign in to comment.