-
Notifications
You must be signed in to change notification settings - Fork 10
Add tutorial accessing Linear and Nonlinear Constraints #156
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
10
commits into
JuliaSmoothOptimizers:main
Choose a base branch
from
arnavk23:main
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.
+85
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b95806b
changing example due to failure
arnavk23 8a7891c
adding copilot suggestions
arnavk23 41937e0
adding failing reset!
arnavk23 6136c05
Update index.jmd
arnavk23 fb520c0
Fix tests to accept index.* filenames; add linear-api tutorial (close…
arnavk23 39bced6
Merge branch 'main' of https://github.com/arnavk23/JSOTutorials.jl
arnavk23 11346c4
Implemented exactly as suggested: the tutorial now uses deterministic…
arnavk23 c3bc17c
Merge branch 'JuliaSmoothOptimizers:main' into main
arnavk23 0ec91db
Apply suggestions from code review
arnavk23 d594bc2
Merge branch 'main' of https://github.com/arnavk23/JSOTutorials.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [deps] | ||
| NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6" | ||
| NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856" | ||
| LinearOperators = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125" | ||
|
|
||
| [compat] | ||
| julia = "1" | ||
| NLPModels = "0.20" | ||
| NLPModelsTest = "0.9" | ||
| LinearOperators = "2.2" | ||
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,75 @@ | ||
| --- | ||
| title: "Accessing Linear and Nonlinear Constraints (Linear API)" | ||
| tags: ["models", "linear", "constraints", "linear_api"] | ||
| author: "arnavk23" | ||
| --- | ||
|
|
||
| # Accessing Linear and Nonlinear Constraints (Linear API) | ||
|
|
||
| This short tutorial illustrates how to inspect and operate on the constraint Jacobian using the "linear API" utilities available in the NLPModels ecosystem. The linear API is useful when a problem contains both linear and nonlinear constraints and you want to test or operate on the Jacobian and related linear operators without materializing dense matrices. | ||
|
|
||
| We demonstrate how to: | ||
|
|
||
| - evaluate the constraint vector; | ||
| - obtain the Jacobian sparsity and coordinates; | ||
| - build the `LinearOperator` representation of the Jacobian and use `mul!` to compute Jacobian-vector products; and | ||
| - compare direct `jprod!`/`jtprod!` evaluations with the operator-based `mul!`. | ||
|
|
||
| ## Example using a test problem | ||
|
|
||
| We use the `HS21` problem from `NLPModelsTest`, a standard constrained Hock--Schittkowski test problem. Choosing it by name keeps this tutorial stable across package versions and ensures the example exercises the constraint/Jacobian APIs that the linear API is designed to inspect. | ||
|
|
||
| ```julia | ||
| using NLPModelsTest, NLPModels, LinearOperators, LinearAlgebra | ||
|
|
||
| # Load a specific constrained test problem by name instead of relying on | ||
| # the ordering of `nlp_problems`, which can change across package versions. | ||
| problem_name = "HS21" | ||
| @assert problem_name in NLPModelsTest.nlp_problems "$(problem_name) is not available in NLPModelsTest.nlp_problems" | ||
| nlp = getfield(NLPModelsTest, Symbol(problem_name))() | ||
|
|
||
| # Point at which to evaluate | ||
| x = nlp.meta.x0 | ||
|
|
||
| # Evaluate constraints | ||
| c = zeros(nlp.meta.ncon) | ||
| cons!(nlp, x, c) | ||
| println("c = ", c) | ||
|
|
||
| # Get Jacobian sparsity and values | ||
| rows = zeros(Int, nlp.meta.nnzj) | ||
| cols = zeros(Int, nlp.meta.nnzj) | ||
| jac_structure!(nlp, rows, cols) | ||
| vals = zeros(Float64, nlp.meta.nnzj) | ||
| jac_coord!(nlp, x, vals) | ||
|
|
||
| println("Jacobian nonzero pattern (first 10):") | ||
| println(hcat(rows[1:min(end,10)], cols[1:min(end,10)], vals[1:min(end,10)])) | ||
|
|
||
| # Build a LinearOperator for the Jacobian (operator acts on variable-space vectors) | ||
| Jv = zeros(nlp.meta.ncon) | ||
| Jtv = zeros(nlp.meta.nvar) | ||
| J = jac_op!(nlp, x, Jv, Jtv) # returns a LinearOperator representing the Jacobian | ||
|
|
||
| # Compare operator-based J*v with jprod! | ||
| v = ones(nlp.meta.nvar) | ||
| Jv_op = similar(Jv) | ||
| mul!(Jv_op, J, v) # operator-based product | ||
| Jv_direct = zeros(nlp.meta.ncon) | ||
| jprod!(nlp, x, v, Jv_direct) # direct Jacobian-vector product API | ||
|
|
||
| println("||J*v (op) - J*v (jprod!)|| = ", norm(Jv_op - Jv_direct)) | ||
|
|
||
| # And similarly for J'*w | ||
| w = ones(nlp.meta.ncon) | ||
| Jt_w_op = zeros(nlp.meta.nvar) | ||
| mul!(Jt_w_op, adjoint(J), w) | ||
| Jt_w_direct = zeros(nlp.meta.nvar) | ||
| jtprod!(nlp, x, w, Jt_w_direct) | ||
| println("||J' * w (op) - J' * w (jtprod!)|| = ", norm(Jt_w_op - Jt_w_direct)) | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - The `jac_op!`/`hess_op!` helpers produce `LinearOperator` objects (see `LinearOperators.jl`), which allow efficient `mul!` operations without assembling dense matrices. | ||
| - The testing utilities in this repository expose a `linear_api` option (for example in `test_allocs_nlpmodels`) that enables checking the linear-specific functions; see the `Test allocations of NLPModels` tutorial for details. |
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.