Skip to content

Commit fb520c0

Browse files
committed
Fix tests to accept index.* filenames; add linear-api tutorial (closes #135)
1 parent 41937e0 commit fb520c0

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

tutorials/linear-api/Project.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[deps]
2+
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
3+
NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856"
4+
LinearOperators = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125"
5+
6+
[compat]
7+
NLPModels = "0.20"
8+
NLPModelsTest = "0.9"
9+
LinearOperators = "2.2"

tutorials/linear-api/index.jmd

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: "Accessing Linear and Nonlinear Constraints (Linear API)"
3+
tags: ["models", "linear", "constraints", "linear_api"]
4+
author: "arnavk23"
5+
---
6+
7+
# Accessing Linear and Nonlinear Constraints (Linear API)
8+
9+
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.
10+
11+
We demonstrate how to:
12+
13+
- evaluate the constraint vector;
14+
- obtain the Jacobian sparsity and coordinates;
15+
- build the `LinearOperator` representation of the Jacobian and use `mul!` to compute Jacobian-vector products; and
16+
- compare direct `jprod!`/`jtprod!` evaluations with the operator-based `mul!`.
17+
18+
## Example using a test problem
19+
20+
We use a problem from `NLPModelsTest` which may contain both linear and nonlinear constraints. The `NLPModelsTest` package exposes a collection of example problems useful for experimenting with the APIs.
21+
22+
```julia
23+
using NLPModelsTest, NLPModels, LinearOperators, LinearAlgebra
24+
25+
# Load a test problem (choose one that has constraints)
26+
list = NLPModelsTest.nlp_problems
27+
nlp = eval(Symbol(list[7]))()
28+
29+
# Point at which to evaluate
30+
x = nlp.meta.x0
31+
32+
# Evaluate constraints
33+
c = zeros(nlp.meta.ncon)
34+
cons!(nlp, x, c)
35+
println("c = ", c)
36+
37+
# Get Jacobian sparsity and values
38+
rows = zeros(Int, nlp.meta.nnzj)
39+
cols = zeros(Int, nlp.meta.nnzj)
40+
jac_structure!(nlp, rows, cols)
41+
vals = zeros(Float64, nlp.meta.nnzj)
42+
jac_coord!(nlp, x, vals)
43+
44+
println("Jacobian nonzero pattern (first 10):")
45+
println(hcat(rows[1:min(end,10)], cols[1:min(end,10)], vals[1:min(end,10)]))
46+
47+
# Build a LinearOperator for the Jacobian (operator acts on variable-space vectors)
48+
Jv = zeros(nlp.meta.ncon)
49+
Jtv = zeros(nlp.meta.nvar)
50+
J = jac_op!(nlp, x, Jv, Jtv) # returns a LinearOperator representing the Jacobian
51+
52+
# Compare operator-based J*v with jprod!
53+
v = randn(nlp.meta.nvar)
54+
Jv_op = similar(Jv)
55+
mul!(Jv_op, J, v) # operator-based product
56+
Jv_direct = zeros(nlp.meta.ncon)
57+
jprod!(nlp, x, v, Jv_direct) # direct Jacobian-vector product API
58+
59+
println("||J*v (op) - J*v (jprod!)|| = ", norm(Jv_op - Jv_direct))
60+
61+
# And similarly for J'*w
62+
w = randn(nlp.meta.ncon)
63+
Jt_w_op = zeros(nlp.meta.nvar)
64+
mul!(Jt_w_op, adjoint(J), w)
65+
Jt_w_direct = zeros(nlp.meta.nvar)
66+
jtprod!(nlp, x, w, Jt_w_direct)
67+
println("||J' * w (op) - J' * w (jtprod!)|| = ", norm(Jt_w_op - Jt_w_direct))
68+
```
69+
70+
## Notes
71+
72+
- The `jac_op!`/`hess_op!` helpers produce `LinearOperator` objects (see `LinearOperators.jl`), which allow efficient `mul!` operations without assembling dense matrices.
73+
- 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.

0 commit comments

Comments
 (0)