mmahelpy 0.1.0__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.
- mmahelpy-0.1.0/LICENSE +201 -0
- mmahelpy-0.1.0/PKG-INFO +90 -0
- mmahelpy-0.1.0/README.md +63 -0
- mmahelpy-0.1.0/pyproject.toml +42 -0
- mmahelpy-0.1.0/setup.cfg +4 -0
- mmahelpy-0.1.0/src/mmahelpy.egg-info/PKG-INFO +90 -0
- mmahelpy-0.1.0/src/mmahelpy.egg-info/SOURCES.txt +11 -0
- mmahelpy-0.1.0/src/mmahelpy.egg-info/dependency_links.txt +1 -0
- mmahelpy-0.1.0/src/mmahelpy.egg-info/requires.txt +5 -0
- mmahelpy-0.1.0/src/mmahelpy.egg-info/top_level.txt +1 -0
- mmahelpy-0.1.0/src/mmapy/__init__.py +5 -0
- mmahelpy-0.1.0/src/mmapy/mma.py +315 -0
- mmahelpy-0.1.0/tests/test_mma.py +101 -0
mmahelpy-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
mmahelpy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mmahelpy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A reusable Python implementation of the Method of Moving Asymptotes
|
|
5
|
+
Author: Kevin Tofu
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/kevin-tofu/mmahelpy
|
|
8
|
+
Project-URL: Issues, https://github.com/kevin-tofu/mmahelpy/issues
|
|
9
|
+
Keywords: mma,moving-asymptotes,optimization
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: numpy>=1.24
|
|
23
|
+
Requires-Dist: scipy>=1.10
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# mmapy source package
|
|
29
|
+
|
|
30
|
+
`mmapy` is a small, stateful implementation of the Method of Moving
|
|
31
|
+
Asymptotes (MMA). It solves a separable convex approximation at each call and
|
|
32
|
+
retains the design and asymptote history for the next outer iteration.
|
|
33
|
+
|
|
34
|
+
The package is independent of finite-element and topology-optimization
|
|
35
|
+
libraries. Supply objective and inequality-constraint values and gradients at
|
|
36
|
+
the current point. Constraints use the convention `g(x) <= 0`.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install mmahelpy
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Example
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import numpy as np
|
|
48
|
+
from mmapy import MMA, MMAOptions
|
|
49
|
+
|
|
50
|
+
optimizer = MMA(MMAOptions(move_limit=0.2))
|
|
51
|
+
x = np.array([0.8, 0.8])
|
|
52
|
+
lower = np.zeros(2)
|
|
53
|
+
upper = np.ones(2)
|
|
54
|
+
|
|
55
|
+
for _ in range(30):
|
|
56
|
+
# Minimize x[0]**2 + x[1]**2 subject to x[0] + x[1] >= 0.9.
|
|
57
|
+
objective = float(x @ x)
|
|
58
|
+
objective_gradient = 2.0 * x
|
|
59
|
+
constraints = np.array([0.9 - x.sum()])
|
|
60
|
+
constraint_jacobian = np.array([[-1.0, -1.0]])
|
|
61
|
+
result = optimizer.step(
|
|
62
|
+
x,
|
|
63
|
+
objective,
|
|
64
|
+
objective_gradient,
|
|
65
|
+
constraints,
|
|
66
|
+
constraint_jacobian,
|
|
67
|
+
lower,
|
|
68
|
+
upper,
|
|
69
|
+
)
|
|
70
|
+
x = result.x
|
|
71
|
+
|
|
72
|
+
print(x)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Create one `MMA` object per optimization run. Its `step` method returns an
|
|
76
|
+
`MMAResult` with the next design, dual multipliers, approximate constraint
|
|
77
|
+
values, and dual-solver status. Call `reset()` to reuse the object for a new
|
|
78
|
+
problem. Multiple inequality constraints are supported.
|
|
79
|
+
|
|
80
|
+
## Development
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
PYTHONPATH=src pytest
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The distribution is named `mmahelpy` because the `mmapy` name is already
|
|
87
|
+
used on PyPI. The installed import remains `mmapy`. Scikit-Topt depends on
|
|
88
|
+
`mmahelpy` and adapts its public `MMA_Config` to the generic optimizer for
|
|
89
|
+
topology optimization. The solver focuses on continuous, differentiable
|
|
90
|
+
problems with box bounds and inequality constraints.
|
mmahelpy-0.1.0/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# mmapy source package
|
|
2
|
+
|
|
3
|
+
`mmapy` is a small, stateful implementation of the Method of Moving
|
|
4
|
+
Asymptotes (MMA). It solves a separable convex approximation at each call and
|
|
5
|
+
retains the design and asymptote history for the next outer iteration.
|
|
6
|
+
|
|
7
|
+
The package is independent of finite-element and topology-optimization
|
|
8
|
+
libraries. Supply objective and inequality-constraint values and gradients at
|
|
9
|
+
the current point. Constraints use the convention `g(x) <= 0`.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install mmahelpy
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
import numpy as np
|
|
21
|
+
from mmapy import MMA, MMAOptions
|
|
22
|
+
|
|
23
|
+
optimizer = MMA(MMAOptions(move_limit=0.2))
|
|
24
|
+
x = np.array([0.8, 0.8])
|
|
25
|
+
lower = np.zeros(2)
|
|
26
|
+
upper = np.ones(2)
|
|
27
|
+
|
|
28
|
+
for _ in range(30):
|
|
29
|
+
# Minimize x[0]**2 + x[1]**2 subject to x[0] + x[1] >= 0.9.
|
|
30
|
+
objective = float(x @ x)
|
|
31
|
+
objective_gradient = 2.0 * x
|
|
32
|
+
constraints = np.array([0.9 - x.sum()])
|
|
33
|
+
constraint_jacobian = np.array([[-1.0, -1.0]])
|
|
34
|
+
result = optimizer.step(
|
|
35
|
+
x,
|
|
36
|
+
objective,
|
|
37
|
+
objective_gradient,
|
|
38
|
+
constraints,
|
|
39
|
+
constraint_jacobian,
|
|
40
|
+
lower,
|
|
41
|
+
upper,
|
|
42
|
+
)
|
|
43
|
+
x = result.x
|
|
44
|
+
|
|
45
|
+
print(x)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Create one `MMA` object per optimization run. Its `step` method returns an
|
|
49
|
+
`MMAResult` with the next design, dual multipliers, approximate constraint
|
|
50
|
+
values, and dual-solver status. Call `reset()` to reuse the object for a new
|
|
51
|
+
problem. Multiple inequality constraints are supported.
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
PYTHONPATH=src pytest
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The distribution is named `mmahelpy` because the `mmapy` name is already
|
|
60
|
+
used on PyPI. The installed import remains `mmapy`. Scikit-Topt depends on
|
|
61
|
+
`mmahelpy` and adapts its public `MMA_Config` to the generic optimizer for
|
|
62
|
+
topology optimization. The solver focuses on continuous, differentiable
|
|
63
|
+
problems with box bounds and inequality constraints.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mmahelpy"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A reusable Python implementation of the Method of Moving Asymptotes"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
authors = [{ name = "Kevin Tofu" }]
|
|
13
|
+
dependencies = ["numpy>=1.24", "scipy>=1.10"]
|
|
14
|
+
keywords = ["mma", "moving-asymptotes", "optimization"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Repository = "https://github.com/kevin-tofu/mmahelpy"
|
|
29
|
+
Issues = "https://github.com/kevin-tofu/mmahelpy/issues"
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
test = ["pytest>=8"]
|
|
33
|
+
|
|
34
|
+
[tool.setuptools]
|
|
35
|
+
package-dir = {"" = "src"}
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
where = ["src"]
|
|
39
|
+
|
|
40
|
+
[tool.pytest.ini_options]
|
|
41
|
+
testpaths = ["tests"]
|
|
42
|
+
addopts = "-q"
|
mmahelpy-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mmahelpy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A reusable Python implementation of the Method of Moving Asymptotes
|
|
5
|
+
Author: Kevin Tofu
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/kevin-tofu/mmahelpy
|
|
8
|
+
Project-URL: Issues, https://github.com/kevin-tofu/mmahelpy/issues
|
|
9
|
+
Keywords: mma,moving-asymptotes,optimization
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: numpy>=1.24
|
|
23
|
+
Requires-Dist: scipy>=1.10
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# mmapy source package
|
|
29
|
+
|
|
30
|
+
`mmapy` is a small, stateful implementation of the Method of Moving
|
|
31
|
+
Asymptotes (MMA). It solves a separable convex approximation at each call and
|
|
32
|
+
retains the design and asymptote history for the next outer iteration.
|
|
33
|
+
|
|
34
|
+
The package is independent of finite-element and topology-optimization
|
|
35
|
+
libraries. Supply objective and inequality-constraint values and gradients at
|
|
36
|
+
the current point. Constraints use the convention `g(x) <= 0`.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install mmahelpy
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Example
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import numpy as np
|
|
48
|
+
from mmapy import MMA, MMAOptions
|
|
49
|
+
|
|
50
|
+
optimizer = MMA(MMAOptions(move_limit=0.2))
|
|
51
|
+
x = np.array([0.8, 0.8])
|
|
52
|
+
lower = np.zeros(2)
|
|
53
|
+
upper = np.ones(2)
|
|
54
|
+
|
|
55
|
+
for _ in range(30):
|
|
56
|
+
# Minimize x[0]**2 + x[1]**2 subject to x[0] + x[1] >= 0.9.
|
|
57
|
+
objective = float(x @ x)
|
|
58
|
+
objective_gradient = 2.0 * x
|
|
59
|
+
constraints = np.array([0.9 - x.sum()])
|
|
60
|
+
constraint_jacobian = np.array([[-1.0, -1.0]])
|
|
61
|
+
result = optimizer.step(
|
|
62
|
+
x,
|
|
63
|
+
objective,
|
|
64
|
+
objective_gradient,
|
|
65
|
+
constraints,
|
|
66
|
+
constraint_jacobian,
|
|
67
|
+
lower,
|
|
68
|
+
upper,
|
|
69
|
+
)
|
|
70
|
+
x = result.x
|
|
71
|
+
|
|
72
|
+
print(x)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Create one `MMA` object per optimization run. Its `step` method returns an
|
|
76
|
+
`MMAResult` with the next design, dual multipliers, approximate constraint
|
|
77
|
+
values, and dual-solver status. Call `reset()` to reuse the object for a new
|
|
78
|
+
problem. Multiple inequality constraints are supported.
|
|
79
|
+
|
|
80
|
+
## Development
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
PYTHONPATH=src pytest
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The distribution is named `mmahelpy` because the `mmapy` name is already
|
|
87
|
+
used on PyPI. The installed import remains `mmapy`. Scikit-Topt depends on
|
|
88
|
+
`mmahelpy` and adapts its public `MMA_Config` to the generic optimizer for
|
|
89
|
+
topology optimization. The solver focuses on continuous, differentiable
|
|
90
|
+
problems with box bounds and inequality constraints.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/mmahelpy.egg-info/PKG-INFO
|
|
5
|
+
src/mmahelpy.egg-info/SOURCES.txt
|
|
6
|
+
src/mmahelpy.egg-info/dependency_links.txt
|
|
7
|
+
src/mmahelpy.egg-info/requires.txt
|
|
8
|
+
src/mmahelpy.egg-info/top_level.txt
|
|
9
|
+
src/mmapy/__init__.py
|
|
10
|
+
src/mmapy/mma.py
|
|
11
|
+
tests/test_mma.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mmapy
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"""A stateful, separable-convex MMA optimizer with multiple constraints."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from numpy.typing import ArrayLike, NDArray
|
|
9
|
+
from scipy.optimize import minimize
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
FloatArray = NDArray[np.float64]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class MMAOptions:
|
|
17
|
+
"""Numerical settings for the MMA outer iteration."""
|
|
18
|
+
|
|
19
|
+
move_limit: float | None = None
|
|
20
|
+
initial_asymptote: float = 0.5
|
|
21
|
+
asymptote_increase: float = 1.2
|
|
22
|
+
asymptote_decrease: float = 0.7
|
|
23
|
+
asymptote_minimum: float = 0.01
|
|
24
|
+
asymptote_maximum: float = 10.0
|
|
25
|
+
regularization: float = 1e-5
|
|
26
|
+
dual_tolerance: float = 1e-9
|
|
27
|
+
dual_max_iterations: int = 200
|
|
28
|
+
|
|
29
|
+
def __post_init__(self) -> None:
|
|
30
|
+
if self.move_limit is not None and self.move_limit <= 0.0:
|
|
31
|
+
raise ValueError("move_limit must be positive")
|
|
32
|
+
if self.initial_asymptote <= 0.0:
|
|
33
|
+
raise ValueError("initial_asymptote must be positive")
|
|
34
|
+
if self.asymptote_increase < 1.0:
|
|
35
|
+
raise ValueError("asymptote_increase must be at least one")
|
|
36
|
+
if not 0.0 < self.asymptote_decrease <= 1.0:
|
|
37
|
+
raise ValueError("asymptote_decrease must be in (0, 1]")
|
|
38
|
+
if not 0.0 < self.asymptote_minimum < self.asymptote_maximum:
|
|
39
|
+
raise ValueError("asymptote limits must satisfy 0 < min < max")
|
|
40
|
+
if self.regularization <= 0.0:
|
|
41
|
+
raise ValueError("regularization must be positive")
|
|
42
|
+
if self.dual_tolerance <= 0.0:
|
|
43
|
+
raise ValueError("dual_tolerance must be positive")
|
|
44
|
+
if self.dual_max_iterations < 1:
|
|
45
|
+
raise ValueError("dual_max_iterations must be positive")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True)
|
|
49
|
+
class MMAResult:
|
|
50
|
+
"""Result of one MMA subproblem solve."""
|
|
51
|
+
|
|
52
|
+
x: FloatArray
|
|
53
|
+
multipliers: FloatArray
|
|
54
|
+
approximated_constraints: FloatArray
|
|
55
|
+
dual_iterations: int
|
|
56
|
+
dual_success: bool
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _asymptote_coefficients(
|
|
60
|
+
gradient: FloatArray,
|
|
61
|
+
x: FloatArray,
|
|
62
|
+
lower: FloatArray,
|
|
63
|
+
upper: FloatArray,
|
|
64
|
+
regularization: float,
|
|
65
|
+
) -> tuple[FloatArray, FloatArray]:
|
|
66
|
+
"""Return reciprocal coefficients matching value and gradient at x."""
|
|
67
|
+
span = upper - lower
|
|
68
|
+
p = np.maximum(gradient, 0.0)
|
|
69
|
+
q = np.maximum(-gradient, 0.0)
|
|
70
|
+
stabilizer = 0.001 * (p + q) + regularization / span
|
|
71
|
+
return (
|
|
72
|
+
(p + stabilizer) * (upper - x) ** 2,
|
|
73
|
+
(q + stabilizer) * (x - lower) ** 2,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _approximation(
|
|
78
|
+
x: FloatArray,
|
|
79
|
+
x0: FloatArray,
|
|
80
|
+
value0: FloatArray,
|
|
81
|
+
p: FloatArray,
|
|
82
|
+
q: FloatArray,
|
|
83
|
+
lower: FloatArray,
|
|
84
|
+
upper: FloatArray,
|
|
85
|
+
) -> FloatArray:
|
|
86
|
+
"""Evaluate one or more reciprocal MMA approximations."""
|
|
87
|
+
return value0 + np.sum(
|
|
88
|
+
p * (1.0 / (upper - x) - 1.0 / (upper - x0))
|
|
89
|
+
+ q * (1.0 / (x - lower) - 1.0 / (x0 - lower)),
|
|
90
|
+
axis=-1,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class MMA:
|
|
95
|
+
"""Stateful MMA optimizer for differentiable bound-constrained problems.
|
|
96
|
+
|
|
97
|
+
Call :meth:`step` once per outer iteration with the objective and
|
|
98
|
+
inequality constraints evaluated at the current design. Constraint values
|
|
99
|
+
must use the convention ``g(x) <= 0``. The optimizer retains design and
|
|
100
|
+
asymptote history between calls.
|
|
101
|
+
|
|
102
|
+
Parameters
|
|
103
|
+
----------
|
|
104
|
+
config:
|
|
105
|
+
Moving-asymptote and dual-solver settings.
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
def __init__(self, config: MMAOptions | None = None) -> None:
|
|
109
|
+
self.config = config or MMAOptions()
|
|
110
|
+
self._previous_x: FloatArray | None = None
|
|
111
|
+
self._previous_previous_x: FloatArray | None = None
|
|
112
|
+
self._lower_asymptote: FloatArray | None = None
|
|
113
|
+
self._upper_asymptote: FloatArray | None = None
|
|
114
|
+
|
|
115
|
+
def reset(self) -> None:
|
|
116
|
+
"""Clear iteration history before starting a new optimization."""
|
|
117
|
+
self._previous_x = None
|
|
118
|
+
self._previous_previous_x = None
|
|
119
|
+
self._lower_asymptote = None
|
|
120
|
+
self._upper_asymptote = None
|
|
121
|
+
|
|
122
|
+
def step(
|
|
123
|
+
self,
|
|
124
|
+
x: ArrayLike,
|
|
125
|
+
objective: float,
|
|
126
|
+
objective_gradient: ArrayLike,
|
|
127
|
+
constraints: ArrayLike,
|
|
128
|
+
constraint_jacobian: ArrayLike,
|
|
129
|
+
lower_bounds: ArrayLike,
|
|
130
|
+
upper_bounds: ArrayLike,
|
|
131
|
+
*,
|
|
132
|
+
move_limit: float | ArrayLike | None = None,
|
|
133
|
+
) -> MMAResult:
|
|
134
|
+
"""Solve one local MMA approximation and update optimizer history.
|
|
135
|
+
|
|
136
|
+
``constraint_jacobian`` has shape ``(n_constraints, n_variables)``.
|
|
137
|
+
Empty one-dimensional inputs are accepted when there are no
|
|
138
|
+
constraints. Bounds are global design bounds; an optional move limit
|
|
139
|
+
further restricts this iteration's step.
|
|
140
|
+
"""
|
|
141
|
+
x0 = np.asarray(x, dtype=float)
|
|
142
|
+
df0 = np.asarray(objective_gradient, dtype=float)
|
|
143
|
+
g0 = np.atleast_1d(np.asarray(constraints, dtype=float))
|
|
144
|
+
dg0 = np.asarray(constraint_jacobian, dtype=float)
|
|
145
|
+
xmin = np.broadcast_to(np.asarray(lower_bounds, dtype=float), x0.shape)
|
|
146
|
+
xmax = np.broadcast_to(np.asarray(upper_bounds, dtype=float), x0.shape)
|
|
147
|
+
if x0.ndim != 1 or x0.size == 0:
|
|
148
|
+
raise ValueError("x must be a nonempty one-dimensional array")
|
|
149
|
+
n = x0.size
|
|
150
|
+
if df0.shape != (n,):
|
|
151
|
+
raise ValueError("objective_gradient must have shape (n_variables,)")
|
|
152
|
+
if xmin.shape != (n,) or xmax.shape != (n,):
|
|
153
|
+
raise ValueError("bounds must broadcast to x")
|
|
154
|
+
if (
|
|
155
|
+
np.any(~np.isfinite(x0))
|
|
156
|
+
or np.any(~np.isfinite(df0))
|
|
157
|
+
or np.any(~np.isfinite(xmin))
|
|
158
|
+
or np.any(~np.isfinite(xmax))
|
|
159
|
+
):
|
|
160
|
+
raise ValueError("x, gradients, and bounds must be finite")
|
|
161
|
+
if np.any(~np.isfinite(g0)) or np.any(~np.isfinite(dg0)):
|
|
162
|
+
raise ValueError("constraints and constraint_jacobian must be finite")
|
|
163
|
+
if np.any(xmin >= xmax) or np.any(x0 < xmin) or np.any(x0 > xmax):
|
|
164
|
+
raise ValueError("x must lie within nondegenerate global bounds")
|
|
165
|
+
|
|
166
|
+
m = g0.size
|
|
167
|
+
if m == 0:
|
|
168
|
+
dg0 = np.empty((0, n), dtype=float)
|
|
169
|
+
elif dg0.shape != (m, n):
|
|
170
|
+
raise ValueError(
|
|
171
|
+
"constraint_jacobian must have shape (n_constraints, n_variables)"
|
|
172
|
+
)
|
|
173
|
+
if not np.isfinite(objective):
|
|
174
|
+
raise ValueError("objective must be finite")
|
|
175
|
+
|
|
176
|
+
cfg = self.config
|
|
177
|
+
span = xmax - xmin
|
|
178
|
+
low, upp = self._moving_asymptotes(x0, span)
|
|
179
|
+
move = cfg.move_limit if move_limit is None else move_limit
|
|
180
|
+
if move is None:
|
|
181
|
+
move_array = span
|
|
182
|
+
else:
|
|
183
|
+
move_array = np.broadcast_to(np.asarray(move, dtype=float), x0.shape)
|
|
184
|
+
if np.any(~np.isfinite(move_array)) or np.any(move_array <= 0.0):
|
|
185
|
+
raise ValueError("move_limit must contain finite positive values")
|
|
186
|
+
alpha = np.maximum.reduce((
|
|
187
|
+
xmin,
|
|
188
|
+
low + 0.1 * (x0 - low),
|
|
189
|
+
x0 - move_array,
|
|
190
|
+
))
|
|
191
|
+
beta = np.minimum.reduce((
|
|
192
|
+
xmax,
|
|
193
|
+
upp - 0.1 * (upp - x0),
|
|
194
|
+
x0 + move_array,
|
|
195
|
+
))
|
|
196
|
+
if np.any(alpha >= beta):
|
|
197
|
+
raise ValueError("MMA move limits leave no feasible step")
|
|
198
|
+
|
|
199
|
+
p0, q0 = _asymptote_coefficients(
|
|
200
|
+
df0, x0, low, upp, cfg.regularization
|
|
201
|
+
)
|
|
202
|
+
if m:
|
|
203
|
+
p_constraints = np.empty((m, n), dtype=float)
|
|
204
|
+
q_constraints = np.empty((m, n), dtype=float)
|
|
205
|
+
for i in range(m):
|
|
206
|
+
p_constraints[i], q_constraints[i] = _asymptote_coefficients(
|
|
207
|
+
dg0[i], x0, low, upp, cfg.regularization
|
|
208
|
+
)
|
|
209
|
+
else:
|
|
210
|
+
p_constraints = np.empty((0, n), dtype=float)
|
|
211
|
+
q_constraints = np.empty((0, n), dtype=float)
|
|
212
|
+
|
|
213
|
+
def primal(multipliers: FloatArray) -> FloatArray:
|
|
214
|
+
p = p0 + multipliers @ p_constraints if m else p0
|
|
215
|
+
q = q0 + multipliers @ q_constraints if m else q0
|
|
216
|
+
sqrt_p = np.sqrt(np.maximum(p, np.finfo(float).tiny))
|
|
217
|
+
sqrt_q = np.sqrt(np.maximum(q, np.finfo(float).tiny))
|
|
218
|
+
unconstrained = (
|
|
219
|
+
sqrt_q * upp + sqrt_p * low
|
|
220
|
+
) / (sqrt_p + sqrt_q)
|
|
221
|
+
return np.clip(unconstrained, alpha, beta)
|
|
222
|
+
|
|
223
|
+
def objective_approximation(y: FloatArray) -> float:
|
|
224
|
+
return float(_approximation(
|
|
225
|
+
y, x0, np.asarray(objective), p0, q0, low, upp
|
|
226
|
+
))
|
|
227
|
+
|
|
228
|
+
def constraints_approximation(y: FloatArray) -> FloatArray:
|
|
229
|
+
if not m:
|
|
230
|
+
return np.empty(0, dtype=float)
|
|
231
|
+
return _approximation(
|
|
232
|
+
y, x0, g0, p_constraints, q_constraints, low, upp
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
if m:
|
|
236
|
+
def negative_dual(lam: FloatArray) -> tuple[float, FloatArray]:
|
|
237
|
+
y = primal(lam)
|
|
238
|
+
g_approx = constraints_approximation(y)
|
|
239
|
+
value = objective_approximation(y) + float(lam @ g_approx)
|
|
240
|
+
return -value, -g_approx
|
|
241
|
+
|
|
242
|
+
dual = minimize(
|
|
243
|
+
negative_dual,
|
|
244
|
+
np.zeros(m, dtype=float),
|
|
245
|
+
method="L-BFGS-B",
|
|
246
|
+
jac=True,
|
|
247
|
+
bounds=[(0.0, None)] * m,
|
|
248
|
+
options={
|
|
249
|
+
"ftol": cfg.dual_tolerance,
|
|
250
|
+
"gtol": cfg.dual_tolerance,
|
|
251
|
+
"maxiter": cfg.dual_max_iterations,
|
|
252
|
+
"maxls": 40,
|
|
253
|
+
},
|
|
254
|
+
)
|
|
255
|
+
multipliers = np.asarray(dual.x, dtype=float)
|
|
256
|
+
x_new = primal(multipliers)
|
|
257
|
+
approximated_constraints = constraints_approximation(x_new)
|
|
258
|
+
dual_iterations = int(dual.nit)
|
|
259
|
+
dual_success = bool(dual.success)
|
|
260
|
+
else:
|
|
261
|
+
multipliers = np.empty(0, dtype=float)
|
|
262
|
+
x_new = primal(multipliers)
|
|
263
|
+
approximated_constraints = np.empty(0, dtype=float)
|
|
264
|
+
dual_iterations = 0
|
|
265
|
+
dual_success = True
|
|
266
|
+
|
|
267
|
+
self._advance_history(x0, low, upp)
|
|
268
|
+
return MMAResult(
|
|
269
|
+
x=x_new.copy(),
|
|
270
|
+
multipliers=multipliers.copy(),
|
|
271
|
+
approximated_constraints=approximated_constraints.copy(),
|
|
272
|
+
dual_iterations=dual_iterations,
|
|
273
|
+
dual_success=dual_success,
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
def _moving_asymptotes(
|
|
277
|
+
self,
|
|
278
|
+
x: FloatArray,
|
|
279
|
+
span: FloatArray,
|
|
280
|
+
) -> tuple[FloatArray, FloatArray]:
|
|
281
|
+
cfg = self.config
|
|
282
|
+
if self._lower_asymptote is None or self._upper_asymptote is None:
|
|
283
|
+
distance = cfg.initial_asymptote * span
|
|
284
|
+
return x - distance, x + distance
|
|
285
|
+
|
|
286
|
+
factor = np.ones_like(x)
|
|
287
|
+
if self._previous_x is not None and self._previous_previous_x is not None:
|
|
288
|
+
trend = (x - self._previous_x) * (
|
|
289
|
+
self._previous_x - self._previous_previous_x
|
|
290
|
+
)
|
|
291
|
+
factor[trend > 0.0] = cfg.asymptote_increase
|
|
292
|
+
factor[trend < 0.0] = cfg.asymptote_decrease
|
|
293
|
+
|
|
294
|
+
lower = x - factor * (self._previous_x - self._lower_asymptote)
|
|
295
|
+
upper = x + factor * (self._upper_asymptote - self._previous_x)
|
|
296
|
+
min_distance = cfg.asymptote_minimum * span
|
|
297
|
+
max_distance = cfg.asymptote_maximum * span
|
|
298
|
+
lower = np.minimum(x - min_distance, np.maximum(x - max_distance, lower))
|
|
299
|
+
upper = np.maximum(x + min_distance, np.minimum(x + max_distance, upper))
|
|
300
|
+
return lower, upper
|
|
301
|
+
|
|
302
|
+
def _advance_history(
|
|
303
|
+
self,
|
|
304
|
+
x: FloatArray,
|
|
305
|
+
lower: FloatArray,
|
|
306
|
+
upper: FloatArray,
|
|
307
|
+
) -> None:
|
|
308
|
+
if self._previous_x is not None:
|
|
309
|
+
self._previous_previous_x = self._previous_x.copy()
|
|
310
|
+
self._previous_x = x.copy()
|
|
311
|
+
self._lower_asymptote = lower.copy()
|
|
312
|
+
self._upper_asymptote = upper.copy()
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
__all__ = ["MMA", "MMAOptions", "MMAResult"]
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from mmapy import MMA, MMAOptions
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_mma_solves_a_linear_single_constraint_subproblem():
|
|
7
|
+
optimizer = MMA(MMAOptions(move_limit=0.4))
|
|
8
|
+
result = optimizer.step(
|
|
9
|
+
x=[0.8],
|
|
10
|
+
objective=0.8,
|
|
11
|
+
objective_gradient=[1.0],
|
|
12
|
+
constraints=[0.5 - 0.8],
|
|
13
|
+
constraint_jacobian=[[-1.0]],
|
|
14
|
+
lower_bounds=[0.0],
|
|
15
|
+
upper_bounds=[1.0],
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
assert 0.5 <= result.x[0] < 0.8
|
|
19
|
+
assert abs(result.approximated_constraints[0]) < 1e-6
|
|
20
|
+
assert result.multipliers[0] >= 0.0
|
|
21
|
+
assert result.dual_success
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_mma_supports_multiple_inequality_constraints():
|
|
25
|
+
optimizer = MMA(MMAOptions(move_limit=0.4))
|
|
26
|
+
result = optimizer.step(
|
|
27
|
+
x=[0.8, 0.8],
|
|
28
|
+
objective=1.6,
|
|
29
|
+
objective_gradient=[1.0, 1.0],
|
|
30
|
+
constraints=[0.4 - 0.8, 0.6 - 0.8],
|
|
31
|
+
constraint_jacobian=[[-1.0, 0.0], [0.0, -1.0]],
|
|
32
|
+
lower_bounds=[0.0, 0.0],
|
|
33
|
+
upper_bounds=[1.0, 1.0],
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
assert result.x[0] >= 0.4
|
|
37
|
+
assert result.x[1] >= 0.6
|
|
38
|
+
assert np.all(np.abs(result.approximated_constraints) < 2e-3)
|
|
39
|
+
assert result.multipliers.shape == (2,)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_mma_converges_on_a_quadratic_problem_with_a_linear_constraint():
|
|
43
|
+
optimizer = MMA(MMAOptions(move_limit=0.2))
|
|
44
|
+
x = np.array([0.8, 0.8])
|
|
45
|
+
for _ in range(30):
|
|
46
|
+
result = optimizer.step(
|
|
47
|
+
x=x,
|
|
48
|
+
objective=float(x @ x),
|
|
49
|
+
objective_gradient=2.0 * x,
|
|
50
|
+
constraints=[0.9 - x.sum()],
|
|
51
|
+
constraint_jacobian=[[-1.0, -1.0]],
|
|
52
|
+
lower_bounds=[0.0, 0.0],
|
|
53
|
+
upper_bounds=[1.0, 1.0],
|
|
54
|
+
)
|
|
55
|
+
assert result.dual_success
|
|
56
|
+
x = result.x
|
|
57
|
+
|
|
58
|
+
np.testing.assert_allclose(x, [0.45, 0.45], atol=1e-5)
|
|
59
|
+
assert x.sum() >= 0.9 - 1e-6
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_mma_keeps_asymptote_history_and_reset_clears_it():
|
|
63
|
+
optimizer = MMA(MMAOptions(move_limit=0.2))
|
|
64
|
+
args = dict(
|
|
65
|
+
objective=0.0,
|
|
66
|
+
objective_gradient=[-1.0],
|
|
67
|
+
constraints=[],
|
|
68
|
+
constraint_jacobian=np.empty((0, 1)),
|
|
69
|
+
lower_bounds=[0.0],
|
|
70
|
+
upper_bounds=[1.0],
|
|
71
|
+
)
|
|
72
|
+
first = optimizer.step(x=[0.5], **args)
|
|
73
|
+
assert optimizer._lower_asymptote is not None
|
|
74
|
+
assert optimizer._upper_asymptote is not None
|
|
75
|
+
|
|
76
|
+
optimizer.step(x=first.x, **args)
|
|
77
|
+
assert optimizer._previous_previous_x is not None
|
|
78
|
+
|
|
79
|
+
optimizer.reset()
|
|
80
|
+
assert optimizer._previous_x is None
|
|
81
|
+
assert optimizer._previous_previous_x is None
|
|
82
|
+
assert optimizer._lower_asymptote is None
|
|
83
|
+
assert optimizer._upper_asymptote is None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_mma_rejects_inconsistent_constraint_jacobian_shape():
|
|
87
|
+
optimizer = MMA()
|
|
88
|
+
try:
|
|
89
|
+
optimizer.step(
|
|
90
|
+
x=[0.5, 0.5],
|
|
91
|
+
objective=1.0,
|
|
92
|
+
objective_gradient=[1.0, 1.0],
|
|
93
|
+
constraints=[-0.1],
|
|
94
|
+
constraint_jacobian=[[1.0]],
|
|
95
|
+
lower_bounds=[0.0, 0.0],
|
|
96
|
+
upper_bounds=[1.0, 1.0],
|
|
97
|
+
)
|
|
98
|
+
except ValueError as error:
|
|
99
|
+
assert "constraint_jacobian" in str(error)
|
|
100
|
+
else:
|
|
101
|
+
raise AssertionError("shape mismatch should have raised ValueError")
|