Skip to content

Commit edcec16

Browse files
committed
Update the API
1 parent 0b6e25b commit edcec16

4 files changed

Lines changed: 112 additions & 161 deletions

File tree

src/nlp/batch_api.jl

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export AbstractBatchNLPModel
2+
export obj!
23

34
"""
45
AbstractBatchNLPModel
@@ -11,211 +12,199 @@ and the sparsity patterns of the Jacobian and the Hessian of the Lagrangian are
1112
abstract type AbstractBatchNLPModel{T, S} end
1213

1314
"""
14-
bf = obj(bnlp, bx)
15+
bf = obj(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix)
1516
"""
16-
function obj(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractVector) where {T, S}
17-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx
18-
bf = S(undef, bnlp.meta.nbatch)
17+
function obj(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractMatrix) where {T, S}
18+
bf = S(undef, 1, bnlp.meta.nbatch) |> vec
1919
obj!(bnlp, bx, bf)
2020
return bf
2121
end
2222

2323
"""
24-
bf = obj!(bnlp, bx, bf)
24+
bf = obj!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bf::AbstractVector)
2525
"""
2626
function obj! end
2727

2828
"""
29-
bg = grad(bnlp, bx)
29+
bg = grad(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix)
3030
3131
This function is only available if `bnlp.meta.grad_available` is set to `true`.
3232
"""
33-
function grad(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractVector) where {T, S}
34-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx
35-
bg = S(undef, bnlp.meta.nvar * bnlp.meta.nbatch)
33+
function grad(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractMatrix) where {T, S}
34+
bg = S(undef, bnlp.meta.nvar, bnlp.meta.nbatch)
3635
grad!(bnlp, bx, bg)
3736
return bg
3837
end
3938

4039
"""
41-
bg = grad!(bnlp, bx, bg)
40+
bg = grad!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bg::AbstractMatrix)
4241
4342
This function is only available if `bnlp.meta.grad_available` is set to `true`.
4443
"""
4544
# function grad! end
4645

4746
"""
48-
bc = cons(bnlp, bx)
47+
bc = cons(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix)
4948
"""
50-
function cons(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractVector) where {T, S}
51-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx
52-
bc = S(undef, bnlp.meta.ncon * bnlp.meta.nbatch)
49+
function cons(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractMatrix) where {T, S}
50+
bc = S(undef, bnlp.meta.ncon, bnlp.meta.nbatch)
5351
cons!(bnlp, bx, bc)
5452
return bc
5553
end
5654

5755
"""
58-
bc = cons!(bnlp, bx, bc)
56+
bc = cons!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bc::AbstractMatrix)
5957
"""
6058
# function cons! end
6159

6260
"""
63-
(jrows, jcols) = jac_structure(bnlp)
61+
(jrows, jcols) = jac_structure(bnlp::AbstractBatchNLPModel)
6462
6563
This function is only available if `bnlp.meta.jac_available` is set to `true`.
6664
"""
67-
function jac_structure(bnlp::AbstractBatchNLPModel{T, S}) where {T, S}
65+
function jac_structure(bnlp::AbstractBatchNLPModel)
6866
jrows = Vector{Int}(undef, bnlp.meta.nnzj)
6967
jcols = Vector{Int}(undef, bnlp.meta.nnzj)
7068
jac_structure!(bnlp, jrows, jcols)
7169
return (jrows, jcols)
7270
end
7371

7472
"""
75-
(jrows, jcols) = jac_structure!(bnlp, jrows, jcols)
73+
(jrows, jcols) = jac_structure!(bnlp::AbstractBatchNLPModel, jrows::AbstractVector, jcols::AbstractVector)
7674
7775
This function is only available if `bnlp.meta.jac_available` is set to `true`.
7876
"""
7977
# function jac_structure! end
8078

8179
"""
82-
bjvals = jac_coord(bnlp, bx)
80+
bjvals = jac_coord(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix)
8381
8482
This function is only available if `bnlp.meta.jac_available` is set to `true`.
8583
"""
86-
function jac_coord(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractVector) where {T, S}
87-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx
88-
bjvals = S(undef, bnlp.meta.nnzj * bnlp.meta.nbatch)
84+
function jac_coord(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractMatrix) where {T, S}
85+
bjvals = S(undef, bnlp.meta.nnzj, bnlp.meta.nbatch)
8986
jac_coord!(bnlp, bx, bjvals)
9087
return bjvals
9188
end
9289

9390
"""
94-
bjvals = jac_coord!(bnlp, bx, bjvals)
91+
bjvals = jac_coord!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bjvals::AbstractMatrix)
9592
9693
This function is only available if `bnlp.meta.jac_available` is set to `true`.
9794
"""
9895
# function jac_coord! end
9996

10097
"""
101-
bJx = jac_dense!(bnlp, bx, bJx)
98+
bJx = jac_dense!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bJx::AbstractArray)
10299
103100
This function is only available if `bnlp.meta.jac_available` is set to `true`.
104101
"""
105102
# function jac_dense! end
106103

107104
"""
108-
bJv = jprod(bnlp, bx, bv)
105+
bJv = jprod(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bv::AbstractMatrix)
109106
110107
This function is only available if `bnlp.meta.jprod_available` is set to `true`.
111108
"""
112-
function jprod(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractVector, bv::AbstractVector) where {T, S}
113-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx bv
114-
bJv = S(undef, bnlp.meta.ncon * bnlp.meta.nbatch)
109+
function jprod(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractMatrix, bv::AbstractMatrix) where {T, S}
110+
bJv = S(undef, bnlp.meta.ncon, bnlp.meta.nbatch)
115111
jprod!(bnlp, bx, bv, bJv)
116112
return bJv
117113
end
118114

119115
"""
120-
bJv = jprod!(bnlp, bx, bv, bJv)
116+
bJv = jprod!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bv::AbstractMatrix, bJv::AbstractMatrix)
121117
122118
This function is only available if `bnlp.meta.jprod_available` is set to `true`.
123119
"""
124120
# function jprod! end
125121

126122
"""
127-
bJtv = jtprod(bnlp, bx, bv)
123+
bJtv = jtprod(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bv::AbstractMatrix)
128124
129125
This function is only available if `bnlp.meta.jtprod_available` is set to `true`.
130126
"""
131-
function jtprod(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractVector, bv::AbstractVector) where {T, S}
132-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx
133-
@lencheck (bnlp.meta.ncon * bnlp.meta.nbatch) bv
134-
bJtv = S(undef, bnlp.meta.nvar * bnlp.meta.nbatch)
127+
function jtprod(bnlp::AbstractBatchNLPModel{T, S}, bx::AbstractMatrix, bv::AbstractMatrix) where {T, S}
128+
bJtv = S(undef, bnlp.meta.nvar, bnlp.meta.nbatch)
135129
jtprod!(bnlp, bx, bv, bJtv)
136130
return bJtv
137131
end
138132

139133
"""
140-
bJtv = jtprod!(bnlp, bx, bv, bJtv)
134+
bJtv = jtprod!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, bv::AbstractMatrix, bJtv::AbstractMatrix)
141135
142136
This function is only available if `bnlp.meta.jtprod_available` is set to `true`.
143137
"""
144138
# function jtprod! end
145139

146140
"""
147-
(hrows, hcols) = hess_structure(bnlp)
141+
(hrows, hcols) = hess_structure(bnlp::AbstractBatchNLPModel)
148142
149143
This function is only available if `bnlp.meta.hess_available` is set to `true`.
150144
"""
151-
function hess_structure(bnlp::AbstractBatchNLPModel{T,S}) where {T, S}
145+
function hess_structure(bnlp::AbstractBatchNLPModel)
152146
hrows = Vector{Int}(undef, bnlp.meta.nnzh)
153147
hcols = Vector{Int}(undef, bnlp.meta.nnzh)
154148
hess_structure!(bnlp, hrows, hcols)
155149
return hrows, hcols
156150
end
157151

158152
"""
159-
(hrows, hcols) = hess_structure!(bnlp, hrows, hcols)
153+
(hrows, hcols) = hess_structure!(bnlp::AbstractBatchNLPModel, hrows::AbstractVector, hcols::AbstractVector)
160154
161155
This function is only available if `bnlp.meta.hess_available` is set to `true`.
162156
"""
163157
# function hess_structure! end
164158

165159
"""
166-
bhvals = hess_coord(bnlp, bx, by, bobj_weight)
160+
bhvals = hess_coord(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, by::AbstractMatrix, bobj_weight::AbstractVector)
167161
168162
This function is only available if `bnlp.meta.hess_available` is set to `true`.
169163
"""
170164
function hess_coord(
171165
bnlp::AbstractBatchNLPModel{T, S},
172-
bx::AbstractVector,
173-
by::AbstractVector,
166+
bx::AbstractMatrix,
167+
by::AbstractMatrix,
174168
bobj_weight::AbstractVector,
175169
) where {T, S}
176-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx
177-
@lencheck (bnlp.meta.ncon * bnlp.meta.nbatch) by
178-
@lencheck bnlp.meta.nbatch bobj_weight
179-
bhvals = S(undef, bnlp.meta.nnzh * bnlp.meta.nbatch)
180-
return hess_coord!(bnlp, bx, by, bobj_weight, bhvals)
170+
bhvals = S(undef, bnlp.meta.nnzh, bnlp.meta.nbatch)
171+
hess_coord!(bnlp, bx, by, bobj_weight, bhvals)
172+
return bhvals
181173
end
182174

183175
"""
184-
bhvals = hess_coord!(bnlp, bx, by, bobj_weight, bhvals)
176+
bhvals = hess_coord!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, by::AbstractMatrix, bobj_weight::AbstractVector, bhvals::AbstractMatrix)
185177
186178
This function is only available if `bnlp.meta.hess_available` is set to `true`.
187179
"""
188180
# function hess_coord! end
189181

190182
"""
191-
bHx = hess_dense!(bnlp, bx, by, bHx, bobj_weight)
183+
bHx = hess_dense!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, by::AbstractMatrix, bobj_weight::AbstractVector, bHx::AbstractArray)
192184
193185
This function is only available when `bnlp.meta.hess_available` is set to `true`.
194186
"""
195187
# function hess_dense! end
196188

197189
"""
198-
bHv = hprod(bnlp, bx, by, bv, bobj_weight)
190+
bHv = hprod(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, by::AbstractMatrix, bv::AbstractMatrix, bobj_weight::AbstractVector)
199191
200192
This function is only available if `bnlp.meta.hprod_available` is set to `true`.
201193
"""
202194
function hprod(
203195
bnlp::AbstractBatchNLPModel{T, S},
204-
bx::AbstractVector,
205-
by::AbstractVector,
206-
bv::AbstractVector,
196+
bx::AbstractMatrix,
197+
by::AbstractMatrix,
198+
bv::AbstractMatrix,
207199
bobj_weight::AbstractVector,
208200
) where {T, S}
209-
@lencheck (bnlp.meta.nvar * bnlp.meta.nbatch) bx bv
210-
@lencheck (bnlp.meta.ncon * bnlp.meta.nbatch) by
211-
@lencheck bnlp.meta.nbatch bobj_weight
212-
bHv = S(undef, bnlp.meta.nvar * bnlp.meta.nbatch)
201+
bHv = S(undef, bnlp.meta.nvar, bnlp.meta.nbatch)
213202
hprod!(bnlp, bx, by, bv, bobj_weight, bHv)
214203
return bHv
215204
end
216205

217206
"""
218-
bHv = hprod!(bnlp, bx, by, bv, bobj_weight, bHv)
207+
bHv = hprod!(bnlp::AbstractBatchNLPModel, bx::AbstractMatrix, by::AbstractMatrix, bv::AbstractMatrix, bobj_weight::AbstractVector, bHv::AbstractMatrix)
219208
220209
This function is only available if `bnlp.meta.hprod_available` is set to `true`.
221210
"""

src/nlp/batch_meta.jl

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ nonlinear optimization problems sharing the same structure.
1515
1616
Each batch contains `nbatch` independent NLP models of the form:
1717
18-
optimize objᵢ(xᵢ)
19-
subject to lvarᵢ ≤ xᵢ ≤ uvarᵢ
20-
lconᵢ ≤ consᵢ(xᵢ) ≤ uconᵢ
18+
optimize objᵢ(x)
19+
subject to lvarᵢ ≤ x ≤ uvarᵢ
20+
lconᵢ ≤ consᵢ(x) ≤ uconᵢ
2121
2222
for i = 1, ..., nbatch.
2323
24-
Each model variable vector `xᵢ` has dimension `nvar`, and constraint vector
25-
`consᵢ(xᵢ)` has dimension `ncon`.
24+
Each model variable vector `x` has dimension `nvar`, and constraint vector
25+
`consᵢ(x)` has dimension `ncon`.
2626
27-
All batch data are stored in concatenated vectors of length:
27+
All batch data are stored in matrices of size:
2828
29-
- `nvar * nbatch` for variables and bounds (`x0`, `lvar`, `uvar`)
30-
- `ncon * nbatch` for constraints and multipliers (`y0`, `lcon`, `ucon`)
29+
- `(nvar, nbatch)` for variables and bounds (`x0`, `lvar`, `uvar`)
30+
- `(ncon, nbatch)` for constraints and multipliers (`y0`, `lcon`, `ucon`)
3131
3232
---
3333
@@ -36,17 +36,17 @@ All batch data are stored in concatenated vectors of length:
3636
Create a `BatchNLPModelMeta` with `nbatch` models, each having `nvar` variables.
3737
The following keyword arguments are accepted:
3838
- `x0`: initial guess
39-
- `lvar`: vector of lower bounds
40-
- `uvar`: vector of upper bounds
39+
- `lvar`: matrix of lower bounds
40+
- `uvar`: matrix of upper bounds
4141
- `ncon`: number of general constraints
4242
- `y0`: initial Lagrange multipliers
43-
- `lcon`: vector of constraint lower bounds
44-
- `ucon`: vector of constraint upper bounds
43+
- `lcon`: matrix of constraint lower bounds
44+
- `ucon`: matrix of constraint upper bounds
4545
- `nnzj`: number of elements needed to store the nonzeros in the sparse Jacobian
4646
- `nnzh`: number of elements needed to store the nonzeros in the sparse Hessian
4747
- `minimize`: true if optimize == minimize
48-
- `islp`: true if the problem is a linear program
49-
- `name`: problem name
48+
- `islp`: true if the problems are linear programs
49+
- `name`: problem name for the batch
5050
- `sparse_jacobian`: indicates whether the Jacobian of the constraints is sparse
5151
- `sparse_hessian`: indicates whether the Hessian of the Lagrangian is sparse
5252
- `grad_available`: indicates whether the gradient of the objective is available
@@ -58,34 +58,29 @@ The following keyword arguments are accepted:
5858
"""
5959
struct BatchNLPModelMeta{T, S} <: AbstractBatchNLPModelMeta{T, S}
6060
nbatch::Int
61-
6261
nvar::Int
6362
x0::S
6463
lvar::S
6564
uvar::S
66-
6765
ncon::Int
6866
y0::S
6967
lcon::S
7068
ucon::S
71-
7269
nnzj::Int
7370
nnzh::Int
74-
7571
minimize::Bool
7672
islp::Bool
7773
name::String
78-
7974
sparse_jacobian::Bool
8075
sparse_hessian::Bool
81-
8276
grad_available::Bool
8377
jac_available::Bool
8478
hess_available::Bool
8579
jprod_available::Bool
8680
jtprod_available::Bool
8781
hprod_available::Bool
8882
end
83+
8984
for field in fieldnames(BatchNLPModelMeta)
9085
meth = Symbol("get_", field)
9186
@eval begin
@@ -94,16 +89,17 @@ for field in fieldnames(BatchNLPModelMeta)
9489
@eval $meth(bnlp::AbstractBatchNLPModel) = $meth(bnlp.meta)
9590
@eval export $meth
9691
end
92+
9793
function BatchNLPModelMeta{T, S}(
9894
nbatch::Int,
9995
nvar::Int;
100-
x0::S = fill!(S(undef, nvar * nbatch), zero(T)),
101-
lvar::S = fill!(S(undef, nvar * nbatch), T(-Inf)),
102-
uvar::S = fill!(S(undef, nvar * nbatch), T(Inf)),
96+
x0::S = fill!(S(undef, nvar, nbatch), zero(T)),
97+
lvar::S = fill!(S(undef, nvar, nbatch), T(-Inf)),
98+
uvar::S = fill!(S(undef, nvar, nbatch), T(Inf)),
10399
ncon::Int = 0,
104-
y0::S = fill!(S(undef, ncon * nbatch), zero(T)),
105-
lcon::S = fill!(S(undef, ncon * nbatch), T(-Inf)),
106-
ucon::S = fill!(S(undef, ncon * nbatch), T(Inf)),
100+
y0::S = fill!(S(undef, ncon, nbatch), zero(T)),
101+
lcon::S = fill!(S(undef, ncon, nbatch), T(-Inf)),
102+
ucon::S = fill!(S(undef, ncon, nbatch), T(Inf)),
107103
nnzj::Int = nvar * ncon,
108104
nnzh::Int = nvar * (nvar + 1) ÷ 2,
109105
minimize::Bool = true,

0 commit comments

Comments
 (0)