|
| 1 | +export AbstractBatchNLPModelMeta, BatchNLPModelMeta |
| 2 | + |
| 3 | +""" |
| 4 | + AbstractBatchNLPModelMeta |
| 5 | +
|
| 6 | +Abstract base type for metadata related to batched nonlinear optimization models. |
| 7 | +""" |
| 8 | +abstract type AbstractBatchNLPModelMeta{T, S, VI} end |
| 9 | + |
| 10 | +""" |
| 11 | + BatchNLPModelMeta <: AbstractBatchNLPModelMeta |
| 12 | +
|
| 13 | +A composite type that represents the main features of the optimization problem |
| 14 | +
|
| 15 | + optimize obj(x) |
| 16 | + subject to lvar ≤ x ≤ uvar |
| 17 | + lcon ≤ cons(x) ≤ ucon |
| 18 | +
|
| 19 | +where `x` is an `nvar`-dimensional vector, |
| 20 | + `obj` is the real-valued objective function, |
| 21 | + `cons` is the vector-valued constraint function, |
| 22 | + `optimize` is either "minimize" or "maximize". |
| 23 | +
|
| 24 | +Here, `lvar`, `uvar`, `lcon` and `ucon` are vectors. |
| 25 | +Some of their components may be infinite to indicate that the corresponding bound or general constraint is not present. |
| 26 | +
|
| 27 | +--- |
| 28 | +
|
| 29 | + BatchNLPModelMeta(nbatch::Int, nvar; kwargs...) |
| 30 | +
|
| 31 | +Create an `BatchNLPModelMeta` with `nvar` variables. |
| 32 | +Alternatively, create an `NLPModelMeta` copy from another `AbstractNLPModelMeta`. |
| 33 | +The following keyword arguments are accepted: |
| 34 | +- `x0`: initial guess |
| 35 | +- `lvar`: vector of lower bounds |
| 36 | +- `uvar`: vector of upper bounds |
| 37 | +- `ncon`: number of general constraints |
| 38 | +- `y0`: initial Lagrange multipliers |
| 39 | +- `lcon`: vector of constraint lower bounds |
| 40 | +- `ucon`: vector of constraint upper bounds |
| 41 | +- `nnzj`: number of elements needed to store the nonzeros in the sparse Jacobian |
| 42 | +- `nnzh`: number of elements needed to store the nonzeros in the sparse Hessian |
| 43 | +- `minimize`: true if optimize == minimize |
| 44 | +- `islp`: true if the problem is a linear program |
| 45 | +- `name`: problem name |
| 46 | +- `grad_available`: indicates whether the gradient of the objective is available |
| 47 | +- `jac_available`: indicates whether the sparse Jacobian of the constraints is available |
| 48 | +- `hess_available`: indicates whether the sparse Hessian of the Lagrangian is available |
| 49 | +- `jprod_available`: indicates whether the Jacobian-vector product `J * v` is available |
| 50 | +- `jtprod_available`: indicates whether the transpose Jacobian-vector product `J' * v` is available |
| 51 | +- `hprod_available`: indicates whether the Hessian-vector product of the Lagrangian `H * v` is available |
| 52 | +
|
| 53 | +`BatchNLPModelMeta` also contains the following attributes, which are computed from the variables above: |
| 54 | +- `nbatch`: number of models |
| 55 | +- `nvar`: number of variables |
| 56 | +- `ifix`: indices of fixed variables |
| 57 | +- `ilow`: indices of variables with lower bound only |
| 58 | +- `iupp`: indices of variables with upper bound only |
| 59 | +- `irng`: indices of variables with lower and upper bound (range) |
| 60 | +- `ifree`: indices of free variables |
| 61 | +- `iinf`: indices of visibly infeasible bounds |
| 62 | +- `jfix`: indices of equality constraints |
| 63 | +- `jlow`: indices of constraints of the form c(x) ≥ cl |
| 64 | +- `jupp`: indices of constraints of the form c(x) ≤ cu |
| 65 | +- `jrng`: indices of constraints of the form cl ≤ c(x) ≤ cu |
| 66 | +- `jfree`: indices of "free" constraints (there shouldn't be any) |
| 67 | +- `jinf`: indices of the visibly infeasible constraints |
| 68 | +""" |
| 69 | +struct BatchNLPModelMeta{T, S, VI} <: AbstractBatchNLPModelMeta{T, S, VI} |
| 70 | + nbatch::Int |
| 71 | + nvar::Int |
| 72 | + x0::S |
| 73 | + lvar::S |
| 74 | + uvar::S |
| 75 | + |
| 76 | + ifix::VI |
| 77 | + ilow::VI |
| 78 | + iupp::VI |
| 79 | + irng::VI |
| 80 | + ifree::VI |
| 81 | + iinf::VI |
| 82 | + |
| 83 | + ncon::Int |
| 84 | + y0::S |
| 85 | + lcon::S |
| 86 | + ucon::S |
| 87 | + |
| 88 | + jfix::VI |
| 89 | + jlow::VI |
| 90 | + jupp::VI |
| 91 | + jrng::VI |
| 92 | + jfree::VI |
| 93 | + jinf::VI |
| 94 | + |
| 95 | + nnzj::Int |
| 96 | + nnzh::Int |
| 97 | + |
| 98 | + minimize::Bool |
| 99 | + islp::Bool |
| 100 | + name::String |
| 101 | + |
| 102 | + grad_available::Bool |
| 103 | + jac_available::Bool |
| 104 | + hess_available::Bool |
| 105 | + jprod_available::Bool |
| 106 | + jtprod_available::Bool |
| 107 | + hprod_available::Bool |
| 108 | +end |
| 109 | + |
| 110 | +function BatchNLPModelMeta{T, S, VI}( |
| 111 | + nbatch::Int, |
| 112 | + nvar::Int; |
| 113 | + x0::S = fill!(S(undef, nvar * nbatch), zero(T)), |
| 114 | + lvar::S = fill!(S(undef, nvar * nbatch), T(-Inf)), |
| 115 | + uvar::S = fill!(S(undef, nvar * nbatch), T(Inf)), |
| 116 | + ncon::Int = 0, |
| 117 | + y0::S = fill!(S(undef, ncon * nbatch), zero(T)), |
| 118 | + lcon::S = fill!(S(undef, ncon * nbatch), T(-Inf)), |
| 119 | + ucon::S = fill!(S(undef, ncon * nbatch), T(Inf)), |
| 120 | + nnzj::Int = nvar * ncon, |
| 121 | + nnzh::Int = nvar * (nvar + 1) ÷ 2, |
| 122 | + minimize::Bool = true, |
| 123 | + islp::Bool = false, |
| 124 | + name::String = "Batch NLP", |
| 125 | + grad_available::Bool = true, |
| 126 | + jac_available::Bool = (ncon > 0), |
| 127 | + hess_available::Bool = true, |
| 128 | + jprod_available::Bool = (ncon > 0), |
| 129 | + jtprod_available::Bool = (ncon > 0), |
| 130 | + hprod_available::Bool = true, |
| 131 | +) where {T, S, VI} |
| 132 | + if (nvar < 1) || (ncon < 0) || (nnzj < 0) || (nnzh < 0) |
| 133 | + error("Nonsensical dimensions") |
| 134 | + end |
| 135 | + |
| 136 | + ifix = VI(undef, nvar * nbatch) |
| 137 | + ilow = VI(undef, nvar * nbatch) |
| 138 | + iupp = VI(undef, nvar * nbatch) |
| 139 | + irng = VI(undef, nvar * nbatch) |
| 140 | + ifree = VI(undef, nvar * nbatch) |
| 141 | + iinf = VI(undef, nvar * nbatch) |
| 142 | + |
| 143 | + map!((lv, uv) -> lv == uv, ifix, lvar, uvar) |
| 144 | + map!((lv, uv) -> (lv > T(-Inf)) & (uv == T(Inf)), ilow, lvar, uvar) |
| 145 | + map!((lv, uv) -> (lv == T(-Inf)) & (uv < T(Inf)), iupp, lvar, uvar) |
| 146 | + map!((lv, uv) -> (lv > T(-Inf)) & (uv < T(Inf)), irng, lvar, uvar) |
| 147 | + map!((lv, uv) -> (lv == T(-Inf)) & (uv == T(Inf)), ifree, lvar, uvar) |
| 148 | + map!((lv, uv) -> lv > uv, iinf, lvar, uvar) |
| 149 | + |
| 150 | + jfix = VI(undef, ncon * nbatch) |
| 151 | + jlow = VI(undef, ncon * nbatch) |
| 152 | + jupp = VI(undef, ncon * nbatch) |
| 153 | + jrng = VI(undef, ncon * nbatch) |
| 154 | + jfree = VI(undef, ncon * nbatch) |
| 155 | + jinf = VI(undef, ncon * nbatch) |
| 156 | + |
| 157 | + if ncon > 0 |
| 158 | + map!((lc, uc) -> lc == uc, jfix, lcon, ucon) |
| 159 | + map!((lc, uc) -> (lc > T(-Inf)) & (uc == T(Inf)), jlow, lcon, ucon) |
| 160 | + map!((lc, uc) -> (lc == T(-Inf)) & (uc < T(Inf)), jupp, lcon, ucon) |
| 161 | + map!((lc, uc) -> (lc > T(-Inf)) & (uc < T(Inf)), jrng, lcon, ucon) |
| 162 | + map!((lc, uc) -> (lc == T(-Inf)) & (uc == T(Inf)), jfree, lcon, ucon) |
| 163 | + map!((lc, uc) -> lc > uc, jinf, lcon, ucon) |
| 164 | + end |
| 165 | + |
| 166 | + BatchNLPModelMeta{T, S, VI}( |
| 167 | + nbatch, |
| 168 | + nvar, |
| 169 | + x0, |
| 170 | + lvar, |
| 171 | + uvar, |
| 172 | + ifix, |
| 173 | + ilow, |
| 174 | + iupp, |
| 175 | + irng, |
| 176 | + ifree, |
| 177 | + iinf, |
| 178 | + ncon, |
| 179 | + y0, |
| 180 | + lcon, |
| 181 | + ucon, |
| 182 | + jfix, |
| 183 | + jlow, |
| 184 | + jupp, |
| 185 | + jrng, |
| 186 | + jfree, |
| 187 | + jinf, |
| 188 | + nnzj, |
| 189 | + nnzh, |
| 190 | + minimize, |
| 191 | + islp, |
| 192 | + name, |
| 193 | + grad_available, |
| 194 | + jac_available, |
| 195 | + hess_available, |
| 196 | + jprod_available, |
| 197 | + jtprod_available, |
| 198 | + hprod_available, |
| 199 | + ) |
| 200 | +end |
0 commit comments