-
Notifications
You must be signed in to change notification settings - Fork 38
Separate default implementations from api.jl #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arnavk23
wants to merge
7
commits into
JuliaSmoothOptimizers:main
Choose a base branch
from
arnavk23:separate-api-defaults
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
43a96d1
Separate default implementations from api.jl
arnavk23 d826ab7
Update src/NLPModels.jl
arnavk23 eda7309
Update NLPModels.jl
arnavk23 3097564
Update defaults.jl
arnavk23 844ef56
Update NLPModels.jl
arnavk23 b1aedea
Update src/nlp/defaults.jl
arnavk23 57e330e
Add NLS defaults and move default implementations out of nls/api.jl
arnavk23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Default implementations for NLP API functions | ||
# This file contains the default implementations that were separated from api.jl | ||
# to improve code organization and separate API definitions from implementations. | ||
|
||
# Default implementations for core functions | ||
arnavk23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
function grad(nlp::AbstractNLPModel{T, S}, x::AbstractVector) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
g = S(undef, nlp.meta.nvar) | ||
return grad!(nlp, x, g) | ||
end | ||
|
||
function cons(nlp::AbstractNLPModel{T, S}, x::AbstractVector) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
c = S(undef, nlp.meta.ncon) | ||
return cons!(nlp, x, c) | ||
end | ||
|
||
function cons!(nlp::AbstractNLPModel, x::AbstractVector, cx::AbstractVector) | ||
@lencheck nlp.meta.nvar x | ||
@lencheck nlp.meta.ncon cx | ||
increment!(nlp, :neval_cons) | ||
if nlp.meta.nlin > 0 | ||
if nlp.meta.nnln == 0 | ||
cons_lin!(nlp, x, cx) | ||
else | ||
cons_lin!(nlp, x, view(cx, nlp.meta.lin)) | ||
end | ||
end | ||
if nlp.meta.nnln > 0 | ||
if nlp.meta.nlin == 0 | ||
cons_nln!(nlp, x, cx) | ||
else | ||
cons_nln!(nlp, x, view(cx, nlp.meta.nln)) | ||
end | ||
end | ||
return cx | ||
end | ||
|
||
function cons_lin(nlp::AbstractNLPModel{T, S}, x::AbstractVector) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
c = S(undef, nlp.meta.nlin) | ||
return cons_lin!(nlp, x, c) | ||
end | ||
|
||
function cons_nln(nlp::AbstractNLPModel{T, S}, x::AbstractVector) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
c = S(undef, nlp.meta.nnln) | ||
return cons_nln!(nlp, x, c) | ||
end | ||
|
||
function jth_congrad(nlp::AbstractNLPModel{T, S}, x::AbstractVector, j::Integer) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
g = S(undef, nlp.meta.nvar) | ||
return jth_congrad!(nlp, x, j, g) | ||
end | ||
|
||
function objcons(nlp::AbstractNLPModel{T, S}, x::AbstractVector) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
c = S(undef, nlp.meta.ncon) | ||
return objcons!(nlp, x, c) | ||
end | ||
|
||
function objcons!(nlp::AbstractNLPModel, x::AbstractVector, c::AbstractVector) | ||
@lencheck nlp.meta.nvar x | ||
@lencheck nlp.meta.ncon c | ||
f = obj(nlp, x) | ||
cons!(nlp, x, c) | ||
return f, c | ||
end | ||
|
||
function objgrad(nlp::AbstractNLPModel{T, S}, x::AbstractVector) where {T, S} | ||
@lencheck nlp.meta.nvar x | ||
g = S(undef, nlp.meta.nvar) | ||
return objgrad!(nlp, x, g) | ||
end | ||
|
||
function objgrad!(nlp::AbstractNLPModel, x::AbstractVector, g::AbstractVector) | ||
@lencheck nlp.meta.nvar x g | ||
f = obj(nlp, x) | ||
grad!(nlp, x, g) | ||
return f, g | ||
end | ||
|
||
function jac_structure(nlp::AbstractNLPModel) | ||
rows = Vector{Int}(undef, nlp.meta.nnzj) | ||
cols = Vector{Int}(undef, nlp.meta.nnzj) | ||
jac_structure!(nlp, rows, cols) | ||
end | ||
|
||
function jac_structure!( | ||
nlp::AbstractNLPModel, | ||
rows::AbstractVector{T}, | ||
cols::AbstractVector{T}, | ||
) where {T} | ||
@lencheck nlp.meta.nnzj rows cols | ||
lin_ind = 1:(nlp.meta.lin_nnzj) | ||
if nlp.meta.nlin > 0 | ||
if nlp.meta.nnln == 0 | ||
jac_lin_structure!(nlp, rows, cols) | ||
else | ||
jac_lin_structure!(nlp, view(rows, lin_ind), view(cols, lin_ind)) | ||
for i in lin_ind | ||
rows[i] += count(x < nlp.meta.lin[rows[i]] for x in nlp.meta.nln) | ||
end | ||
end | ||
end | ||
if nlp.meta.nnln > 0 | ||
if nlp.meta.nlin == 0 | ||
jac_nln_structure!(nlp, rows, cols) | ||
else | ||
nln_ind = (nlp.meta.lin_nnzj + 1):(nlp.meta.lin_nnzj + nlp.meta.nln_nnzj) | ||
jac_nln_structure!(nlp, view(rows, nln_ind), view(cols, nln_ind)) | ||
for i in nln_ind | ||
rows[i] += count(x < nlp.meta.nln[rows[i]] for x in nlp.meta.lin) | ||
end | ||
end | ||
end | ||
return rows, cols | ||
end | ||
|
||
function jac_lin_structure(nlp::AbstractNLPModel) | ||
rows = Vector{Int}(undef, nlp.meta.lin_nnzj) | ||
cols = Vector{Int}(undef, nlp.meta.lin_nnzj) | ||
jac_lin_structure!(nlp, rows, cols) | ||
end | ||
|
||
function jac_nln_structure(nlp::AbstractNLPModel) | ||
rows = Vector{Int}(undef, nlp.meta.nln_nnzj) | ||
cols = Vector{Int}(undef, nlp.meta.nln_nnzj) | ||
jac_nln_structure!(nlp, rows, cols) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.