septal 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- septal/__init__.py +42 -0
- septal/casadax/__init__.py +55 -0
- septal/casadax/callbacks.py +304 -0
- septal/casadax/factory.py +171 -0
- septal/casadax/schema.py +102 -0
- septal/casadax/solvers.py +367 -0
- septal/casadax/utilities.py +69 -0
- septal/jax/__init__.py +5 -0
- septal/jax/sqp/__init__.py +94 -0
- septal/jax/sqp/convergence.py +153 -0
- septal/jax/sqp/factory.py +230 -0
- septal/jax/sqp/filter.py +542 -0
- septal/jax/sqp/hessian.py +222 -0
- septal/jax/sqp/line_search.py +269 -0
- septal/jax/sqp/qp_subproblem.py +630 -0
- septal/jax/sqp/schema.py +394 -0
- septal/jax/sqp/solver.py +943 -0
- septal/py.typed +0 -0
- septal-0.2.0.dist-info/METADATA +495 -0
- septal-0.2.0.dist-info/RECORD +23 -0
- septal-0.2.0.dist-info/WHEEL +5 -0
- septal-0.2.0.dist-info/licenses/LICENSE +21 -0
- septal-0.2.0.dist-info/top_level.txt +1 -0
septal/__init__.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""
|
|
2
|
+
septal — JAX-CasADi NLP solver library.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from septal import casadax
|
|
6
|
+
from septal.casadax import (
|
|
7
|
+
BaseSolver,
|
|
8
|
+
CasadiSolver,
|
|
9
|
+
JaxCallbackForward,
|
|
10
|
+
JaxCallbackReverse,
|
|
11
|
+
JaxCasADiEvaluator,
|
|
12
|
+
JaxSolver,
|
|
13
|
+
NLPProblem,
|
|
14
|
+
SolveResult,
|
|
15
|
+
SolverFactory,
|
|
16
|
+
casadify,
|
|
17
|
+
casadify_forward,
|
|
18
|
+
casadify_reverse,
|
|
19
|
+
generate_initial_guess,
|
|
20
|
+
)
|
|
21
|
+
from septal.jax import sqp
|
|
22
|
+
|
|
23
|
+
__version__ = "0.2.0"
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"__version__",
|
|
27
|
+
"NLPProblem",
|
|
28
|
+
"SolveResult",
|
|
29
|
+
"JaxCasADiEvaluator",
|
|
30
|
+
"JaxCallbackForward",
|
|
31
|
+
"JaxCallbackReverse",
|
|
32
|
+
"casadify",
|
|
33
|
+
"casadify_forward",
|
|
34
|
+
"casadify_reverse",
|
|
35
|
+
"BaseSolver",
|
|
36
|
+
"CasadiSolver",
|
|
37
|
+
"JaxSolver",
|
|
38
|
+
"SolverFactory",
|
|
39
|
+
"generate_initial_guess",
|
|
40
|
+
"sqp",
|
|
41
|
+
"casadax",
|
|
42
|
+
]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
septal.casadax — CasADi/JAX NLP solver backends.
|
|
3
|
+
|
|
4
|
+
Provides a standard schema for nonlinear programmes and two solver backends:
|
|
5
|
+
|
|
6
|
+
* CasADi / IPOPT multi-start (``CasadiSolver``)
|
|
7
|
+
* JAXopt L-BFGS-B multi-start (``JaxSolver``)
|
|
8
|
+
|
|
9
|
+
JAX functions are connected to CasADi via efficient AD callbacks
|
|
10
|
+
(forward-mode ``jvp`` or reverse-mode ``vjp``) exported as
|
|
11
|
+
``casadify_forward`` / ``casadify_reverse`` / ``casadify``.
|
|
12
|
+
|
|
13
|
+
Typical usage (IPOPT)::
|
|
14
|
+
|
|
15
|
+
from septal.casadax import NLPProblem, SolverFactory, casadify_reverse
|
|
16
|
+
|
|
17
|
+
objective_cb = casadify_reverse(my_jax_objective, n_d)
|
|
18
|
+
problem = NLPProblem(objective=objective_cb, bounds=[lb, ub])
|
|
19
|
+
factory = SolverFactory.from_problem(cfg, "general_constrained_nlp", problem)
|
|
20
|
+
result = factory.solve(factory.initial_guess())
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from septal.casadax.callbacks import (
|
|
24
|
+
JaxCallbackForward,
|
|
25
|
+
JaxCallbackReverse,
|
|
26
|
+
JaxCasADiEvaluator,
|
|
27
|
+
casadify,
|
|
28
|
+
casadify_forward,
|
|
29
|
+
casadify_reverse,
|
|
30
|
+
)
|
|
31
|
+
from septal.casadax.factory import SolverFactory
|
|
32
|
+
from septal.casadax.schema import NLPProblem, SolveResult
|
|
33
|
+
from septal.casadax.solvers import BaseSolver, CasadiSolver, JaxSolver
|
|
34
|
+
from septal.casadax.utilities import generate_initial_guess
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
# Schema
|
|
38
|
+
"NLPProblem",
|
|
39
|
+
"SolveResult",
|
|
40
|
+
# Callbacks
|
|
41
|
+
"JaxCasADiEvaluator",
|
|
42
|
+
"JaxCallbackForward",
|
|
43
|
+
"JaxCallbackReverse",
|
|
44
|
+
"casadify",
|
|
45
|
+
"casadify_forward",
|
|
46
|
+
"casadify_reverse",
|
|
47
|
+
# Solvers
|
|
48
|
+
"BaseSolver",
|
|
49
|
+
"CasadiSolver",
|
|
50
|
+
"JaxSolver",
|
|
51
|
+
# Factory
|
|
52
|
+
"SolverFactory",
|
|
53
|
+
# Utilities
|
|
54
|
+
"generate_initial_guess",
|
|
55
|
+
]
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""
|
|
2
|
+
JAX-CasADi callback wrappers.
|
|
3
|
+
|
|
4
|
+
Wraps arbitrary JAX functions so they can be used inside CasADi NLP problems
|
|
5
|
+
while still benefiting from JAX's JIT compilation and automatic differentiation.
|
|
6
|
+
|
|
7
|
+
Two AD modes are provided:
|
|
8
|
+
- Forward mode (JaxCallbackForward / casadify_forward) — preferred when
|
|
9
|
+
n_constraints > n_decision (tall Jacobian).
|
|
10
|
+
- Reverse mode (JaxCallbackReverse / casadify_reverse) — preferred when
|
|
11
|
+
n_constraints <= n_decision (wide Jacobian; e.g. scalar objective).
|
|
12
|
+
|
|
13
|
+
The choice of mode is made automatically by :func:`casadify` based on the
|
|
14
|
+
relative dimensions of the function.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import time
|
|
20
|
+
from collections.abc import Callable
|
|
21
|
+
|
|
22
|
+
import jax.numpy as jnp
|
|
23
|
+
import numpy as np
|
|
24
|
+
from casadi import DM, Callback, Function, Sparsity
|
|
25
|
+
from jax import jit, jvp, vjp
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class JaxCasADiEvaluator(Callback):
|
|
29
|
+
"""Base CasADi Callback that wraps a JIT-compiled JAX function.
|
|
30
|
+
|
|
31
|
+
Performs a single dummy forward pass on construction to determine the
|
|
32
|
+
output shape so CasADi can set up its sparsity patterns.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
functn:
|
|
37
|
+
JAX callable ``f(x)`` where ``x`` has shape ``(nd, 1)``.
|
|
38
|
+
nd:
|
|
39
|
+
Number of input dimensions.
|
|
40
|
+
name:
|
|
41
|
+
CasADi function name (must be unique within a session).
|
|
42
|
+
opts:
|
|
43
|
+
CasADi callback options dict.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
functn: Callable,
|
|
49
|
+
nd: int,
|
|
50
|
+
name: str = "JaxCasADiEvaluator",
|
|
51
|
+
opts: dict | None = None,
|
|
52
|
+
) -> None:
|
|
53
|
+
if opts is None:
|
|
54
|
+
opts = {}
|
|
55
|
+
super().__init__()
|
|
56
|
+
self.nd = nd
|
|
57
|
+
self.refs: list = []
|
|
58
|
+
self._forward_pass = jit(functn)
|
|
59
|
+
|
|
60
|
+
# Determine output shape via a dummy trace
|
|
61
|
+
dummy_x = jnp.zeros((nd, 1))
|
|
62
|
+
dummy_y = self._forward_pass(dummy_x)
|
|
63
|
+
self.output_shape = dummy_y.shape
|
|
64
|
+
self.n_out_dim = self.output_shape[0]
|
|
65
|
+
|
|
66
|
+
self.construct(name, opts)
|
|
67
|
+
|
|
68
|
+
# CasADi interface ----------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
def get_n_in(self) -> int:
|
|
71
|
+
return 1
|
|
72
|
+
|
|
73
|
+
def get_n_out(self) -> int:
|
|
74
|
+
return 1
|
|
75
|
+
|
|
76
|
+
def get_sparsity_in(self, i: int) -> Sparsity:
|
|
77
|
+
assert i == 0
|
|
78
|
+
return Sparsity.dense(self.nd, 1)
|
|
79
|
+
|
|
80
|
+
def get_sparsity_out(self, i: int) -> Sparsity:
|
|
81
|
+
assert i == 0
|
|
82
|
+
return Sparsity.dense(self.n_out_dim, 1)
|
|
83
|
+
|
|
84
|
+
def eval(self, arg): # type: ignore[no-untyped-def]
|
|
85
|
+
x_numpy = np.array(arg[0]).reshape(self.nd, 1)
|
|
86
|
+
x_jax = jnp.array(x_numpy)
|
|
87
|
+
y_jax = jnp.reshape(self._forward_pass(x_jax), (self.n_out_dim, 1))
|
|
88
|
+
return [DM(np.asarray(y_jax))]
|
|
89
|
+
|
|
90
|
+
def has_reverse(self, nadj: int) -> bool:
|
|
91
|
+
return nadj == 0
|
|
92
|
+
|
|
93
|
+
def has_forward(self, nfwd: int) -> bool:
|
|
94
|
+
return nfwd == 0
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class JaxCallbackForward(JaxCasADiEvaluator):
|
|
98
|
+
"""JAX-CasADi callback using *forward-mode* AD (jvp).
|
|
99
|
+
|
|
100
|
+
Preferred when the number of constraints exceeds the number of decision
|
|
101
|
+
variables (tall Jacobian).
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def __init__(
|
|
105
|
+
self,
|
|
106
|
+
functn: Callable,
|
|
107
|
+
nd: int,
|
|
108
|
+
name: str = "JaxCallbackForward",
|
|
109
|
+
opts: dict | None = None,
|
|
110
|
+
) -> None:
|
|
111
|
+
super().__init__(functn, nd, name, opts)
|
|
112
|
+
self.counter = 0
|
|
113
|
+
self.time = 0.0
|
|
114
|
+
self.forward_pass = jit(functn)
|
|
115
|
+
|
|
116
|
+
@jit
|
|
117
|
+
def _fwd(primals, tangents): # type: ignore[no-untyped-def]
|
|
118
|
+
return jvp(self.forward_pass, (primals,), (tangents,))[1]
|
|
119
|
+
|
|
120
|
+
self.forward_sensitivities = _fwd
|
|
121
|
+
|
|
122
|
+
def eval(self, arg): # type: ignore[no-untyped-def]
|
|
123
|
+
self.counter += 1
|
|
124
|
+
t0 = time.time()
|
|
125
|
+
ret = JaxCasADiEvaluator.eval(self, arg)
|
|
126
|
+
self.time += time.time() - t0
|
|
127
|
+
return ret
|
|
128
|
+
|
|
129
|
+
def has_forward(self, nfwd: int) -> bool:
|
|
130
|
+
return nfwd == 1
|
|
131
|
+
|
|
132
|
+
def get_forward(self, nfwd, name, inames, onames, opts): # type: ignore[no-untyped-def]
|
|
133
|
+
assert nfwd == 1
|
|
134
|
+
forward_sens = self.forward_sensitivities
|
|
135
|
+
nd = self.nd
|
|
136
|
+
n_out_dim = self.n_out_dim
|
|
137
|
+
|
|
138
|
+
class ForwardEvaluator(Callback):
|
|
139
|
+
def __init__(self, forward_fn, nd, n_out_dim, name, opts): # type: ignore[no-untyped-def]
|
|
140
|
+
super().__init__()
|
|
141
|
+
self._fwd = forward_fn
|
|
142
|
+
self.nd = nd
|
|
143
|
+
self.n_out_dim = n_out_dim
|
|
144
|
+
self.construct(name, opts)
|
|
145
|
+
|
|
146
|
+
def get_n_in(self) -> int:
|
|
147
|
+
return 3 # x, y (unused), fwd_seed
|
|
148
|
+
|
|
149
|
+
def get_n_out(self) -> int:
|
|
150
|
+
return 1 # fwd_y
|
|
151
|
+
|
|
152
|
+
def get_sparsity_in(self, i): # type: ignore[no-untyped-def]
|
|
153
|
+
if i == 0:
|
|
154
|
+
return Sparsity.dense(self.nd, 1)
|
|
155
|
+
if i == 1:
|
|
156
|
+
return Sparsity.dense(self.n_out_dim, 1)
|
|
157
|
+
if i == 2:
|
|
158
|
+
return Sparsity.dense(self.nd, 1)
|
|
159
|
+
|
|
160
|
+
def get_sparsity_out(self, i): # type: ignore[no-untyped-def]
|
|
161
|
+
return Sparsity.dense(self.n_out_dim, 1)
|
|
162
|
+
|
|
163
|
+
def eval(self, arg): # type: ignore[no-untyped-def]
|
|
164
|
+
x = jnp.array(arg[0]).reshape(self.nd)
|
|
165
|
+
tangents = jnp.array(arg[2]).reshape(self.nd)
|
|
166
|
+
sens = self._fwd(x, tangents)
|
|
167
|
+
return [DM(np.array(sens))]
|
|
168
|
+
|
|
169
|
+
fwd_cb = ForwardEvaluator(forward_sens, nd, n_out_dim, f"{name}_forward", opts)
|
|
170
|
+
self.refs.append(fwd_cb)
|
|
171
|
+
|
|
172
|
+
nominal_in = self.mx_in()
|
|
173
|
+
nominal_out = self.mx_out()
|
|
174
|
+
fwd_seed = self.mx_in()
|
|
175
|
+
|
|
176
|
+
return Function(
|
|
177
|
+
name,
|
|
178
|
+
nominal_in + nominal_out + fwd_seed,
|
|
179
|
+
fwd_cb.call(nominal_in + nominal_out + fwd_seed),
|
|
180
|
+
inames,
|
|
181
|
+
onames,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class JaxCallbackReverse(JaxCasADiEvaluator):
|
|
186
|
+
"""JAX-CasADi callback using *reverse-mode* AD (vjp).
|
|
187
|
+
|
|
188
|
+
Preferred when the number of constraints does not exceed the number of
|
|
189
|
+
decision variables (wide Jacobian; e.g. scalar objective).
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
def __init__(
|
|
193
|
+
self,
|
|
194
|
+
functn: Callable,
|
|
195
|
+
nd: int,
|
|
196
|
+
name: str = "JaxCallbackReverse",
|
|
197
|
+
opts: dict | None = None,
|
|
198
|
+
) -> None:
|
|
199
|
+
super().__init__(functn, nd, name, opts)
|
|
200
|
+
self.counter = 0
|
|
201
|
+
self.time = 0.0
|
|
202
|
+
self.forward_pass = jit(functn)
|
|
203
|
+
|
|
204
|
+
@jit
|
|
205
|
+
def _vjp(primals, tangents): # type: ignore[no-untyped-def]
|
|
206
|
+
return vjp(self._forward_pass, primals)[1](tangents)[0]
|
|
207
|
+
|
|
208
|
+
self.reverse_pass = _vjp
|
|
209
|
+
|
|
210
|
+
def eval(self, arg): # type: ignore[no-untyped-def]
|
|
211
|
+
self.counter += 1
|
|
212
|
+
t0 = time.time()
|
|
213
|
+
ret = JaxCasADiEvaluator.eval(self, arg)
|
|
214
|
+
self.time += time.time() - t0
|
|
215
|
+
return ret
|
|
216
|
+
|
|
217
|
+
def has_reverse(self, nadj: int) -> bool:
|
|
218
|
+
return nadj == 1
|
|
219
|
+
|
|
220
|
+
def get_reverse(self, nadj, name, inames, onames, opts): # type: ignore[no-untyped-def]
|
|
221
|
+
assert nadj == 1
|
|
222
|
+
reverse_pass = self.reverse_pass
|
|
223
|
+
nd = self.nd
|
|
224
|
+
n_out_dim = self.n_out_dim
|
|
225
|
+
|
|
226
|
+
class ReverseEvaluator(Callback):
|
|
227
|
+
def __init__(self, rev_fn, nd, n_out_dim, name, opts): # type: ignore[no-untyped-def]
|
|
228
|
+
super().__init__()
|
|
229
|
+
self._rev = rev_fn
|
|
230
|
+
self.nd = nd
|
|
231
|
+
self.n_out_dim = n_out_dim
|
|
232
|
+
self.construct(name, opts)
|
|
233
|
+
|
|
234
|
+
def get_n_in(self) -> int:
|
|
235
|
+
return 3 # x, y (unused), adj_seed
|
|
236
|
+
|
|
237
|
+
def get_n_out(self) -> int:
|
|
238
|
+
return 1 # grad_x
|
|
239
|
+
|
|
240
|
+
def get_sparsity_in(self, i): # type: ignore[no-untyped-def]
|
|
241
|
+
if i == 0:
|
|
242
|
+
return Sparsity.dense(self.nd, 1)
|
|
243
|
+
if i == 1:
|
|
244
|
+
return Sparsity.dense(self.n_out_dim, 1)
|
|
245
|
+
if i == 2:
|
|
246
|
+
return Sparsity.dense(self.n_out_dim, 1)
|
|
247
|
+
|
|
248
|
+
def get_sparsity_out(self, i): # type: ignore[no-untyped-def]
|
|
249
|
+
return Sparsity.dense(self.nd, 1)
|
|
250
|
+
|
|
251
|
+
def eval(self, arg): # type: ignore[no-untyped-def]
|
|
252
|
+
x = jnp.array(arg[0]).reshape(self.nd, 1)
|
|
253
|
+
adj_seed = jnp.array(arg[2]).reshape(self.n_out_dim, 1)
|
|
254
|
+
grad = self._rev(x, adj_seed)
|
|
255
|
+
return [DM(np.array(grad))]
|
|
256
|
+
|
|
257
|
+
rev_cb = ReverseEvaluator(reverse_pass, nd, n_out_dim, f"{name}_reverse", opts)
|
|
258
|
+
self.refs.append(rev_cb)
|
|
259
|
+
|
|
260
|
+
nominal_in = self.mx_in()
|
|
261
|
+
nominal_out = self.mx_out()
|
|
262
|
+
rev_seed = self.mx_out()
|
|
263
|
+
|
|
264
|
+
return Function(
|
|
265
|
+
name,
|
|
266
|
+
nominal_in + nominal_out + rev_seed,
|
|
267
|
+
rev_cb.call(nominal_in + nominal_out + rev_seed),
|
|
268
|
+
inames,
|
|
269
|
+
onames,
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
# Factory helpers ----------------------------------------------------------------
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def casadify_forward(functn: Callable, nd: int) -> JaxCallbackForward:
|
|
277
|
+
"""Wrap a JAX function with forward-mode AD for use in CasADi."""
|
|
278
|
+
return JaxCallbackForward(functn, nd)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def casadify_reverse(functn: Callable, nd: int) -> JaxCallbackReverse:
|
|
282
|
+
"""Wrap a JAX function with reverse-mode AD for use in CasADi."""
|
|
283
|
+
return JaxCallbackReverse(functn, nd)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def casadify(functn: Callable, nd: int, n_out: int) -> JaxCasADiEvaluator:
|
|
287
|
+
"""Auto-select forward or reverse mode based on Jacobian shape.
|
|
288
|
+
|
|
289
|
+
Parameters
|
|
290
|
+
----------
|
|
291
|
+
functn:
|
|
292
|
+
JAX function ``f(x)`` where ``x`` has shape ``(nd, 1)``.
|
|
293
|
+
nd:
|
|
294
|
+
Number of input dimensions.
|
|
295
|
+
n_out:
|
|
296
|
+
Number of output dimensions (rows of the Jacobian).
|
|
297
|
+
|
|
298
|
+
Returns
|
|
299
|
+
-------
|
|
300
|
+
JaxCallbackForward or JaxCallbackReverse
|
|
301
|
+
"""
|
|
302
|
+
if n_out > nd:
|
|
303
|
+
return casadify_forward(functn, nd)
|
|
304
|
+
return casadify_reverse(functn, nd)
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SolverFactory — creates the right solver backend from a configuration key.
|
|
3
|
+
|
|
4
|
+
Supported solver types
|
|
5
|
+
----------------------
|
|
6
|
+
``"general_constrained_nlp"``
|
|
7
|
+
CasADi / IPOPT multi-start (via :class:`~septal.casadax.solvers.CasadiSolver`).
|
|
8
|
+
|
|
9
|
+
``"box_constrained_nlp"``
|
|
10
|
+
JAXopt L-BFGS-B multi-start (via :class:`~septal.casadax.solvers.JaxSolver`).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from collections.abc import Callable
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from septal.casadax.schema import NLPProblem, SolveResult
|
|
19
|
+
from septal.casadax.solvers import BaseSolver, CasadiSolver, JaxSolver
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class SolverFactory:
|
|
23
|
+
"""Factory that instantiates and holds a solver for a given NLP.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
cfg:
|
|
28
|
+
Sub-config for the solver (must contain ``n_starts``, ``max_solution_time``,
|
|
29
|
+
``jax_opt_options.error_tol``, etc.).
|
|
30
|
+
solver_type:
|
|
31
|
+
One of ``"general_constrained_nlp"`` or ``"box_constrained_nlp"``.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, cfg: Any, solver_type: str) -> None:
|
|
35
|
+
self.cfg = cfg
|
|
36
|
+
self.solver_type = solver_type
|
|
37
|
+
self.solver: BaseSolver | None = None
|
|
38
|
+
self.objective_func: Callable | None = None
|
|
39
|
+
self.constraints_func: Callable | None = None
|
|
40
|
+
self.bounds: list[Any] | None = None
|
|
41
|
+
self.constraint_lhs: Any | None = None
|
|
42
|
+
self.constraint_rhs: Any | None = None
|
|
43
|
+
|
|
44
|
+
# ------------------------------------------------------------------
|
|
45
|
+
# Class-level constructor (preferred entry point)
|
|
46
|
+
# ------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def from_problem(
|
|
50
|
+
cls, cfg: Any, solver_type: str, problem: NLPProblem
|
|
51
|
+
) -> SolverFactory:
|
|
52
|
+
"""Build a factory and construct the solver from an :class:`~septal.casadax.schema.NLPProblem`.
|
|
53
|
+
|
|
54
|
+
Parameters
|
|
55
|
+
----------
|
|
56
|
+
cfg:
|
|
57
|
+
Solver configuration.
|
|
58
|
+
solver_type:
|
|
59
|
+
``"general_constrained_nlp"`` or ``"box_constrained_nlp"``.
|
|
60
|
+
problem:
|
|
61
|
+
The NLP to solve.
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
SolverFactory
|
|
66
|
+
Factory with :attr:`solver` ready to call.
|
|
67
|
+
"""
|
|
68
|
+
factory = cls(cfg, solver_type)
|
|
69
|
+
factory.load_objective(problem.objective)
|
|
70
|
+
factory.load_bounds(problem.bounds)
|
|
71
|
+
factory.load_constraints(
|
|
72
|
+
problem.constraints, problem.constraint_lhs, problem.constraint_rhs
|
|
73
|
+
)
|
|
74
|
+
factory.construct_solver()
|
|
75
|
+
return factory
|
|
76
|
+
|
|
77
|
+
@classmethod
|
|
78
|
+
def from_method(
|
|
79
|
+
cls,
|
|
80
|
+
cfg: Any,
|
|
81
|
+
solver_type: str,
|
|
82
|
+
objective_func: Callable,
|
|
83
|
+
bounds: list[Any],
|
|
84
|
+
constraints_func: Callable | None = None,
|
|
85
|
+
constraint_lhs: Any | None = None,
|
|
86
|
+
constraint_rhs: Any | None = None,
|
|
87
|
+
) -> SolverFactory:
|
|
88
|
+
"""Build a factory from individual components (legacy / low-level entry).
|
|
89
|
+
|
|
90
|
+
Parameters
|
|
91
|
+
----------
|
|
92
|
+
cfg:
|
|
93
|
+
Solver configuration.
|
|
94
|
+
solver_type:
|
|
95
|
+
Backend type.
|
|
96
|
+
objective_func:
|
|
97
|
+
Pre-built objective (CasADi callback or JAX callable).
|
|
98
|
+
bounds:
|
|
99
|
+
``[lb, ub]``.
|
|
100
|
+
constraints_func:
|
|
101
|
+
Optional pre-built constraint callback.
|
|
102
|
+
constraint_lhs, constraint_rhs:
|
|
103
|
+
Bounds on ``g(x)``.
|
|
104
|
+
|
|
105
|
+
Returns
|
|
106
|
+
-------
|
|
107
|
+
SolverFactory
|
|
108
|
+
"""
|
|
109
|
+
factory = cls(cfg, solver_type)
|
|
110
|
+
factory.load_objective(objective_func)
|
|
111
|
+
factory.load_bounds(bounds)
|
|
112
|
+
factory.load_constraints(constraints_func, constraint_lhs, constraint_rhs)
|
|
113
|
+
factory.construct_solver()
|
|
114
|
+
return factory
|
|
115
|
+
|
|
116
|
+
# ------------------------------------------------------------------
|
|
117
|
+
# Builder steps
|
|
118
|
+
# ------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
def load_objective(self, objective_func: Callable) -> None:
|
|
121
|
+
self.objective_func = objective_func
|
|
122
|
+
|
|
123
|
+
def load_bounds(self, bounds: list[Any]) -> None:
|
|
124
|
+
self.bounds = bounds
|
|
125
|
+
|
|
126
|
+
def load_constraints(
|
|
127
|
+
self,
|
|
128
|
+
constraints_func: Callable | None,
|
|
129
|
+
lhs: Any | None = None,
|
|
130
|
+
rhs: Any | None = None,
|
|
131
|
+
) -> None:
|
|
132
|
+
self.constraints_func = constraints_func
|
|
133
|
+
self.constraint_lhs = lhs
|
|
134
|
+
self.constraint_rhs = rhs
|
|
135
|
+
|
|
136
|
+
def construct_solver(self) -> None:
|
|
137
|
+
if self.objective_func is None:
|
|
138
|
+
raise ValueError("objective_func must be set before construct_solver().")
|
|
139
|
+
if self.bounds is None:
|
|
140
|
+
raise ValueError("bounds must be set before construct_solver().")
|
|
141
|
+
|
|
142
|
+
if self.solver_type == "general_constrained_nlp":
|
|
143
|
+
self.solver = CasadiSolver(
|
|
144
|
+
self.cfg,
|
|
145
|
+
self.objective_func,
|
|
146
|
+
self.bounds,
|
|
147
|
+
constraints_fn=self.constraints_func,
|
|
148
|
+
constraint_lhs=self.constraint_lhs,
|
|
149
|
+
constraint_rhs=self.constraint_rhs,
|
|
150
|
+
)
|
|
151
|
+
elif self.solver_type == "box_constrained_nlp":
|
|
152
|
+
self.solver = JaxSolver(self.cfg, self.objective_func, self.bounds)
|
|
153
|
+
else:
|
|
154
|
+
raise NotImplementedError(
|
|
155
|
+
f"Solver type '{self.solver_type}' is not implemented."
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
# ------------------------------------------------------------------
|
|
159
|
+
# Solve interface
|
|
160
|
+
# ------------------------------------------------------------------
|
|
161
|
+
|
|
162
|
+
def initial_guess(self) -> Any:
|
|
163
|
+
assert self.solver is not None, "Call construct_solver() first."
|
|
164
|
+
return self.solver.initial_guess()
|
|
165
|
+
|
|
166
|
+
def solve(self, initial_guesses: Any) -> SolveResult:
|
|
167
|
+
assert self.solver is not None, "Call construct_solver() first."
|
|
168
|
+
return self.solver.solve(initial_guesses)
|
|
169
|
+
|
|
170
|
+
def __call__(self, initial_guesses: Any) -> SolveResult:
|
|
171
|
+
return self.solve(initial_guesses)
|
septal/casadax/schema.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Standard NLP schema for septal.
|
|
3
|
+
|
|
4
|
+
Defines the input problem (NLPProblem) and output result (SolveResult)
|
|
5
|
+
dataclasses used across all solver backends.
|
|
6
|
+
|
|
7
|
+
NLP standard form:
|
|
8
|
+
min f(x)
|
|
9
|
+
s.t. lhs <= g(x) <= rhs
|
|
10
|
+
lb <= x <= ub
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from collections.abc import Callable
|
|
16
|
+
from dataclasses import dataclass
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class NLPProblem:
|
|
22
|
+
"""Describes a nonlinear programme in standard form.
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
objective:
|
|
27
|
+
JAX function ``f(x) -> scalar`` (or CasADi-compatible callback).
|
|
28
|
+
Will be wrapped for the chosen backend automatically.
|
|
29
|
+
bounds:
|
|
30
|
+
``[lb, ub]`` — list/tuple of two JAX/NumPy arrays, each of shape
|
|
31
|
+
``(n_decision,)``.
|
|
32
|
+
constraints:
|
|
33
|
+
Optional JAX function ``g(x) -> (n_g,)`` vector. Paired with
|
|
34
|
+
*constraint_lhs* / *constraint_rhs* to form ``lhs <= g(x) <= rhs``.
|
|
35
|
+
constraint_lhs:
|
|
36
|
+
Lower bound on ``g(x)``; shape ``(n_g,)`` or ``(n_g, 1)``.
|
|
37
|
+
``-inf`` entries are unconstrained from below.
|
|
38
|
+
constraint_rhs:
|
|
39
|
+
Upper bound on ``g(x)``; shape ``(n_g,)`` or ``(n_g, 1)``.
|
|
40
|
+
Typically ``0`` for inequality constraints ``g(x) <= 0``.
|
|
41
|
+
n_decision:
|
|
42
|
+
Dimension of ``x``. Inferred from *bounds* if not supplied.
|
|
43
|
+
n_starts:
|
|
44
|
+
Number of multi-start initial guesses to generate / evaluate.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
objective: Callable
|
|
48
|
+
bounds: list[Any]
|
|
49
|
+
constraints: Callable | None = None
|
|
50
|
+
constraint_lhs: Any | None = None
|
|
51
|
+
constraint_rhs: Any | None = None
|
|
52
|
+
n_decision: int | None = None
|
|
53
|
+
n_starts: int = 5
|
|
54
|
+
|
|
55
|
+
def __post_init__(self) -> None:
|
|
56
|
+
if self.n_decision is None:
|
|
57
|
+
self.n_decision = len(self.bounds[0])
|
|
58
|
+
|
|
59
|
+
# Convenience ----------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def lb(self) -> Any:
|
|
63
|
+
return self.bounds[0]
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def ub(self) -> Any:
|
|
67
|
+
return self.bounds[1]
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def has_constraints(self) -> bool:
|
|
71
|
+
return self.constraints is not None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass
|
|
75
|
+
class SolveResult:
|
|
76
|
+
"""Result returned by every septal solver.
|
|
77
|
+
|
|
78
|
+
Parameters
|
|
79
|
+
----------
|
|
80
|
+
success:
|
|
81
|
+
``True`` if the solver converged to a (locally) optimal feasible point.
|
|
82
|
+
objective:
|
|
83
|
+
Optimal objective value ``f(x*)``.
|
|
84
|
+
decision_variables:
|
|
85
|
+
Optimal primal solution ``x*``.
|
|
86
|
+
constraints:
|
|
87
|
+
Value of ``g(x*)``; ``None`` when the problem has no constraints.
|
|
88
|
+
message:
|
|
89
|
+
Solver status string (e.g. IPOPT's *return_status*).
|
|
90
|
+
timing:
|
|
91
|
+
Wall-clock time spent in the solver (seconds).
|
|
92
|
+
n_solves:
|
|
93
|
+
Number of multi-start solves that returned feasible solutions.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
success: bool
|
|
97
|
+
objective: Any
|
|
98
|
+
decision_variables: Any
|
|
99
|
+
constraints: Any | None = None
|
|
100
|
+
message: str = ""
|
|
101
|
+
timing: float = 0.0
|
|
102
|
+
n_solves: int = 0
|