Skip to content

Commit

Permalink
Format .jl files
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 21, 2024
1 parent dc1f118 commit 3dd5054
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
43 changes: 30 additions & 13 deletions src/moi_nlp_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ end

function nlp_model(moimodel::MOI.ModelLike; hessian::Bool = true, name::String = "Generic")
index_map, nvar, lvar, uvar, x0 = parser_variables(moimodel)
nlin, lincon, lin_lcon, lin_ucon, quadcon, quad_lcon, quad_ucon = parser_MOI(moimodel, index_map, nvar)
nlin, lincon, lin_lcon, lin_ucon, quadcon, quad_lcon, quad_ucon =
parser_MOI(moimodel, index_map, nvar)

nlp_data = _nlp_block(moimodel)
nnln, nlcon, nl_lcon, nl_ucon = parser_NL(nlp_data, hessian = hessian)
Expand Down Expand Up @@ -66,7 +67,8 @@ function nlp_model(moimodel::MOI.ModelLike; hessian::Bool = true, name::String =
name = name,
)

return MathOptNLPModel(meta, nlp_data.evaluator, lincon, quadcon, nlcon, λ, obj, Counters()), index_map
return MathOptNLPModel(meta, nlp_data.evaluator, lincon, quadcon, nlcon, λ, obj, Counters()),
index_map
end

function NLPModels.obj(nlp::MathOptNLPModel, x::AbstractVector)
Expand Down Expand Up @@ -138,8 +140,8 @@ function NLPModels.jac_nln_structure!(
index = 0
for i = 1:(nlp.quadcon.nquad)
qcon = nlp.quadcon.constraints[i]
view(rows, index+1:index+qcon.nnzg) .= i
view(cols, index+1:index+qcon.nnzg) .= qcon.g
view(rows, (index + 1):(index + qcon.nnzg)) .= i
view(cols, (index + 1):(index + qcon.nnzg)) .= qcon.g
index += qcon.nnzg
end
end
Expand All @@ -161,22 +163,22 @@ function NLPModels.jac_nln_coord!(nlp::MathOptNLPModel, x::AbstractVector, vals:
increment!(nlp, :neval_jac_nln)
if nlp.quadcon.nquad > 0
index = 0
view(vals, 1:nlp.quadcon.nnzj) .= 0.0
view(vals, 1:(nlp.quadcon.nnzj)) .= 0.0
for i = 1:(nlp.quadcon.nquad)
qcon = nlp.quadcon.constraints[i]
for (j, ind) in enumerate(qcon.b.nzind)
k = qcon.dg[ind]
vals[index+k] += qcon.b.nzval[j]
vals[index + k] += qcon.b.nzval[j]
end
for j = 1:qcon.nnzh
for j = 1:(qcon.nnzh)
row = qcon.A.rows[j]
col = qcon.A.cols[j]
val = qcon.A.vals[j]
k1 = qcon.dg[row]
vals[index+k1] += val * x[col]
vals[index + k1] += val * x[col]
if row != col
k2 = qcon.dg[col]
vals[index+k2] += val * x[row]
vals[index + k2] += val * x[row]
end
end
index += qcon.nnzg
Expand Down Expand Up @@ -306,11 +308,12 @@ function NLPModels.hess_coord!(
end
if (nlp.obj.type == "NONLINEAR") || (nlp.meta.nnln > nlp.quadcon.nquad)
λ = view(y, (nlp.meta.nlin + nlp.quadcon.nquad + 1):(nlp.meta.ncon))
MOI.eval_hessian_lagrangian(nlp.eval,
MOI.eval_hessian_lagrangian(
nlp.eval,
view(vals, (nlp.obj.nnzh + nlp.quadcon.nnzh + 1):(nlp.meta.nnzh)),
x,
obj_weight,
λ
λ,
)
end
if nlp.quadcon.nquad > 0
Expand Down Expand Up @@ -364,7 +367,14 @@ function NLPModels.hprod!(
end
if nlp.obj.type == "QUADRATIC"
(nlp.meta.nnln == nlp.quadcon.nquad) && (hv .= 0.0)
coo_sym_add_mul!(nlp.obj.hessian.rows, nlp.obj.hessian.cols, nlp.obj.hessian.vals, v, hv, obj_weight)
coo_sym_add_mul!(
nlp.obj.hessian.rows,
nlp.obj.hessian.cols,
nlp.obj.hessian.vals,
v,
hv,
obj_weight,
)
end
if nlp.quadcon.nquad > 0
(nlp.obj.type == "LINEAR") && (hv .= 0.0)
Expand All @@ -389,7 +399,14 @@ function NLPModels.hprod!(
end
if nlp.obj.type == "QUADRATIC"
hv .= 0.0
coo_sym_add_mul!(nlp.obj.hessian.rows, nlp.obj.hessian.cols, nlp.obj.hessian.vals, v, hv, obj_weight)
coo_sym_add_mul!(
nlp.obj.hessian.rows,
nlp.obj.hessian.cols,
nlp.obj.hessian.vals,
v,
hv,
obj_weight,
)
end
if nlp.obj.type == "NONLINEAR"
MOI.eval_hessian_lagrangian_product(nlp.eval, hv, x, v, obj_weight, nlp.λ)
Expand Down
19 changes: 10 additions & 9 deletions src/moi_nls_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function MathOptNLSModel(cmodel::JuMP.Model, F; hessian::Bool = true, name::Stri

_nlp_sync!(cmodel)
moimodel = backend(cmodel)
nlin, lincon, lin_lcon, lin_ucon, quadcon, quad_lcon, quad_ucon = parser_MOI(moimodel, index_map, nvar)
nlin, lincon, lin_lcon, lin_ucon, quadcon, quad_lcon, quad_ucon =
parser_MOI(moimodel, index_map, nvar)

nlp_data = _nlp_block(moimodel)
nnln, nlcon, nl_lcon, nl_ucon = parser_NL(nlp_data, hessian = hessian)
Expand Down Expand Up @@ -287,8 +288,8 @@ function NLPModels.jac_nln_structure!(
index = 0
for i = 1:(nls.quadcon.nquad)
qcon = nls.quadcon.constraints[i]
view(rows, index+1:index+qcon.nnzg) .= i
view(cols, index+1:index+qcon.nnzg) .= qcon.g
view(rows, (index + 1):(index + qcon.nnzg)) .= i
view(cols, (index + 1):(index + qcon.nnzg)) .= qcon.g
index += qcon.nnzg
end
end
Expand All @@ -310,22 +311,22 @@ function NLPModels.jac_nln_coord!(nls::MathOptNLSModel, x::AbstractVector, vals:
increment!(nls, :neval_jac_nln)
if nls.quadcon.nquad > 0
index = 0
view(vals, 1:nls.quadcon.nnzj) .= 0.0
view(vals, 1:(nls.quadcon.nnzj)) .= 0.0
for i = 1:(nls.quadcon.nquad)
qcon = nls.quadcon.constraints[i]
for (j, ind) in enumerate(qcon.b.nzind)
k = qcon.dg[ind]
vals[index+k] += qcon.b.nzval[j]
vals[index + k] += qcon.b.nzval[j]
end
for j = 1:qcon.nnzh
for j = 1:(qcon.nnzh)
row = qcon.A.rows[j]
col = qcon.A.cols[j]
val = qcon.A.vals[j]
k1 = qcon.dg[row]
vals[index+k1] += val * x[col]
vals[index + k1] += val * x[col]
if row != col
k2 = qcon.dg[col]
vals[index+k2] += val * x[row]
vals[index + k2] += val * x[row]
end
end
index += qcon.nnzg
Expand Down Expand Up @@ -460,7 +461,7 @@ function NLPModels.hess_coord!(
view(vals, (nls.lls.nnzh + nls.quadcon.nnzh + 1):(nls.meta.nnzh)),
x,
obj_weight,
λ
λ,
)
end
if nls.quadcon.nquad > 0
Expand Down
16 changes: 10 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const VI = MOI.VariableIndex # VariableIndex(value)
# ScalarAffineFunctions and VectorAffineFunctions
const SAF = MOI.ScalarAffineFunction{Float64} # ScalarAffineFunction{T}(terms, constant)
const VAF = MOI.VectorAffineFunction{Float64} # VectorAffineFunction{T}(terms, constants)
const AF = Union{SAF, VAF}
const AF = Union{SAF, VAF}

# ScalarQuadraticFunctions and VectorQuadraticFunctions
const SQF = MOI.ScalarQuadraticFunction{Float64} # ScalarQuadraticFunction{T}(affine_terms, quadratic_terms, constant)
const VQF = MOI.VectorQuadraticFunction{Float64} # VectorQuadraticFunction{T}(affine_terms, quadratic_terms, constants)
const QF = Union{SQF, VQF}
const QF = Union{SQF, VQF}

# AffLinSets and VecLinSets
const ALS = Union{
Expand Down Expand Up @@ -57,7 +57,7 @@ mutable struct QuadraticConstraint
A::COO
b::SparseVector{Float64}
g::Vector{Int}
dg::Dict{Int,Int}
dg::Dict{Int, Int}
nnzg::Int
nnzh::Int
end
Expand Down Expand Up @@ -279,7 +279,7 @@ function parser_SQF(fun, set, nvar, qcons, quad_lcon, quad_ucon, index_map)
# - The key `r` specifies a row index in the vector Ax + b.
# - The value `dg[r]` is a position in the vector (of length nnzg)
# where the non-zero entries of the Jacobian for row `r` are stored.
dg = Dict{Int,Int}(g[p] => p for p = 1:nnzg)
dg = Dict{Int, Int}(g[p] => p for p = 1:nnzg)
nnzh = length(vals)
qcon = QuadraticConstraint(A, b, g, dg, nnzg, nnzh)
push!(qcons, qcon)
Expand Down Expand Up @@ -345,7 +345,7 @@ function parser_VQF(fun, set, nvar, qcons, quad_lcon, quad_ucon, index_map)
# - The key `r` specifies a row index in the vector Ax + b.
# - The value `dg[r]` is a position in the vector (of length nnzg)
# where the non-zero entries of the Jacobian for row `r` are stored.
dg = Dict{Int,Int}(g[p] => p for p = 1:nnzg)
dg = Dict{Int, Int}(g[p] => p for p = 1:nnzg)
nnzh = length(vals)
qcon = QuadraticConstraint(A, b, g, dg, nnzg, nnzh)
push!(qcons, qcon)
Expand Down Expand Up @@ -375,7 +375,11 @@ function parser_MOI(moimodel, index_map, nvar)

contypes = MOI.get(moimodel, MOI.ListOfConstraintTypesPresent())
for (F, S) in contypes
F <: AF || F <: QF || F == MOI.ScalarNonlinearFunction || F == VI || error("Function $F is not supported.")
F <: AF ||
F <: QF ||
F == MOI.ScalarNonlinearFunction ||
F == VI ||
error("Function $F is not supported.")
S <: LS || error("Set $S is not supported.")

conindices = MOI.get(moimodel, MOI.ListOfConstraintIndices{F, S}())
Expand Down
2 changes: 1 addition & 1 deletion test/nlp_consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ for problem in nlp_problems
problem_f = eval(Symbol(lowercase(problem)))
nlp_moi = MathOptNLPModel(problem_f())
nlps = [nlp_manual; nlp_moi]
consistent_nlps(nlps, linear_api=true, test_slack=false)
consistent_nlps(nlps, linear_api = true, test_slack = false)
view_subarray_nlp(nlp_moi)
end
end
6 changes: 1 addition & 5 deletions test/nlp_problems/hs219.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ function hs219(args...; kwargs...)
@constraint(nlp, x[1]^2 - x[2] - x[4]^2 == 0)
@NLconstraint(nlp, x[2] - x[1]^3 - x[3]^2 == 0)

@NLobjective(
nlp,
Min,
-x[1]
)
@NLobjective(nlp, Min, -x[1])

return nlp
end
2 changes: 1 addition & 1 deletion test/nls_consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ for problem in nls_problems
if isdefined(Main, Symbol(spc))
push!(nlss, eval(Meta.parse(spc))())
end
consistent_nlss(nlss, linear_api=true, test_slack=false)
consistent_nlss(nlss, linear_api = true, test_slack = false)
view_subarray_nls(nls_moi)
end
end

0 comments on commit 3dd5054

Please sign in to comment.