kkt-hardnet 1.0.1__tar.gz
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.
- kkt_hardnet-1.0.1/LICENSE +21 -0
- kkt_hardnet-1.0.1/PKG-INFO +156 -0
- kkt_hardnet-1.0.1/README.md +133 -0
- kkt_hardnet-1.0.1/pyproject.toml +39 -0
- kkt_hardnet-1.0.1/setup.cfg +4 -0
- kkt_hardnet-1.0.1/src/jaxmodel/__init__.py +52 -0
- kkt_hardnet-1.0.1/src/jaxmodel/approximations.py +181 -0
- kkt_hardnet-1.0.1/src/jaxmodel/autodiff.py +24 -0
- kkt_hardnet-1.0.1/src/jaxmodel/blocks.py +164 -0
- kkt_hardnet-1.0.1/src/jaxmodel/bounds.py +11 -0
- kkt_hardnet-1.0.1/src/jaxmodel/builder.py +67 -0
- kkt_hardnet-1.0.1/src/jaxmodel/constraints.py +236 -0
- kkt_hardnet-1.0.1/src/jaxmodel/highlevel.py +286 -0
- kkt_hardnet-1.0.1/src/jaxmodel/model.py +293 -0
- kkt_hardnet-1.0.1/src/jaxmodel/objective.py +68 -0
- kkt_hardnet-1.0.1/src/jaxmodel/parameters.py +30 -0
- kkt_hardnet-1.0.1/src/jaxmodel/types.py +11 -0
- kkt_hardnet-1.0.1/src/jaxmodel/variables.py +97 -0
- kkt_hardnet-1.0.1/src/kkt_hardnet.egg-info/PKG-INFO +156 -0
- kkt_hardnet-1.0.1/src/kkt_hardnet.egg-info/SOURCES.txt +27 -0
- kkt_hardnet-1.0.1/src/kkt_hardnet.egg-info/dependency_links.txt +1 -0
- kkt_hardnet-1.0.1/src/kkt_hardnet.egg-info/requires.txt +13 -0
- kkt_hardnet-1.0.1/src/kkt_hardnet.egg-info/top_level.txt +2 -0
- kkt_hardnet-1.0.1/src/kkthn/__init__.py +7 -0
- kkt_hardnet-1.0.1/src/kkthn/backbone.py +36 -0
- kkt_hardnet-1.0.1/src/kkthn/builder.py +751 -0
- kkt_hardnet-1.0.1/src/kkthn/projection.py +247 -0
- kkt_hardnet-1.0.1/src/kkthn/string_problem.py +171 -0
- kkt_hardnet-1.0.1/src/kkthn/training.py +700 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MM Faruque Hasan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kkt-hardnet
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: KKT-HardNet: Physics-constrained neural networks with hard nonlinear equality and inequality constraint satisfaction guarantees
|
|
5
|
+
Author-email: Rahul Golder <rahulgolder8420@tamu.edu>, Bimol Nath Roy <bimolnathroy@tamu.edu>, Ashfaq Iftakher <iftakher@tamu.edu>, MM Faruque Hasan <hasan@tamu.edu>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: jax==0.4.30
|
|
11
|
+
Requires-Dist: jaxlib==0.4.30
|
|
12
|
+
Requires-Dist: flax==0.8.5
|
|
13
|
+
Requires-Dist: optax==0.2.4
|
|
14
|
+
Requires-Dist: chex==0.1.90
|
|
15
|
+
Requires-Dist: numpy==1.26.4
|
|
16
|
+
Requires-Dist: scipy==1.13.1
|
|
17
|
+
Requires-Dist: pandas==2.2.3
|
|
18
|
+
Requires-Dist: matplotlib==3.9.4
|
|
19
|
+
Provides-Extra: cuda12
|
|
20
|
+
Requires-Dist: jax[cuda12]==0.4.30; extra == "cuda12"
|
|
21
|
+
Requires-Dist: jaxlib==0.4.30; extra == "cuda12"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# kkt-hardnet
|
|
25
|
+
|
|
26
|
+
`kkt-hardnet` is the publishable Python package for KKT-HardNet.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
Editable install from this repository:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install -e kkthn
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Editable install with CUDA 12 support:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install -e "kkthn[cuda12]"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or use pip after publishing:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install kkt-hardnet
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install "kkt-hardnet[cuda12]"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Import
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from kkthn import KKTHardNet
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Core Methods
|
|
59
|
+
|
|
60
|
+
- `model()` for supervised surrogate learning from `parameters.csv` and `variables.csv`
|
|
61
|
+
- `optimize()` for unsupervised optimization from `parameters.csv`
|
|
62
|
+
- `estimate()` for inverse parameter estimation from `parameters.csv` and `variables.csv`
|
|
63
|
+
- `load(metadata_path)` to reload a trained run
|
|
64
|
+
- `predict(x)` to infer variables for new parameter values
|
|
65
|
+
|
|
66
|
+
## Packaging
|
|
67
|
+
|
|
68
|
+
The PyPI project name is `kkt-hardnet`, while the import path remains `kkthn`. The default install is CPU-oriented; CUDA support is selected explicitly with the `cuda12` extra instead of automatic device detection.
|
|
69
|
+
|
|
70
|
+
# Modeling Workflow
|
|
71
|
+
|
|
72
|
+
Use `KKTHardNet` to define symbolic constrained problems with:
|
|
73
|
+
|
|
74
|
+
- named parameters, decision variables, and optional inverse parameters
|
|
75
|
+
- equality and inequality constraints written as Python expressions
|
|
76
|
+
- optional objectives for optimization tasks
|
|
77
|
+
- hard KKT projection during training and inference
|
|
78
|
+
- saved run artifacts including weights, predictions, history, summary, and metadata
|
|
79
|
+
|
|
80
|
+
Available methods:
|
|
81
|
+
|
|
82
|
+
- `dataset(parameters=..., variables=...)` to attach training data
|
|
83
|
+
- `model()` for supervised surrogate learning
|
|
84
|
+
- `optimize()` for unsupervised optimization
|
|
85
|
+
- `estimate()` for inverse parameter estimation
|
|
86
|
+
- `load(metadata_path)` to reload a trained run
|
|
87
|
+
- `predict(x)` to evaluate a trained or loaded model on new inputs
|
|
88
|
+
|
|
89
|
+
The example below shows one complete workflow. The CSV column names must match the declared parameter and variable names. Each run writes `<model_name>_<timestamp>/` with `parameters.csv`, optional `variables.csv`, `history.csv`, `predictions.csv`, `model_weights.npz`, `summary.json`, and `metadata.json`.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from kkthn import KKTHardNet
|
|
93
|
+
|
|
94
|
+
TRAIN = {
|
|
95
|
+
"epochs": 1200,
|
|
96
|
+
"batch_size": 32,
|
|
97
|
+
"learning_rate": 1e-3,
|
|
98
|
+
"train_frac": 0.8,
|
|
99
|
+
"hidden_size": 64,
|
|
100
|
+
"hidden_layers": 2,
|
|
101
|
+
"seed": 42,
|
|
102
|
+
"dtype": "float64",
|
|
103
|
+
"print_every": 1,
|
|
104
|
+
"newton_step_length": 0.5,
|
|
105
|
+
"newton_tol": 1e-6,
|
|
106
|
+
"newton_reg_factor": 1e-3,
|
|
107
|
+
"max_newton_iter": 30,
|
|
108
|
+
"max_backtrack_iter": 10,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
# Build a symbolic problem.
|
|
112
|
+
model = KKTHardNet(name="demo_model", train=TRAIN)
|
|
113
|
+
x = model.add_parameter(["x1", "x2"])
|
|
114
|
+
theta = model.add_inverse_parameter(["a0", "a1"], init_value=[10.0, -10.0])
|
|
115
|
+
y = model.add_variable(["y1", "y2", "y3"])
|
|
116
|
+
|
|
117
|
+
# Objectives are optional for surrogate modeling and inverse estimation,
|
|
118
|
+
# but required for optimize().
|
|
119
|
+
model.objective = 0.5 * (y.y1**2 + y.y2**2 + y.y3**2)
|
|
120
|
+
model.constraints.add(
|
|
121
|
+
theta.a0 * y.y1 + y.y2 - x.x1 == 0,
|
|
122
|
+
y.y2 - theta.a1 * y.y3 - x.x2 == 0,
|
|
123
|
+
y.y1**2 + y.y3**2 <= 2.0,
|
|
124
|
+
y.y1 >= 0,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# Attach data.
|
|
128
|
+
# For model() or estimate(): provide both parameters and variables.
|
|
129
|
+
# For optimize(): provide only parameters.
|
|
130
|
+
model.dataset(parameters="parameters.csv", variables="variables.csv")
|
|
131
|
+
|
|
132
|
+
# Choose one training mode.
|
|
133
|
+
surrogate_result = model.model()
|
|
134
|
+
# optimize_result = model.optimize()
|
|
135
|
+
# estimate_result = model.estimate()
|
|
136
|
+
|
|
137
|
+
# Reload a saved run later and predict on a new parameter vector.
|
|
138
|
+
reloaded = KKTHardNet()
|
|
139
|
+
reloaded.load("demo_model_20260414_120000/metadata.json")
|
|
140
|
+
prediction = reloaded.predict([1.0, 2.0])
|
|
141
|
+
```
|
|
142
|
+
⚠️ Please cite our work if you use this code in your research.
|
|
143
|
+
Citation formats are provided below.
|
|
144
|
+
|
|
145
|
+
arXiv Preprint: https://arxiv.org/pdf/2507.08124
|
|
146
|
+
Journal: https://doi.org/10.1016/j.compchemeng.2025.109418
|
|
147
|
+
|
|
148
|
+
```bibtex
|
|
149
|
+
@article{iftakher2025physics,
|
|
150
|
+
title={Physics-informed neural networks with hard nonlinear equality and inequality constraints},
|
|
151
|
+
author={Iftakher, Ashfaq and Golder, Rahul and Nath Roy, Bimol and Hasan, MM Faruque},
|
|
152
|
+
journal={Computers \& Chemical Engineering},
|
|
153
|
+
pages={109418},
|
|
154
|
+
year={2025},
|
|
155
|
+
publisher={Elsevier}
|
|
156
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# kkt-hardnet
|
|
2
|
+
|
|
3
|
+
`kkt-hardnet` is the publishable Python package for KKT-HardNet.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Editable install from this repository:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install -e kkthn
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Editable install with CUDA 12 support:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install -e "kkthn[cuda12]"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or use pip after publishing:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install kkt-hardnet
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install "kkt-hardnet[cuda12]"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Import
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from kkthn import KKTHardNet
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Core Methods
|
|
36
|
+
|
|
37
|
+
- `model()` for supervised surrogate learning from `parameters.csv` and `variables.csv`
|
|
38
|
+
- `optimize()` for unsupervised optimization from `parameters.csv`
|
|
39
|
+
- `estimate()` for inverse parameter estimation from `parameters.csv` and `variables.csv`
|
|
40
|
+
- `load(metadata_path)` to reload a trained run
|
|
41
|
+
- `predict(x)` to infer variables for new parameter values
|
|
42
|
+
|
|
43
|
+
## Packaging
|
|
44
|
+
|
|
45
|
+
The PyPI project name is `kkt-hardnet`, while the import path remains `kkthn`. The default install is CPU-oriented; CUDA support is selected explicitly with the `cuda12` extra instead of automatic device detection.
|
|
46
|
+
|
|
47
|
+
# Modeling Workflow
|
|
48
|
+
|
|
49
|
+
Use `KKTHardNet` to define symbolic constrained problems with:
|
|
50
|
+
|
|
51
|
+
- named parameters, decision variables, and optional inverse parameters
|
|
52
|
+
- equality and inequality constraints written as Python expressions
|
|
53
|
+
- optional objectives for optimization tasks
|
|
54
|
+
- hard KKT projection during training and inference
|
|
55
|
+
- saved run artifacts including weights, predictions, history, summary, and metadata
|
|
56
|
+
|
|
57
|
+
Available methods:
|
|
58
|
+
|
|
59
|
+
- `dataset(parameters=..., variables=...)` to attach training data
|
|
60
|
+
- `model()` for supervised surrogate learning
|
|
61
|
+
- `optimize()` for unsupervised optimization
|
|
62
|
+
- `estimate()` for inverse parameter estimation
|
|
63
|
+
- `load(metadata_path)` to reload a trained run
|
|
64
|
+
- `predict(x)` to evaluate a trained or loaded model on new inputs
|
|
65
|
+
|
|
66
|
+
The example below shows one complete workflow. The CSV column names must match the declared parameter and variable names. Each run writes `<model_name>_<timestamp>/` with `parameters.csv`, optional `variables.csv`, `history.csv`, `predictions.csv`, `model_weights.npz`, `summary.json`, and `metadata.json`.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from kkthn import KKTHardNet
|
|
70
|
+
|
|
71
|
+
TRAIN = {
|
|
72
|
+
"epochs": 1200,
|
|
73
|
+
"batch_size": 32,
|
|
74
|
+
"learning_rate": 1e-3,
|
|
75
|
+
"train_frac": 0.8,
|
|
76
|
+
"hidden_size": 64,
|
|
77
|
+
"hidden_layers": 2,
|
|
78
|
+
"seed": 42,
|
|
79
|
+
"dtype": "float64",
|
|
80
|
+
"print_every": 1,
|
|
81
|
+
"newton_step_length": 0.5,
|
|
82
|
+
"newton_tol": 1e-6,
|
|
83
|
+
"newton_reg_factor": 1e-3,
|
|
84
|
+
"max_newton_iter": 30,
|
|
85
|
+
"max_backtrack_iter": 10,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
# Build a symbolic problem.
|
|
89
|
+
model = KKTHardNet(name="demo_model", train=TRAIN)
|
|
90
|
+
x = model.add_parameter(["x1", "x2"])
|
|
91
|
+
theta = model.add_inverse_parameter(["a0", "a1"], init_value=[10.0, -10.0])
|
|
92
|
+
y = model.add_variable(["y1", "y2", "y3"])
|
|
93
|
+
|
|
94
|
+
# Objectives are optional for surrogate modeling and inverse estimation,
|
|
95
|
+
# but required for optimize().
|
|
96
|
+
model.objective = 0.5 * (y.y1**2 + y.y2**2 + y.y3**2)
|
|
97
|
+
model.constraints.add(
|
|
98
|
+
theta.a0 * y.y1 + y.y2 - x.x1 == 0,
|
|
99
|
+
y.y2 - theta.a1 * y.y3 - x.x2 == 0,
|
|
100
|
+
y.y1**2 + y.y3**2 <= 2.0,
|
|
101
|
+
y.y1 >= 0,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# Attach data.
|
|
105
|
+
# For model() or estimate(): provide both parameters and variables.
|
|
106
|
+
# For optimize(): provide only parameters.
|
|
107
|
+
model.dataset(parameters="parameters.csv", variables="variables.csv")
|
|
108
|
+
|
|
109
|
+
# Choose one training mode.
|
|
110
|
+
surrogate_result = model.model()
|
|
111
|
+
# optimize_result = model.optimize()
|
|
112
|
+
# estimate_result = model.estimate()
|
|
113
|
+
|
|
114
|
+
# Reload a saved run later and predict on a new parameter vector.
|
|
115
|
+
reloaded = KKTHardNet()
|
|
116
|
+
reloaded.load("demo_model_20260414_120000/metadata.json")
|
|
117
|
+
prediction = reloaded.predict([1.0, 2.0])
|
|
118
|
+
```
|
|
119
|
+
⚠️ Please cite our work if you use this code in your research.
|
|
120
|
+
Citation formats are provided below.
|
|
121
|
+
|
|
122
|
+
arXiv Preprint: https://arxiv.org/pdf/2507.08124
|
|
123
|
+
Journal: https://doi.org/10.1016/j.compchemeng.2025.109418
|
|
124
|
+
|
|
125
|
+
```bibtex
|
|
126
|
+
@article{iftakher2025physics,
|
|
127
|
+
title={Physics-informed neural networks with hard nonlinear equality and inequality constraints},
|
|
128
|
+
author={Iftakher, Ashfaq and Golder, Rahul and Nath Roy, Bimol and Hasan, MM Faruque},
|
|
129
|
+
journal={Computers \& Chemical Engineering},
|
|
130
|
+
pages={109418},
|
|
131
|
+
year={2025},
|
|
132
|
+
publisher={Elsevier}
|
|
133
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "kkt-hardnet"
|
|
7
|
+
version = "1.0.1"
|
|
8
|
+
description = "KKT-HardNet: Physics-constrained neural networks with hard nonlinear equality and inequality constraint satisfaction guarantees"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [{name = "Rahul Golder", email = "rahulgolder8420@tamu.edu"},
|
|
13
|
+
{name = "Bimol Nath Roy", email = "bimolnathroy@tamu.edu"},
|
|
14
|
+
{name = "Ashfaq Iftakher", email = "iftakher@tamu.edu"},
|
|
15
|
+
{name = "MM Faruque Hasan", email = "hasan@tamu.edu"}
|
|
16
|
+
]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"jax==0.4.30",
|
|
19
|
+
"jaxlib==0.4.30",
|
|
20
|
+
"flax==0.8.5",
|
|
21
|
+
"optax==0.2.4",
|
|
22
|
+
"chex==0.1.90",
|
|
23
|
+
"numpy==1.26.4",
|
|
24
|
+
"scipy==1.13.1",
|
|
25
|
+
"pandas==2.2.3",
|
|
26
|
+
"matplotlib==3.9.4",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
cuda12 = [
|
|
31
|
+
"jax[cuda12]==0.4.30",
|
|
32
|
+
"jaxlib==0.4.30",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.setuptools]
|
|
36
|
+
package-dir = {"" = "src"}
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.packages.find]
|
|
39
|
+
where = ["src"]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from .variables import VariableBuilder, VariableSpec
|
|
2
|
+
from .parameters import ParameterSpec
|
|
3
|
+
from .bounds import BoundSpec
|
|
4
|
+
from .builder import NLPBuilder
|
|
5
|
+
from .highlevel import HighLevelNLPBuilder
|
|
6
|
+
from .objective import QuadraticObjective
|
|
7
|
+
from .constraints import (
|
|
8
|
+
ConstraintEntry,
|
|
9
|
+
eq_scalar,
|
|
10
|
+
ineq_scalar,
|
|
11
|
+
eq_block,
|
|
12
|
+
ineq_block,
|
|
13
|
+
quadratic_eq_scalar,
|
|
14
|
+
quadratic_ineq_scalar,
|
|
15
|
+
)
|
|
16
|
+
from .blocks import (
|
|
17
|
+
affine_eq_from_parts,
|
|
18
|
+
affine_ineq_from_parts,
|
|
19
|
+
nonlinear_eq_from_parts,
|
|
20
|
+
nonlinear_ineq_from_parts,
|
|
21
|
+
)
|
|
22
|
+
from .approximations import (
|
|
23
|
+
LinearizationData,
|
|
24
|
+
QuadraticObjectiveData,
|
|
25
|
+
SQPSubproblemData,
|
|
26
|
+
)
|
|
27
|
+
from .model import JaxNLPModel
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"VariableBuilder",
|
|
31
|
+
"VariableSpec",
|
|
32
|
+
"ParameterSpec",
|
|
33
|
+
"BoundSpec",
|
|
34
|
+
"NLPBuilder",
|
|
35
|
+
"HighLevelNLPBuilder",
|
|
36
|
+
"QuadraticObjective",
|
|
37
|
+
"ConstraintEntry",
|
|
38
|
+
"eq_scalar",
|
|
39
|
+
"ineq_scalar",
|
|
40
|
+
"eq_block",
|
|
41
|
+
"ineq_block",
|
|
42
|
+
"quadratic_eq_scalar",
|
|
43
|
+
"quadratic_ineq_scalar",
|
|
44
|
+
"affine_eq_from_parts",
|
|
45
|
+
"affine_ineq_from_parts",
|
|
46
|
+
"nonlinear_eq_from_parts",
|
|
47
|
+
"nonlinear_ineq_from_parts",
|
|
48
|
+
"LinearizationData",
|
|
49
|
+
"QuadraticObjectiveData",
|
|
50
|
+
"SQPSubproblemData",
|
|
51
|
+
"JaxNLPModel",
|
|
52
|
+
]
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import jax.numpy as jnp
|
|
2
|
+
from typing import NamedTuple, Optional
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class LinearizationData(NamedTuple):
|
|
6
|
+
A: jnp.ndarray
|
|
7
|
+
b: jnp.ndarray
|
|
8
|
+
C: jnp.ndarray
|
|
9
|
+
d: jnp.ndarray
|
|
10
|
+
h_val: jnp.ndarray
|
|
11
|
+
g_val: jnp.ndarray
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class QuadraticObjectiveData(NamedTuple):
|
|
15
|
+
grad_f: jnp.ndarray
|
|
16
|
+
Q: jnp.ndarray
|
|
17
|
+
Q_diag: jnp.ndarray
|
|
18
|
+
c: jnp.ndarray
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class SQPSubproblemData(NamedTuple):
|
|
22
|
+
objective: QuadraticObjectiveData
|
|
23
|
+
constraints: LinearizationData
|
|
24
|
+
l: Optional[jnp.ndarray]
|
|
25
|
+
u: Optional[jnp.ndarray]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def linearize_constraints_data(eq_fun, ineq_fun, jac_eq, jac_ineq, params, y):
|
|
29
|
+
A = jac_eq(y, params)
|
|
30
|
+
h = eq_fun(params, y)
|
|
31
|
+
b = A @ y - h
|
|
32
|
+
|
|
33
|
+
C = jac_ineq(y, params)
|
|
34
|
+
g = ineq_fun(params, y)
|
|
35
|
+
d = C @ y - g
|
|
36
|
+
|
|
37
|
+
return LinearizationData(A=A, b=b, C=C, d=d, h_val=h, g_val=g)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def linearize_constraints(eq_fun, ineq_fun, jac_eq, jac_ineq, params, y):
|
|
41
|
+
data = linearize_constraints_data(
|
|
42
|
+
eq_fun=eq_fun,
|
|
43
|
+
ineq_fun=ineq_fun,
|
|
44
|
+
jac_eq=jac_eq,
|
|
45
|
+
jac_ineq=jac_ineq,
|
|
46
|
+
params=params,
|
|
47
|
+
y=y,
|
|
48
|
+
)
|
|
49
|
+
return dict(data._asdict())
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def quadraticize_objective_data(
|
|
53
|
+
grad_fun,
|
|
54
|
+
hess_fun,
|
|
55
|
+
diag_hess_fun,
|
|
56
|
+
params,
|
|
57
|
+
y,
|
|
58
|
+
rho: float = 1.0,
|
|
59
|
+
use_diagonal_hessian: bool = True,
|
|
60
|
+
diag_floor: Optional[float] = None,
|
|
61
|
+
):
|
|
62
|
+
grad = grad_fun(y, params)
|
|
63
|
+
|
|
64
|
+
if use_diagonal_hessian:
|
|
65
|
+
qdiag = diag_hess_fun(params, y)
|
|
66
|
+
if diag_floor is not None:
|
|
67
|
+
qdiag = jnp.maximum(qdiag, diag_floor)
|
|
68
|
+
Q = jnp.diag(rho * qdiag)
|
|
69
|
+
else:
|
|
70
|
+
Q = rho * hess_fun(y, params)
|
|
71
|
+
|
|
72
|
+
c = grad - Q @ y
|
|
73
|
+
|
|
74
|
+
return QuadraticObjectiveData(grad_f=grad, Q=Q, Q_diag=jnp.diag(Q), c=c)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def quadraticize_objective(
|
|
78
|
+
grad_fun,
|
|
79
|
+
hess_fun,
|
|
80
|
+
diag_hess_fun,
|
|
81
|
+
params,
|
|
82
|
+
y,
|
|
83
|
+
rho: float = 1.0,
|
|
84
|
+
use_diagonal_hessian: bool = True,
|
|
85
|
+
diag_floor: Optional[float] = None,
|
|
86
|
+
):
|
|
87
|
+
data = quadraticize_objective_data(
|
|
88
|
+
grad_fun=grad_fun,
|
|
89
|
+
hess_fun=hess_fun,
|
|
90
|
+
diag_hess_fun=diag_hess_fun,
|
|
91
|
+
params=params,
|
|
92
|
+
y=y,
|
|
93
|
+
rho=rho,
|
|
94
|
+
use_diagonal_hessian=use_diagonal_hessian,
|
|
95
|
+
diag_floor=diag_floor,
|
|
96
|
+
)
|
|
97
|
+
return dict(data._asdict())
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def build_sqp_subproblem_data(
|
|
101
|
+
eq_fun,
|
|
102
|
+
ineq_fun,
|
|
103
|
+
jac_eq,
|
|
104
|
+
jac_ineq,
|
|
105
|
+
grad_fun,
|
|
106
|
+
hess_fun,
|
|
107
|
+
diag_hess_fun,
|
|
108
|
+
lower_fun,
|
|
109
|
+
upper_fun,
|
|
110
|
+
params,
|
|
111
|
+
y,
|
|
112
|
+
rho: float = 1.0,
|
|
113
|
+
use_diagonal_hessian: bool = True,
|
|
114
|
+
diag_floor: Optional[float] = None,
|
|
115
|
+
):
|
|
116
|
+
qdata = quadraticize_objective_data(
|
|
117
|
+
grad_fun=grad_fun,
|
|
118
|
+
hess_fun=hess_fun,
|
|
119
|
+
diag_hess_fun=diag_hess_fun,
|
|
120
|
+
params=params,
|
|
121
|
+
y=y,
|
|
122
|
+
rho=rho,
|
|
123
|
+
use_diagonal_hessian=use_diagonal_hessian,
|
|
124
|
+
diag_floor=diag_floor,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
cdata = linearize_constraints_data(
|
|
128
|
+
eq_fun=eq_fun,
|
|
129
|
+
ineq_fun=ineq_fun,
|
|
130
|
+
jac_eq=jac_eq,
|
|
131
|
+
jac_ineq=jac_ineq,
|
|
132
|
+
params=params,
|
|
133
|
+
y=y,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
return SQPSubproblemData(
|
|
137
|
+
objective=qdata,
|
|
138
|
+
constraints=cdata,
|
|
139
|
+
l=None if lower_fun is None else lower_fun(params),
|
|
140
|
+
u=None if upper_fun is None else upper_fun(params),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def build_sqp_data(
|
|
145
|
+
eq_fun,
|
|
146
|
+
ineq_fun,
|
|
147
|
+
jac_eq,
|
|
148
|
+
jac_ineq,
|
|
149
|
+
grad_fun,
|
|
150
|
+
hess_fun,
|
|
151
|
+
diag_hess_fun,
|
|
152
|
+
lower_fun,
|
|
153
|
+
upper_fun,
|
|
154
|
+
params,
|
|
155
|
+
y,
|
|
156
|
+
rho: float = 1.0,
|
|
157
|
+
use_diagonal_hessian: bool = True,
|
|
158
|
+
diag_floor: Optional[float] = None,
|
|
159
|
+
):
|
|
160
|
+
data = build_sqp_subproblem_data(
|
|
161
|
+
eq_fun=eq_fun,
|
|
162
|
+
ineq_fun=ineq_fun,
|
|
163
|
+
jac_eq=jac_eq,
|
|
164
|
+
jac_ineq=jac_ineq,
|
|
165
|
+
grad_fun=grad_fun,
|
|
166
|
+
hess_fun=hess_fun,
|
|
167
|
+
diag_hess_fun=diag_hess_fun,
|
|
168
|
+
lower_fun=lower_fun,
|
|
169
|
+
upper_fun=upper_fun,
|
|
170
|
+
params=params,
|
|
171
|
+
y=y,
|
|
172
|
+
rho=rho,
|
|
173
|
+
use_diagonal_hessian=use_diagonal_hessian,
|
|
174
|
+
diag_floor=diag_floor,
|
|
175
|
+
)
|
|
176
|
+
return {
|
|
177
|
+
**dict(data.objective._asdict()),
|
|
178
|
+
**dict(data.constraints._asdict()),
|
|
179
|
+
"l": data.l,
|
|
180
|
+
"u": data.u,
|
|
181
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import jax.numpy as jnp
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def build_grad_y_objective(obj_flat):
|
|
6
|
+
return jax.grad(lambda y, p: obj_flat(p, y), argnums=0)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def build_hess_y_objective(obj_flat):
|
|
10
|
+
return jax.hessian(lambda y, p: obj_flat(p, y), argnums=0)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def build_diag_hess_y_objective(grad_fun):
|
|
14
|
+
def diag_hess(params, y):
|
|
15
|
+
n = y.shape[0]
|
|
16
|
+
basis = jnp.eye(n, dtype=y.dtype)
|
|
17
|
+
_, jvp_fun = jax.linearize(lambda yy: grad_fun(yy, params), y)
|
|
18
|
+
cols = jax.vmap(jvp_fun)(basis)
|
|
19
|
+
return jnp.diag(cols.T)
|
|
20
|
+
return diag_hess
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def build_jac_y_vector_fun(vec_fun):
|
|
24
|
+
return jax.jacrev(lambda y, p: vec_fun(p, y), argnums=0)
|