snn-opt 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.
- snn_opt-0.1.0/LICENSE +201 -0
- snn_opt-0.1.0/MANIFEST.in +3 -0
- snn_opt-0.1.0/PKG-INFO +251 -0
- snn_opt-0.1.0/README.md +214 -0
- snn_opt-0.1.0/pyproject.toml +62 -0
- snn_opt-0.1.0/setup.cfg +4 -0
- snn_opt-0.1.0/setup.py +38 -0
- snn_opt-0.1.0/src/snn_opt/__init__.py +60 -0
- snn_opt-0.1.0/src/snn_opt/_native/bindings.cpp +98 -0
- snn_opt-0.1.0/src/snn_opt/_native/snn_qp_core.hpp +334 -0
- snn_opt-0.1.0/src/snn_opt/projection_variants.py +1376 -0
- snn_opt-0.1.0/src/snn_opt/solver.py +1203 -0
- snn_opt-0.1.0/src/snn_opt.egg-info/PKG-INFO +251 -0
- snn_opt-0.1.0/src/snn_opt.egg-info/SOURCES.txt +17 -0
- snn_opt-0.1.0/src/snn_opt.egg-info/dependency_links.txt +1 -0
- snn_opt-0.1.0/src/snn_opt.egg-info/requires.txt +15 -0
- snn_opt-0.1.0/src/snn_opt.egg-info/top_level.txt +1 -0
- snn_opt-0.1.0/tests/test_c_backend_parity.py +177 -0
- snn_opt-0.1.0/tests/test_installation.py +57 -0
snn_opt-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 tracking or 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 describing the origin of the Work and
|
|
141
|
+
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 or computer 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 Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
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 2026 Ameer Hamza Khan
|
|
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
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
snn_opt-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: snn_opt
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A spiking neural network solver for constrained convex optimization (QP/LP).
|
|
5
|
+
Author-email: Ameer Hamza Khan <ahkhan@tzc.edu.cn>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://snn.ahkhan.me
|
|
8
|
+
Project-URL: Repository, https://github.com/ahkhan03/SNN_opt
|
|
9
|
+
Project-URL: Documentation, https://snn.ahkhan.me/docs
|
|
10
|
+
Project-URL: Issues, https://github.com/ahkhan03/SNN_opt/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/ahkhan03/SNN_opt/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: spiking-neural-networks,convex-optimization,quadratic-programming,neuromorphic-computing,projected-gradient
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: numpy>=1.20
|
|
25
|
+
Requires-Dist: scipy>=1.7
|
|
26
|
+
Provides-Extra: examples
|
|
27
|
+
Requires-Dist: matplotlib>=3.5; extra == "examples"
|
|
28
|
+
Provides-Extra: benchmarks
|
|
29
|
+
Requires-Dist: matplotlib>=3.5; extra == "benchmarks"
|
|
30
|
+
Requires-Dist: cvxpy>=1.4; extra == "benchmarks"
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
33
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
34
|
+
Requires-Dist: matplotlib>=3.5; extra == "dev"
|
|
35
|
+
Requires-Dist: cvxpy>=1.4; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# snn_opt
|
|
39
|
+
|
|
40
|
+
**A spiking neural network solver for constrained convex optimization.**
|
|
41
|
+
|
|
42
|
+
[](LICENSE)
|
|
43
|
+
[](https://www.python.org/downloads/)
|
|
44
|
+
[](CHANGELOG.md)
|
|
45
|
+
[](CITATION.cff)
|
|
46
|
+
[](https://snn.ahkhan.me)
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Abstract
|
|
51
|
+
|
|
52
|
+
`snn_opt` is a Python implementation of the **spiking neural network (SNN) → convex optimization** equivalence, developed as part of an ongoing research program on neuromorphic computation for classical machine-learning problems. It solves quadratic and linear programs of the form
|
|
53
|
+
|
|
54
|
+
$$
|
|
55
|
+
\min_{x \in \mathbb{R}^n}\ \tfrac{1}{2}\, x^\top A x + b^\top x
|
|
56
|
+
\quad\text{subject to}\quad C x + d \le 0,
|
|
57
|
+
$$
|
|
58
|
+
|
|
59
|
+
by alternating gradient descent — playing the role of leaky-integrate membrane drift — with discrete projection events that clamp the trajectory to the constraint boundary, the optimization analogue of an integrate-and-fire **spike**. The construction follows Mancoo, Boerlin and Machens ([NeurIPS 2020](https://papers.nips.cc/paper/2020/hash/64714a86909d401f8feb83e8c2d94b23-Abstract.html)) and is the canonical solver underlying the **SNN-X** publication series (PCA, Ridge, TDSVM, Norm, SVM, CF, KRR, Procrustes — see [Applications](#applications)).
|
|
60
|
+
|
|
61
|
+
This repository is intended both as a **research artifact** — every published SNN-X paper can be reproduced from the code here — and as a **teaching resource** for students entering the area: it ships with annotated examples, a self-contained mathematical writeup, and a benchmark suite that visualizes convergence and projection dynamics.
|
|
62
|
+
|
|
63
|
+
## The problem
|
|
64
|
+
|
|
65
|
+
Given a positive semi-definite Hessian $A \in \mathbb{R}^{n\times n}$, a linear cost $b \in \mathbb{R}^n$, and $m$ linear inequality constraints stacked into $C \in \mathbb{R}^{m \times n}$ and $d \in \mathbb{R}^m$, we seek
|
|
66
|
+
|
|
67
|
+
$$
|
|
68
|
+
x^\star \;=\; \arg\min_{x}\ \tfrac{1}{2}\, x^\top A x + b^\top x \quad\text{s.t.}\quad c_i^\top x + d_i \le 0,\ i = 1,\dots,m.
|
|
69
|
+
$$
|
|
70
|
+
|
|
71
|
+
The class subsumes box-constrained QPs (set $C = [I; -I]$), linear programs ($A = 0$), kernel-ridge subproblems, support-vector machine duals, projected-gradient flows on polytopes, and the bulk of the inner solves that arise in receding-horizon control.
|
|
72
|
+
|
|
73
|
+
## The spiking idea, in one picture
|
|
74
|
+
|
|
75
|
+
The continuous-time dynamics
|
|
76
|
+
|
|
77
|
+
$$
|
|
78
|
+
\dot x \;=\; -\nabla f(x) \;-\; C^\top s(t)
|
|
79
|
+
$$
|
|
80
|
+
|
|
81
|
+
models a population of $n$ leaky integrators driven by the gradient $\nabla f(x) = Ax + b$, with a corrective spike train $s(t)$ that fires whenever an inequality $c_i^\top x + d_i$ would otherwise become positive. Each spike applies a *minimal* projection that re-enters the feasible set; spike inter-arrival times encode constraint *traffic*. Discretized with forward Euler and an adaptive step that reaches the boundary exactly, this becomes a fast projected-gradient solver with diagnostics that double as a neural raster plot.
|
|
82
|
+
|
|
83
|
+
See [`docs/theory.md`](docs/theory.md) for the full derivation, including the eigenvalue-based step-size choice that eliminates `k0` as a hyperparameter and the box-clipping shortcut for problems like SVM.
|
|
84
|
+
|
|
85
|
+
## Convergence and projection dynamics
|
|
86
|
+
|
|
87
|
+
Three diagnostic figures, regenerated from [`benchmarks/`](benchmarks/), give a quick visual sense of what the solver actually does:
|
|
88
|
+
|
|
89
|
+
| | |
|
|
90
|
+
|---|---|
|
|
91
|
+
| **Convergence** on a random 50-D QP with 30 inequalities. The objective gap drops geometrically over a few thousand iterations; the iterate stability `‖x_{t+1}−x_t‖` mirrors it; constraint violation stays at machine-precision floor throughout. |  |
|
|
92
|
+
| **Projection-spike raster** on a 4-D box-constrained problem whose unconstrained optimum lies *outside* the box. Each row is one inequality, each marker a spike (sized by displacement); only the four "active" faces of the box fire. The bottom panel is the corresponding objective gap. This is the literal sense in which the solver is *spiking*. |  |
|
|
93
|
+
| **Warm-start speedup** on a sequence of 30 drifting QPs (a stylized MPC workload). Cold-started solves take ~260 iterations each; warm-started solves drop to ~140 from the second problem onward — an essentially-free 1.8× speedup, the property that makes this dynamic well-suited to receding-horizon problems. |  |
|
|
94
|
+
|
|
95
|
+
Reproduce these from a checkout with `python benchmarks/run_all.py`.
|
|
96
|
+
|
|
97
|
+
### A closer look: trajectory and raw-vs-optimized modes
|
|
98
|
+
|
|
99
|
+
Two figures generated by the example scripts give a more concrete sense of what the dynamics look like in 2-D, where everything is easy to visualize:
|
|
100
|
+
|
|
101
|
+

|
|
102
|
+
|
|
103
|
+
*State evolution and 2-D trajectory for `examples/example1_basic_2d.py` — a constrained QP whose unconstrained minimum lies outside the feasible polytope. Left: per-component value over time, with orange spike markers at projection events. Right: the trajectory in state space superimposed on objective contours; the trajectory glides down the gradient, hits the active facet, and slides along it to the constrained optimum.*
|
|
104
|
+
|
|
105
|
+

|
|
106
|
+
|
|
107
|
+
*Output of `examples/example_raw_mode.py`. Left two panels: the trajectory under "raw" (no auto step size, no adaptive projection) and "optimized" defaults — both reach the optimum, but the optimized run does so in 201 iterations with a single projection event versus 300 / 5. Right: convergence on a log-objective scale; the optimized run gains roughly 30 orders of magnitude per 100 iterations once it leaves the boundary.*
|
|
108
|
+
|
|
109
|
+
## Installation
|
|
110
|
+
|
|
111
|
+
`snn_opt` requires Python 3.9+, NumPy, and SciPy. The fastest path is PyPI:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
pip install snn-opt # core, prebuilt wheel (no compiler needed)
|
|
115
|
+
pip install "snn-opt[examples]" # also installs matplotlib for examples
|
|
116
|
+
pip install "snn-opt[dev]" # examples + cvxpy + pytest + ruff
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> The PyPI distribution name is `snn-opt` (hyphenated, lowercase, per PEP 503); the Python import name is `snn_opt`. So you `pip install snn-opt` and then `import snn_opt`.
|
|
120
|
+
|
|
121
|
+
For an editable install from a checkout (development workflow):
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
git clone https://github.com/ahkhan03/SNN_opt.git
|
|
125
|
+
cd SNN_opt
|
|
126
|
+
pip install -e . # core
|
|
127
|
+
pip install -e ".[examples]" # also installs matplotlib for examples
|
|
128
|
+
pip install -e ".[dev]" # examples + cvxpy + pytest + ruff
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
For a specific commit (reproducibility for papers/collaborators):
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
pip install "git+https://github.com/ahkhan03/SNN_opt.git@<commit-sha>"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The package can also be run **without** installation — every example and test sits next to a small `sys.path` bootstrap that points at `src/`. Smoke test:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
python tests/test_installation.py
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Compiled C++ backend
|
|
144
|
+
|
|
145
|
+
The PyPI wheels ship a precompiled C++ kernel (`snn_opt._kernel`) that accelerates the inner adaptive-projection loop by roughly an order of magnitude over the pure-Python path. Opt in via the `backend` keyword:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
result = solve_qp(A, b, C, d, x0, backend='c') # compiled kernel
|
|
149
|
+
result = solve_qp(A, b, C, d, x0, backend='python') # reference (default)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Both backends are kept in lockstep by the parity test suite (`tests/test_c_backend_parity.py`). The C kernel supports dense problems with `projection_method='adaptive'`; sparse and non-adaptive paths transparently use Python. The same kernel source is HLS-compatible and is the basis for the planned FPGA deployment track. When the precompiled kernel is unavailable on your platform (rare), `backend='c'` raises a clear error and the Python backend continues to work.
|
|
153
|
+
|
|
154
|
+
## Quick start
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
import numpy as np
|
|
158
|
+
from snn_opt import solve_qp
|
|
159
|
+
|
|
160
|
+
# Minimize ||x||^2 subject to x_1 + 2 x_2 <= 1 (and that's it).
|
|
161
|
+
A = np.eye(2)
|
|
162
|
+
b = np.zeros(2)
|
|
163
|
+
C = np.array([[1.0, 2.0]])
|
|
164
|
+
d = np.array([-1.0])
|
|
165
|
+
x0 = np.array([1.0, 1.0])
|
|
166
|
+
|
|
167
|
+
result = solve_qp(A, b, C, d, x0, max_iterations=1000)
|
|
168
|
+
|
|
169
|
+
print(result.summary()) # converged? iterations? spikes?
|
|
170
|
+
print("x* =", result.final_x)
|
|
171
|
+
print("f* =", result.final_objective)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
For repeated solves (warm-started receding-horizon problems), construct an `SNNSolver` once and call `.solve(x0)` per problem instance — see [`examples/example4_warm_start.py`](examples/example4_warm_start.py).
|
|
175
|
+
|
|
176
|
+
## Examples
|
|
177
|
+
|
|
178
|
+
All scripts live under [`examples/`](examples/) and are runnable as plain `python examples/example_name.py`.
|
|
179
|
+
|
|
180
|
+
| # | Script | Problem | Highlights |
|
|
181
|
+
|---|---|---|---|
|
|
182
|
+
| 1 | [`example1_simple_2d.py`](examples/example1_simple_2d.py) | 2D quadratic with two linear cuts | Smallest possible runnable demo |
|
|
183
|
+
| 1b | [`example1_basic_2d.py`](examples/example1_basic_2d.py) | Same problem, with trajectory plot | See `examples/example1_basic_2d.png` |
|
|
184
|
+
| 1c | [`example1_advanced_2d.py`](examples/example1_advanced_2d.py) | Shifted feasible region, infeasible start | Spike raster + violation plot |
|
|
185
|
+
| 2 | [`example2_3d_polytope.py`](examples/example2_3d_polytope.py) | 3D QP with 4 hyperplanes | Multiple active constraints, vertex solution |
|
|
186
|
+
| 3 | [`example3_linear_program.py`](examples/example3_linear_program.py) | Box-constrained LP ($A=0$) | LP via the same machinery |
|
|
187
|
+
| 4 | [`example4_warm_start.py`](examples/example4_warm_start.py) | Sequence of related QPs | Receding-horizon / MPC pattern; spikes drop $30 \to 0$ |
|
|
188
|
+
| 5 | [`example5_infeasible_recovery.py`](examples/example5_infeasible_recovery.py) | Infeasible initializations | Automatic projection to feasibility |
|
|
189
|
+
| 6 | [`example6_equality_constraint.py`](examples/example6_equality_constraint.py) | Equality via a sandwiched band | $x_1 = a$ as a tight $\pm \varepsilon$ inequality pair |
|
|
190
|
+
| 7 | [`example7_svm_dual.py`](examples/example7_svm_dual.py) | SVM dual with kernel | Box clipping + auto step size on a real ML task |
|
|
191
|
+
| — | [`example_raw_mode.py`](examples/example_raw_mode.py) | Bypass auto-config | Compares raw vs. optimized solver settings |
|
|
192
|
+
|
|
193
|
+
Run them all in sequence:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
python examples/run_all_examples.py
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Documentation
|
|
200
|
+
|
|
201
|
+
- [`docs/theory.md`](docs/theory.md) — derivation of the SNN/convex-optimization equivalence, step-size analysis, projection geometry, convergence criteria.
|
|
202
|
+
- [`docs/applications.md`](docs/applications.md) — one-page summary of each SNN-X paper with links to PDFs.
|
|
203
|
+
- [`docs/api.md`](docs/api.md) — hand-curated API reference.
|
|
204
|
+
- [https://snn.ahkhan.me](https://snn.ahkhan.me) — companion site, designed for a broader audience (students, curious researchers).
|
|
205
|
+
|
|
206
|
+
## Applications
|
|
207
|
+
|
|
208
|
+
The framework is currently demonstrated in:
|
|
209
|
+
|
|
210
|
+
- **Khan, Mohammed & Li (2025)** — *Portfolio Optimization: A Neurodynamic
|
|
211
|
+
Approach Based on Spiking Neural Networks*, **Biomimetics**, 10(12):808.
|
|
212
|
+
[doi:10.3390/biomimetics10120808](https://doi.org/10.3390/biomimetics10120808).
|
|
213
|
+
Portfolio selection cast as a constrained QP and solved by the spiking
|
|
214
|
+
dynamics implemented here.
|
|
215
|
+
|
|
216
|
+
Additional applications are in preparation. As they reach publication
|
|
217
|
+
they will be added to [`docs/applications.md`](docs/applications.md).
|
|
218
|
+
|
|
219
|
+
## Citing this work
|
|
220
|
+
|
|
221
|
+
If `snn_opt` plays a role in your research or teaching, please cite both the software and the framework paper:
|
|
222
|
+
|
|
223
|
+
```bibtex
|
|
224
|
+
@software{khan2026snnopt,
|
|
225
|
+
author = {Khan, Ameer Hamza and Li, Shuai},
|
|
226
|
+
title = {snn\_opt: A Spiking Neural Network Solver for Constrained Convex Optimization},
|
|
227
|
+
year = {2026},
|
|
228
|
+
version = {0.1.0},
|
|
229
|
+
url = {https://github.com/ahkhan03/SNN_opt},
|
|
230
|
+
license = {Apache-2.0},
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
@inproceedings{mancoo2020understanding,
|
|
234
|
+
author = {Mancoo, Allan and Boerlin, Martin and Machens, Christian K.},
|
|
235
|
+
title = {Understanding Spiking Networks Through Convex Optimization},
|
|
236
|
+
booktitle = {Advances in Neural Information Processing Systems (NeurIPS)},
|
|
237
|
+
year = {2020},
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
The full per-paper bibliography of the SNN-X series is maintained at [`docs/applications.md`](docs/applications.md).
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
Apache-2.0 — see [`LICENSE`](LICENSE). Permissive, with an explicit patent grant; suitable for both academic and commercial reuse.
|
|
246
|
+
|
|
247
|
+
## Acknowledgments
|
|
248
|
+
|
|
249
|
+
Developed at the **School of Artificial Intelligence, Taizhou University**.
|
|
250
|
+
|
|
251
|
+
This codebase implements the SNN-QP research program led by **Prof. Shuai Li** (IEEE Fellow; Faculty of Information Technology and Electrical Engineering, University of Oulu, Finland), who originated the line of work and co-authors the application papers in the SNN-X series. The mathematical framework follows Mancoo, Boerlin and Machens (NeurIPS 2020) and the broader projection-neural-network lineage (Hopfield–Tank, Kennedy–Chua, Xia–Wang, Liu–Wang). Pull requests, bug reports, and citations of the SNN-X papers in your own work are all warmly welcomed.
|