primalsolver 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.
Files changed (61) hide show
  1. primalsolver-0.1.0/LICENSE +202 -0
  2. primalsolver-0.1.0/MANIFEST.in +10 -0
  3. primalsolver-0.1.0/PKG-INFO +243 -0
  4. primalsolver-0.1.0/README.md +226 -0
  5. primalsolver-0.1.0/csrc/cbf.c +1275 -0
  6. primalsolver-0.1.0/csrc/cbf.h +41 -0
  7. primalsolver-0.1.0/csrc/expcone.c +624 -0
  8. primalsolver-0.1.0/csrc/expcone.h +146 -0
  9. primalsolver-0.1.0/csrc/ipm.c +1150 -0
  10. primalsolver-0.1.0/csrc/ipm.h +74 -0
  11. primalsolver-0.1.0/csrc/linalg.c +1557 -0
  12. primalsolver-0.1.0/csrc/linalg.h +205 -0
  13. primalsolver-0.1.0/csrc/mpsio.c +2227 -0
  14. primalsolver-0.1.0/csrc/mpsio.h +49 -0
  15. primalsolver-0.1.0/csrc/presolve.c +512 -0
  16. primalsolver-0.1.0/csrc/presolve.h +69 -0
  17. primalsolver-0.1.0/csrc/primal.h +2378 -0
  18. primalsolver-0.1.0/csrc/primal_afe.c +1739 -0
  19. primalsolver-0.1.0/csrc/primal_bar.c +887 -0
  20. primalsolver-0.1.0/csrc/primal_conicopt.c +852 -0
  21. primalsolver-0.1.0/csrc/primal_core.c +1566 -0
  22. primalsolver-0.1.0/csrc/primal_djc.c +707 -0
  23. primalsolver-0.1.0/csrc/primal_get.c +579 -0
  24. primalsolver-0.1.0/csrc/primal_info.c +844 -0
  25. primalsolver-0.1.0/csrc/primal_meta.c +870 -0
  26. primalsolver-0.1.0/csrc/primal_mip.c +1064 -0
  27. primalsolver-0.1.0/csrc/primal_mip_opt.c +1105 -0
  28. primalsolver-0.1.0/csrc/primal_misc.c +1412 -0
  29. primalsolver-0.1.0/csrc/primal_names.c +935 -0
  30. primalsolver-0.1.0/csrc/primal_optimize.c +757 -0
  31. primalsolver-0.1.0/csrc/primal_priv.h +464 -0
  32. primalsolver-0.1.0/csrc/primal_put.c +1372 -0
  33. primalsolver-0.1.0/csrc/primal_qcon.c +331 -0
  34. primalsolver-0.1.0/csrc/primal_quad.c +801 -0
  35. primalsolver-0.1.0/csrc/primal_sdptask.c +1458 -0
  36. primalsolver-0.1.0/csrc/primal_solio.c +467 -0
  37. primalsolver-0.1.0/csrc/primal_solution.c +1768 -0
  38. primalsolver-0.1.0/csrc/primal_std.c +598 -0
  39. primalsolver-0.1.0/csrc/primal_vartype.c +470 -0
  40. primalsolver-0.1.0/csrc/primal_verdict.c +966 -0
  41. primalsolver-0.1.0/csrc/scaling.c +118 -0
  42. primalsolver-0.1.0/csrc/scaling.h +37 -0
  43. primalsolver-0.1.0/csrc/sdp.c +1893 -0
  44. primalsolver-0.1.0/csrc/sdp.h +71 -0
  45. primalsolver-0.1.0/csrc/simplex.c +686 -0
  46. primalsolver-0.1.0/csrc/simplex.h +79 -0
  47. primalsolver-0.1.0/csrc/socp.c +1149 -0
  48. primalsolver-0.1.0/csrc/socp.h +54 -0
  49. primalsolver-0.1.0/csrc/stdform.c +507 -0
  50. primalsolver-0.1.0/csrc/stdform.h +102 -0
  51. primalsolver-0.1.0/pyproject.toml +53 -0
  52. primalsolver-0.1.0/setup.cfg +4 -0
  53. primalsolver-0.1.0/setup.py +117 -0
  54. primalsolver-0.1.0/src/primalsolver/__init__.py +373 -0
  55. primalsolver-0.1.0/src/primalsolver/_bindings.py +557 -0
  56. primalsolver-0.1.0/src/primalsolver/_native.py +53 -0
  57. primalsolver-0.1.0/src/primalsolver.egg-info/PKG-INFO +243 -0
  58. primalsolver-0.1.0/src/primalsolver.egg-info/SOURCES.txt +59 -0
  59. primalsolver-0.1.0/src/primalsolver.egg-info/dependency_links.txt +1 -0
  60. primalsolver-0.1.0/src/primalsolver.egg-info/top_level.txt +1 -0
  61. primalsolver-0.1.0/sync_csrc.py +51 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,10 @@
1
+ # sdist contents. The C sources come from the vendored csrc/ snapshot
2
+ # (run sync_csrc.py before building a release), so the sdist is self-contained.
3
+ include LICENSE README.md pyproject.toml setup.py sync_csrc.py MANIFEST.in
4
+ graft csrc
5
+
6
+ # never ship a pre-built, platform-specific library in the sdist
7
+ exclude src/primalsolver/_lib/*.dylib
8
+ exclude src/primalsolver/_lib/*.so
9
+ exclude src/primalsolver/_lib/*.dll
10
+ global-exclude __pycache__ *.pyc
@@ -0,0 +1,243 @@
1
+ Metadata-Version: 2.4
2
+ Name: primalsolver
3
+ Version: 0.1.0
4
+ Summary: PrimalSolver: dependency-free convex optimization (LP/QP/SOCP/SDP/exp-power/MIP) in C99, callable from Python via ctypes.
5
+ Author: Gaetano Minardi
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/c-vision/Primal
8
+ Keywords: optimization,linear-programming,quadratic-programming,socp,sdp,conic,mip,ctypes
9
+ Classifier: Programming Language :: C
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Dynamic: license-file
17
+
18
+ # primalsolver
19
+
20
+ Python bindings for **PrimalSolver** — a dependency-free convex optimization
21
+ solver in C99 (LP / QP / SOCP / SDP / exp-power / MIP).
22
+
23
+ The package ships a pre-built shared library (`libprimal.dylib` / `.so` / `.dll`)
24
+ inside the wheel and calls it through `ctypes`: no C toolchain is needed to
25
+ install, and the whole public API is exposed.
26
+
27
+ > Upstream C library: <https://github.com/c-vision/Primal>. This package treats
28
+ > its sources as read-only input and never modifies them.
29
+
30
+ ```python
31
+ from primalsolver import Model, BK, SOLSTA
32
+
33
+ with Model(maxcon=2, maxvar=2) as m:
34
+ m.obj([0, 1], [-3.0, -2.0]) # min -3 x0 - 2 x1
35
+ m.a_ij(0, 0, 1.0); m.a_ij(0, 1, 1.0) # x0 + x1 <= 4
36
+ m.a_ij(1, 0, 1.0); m.a_ij(1, 1, 3.0) # x0 + 3x1 <= 6
37
+ m.con_bounds(0, BK.UP, up=4.0)
38
+ m.con_bounds(1, BK.UP, up=6.0)
39
+ m.var_bounds(0, BK.RA, 0.0, 3.0) # 0 <= x0 <= 3
40
+ m.var_bounds(1, BK.RA, 0.0, 3.0)
41
+ r = m.solve()
42
+
43
+ assert r.solsta == SOLSTA.OPTIMAL
44
+ print(r.x, r.objective) # [3.0, 1.0] -11.0
45
+ ```
46
+
47
+ ## Requirements
48
+
49
+ - Python >= 3.8, **no runtime dependencies** other than the standard library.
50
+ - A platform wheel is provided per OS/arch (macOS, Linux, Windows). On a platform
51
+ without a wheel you can build from source (see below).
52
+
53
+ ## Install
54
+
55
+ ```sh
56
+ pip install primalsolver
57
+ ```
58
+
59
+ Verify:
60
+
61
+ ```sh
62
+ python -c "import primalsolver as ps; print(ps.version())"
63
+ ```
64
+
65
+ ## Quickstart
66
+
67
+ Build and solve a model in code (above), or solve a **model file** — MPS, CPLEX
68
+ LP, OPF and CBF are auto-detected by content/extension:
69
+
70
+ ```python
71
+ from primalsolver import Model, SOLSTA
72
+
73
+ with Model() as m:
74
+ m.read("model.mps")
75
+ r = m.solve()
76
+
77
+ print(r.solsta, r.x, r.objective)
78
+ ```
79
+
80
+ The high-level `Model` covers the common path. Everything else is reachable
81
+ through the generated low-level binding.
82
+
83
+ ## Full API (all 547 functions)
84
+
85
+ `tools/gen_bindings.py` parses the C header `primal.h` and emits
86
+ `src/primalsolver/_bindings.py` with a ctypes signature for **every** `PRIMAL_*`
87
+ function. `_native.py` applies them and re-exports them, so the entire surface is
88
+ available:
89
+
90
+ ```python
91
+ import primalsolver as ps
92
+
93
+ ps.PRIMAL_getdualray(...) # every PRIMAL_* is available at package level
94
+ ps.FUNCTIONS # name -> ctypes function object (547)
95
+ ps.MISSING # declared in the header but absent in the lib (empty = OK)
96
+ ps._native.optimize # short aliases (PRIMAL_ removed) used by Model
97
+ ```
98
+
99
+ Handles (`PRIMALenv_t` / `PRIMALtask_t`) are opaque C pointers: the functions
100
+ that create/destroy them take a *pointer to the handle*, so pass
101
+ `ctypes.byref(...)`. Callback arguments are exposed as `c_void_p`; the two
102
+ variadic functions (`PRIMAL_echotask`, `PRIMAL_echoenv`) are exposed without
103
+ `argtypes` (ctypes cannot type C varargs) — pass explicit ctypes objects for the
104
+ extra arguments.
105
+
106
+ Regenerate the binding **after any change to `primal.h`**:
107
+
108
+ ```sh
109
+ python tools/gen_bindings.py
110
+ ```
111
+
112
+ ## What is shipped
113
+
114
+ - **Wheel**: only the `primalsolver` package — `__init__.py`, `_native.py`, the
115
+ generated `_bindings.py`, the compiled library in `_lib/`, plus `README.md` and
116
+ `LICENSE`.
117
+ - **Python examples** live in `../python_examples/` (tracked in the C repository),
118
+ not in this package. No test files are shipped.
119
+
120
+ ## Building from source
121
+
122
+ The C sources are **read-only input**; this project never modifies them. `setup.py`
123
+ compiles the shared library into `src/primalsolver/_lib/` before packaging.
124
+
125
+ Source directory resolution, in order:
126
+
127
+ 1. `$PRIMAL_C_SRC` — an explicit C checkout;
128
+ 2. `csrc/` — a vendored snapshot (`python sync_csrc.py`), used by the sdist/CI;
129
+ 3. `..` — the parent directory, i.e. the live C repository (default).
130
+
131
+ `csrc/` is generated by `sync_csrc.py` and is **gitignored**: the C sources have
132
+ a single home in the C repository. CI fetches them from
133
+ `github.com/c-vision/Primal` and regenerates `csrc/` before building.
134
+
135
+ Development install:
136
+
137
+ ```sh
138
+ python -m venv .venv && . .venv/bin/activate
139
+ pip install -U pip setuptools wheel
140
+ pip install -e .
141
+ python -c "import primalsolver as ps; print(ps.version())"
142
+ ```
143
+
144
+ Build a wheel locally (needs only `setuptools` + `wheel`, no `build` package):
145
+
146
+ ```sh
147
+ python -m pip wheel . --no-build-isolation --no-deps -w dist
148
+ ```
149
+
150
+ > The wheel bundles a native library, so it is **platform-specific**:
151
+ > `py3-none-<platform>` (e.g. `py3-none-macosx_11_0_arm64`), never `none-any`.
152
+
153
+ ## Building the distribution wheels (all platforms)
154
+
155
+ Config lives in `[tool.cibuildwheel]` (`pyproject.toml`); `cibuildwheel`
156
+ compiles inside each target environment (required for manylinux) and runs
157
+ `auditwheel` / `delocate`. In CI the C sources are fetched from
158
+ `github.com/c-vision/Primal` and vendored with `sync_csrc.py` before building
159
+ (see `.github/workflows/wheels.yml`).
160
+
161
+ ```sh
162
+ pip install cibuildwheel
163
+ python -m cibuildwheel --output-dir dist # current platform
164
+ python -m cibuildwheel --platform <os> --output-dir dist
165
+ ```
166
+
167
+ Notes:
168
+
169
+ - macOS: set `MACOSX_DEPLOYMENT_TARGET` (e.g. `11.0`) for older systems and
170
+ codesign the dylib (`codesign -s -`); `cibuildwheel` handles `delocate`.
171
+ - Windows: built with mingw-w64 (`before-all = "choco install -y mingw"`) and
172
+ `-static` so the DLL is standalone.
173
+ - Linux: `-pthread` is linked (manylinux glibc < 2.34 keeps pthread in
174
+ `libpthread`).
175
+
176
+ ## Release procedure
177
+
178
+ 1. **Generate the vendored snapshot** (makes the sdist self-contained; it is
179
+ gitignored, not committed):
180
+
181
+ ```sh
182
+ python sync_csrc.py
183
+ ```
184
+
185
+ 2. **Regenerate the binding** if `primal.h` changed:
186
+
187
+ ```sh
188
+ python tools/gen_bindings.py
189
+ ```
190
+
191
+ 3. **Bump the version** — the single source of truth is `__version__` in
192
+ `src/primalsolver/__init__.py` (`pyproject.toml` reads it via
193
+ `dynamic = ["version"]`).
194
+
195
+ 4. **Check locally**:
196
+
197
+ ```sh
198
+ pip install -e .
199
+ python -c "import primalsolver as ps; print(ps.version())"
200
+ python -m pip wheel . --no-build-isolation --no-deps -w dist
201
+ ```
202
+
203
+ 5. **Commit and tag** (in the Python repository, never the C one):
204
+
205
+ ```sh
206
+ git add -A && git commit -m "release vX.Y.Z"
207
+ git tag vX.Y.Z
208
+ git push origin main --tags
209
+ ```
210
+
211
+ 6. **Publish**: a Git **tag** triggers `.github/workflows/wheels.yml`, which
212
+ builds the wheels + sdist and uploads them as artifacts. Publishing to PyPI
213
+ runs only when a **GitHub Release** is published for that tag, using
214
+ **trusted publishing** (OIDC, no API token stored).
215
+
216
+ Prerequisites (one-time): register a PyPI *trusted publisher* for this
217
+ repository and create a `pypi` environment in the GitHub repo settings.
218
+
219
+ Manual/fallback publish (e.g. TestPyPI):
220
+
221
+ ```sh
222
+ pip install build twine
223
+ python -m build
224
+ twine check dist/*
225
+ twine upload --repository testpypi dist/* # then: twine upload dist/*
226
+ ```
227
+
228
+ ## Repository layout
229
+
230
+ ```
231
+ pyproject.toml # metadata + cibuildwheel config
232
+ setup.py # compiles the shared lib; forces the platform wheel tag
233
+ MANIFEST.in # sdist contents (vendored csrc, no prebuilt binaries)
234
+ sync_csrc.py # vendored C-source snapshot -> csrc/
235
+ tools/gen_bindings.py # primal.h -> src/primalsolver/_bindings.py (547 fns)
236
+ csrc/ # C-source snapshot (generated by sync_csrc.py; gitignored)
237
+ src/primalsolver/ # the installed package (__init__, _native, _bindings, _lib)
238
+ .github/workflows/wheels.yml
239
+ ```
240
+
241
+ ## License
242
+
243
+ Apache-2.0, same as PrimalSolver. See `LICENSE`.
@@ -0,0 +1,226 @@
1
+ # primalsolver
2
+
3
+ Python bindings for **PrimalSolver** — a dependency-free convex optimization
4
+ solver in C99 (LP / QP / SOCP / SDP / exp-power / MIP).
5
+
6
+ The package ships a pre-built shared library (`libprimal.dylib` / `.so` / `.dll`)
7
+ inside the wheel and calls it through `ctypes`: no C toolchain is needed to
8
+ install, and the whole public API is exposed.
9
+
10
+ > Upstream C library: <https://github.com/c-vision/Primal>. This package treats
11
+ > its sources as read-only input and never modifies them.
12
+
13
+ ```python
14
+ from primalsolver import Model, BK, SOLSTA
15
+
16
+ with Model(maxcon=2, maxvar=2) as m:
17
+ m.obj([0, 1], [-3.0, -2.0]) # min -3 x0 - 2 x1
18
+ m.a_ij(0, 0, 1.0); m.a_ij(0, 1, 1.0) # x0 + x1 <= 4
19
+ m.a_ij(1, 0, 1.0); m.a_ij(1, 1, 3.0) # x0 + 3x1 <= 6
20
+ m.con_bounds(0, BK.UP, up=4.0)
21
+ m.con_bounds(1, BK.UP, up=6.0)
22
+ m.var_bounds(0, BK.RA, 0.0, 3.0) # 0 <= x0 <= 3
23
+ m.var_bounds(1, BK.RA, 0.0, 3.0)
24
+ r = m.solve()
25
+
26
+ assert r.solsta == SOLSTA.OPTIMAL
27
+ print(r.x, r.objective) # [3.0, 1.0] -11.0
28
+ ```
29
+
30
+ ## Requirements
31
+
32
+ - Python >= 3.8, **no runtime dependencies** other than the standard library.
33
+ - A platform wheel is provided per OS/arch (macOS, Linux, Windows). On a platform
34
+ without a wheel you can build from source (see below).
35
+
36
+ ## Install
37
+
38
+ ```sh
39
+ pip install primalsolver
40
+ ```
41
+
42
+ Verify:
43
+
44
+ ```sh
45
+ python -c "import primalsolver as ps; print(ps.version())"
46
+ ```
47
+
48
+ ## Quickstart
49
+
50
+ Build and solve a model in code (above), or solve a **model file** — MPS, CPLEX
51
+ LP, OPF and CBF are auto-detected by content/extension:
52
+
53
+ ```python
54
+ from primalsolver import Model, SOLSTA
55
+
56
+ with Model() as m:
57
+ m.read("model.mps")
58
+ r = m.solve()
59
+
60
+ print(r.solsta, r.x, r.objective)
61
+ ```
62
+
63
+ The high-level `Model` covers the common path. Everything else is reachable
64
+ through the generated low-level binding.
65
+
66
+ ## Full API (all 547 functions)
67
+
68
+ `tools/gen_bindings.py` parses the C header `primal.h` and emits
69
+ `src/primalsolver/_bindings.py` with a ctypes signature for **every** `PRIMAL_*`
70
+ function. `_native.py` applies them and re-exports them, so the entire surface is
71
+ available:
72
+
73
+ ```python
74
+ import primalsolver as ps
75
+
76
+ ps.PRIMAL_getdualray(...) # every PRIMAL_* is available at package level
77
+ ps.FUNCTIONS # name -> ctypes function object (547)
78
+ ps.MISSING # declared in the header but absent in the lib (empty = OK)
79
+ ps._native.optimize # short aliases (PRIMAL_ removed) used by Model
80
+ ```
81
+
82
+ Handles (`PRIMALenv_t` / `PRIMALtask_t`) are opaque C pointers: the functions
83
+ that create/destroy them take a *pointer to the handle*, so pass
84
+ `ctypes.byref(...)`. Callback arguments are exposed as `c_void_p`; the two
85
+ variadic functions (`PRIMAL_echotask`, `PRIMAL_echoenv`) are exposed without
86
+ `argtypes` (ctypes cannot type C varargs) — pass explicit ctypes objects for the
87
+ extra arguments.
88
+
89
+ Regenerate the binding **after any change to `primal.h`**:
90
+
91
+ ```sh
92
+ python tools/gen_bindings.py
93
+ ```
94
+
95
+ ## What is shipped
96
+
97
+ - **Wheel**: only the `primalsolver` package — `__init__.py`, `_native.py`, the
98
+ generated `_bindings.py`, the compiled library in `_lib/`, plus `README.md` and
99
+ `LICENSE`.
100
+ - **Python examples** live in `../python_examples/` (tracked in the C repository),
101
+ not in this package. No test files are shipped.
102
+
103
+ ## Building from source
104
+
105
+ The C sources are **read-only input**; this project never modifies them. `setup.py`
106
+ compiles the shared library into `src/primalsolver/_lib/` before packaging.
107
+
108
+ Source directory resolution, in order:
109
+
110
+ 1. `$PRIMAL_C_SRC` — an explicit C checkout;
111
+ 2. `csrc/` — a vendored snapshot (`python sync_csrc.py`), used by the sdist/CI;
112
+ 3. `..` — the parent directory, i.e. the live C repository (default).
113
+
114
+ `csrc/` is generated by `sync_csrc.py` and is **gitignored**: the C sources have
115
+ a single home in the C repository. CI fetches them from
116
+ `github.com/c-vision/Primal` and regenerates `csrc/` before building.
117
+
118
+ Development install:
119
+
120
+ ```sh
121
+ python -m venv .venv && . .venv/bin/activate
122
+ pip install -U pip setuptools wheel
123
+ pip install -e .
124
+ python -c "import primalsolver as ps; print(ps.version())"
125
+ ```
126
+
127
+ Build a wheel locally (needs only `setuptools` + `wheel`, no `build` package):
128
+
129
+ ```sh
130
+ python -m pip wheel . --no-build-isolation --no-deps -w dist
131
+ ```
132
+
133
+ > The wheel bundles a native library, so it is **platform-specific**:
134
+ > `py3-none-<platform>` (e.g. `py3-none-macosx_11_0_arm64`), never `none-any`.
135
+
136
+ ## Building the distribution wheels (all platforms)
137
+
138
+ Config lives in `[tool.cibuildwheel]` (`pyproject.toml`); `cibuildwheel`
139
+ compiles inside each target environment (required for manylinux) and runs
140
+ `auditwheel` / `delocate`. In CI the C sources are fetched from
141
+ `github.com/c-vision/Primal` and vendored with `sync_csrc.py` before building
142
+ (see `.github/workflows/wheels.yml`).
143
+
144
+ ```sh
145
+ pip install cibuildwheel
146
+ python -m cibuildwheel --output-dir dist # current platform
147
+ python -m cibuildwheel --platform <os> --output-dir dist
148
+ ```
149
+
150
+ Notes:
151
+
152
+ - macOS: set `MACOSX_DEPLOYMENT_TARGET` (e.g. `11.0`) for older systems and
153
+ codesign the dylib (`codesign -s -`); `cibuildwheel` handles `delocate`.
154
+ - Windows: built with mingw-w64 (`before-all = "choco install -y mingw"`) and
155
+ `-static` so the DLL is standalone.
156
+ - Linux: `-pthread` is linked (manylinux glibc < 2.34 keeps pthread in
157
+ `libpthread`).
158
+
159
+ ## Release procedure
160
+
161
+ 1. **Generate the vendored snapshot** (makes the sdist self-contained; it is
162
+ gitignored, not committed):
163
+
164
+ ```sh
165
+ python sync_csrc.py
166
+ ```
167
+
168
+ 2. **Regenerate the binding** if `primal.h` changed:
169
+
170
+ ```sh
171
+ python tools/gen_bindings.py
172
+ ```
173
+
174
+ 3. **Bump the version** — the single source of truth is `__version__` in
175
+ `src/primalsolver/__init__.py` (`pyproject.toml` reads it via
176
+ `dynamic = ["version"]`).
177
+
178
+ 4. **Check locally**:
179
+
180
+ ```sh
181
+ pip install -e .
182
+ python -c "import primalsolver as ps; print(ps.version())"
183
+ python -m pip wheel . --no-build-isolation --no-deps -w dist
184
+ ```
185
+
186
+ 5. **Commit and tag** (in the Python repository, never the C one):
187
+
188
+ ```sh
189
+ git add -A && git commit -m "release vX.Y.Z"
190
+ git tag vX.Y.Z
191
+ git push origin main --tags
192
+ ```
193
+
194
+ 6. **Publish**: a Git **tag** triggers `.github/workflows/wheels.yml`, which
195
+ builds the wheels + sdist and uploads them as artifacts. Publishing to PyPI
196
+ runs only when a **GitHub Release** is published for that tag, using
197
+ **trusted publishing** (OIDC, no API token stored).
198
+
199
+ Prerequisites (one-time): register a PyPI *trusted publisher* for this
200
+ repository and create a `pypi` environment in the GitHub repo settings.
201
+
202
+ Manual/fallback publish (e.g. TestPyPI):
203
+
204
+ ```sh
205
+ pip install build twine
206
+ python -m build
207
+ twine check dist/*
208
+ twine upload --repository testpypi dist/* # then: twine upload dist/*
209
+ ```
210
+
211
+ ## Repository layout
212
+
213
+ ```
214
+ pyproject.toml # metadata + cibuildwheel config
215
+ setup.py # compiles the shared lib; forces the platform wheel tag
216
+ MANIFEST.in # sdist contents (vendored csrc, no prebuilt binaries)
217
+ sync_csrc.py # vendored C-source snapshot -> csrc/
218
+ tools/gen_bindings.py # primal.h -> src/primalsolver/_bindings.py (547 fns)
219
+ csrc/ # C-source snapshot (generated by sync_csrc.py; gitignored)
220
+ src/primalsolver/ # the installed package (__init__, _native, _bindings, _lib)
221
+ .github/workflows/wheels.yml
222
+ ```
223
+
224
+ ## License
225
+
226
+ Apache-2.0, same as PrimalSolver. See `LICENSE`.