Skip to content

Commit

Permalink
Remove needs_initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jul 17, 2022
1 parent eacc8ba commit a28c7ef
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 100 deletions.
65 changes: 26 additions & 39 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
mult_x_U::Vector{Union{Nothing,Float64}}

nlp_data::MOI.NLPBlockData
nlp_data_needs_initialize::Bool
nlp_dual_start::Union{Nothing,Vector{Float64}}

linear::LinearQuadraticConstraintBlock{Float64}
qp_data::QPBlockData{Float64}

callback::Union{Nothing,Function}

Expand All @@ -48,7 +47,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
MOI.NLPBlockData([], _EmptyNLPEvaluator(), false),
true,
nothing,
LinearQuadraticConstraintBlock{Float64}(),
QPBlockData{Float64}(),
nothing,
)
end
Expand Down Expand Up @@ -85,9 +84,8 @@ function MOI.empty!(model::Optimizer)
empty!(model.mult_x_L)
empty!(model.mult_x_U)
model.nlp_data = MOI.NLPBlockData([], _EmptyNLPEvaluator(), false)
model.nlp_data_needs_initialize = true
model.nlp_dual_start = nothing
model.linear = LinearQuadraticConstraintBlock{Float64}()
model.qp_data = QPBlockData{Float64}()
model.callback = nothing
return
end
Expand Down Expand Up @@ -121,7 +119,7 @@ end

function MOI.get(model::Optimizer, attr::MOI.ListOfConstraintTypesPresent)
ret = MOI.get(model.variables, attr)
append!(MOI.get(model.linear, attr))
append!(MOI.get(model.qp_data, attr))
return ret
end

Expand Down Expand Up @@ -194,7 +192,6 @@ 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.nlp_data_needs_initialize = true
return MOI.add_variable(model.variables)
end

Expand Down Expand Up @@ -235,7 +232,6 @@ function MOI.get(
end

function MOI.add_constraint(model::Optimizer, x::MOI.VariableIndex, set::_SETS)
model.nlp_data_needs_initialize = true
return MOI.add_constraint(model.variables, x, set)
end

Expand All @@ -245,7 +241,6 @@ function MOI.set(
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
set::S,
) where {S<:_SETS}
model.nlp_data_needs_initialize = true
MOI.set(model.variables, MOI.ConstraintSet(), ci, set)
return
end
Expand All @@ -254,7 +249,6 @@ function MOI.delete(
model::Optimizer,
ci::MOI.ConstraintIndex{MOI.VariableIndex,<:_SETS},
)
model.nlp_data_needs_initialize = true
MOI.delete(model.variables, ci)
return
end
Expand All @@ -265,19 +259,18 @@ function MOI.is_valid(
model::Optimizer,
ci::MOI.ConstraintIndex{<:_FUNCTIONS,<:_SETS},
)
return MOI.is_valid(model.linear, ci)
return MOI.is_valid(model.qp_data, ci)
end

function MOI.add_constraint(model::Optimizer, func::_FUNCTIONS, set::_SETS)
model.nlp_data_needs_initialize = true
return MOI.add_constraint(model.linear, func, set)
return MOI.add_constraint(model.qp_data, func, set)
end

function MOI.get(
model::Optimizer,
attr::Union{MOI.NumberOfConstraints{F,S},MOI.ListOfConstraintIndices{F,S}},
) where {F<:_FUNCTIONS,S<:_SETS}
return MOI.get(model.linear, attr)
return MOI.get(model.qp_data, attr)
end

function MOI.get(
Expand All @@ -289,7 +282,7 @@ function MOI.get(
},
c::MOI.ConstraintIndex{F,S},
) where {F<:_FUNCTIONS,S<:_SETS}
return MOI.get(model.linear, attr, c)
return MOI.get(model.qp_data, attr, c)
end

function MOI.supports(
Expand All @@ -307,7 +300,7 @@ function MOI.set(
value::Union{Real,Nothing},
) where {F<:_FUNCTIONS,S<:_SETS}
MOI.throw_if_not_valid(model, ci)
MOI.set(model.linear, attr, ci, value)
MOI.set(model.qp_data, attr, ci, value)
return
end

Expand Down Expand Up @@ -440,7 +433,6 @@ MOI.supports(::Optimizer, ::MOI.NLPBlock) = true

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

Expand All @@ -454,7 +446,6 @@ function MOI.set(
sense::MOI.OptimizationSense,
)
model.sense = sense
model.nlp_data_needs_initialize = true
return
end

Expand All @@ -466,7 +457,7 @@ function MOI.get(
model::Optimizer,
attr::Union{MOI.ObjectiveFunctionType,MOI.ObjectiveFunction},
)
return MOI.get(model.linear, attr)
return MOI.get(model.qp_data, attr)
end

function MOI.supports(
Expand All @@ -481,8 +472,7 @@ function MOI.set(
attr::MOI.ObjectiveFunction{F},
func::F,
) where {F<:Union{MOI.VariableIndex,<:_FUNCTIONS}}
MOI.set(model.linear, attr, func)
model.nlp_data_needs_initialize = true
MOI.set(model.qp_data, attr, func)
return
end

Expand All @@ -496,7 +486,7 @@ function MOI.eval_objective(model::Optimizer, x)
elseif model.nlp_data.has_objective
return MOI.eval_objective(model.nlp_data.evaluator, x)
end
return MOI.eval_objective(model.linear, x)
return MOI.eval_objective(model.qp_data, x)
end

### Eval_Grad_F_CB
Expand All @@ -507,25 +497,25 @@ function MOI.eval_objective_gradient(model::Optimizer, grad, x)
elseif model.nlp_data.has_objective
MOI.eval_objective_gradient(model.nlp_data.evaluator, grad, x)
else
MOI.eval_objective_gradient(model.linear, grad, x)
MOI.eval_objective_gradient(model.qp_data, grad, x)
end
return
end

### Eval_G_CB

function MOI.eval_constraint(model::Optimizer, g, x)
MOI.eval_constraint(model.linear, g, x)
g_nlp = view(g, (length(model.linear)+1):length(g))
MOI.eval_constraint(model.qp_data, g, x)
g_nlp = view(g, (length(model.qp_data)+1):length(g))
MOI.eval_constraint(model.nlp_data.evaluator, g_nlp, x)
return
end

### Eval_Jac_G_CB

function MOI.jacobian_structure(model::Optimizer)
J = MOI.jacobian_structure(model.linear)
offset = length(model.linear)
J = MOI.jacobian_structure(model.qp_data)
offset = length(model.qp_data)
if length(model.nlp_data.constraint_bounds) > 0
for (row, col) in MOI.jacobian_structure(model.nlp_data.evaluator)
push!(J, (row + offset, col))
Expand All @@ -535,7 +525,7 @@ function MOI.jacobian_structure(model::Optimizer)
end

function MOI.eval_constraint_jacobian(model::Optimizer, values, x)
offset = MOI.eval_constraint_jacobian(model.linear, values, x)
offset = MOI.eval_constraint_jacobian(model.qp_data, values, x)
nlp_values = view(values, (offset+1):length(values))
MOI.eval_constraint_jacobian(model.nlp_data.evaluator, nlp_values, x)
return
Expand All @@ -544,23 +534,23 @@ end
### Eval_H_CB

function MOI.hessian_lagrangian_structure(model::Optimizer)
H = MOI.hessian_lagrangian_structure(model.linear)
H = MOI.hessian_lagrangian_structure(model.qp_data)
append!(H, MOI.hessian_lagrangian_structure(model.nlp_data.evaluator))
return H
end

function MOI.eval_hessian_lagrangian(model::Optimizer, H, x, σ, μ)
offset = MOI.eval_hessian_lagrangian(model.linear, H, x, σ, μ)
offset = MOI.eval_hessian_lagrangian(model.qp_data, H, x, σ, μ)
H_nlp = view(H, (offset+1):length(H))
μ_nlp = view(μ, (length(model.linear)+1):length(μ))
μ_nlp = view(μ, (length(model.qp_data)+1):length(μ))
MOI.eval_hessian_lagrangian(model.nlp_data.evaluator, H_nlp, x, σ, μ_nlp)
return
end

### MOI.optimize!

function _setup_model(model::Optimizer)
num_quadratic_constraints = length(model.linear.hessian_structure) > 0
num_quadratic_constraints = length(model.qp_data.hessian_structure) > 0
num_nlp_constraints = length(model.nlp_data.constraint_bounds)
has_hessian = :Hess in MOI.features_available(model.nlp_data.evaluator)
init_feat = [:Grad]
Expand Down Expand Up @@ -600,7 +590,7 @@ function _setup_model(model::Optimizer)
end
return
end
g_L, g_U = copy(model.linear.g_L), copy(model.linear.g_U)
g_L, g_U = copy(model.qp_data.g_L), copy(model.qp_data.g_U)
for bound in model.nlp_data.constraint_bounds
push!(g_L, bound.lower)
push!(g_U, bound.upper)
Expand Down Expand Up @@ -654,15 +644,12 @@ function _setup_model(model::Optimizer)
AddIpoptStrOption(model.inner, "hessian_constant", "yes")
end
end
model.nlp_data_needs_initialize = false
return
end

function MOI.optimize!(model::Optimizer)
start_time = time()
if model.nlp_data_needs_initialize
_setup_model(model)
end
_setup_model(model)
if model.invalid_model
return
end
Expand Down Expand Up @@ -695,7 +682,7 @@ function MOI.optimize!(model::Optimizer)
zeros(Float64, length(model.nlp_data.constraint_bounds))
end
row = 0
for start in model.linear.mult_g
for start in model.qp_data.mult_g
row += 1
model.inner.mult_g[row] = _dual_start(model, start, -1)
end
Expand Down Expand Up @@ -947,7 +934,7 @@ end
function MOI.get(model::Optimizer, attr::MOI.NLPBlockDual)
MOI.check_result_index_bounds(model, attr)
s = -_dual_multiplier(model)
return s .* model.inner.mult_g[(length(model.linear)+1):end]
return s .* model.inner.mult_g[(length(model.qp_data)+1):end]
end

### Ipopt.CallbackFunction
Expand Down
Loading

0 comments on commit a28c7ef

Please sign in to comment.