specsolve 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 (49) hide show
  1. specsolve-0.1.0/.gitignore +38 -0
  2. specsolve-0.1.0/LICENSE +21 -0
  3. specsolve-0.1.0/PKG-INFO +176 -0
  4. specsolve-0.1.0/README.md +149 -0
  5. specsolve-0.1.0/pyproject.toml +334 -0
  6. specsolve-0.1.0/src/specsolve/__init__.py +71 -0
  7. specsolve-0.1.0/src/specsolve/api.py +744 -0
  8. specsolve-0.1.0/src/specsolve/archive.py +172 -0
  9. specsolve-0.1.0/src/specsolve/assumptions.py +82 -0
  10. specsolve-0.1.0/src/specsolve/errors.py +175 -0
  11. specsolve-0.1.0/src/specsolve/expressions.py +82 -0
  12. specsolve-0.1.0/src/specsolve/frames.py +139 -0
  13. specsolve-0.1.0/src/specsolve/lanes.py +147 -0
  14. specsolve-0.1.0/src/specsolve/layout.py +216 -0
  15. specsolve-0.1.0/src/specsolve/py.typed +0 -0
  16. specsolve-0.1.0/src/specsolve/relational/__init__.py +16 -0
  17. specsolve-0.1.0/src/specsolve/relational/collect.py +26 -0
  18. specsolve-0.1.0/src/specsolve/relational/engines/__init__.py +10 -0
  19. specsolve-0.1.0/src/specsolve/relational/engines/polars/__init__.py +6 -0
  20. specsolve-0.1.0/src/specsolve/relational/engines/polars/assembly.py +678 -0
  21. specsolve-0.1.0/src/specsolve/relational/engines/polars/attaching.py +115 -0
  22. specsolve-0.1.0/src/specsolve/relational/engines/polars/compiler.py +681 -0
  23. specsolve-0.1.0/src/specsolve/relational/engines/polars/coverage.py +220 -0
  24. specsolve-0.1.0/src/specsolve/relational/engines/polars/engine.py +514 -0
  25. specsolve-0.1.0/src/specsolve/relational/engines/polars/fragments.py +478 -0
  26. specsolve-0.1.0/src/specsolve/relational/engines/polars/labels.py +222 -0
  27. specsolve-0.1.0/src/specsolve/relational/engines/polars/predicates.py +437 -0
  28. specsolve-0.1.0/src/specsolve/relational/engines/polars/readback.py +315 -0
  29. specsolve-0.1.0/src/specsolve/relational/engines/polars/reindex.py +381 -0
  30. specsolve-0.1.0/src/specsolve/relational/engines/polars/relations.py +219 -0
  31. specsolve-0.1.0/src/specsolve/relational/engines/polars/scope.py +159 -0
  32. specsolve-0.1.0/src/specsolve/relational/parquet.py +441 -0
  33. specsolve-0.1.0/src/specsolve/relational/result.py +829 -0
  34. specsolve-0.1.0/src/specsolve/relational/sinks/README.md +200 -0
  35. specsolve-0.1.0/src/specsolve/relational/sinks/__init__.py +118 -0
  36. specsolve-0.1.0/src/specsolve/relational/sinks/capabilities.py +126 -0
  37. specsolve-0.1.0/src/specsolve/relational/sinks/handoff.py +419 -0
  38. specsolve-0.1.0/src/specsolve/relational/sinks/solvers/__init__.py +84 -0
  39. specsolve-0.1.0/src/specsolve/relational/sinks/solvers/base.py +366 -0
  40. specsolve-0.1.0/src/specsolve/relational/sinks/solvers/gurobi.py +538 -0
  41. specsolve-0.1.0/src/specsolve/relational/sinks/solvers/highs.py +419 -0
  42. specsolve-0.1.0/src/specsolve/relational/sinks/solvers/xpress.py +359 -0
  43. specsolve-0.1.0/src/specsolve/relational/sinks/writers/__init__.py +51 -0
  44. specsolve-0.1.0/src/specsolve/relational/sinks/writers/base.py +51 -0
  45. specsolve-0.1.0/src/specsolve/relational/sinks/writers/lp_file.py +280 -0
  46. specsolve-0.1.0/src/specsolve/relational/sinks/writers/mps_file.py +211 -0
  47. specsolve-0.1.0/src/specsolve/relational/status.py +71 -0
  48. specsolve-0.1.0/src/specsolve/sources.py +622 -0
  49. specsolve-0.1.0/src/specsolve/strategy.py +1916 -0
@@ -0,0 +1,38 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ *.egg
8
+ .eggs/
9
+ .mypy_cache/
10
+ .ruff_cache/
11
+ .pytest_cache/
12
+ .coverage
13
+ htmlcov/
14
+ *.so
15
+ .env
16
+ .venv/
17
+ venv/
18
+ .idea/
19
+ .pixi/
20
+ pixi.lock
21
+
22
+ bench/.cache/
23
+ .benchmarks/
24
+
25
+ scratch/relational_spike/*_data/
26
+ scratch/relational_spike/bench_out/
27
+ scratch/relational_spike/*.lp
28
+ .DS_Store
29
+
30
+ site/
31
+ .cache/
32
+
33
+ docs/*.html
34
+ docs/*.pdf
35
+ # The chart page is a source: it carries its numbers inline.
36
+ !docs/benchmarks-scaling.html
37
+
38
+ bench/results/.inflight
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Felix Bumann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,176 @@
1
+ Metadata-Version: 2.5
2
+ Name: specsolve
3
+ Version: 0.1.0
4
+ Summary: Self-documenting optimisation models: declarative LP/MILP, built relationally and handed straight to the solver
5
+ Project-URL: Documentation, https://specsolve.readthedocs.io
6
+ Project-URL: Repository, https://github.com/fluxopt/specsolve
7
+ Project-URL: Issues, https://github.com/fluxopt/specsolve/issues
8
+ Project-URL: Changelog, https://github.com/fluxopt/specsolve/blob/main/CHANGELOG.md
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: declarative,highs,linear-programming,mixed-integer,operations-research,optimisation,optimization,polars,yaml
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Typing :: Typed
16
+ Requires-Python: >=3.12
17
+ Requires-Dist: highspy>=1.13
18
+ Requires-Dist: mathspec>=0.2.0
19
+ Requires-Dist: numpy>=1.26
20
+ Requires-Dist: polars>=1.30
21
+ Provides-Extra: gurobi
22
+ Requires-Dist: gurobipy>=11; extra == 'gurobi'
23
+ Requires-Dist: scipy>=1.11; extra == 'gurobi'
24
+ Provides-Extra: xpress
25
+ Requires-Dist: xpress>=9.5; extra == 'xpress'
26
+ Description-Content-Type: text/markdown
27
+
28
+ # specsolve
29
+
30
+ <!-- --8<-- [start:badges] -->
31
+
32
+ [![CI](https://img.shields.io/github/actions/workflow/status/fluxopt/specsolve/ci.yml?style=flat-square&branch=main)](https://github.com/fluxopt/specsolve/actions/workflows/ci.yml)
33
+ [![PyPI](https://img.shields.io/pypi/v/specsolve.svg?logo=pypi&logoColor=white&style=flat-square)](https://pypi.org/project/specsolve/)
34
+ [![Python](https://img.shields.io/pypi/pyversions/specsolve?logo=python&logoColor=white&style=flat-square)](https://pypi.org/project/specsolve/)
35
+ [![Docs](https://readthedocs.org/projects/specsolve/badge/?version=latest&style=flat-square)](https://specsolve.readthedocs.io)
36
+ [![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg?style=flat-square)](https://github.com/fluxopt/specsolve/blob/main/LICENSE)
37
+
38
+ <!-- --8<-- [end:badges] -->
39
+
40
+ **Solve an optimisation problem written in YAML. Attach your data as tables, and
41
+ keep the solver loaded for quick updates and warm starts.**
42
+
43
+ <!-- --8<-- [start:intro] -->
44
+
45
+ specsolve solves [mathspec](https://github.com/energy-models/mathspec) specs.
46
+ A spec states the math, and mathspec checks it before any data exists.
47
+ specsolve attaches your tables to the spec, builds the resulting model on
48
+ polars, and hands it to HiGHS, Gurobi or Xpress.
49
+
50
+ <!-- --8<-- [end:intro] -->
51
+
52
+ <!-- --8<-- [start:benefits] -->
53
+
54
+ - **Tables in, tables out.** Pass any Arrow table, such as polars, pandas or
55
+ DuckDB, or a parquet path. Results come back as tables, and an archive keeps
56
+ the spec, its data and its results as parquet, ready for queries, plots or
57
+ BI. [Tables in, tables out →](https://specsolve.readthedocs.io/en/latest/tables/)
58
+ - **Sweeps and rolling horizons built in.** One call runs scenario sweeps,
59
+ rolling horizons and myopic pathways over the same spec. Each window is
60
+ checked against how the model couples before it runs. [Sweep a model →](https://specsolve.readthedocs.io/en/latest/sweep/)
61
+ - **Fast, and hard to get wrong.** Tables hold only the rows that exist, so a
62
+ model's topology does not change its cost. The solver stays loaded:
63
+ `update()` puts new numbers on it, and `keep='progress'` warm-starts from the
64
+ last run. The API is a handful of verbs, with nothing to tune.
65
+ [Benchmarks →](https://specsolve.readthedocs.io/en/latest/about/benchmarks-scaling.html)
66
+ - **Validated against PyPSA.** PyPSA's model is one file here, grown rung by
67
+ rung through storage, unit commitment, multi-period and stochastic runs. All
68
+ 16 rungs match PyPSA's objective, and 12 match its duals row for row.
69
+ [The PyPSA ladder →](https://specsolve.readthedocs.io/en/latest/examples/pypsa_ladder/)
70
+
71
+ <!-- --8<-- [end:benefits] -->
72
+
73
+ ## Example
74
+
75
+ <!-- --8<-- [start:model] -->
76
+ ```yaml
77
+ # dispatch.yaml
78
+ dimensions:
79
+ snapshot: {dtype: int}
80
+ generator: {dtype: str}
81
+ parameters:
82
+ p_max: {dims: [generator]}
83
+ load: {dims: [snapshot]}
84
+ cost: {dims: [generator]}
85
+ variables:
86
+ p:
87
+ dims: [snapshot, generator]
88
+ where: "p_max > 0"
89
+ bounds: {lower: 0, upper: p_max}
90
+ constraints:
91
+ power_balance:
92
+ dims: [snapshot]
93
+ expression: sum(p, over=generator) == load
94
+ objective:
95
+ sense: minimize
96
+ expression: sum(p * cost)
97
+ ```
98
+ <!-- --8<-- [end:model] -->
99
+
100
+ <!-- --8<-- [start:solve] -->
101
+
102
+ ```python
103
+ import specsolve as sps, polars as pl
104
+
105
+ generators = ['wind', 'solar', 'gas']
106
+ sources = { # (1)!
107
+ 'p_max': pl.DataFrame({'generator': generators, 'value': [100.0, 60.0, 200.0]}),
108
+ 'cost': pl.DataFrame({'generator': generators, 'value': [1.0, 2.0, 50.0]}),
109
+ 'load': pl.DataFrame({'snapshot': range(6), 'value': [80.0, 120.0, 150.0, 180.0, 140.0, 100.0]}),
110
+ 'snapshot': range(6),
111
+ 'generator': generators,
112
+ }
113
+
114
+ result = sps.solve('dispatch.yaml', sources, archive='runs/base/') # (2)!
115
+ print(result.objective) # 1920.0
116
+ print(result.primal('p')) # (3)!
117
+ print(result.dual('power_balance'))
118
+
119
+ base = sps.scan_archive('runs/base/') # (4)!
120
+ print(base.answer.primal('p').group_by('generator').agg(pl.col('value').sum()))
121
+ ```
122
+
123
+ 1. A source is any table: polars, pandas, pyarrow or DuckDB. It can also be a
124
+ parquet path, such as `'load': 'load.parquet'`.
125
+ 2. `archive=` writes the spec, the data and the answer to `runs/base/` as
126
+ parquet files.
127
+ 3. A tidy table, with one row per snapshot and generator.
128
+ 4. `scan_archive` reads the archive where it lies. `base.sources` are parquet
129
+ paths, so `sps.solve(base.spec, base.sources)` asks the same question again.
130
+
131
+ <!-- --8<-- [end:solve] -->
132
+
133
+ ## Documentation
134
+
135
+ The documentation is at <https://specsolve.readthedocs.io>. What a file may
136
+ contain is mathspec's
137
+ [language reference](https://mathspec.readthedocs.io/en/latest/reference/language/).
138
+
139
+ ## Installation
140
+
141
+ ```bash
142
+ pip install specsolve
143
+ ```
144
+
145
+ That brings polars, HiGHS and the language. Add the `[gurobi]` or `[xpress]`
146
+ extra for those solvers. The bridges out of a result, `to_pandas` and
147
+ `to_dataarray`, need pandas and xarray, which you install yourself. To work on
148
+ specsolve, see
149
+ [CONTRIBUTING.md](https://github.com/fluxopt/specsolve/blob/main/CONTRIBUTING.md).
150
+
151
+ ## Prior art
152
+
153
+ The YAML surface comes from [Calliope](https://github.com/calliope-project/calliope),
154
+ and [linopy](https://github.com/PyPSA/linopy) supplies the vocabulary, the
155
+ oracle and every benchmark denominator.
156
+ [Prior art and credit](https://specsolve.readthedocs.io/en/latest/about/prior-art/)
157
+ says what came from each.
158
+
159
+ ## Status
160
+
161
+ Alpha, pre-1.0.
162
+
163
+ <!-- --8<-- [start:status] -->
164
+
165
+ **Breaking changes land without a deprecation cycle.** Pin an exact version if
166
+ you depend on this, and read the
167
+ [changelog](https://github.com/fluxopt/specsolve/blob/main/CHANGELOG.md) before
168
+ upgrading. A retired spelling fails at load and names its rewrite. Real models
169
+ round-trip through solve and are tested against linopy. The accepted surface
170
+ is not yet frozen.
171
+
172
+ <!-- --8<-- [end:status] -->
173
+
174
+ ## Licence
175
+
176
+ [MIT](LICENSE).
@@ -0,0 +1,149 @@
1
+ # specsolve
2
+
3
+ <!-- --8<-- [start:badges] -->
4
+
5
+ [![CI](https://img.shields.io/github/actions/workflow/status/fluxopt/specsolve/ci.yml?style=flat-square&branch=main)](https://github.com/fluxopt/specsolve/actions/workflows/ci.yml)
6
+ [![PyPI](https://img.shields.io/pypi/v/specsolve.svg?logo=pypi&logoColor=white&style=flat-square)](https://pypi.org/project/specsolve/)
7
+ [![Python](https://img.shields.io/pypi/pyversions/specsolve?logo=python&logoColor=white&style=flat-square)](https://pypi.org/project/specsolve/)
8
+ [![Docs](https://readthedocs.org/projects/specsolve/badge/?version=latest&style=flat-square)](https://specsolve.readthedocs.io)
9
+ [![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg?style=flat-square)](https://github.com/fluxopt/specsolve/blob/main/LICENSE)
10
+
11
+ <!-- --8<-- [end:badges] -->
12
+
13
+ **Solve an optimisation problem written in YAML. Attach your data as tables, and
14
+ keep the solver loaded for quick updates and warm starts.**
15
+
16
+ <!-- --8<-- [start:intro] -->
17
+
18
+ specsolve solves [mathspec](https://github.com/energy-models/mathspec) specs.
19
+ A spec states the math, and mathspec checks it before any data exists.
20
+ specsolve attaches your tables to the spec, builds the resulting model on
21
+ polars, and hands it to HiGHS, Gurobi or Xpress.
22
+
23
+ <!-- --8<-- [end:intro] -->
24
+
25
+ <!-- --8<-- [start:benefits] -->
26
+
27
+ - **Tables in, tables out.** Pass any Arrow table, such as polars, pandas or
28
+ DuckDB, or a parquet path. Results come back as tables, and an archive keeps
29
+ the spec, its data and its results as parquet, ready for queries, plots or
30
+ BI. [Tables in, tables out →](https://specsolve.readthedocs.io/en/latest/tables/)
31
+ - **Sweeps and rolling horizons built in.** One call runs scenario sweeps,
32
+ rolling horizons and myopic pathways over the same spec. Each window is
33
+ checked against how the model couples before it runs. [Sweep a model →](https://specsolve.readthedocs.io/en/latest/sweep/)
34
+ - **Fast, and hard to get wrong.** Tables hold only the rows that exist, so a
35
+ model's topology does not change its cost. The solver stays loaded:
36
+ `update()` puts new numbers on it, and `keep='progress'` warm-starts from the
37
+ last run. The API is a handful of verbs, with nothing to tune.
38
+ [Benchmarks →](https://specsolve.readthedocs.io/en/latest/about/benchmarks-scaling.html)
39
+ - **Validated against PyPSA.** PyPSA's model is one file here, grown rung by
40
+ rung through storage, unit commitment, multi-period and stochastic runs. All
41
+ 16 rungs match PyPSA's objective, and 12 match its duals row for row.
42
+ [The PyPSA ladder →](https://specsolve.readthedocs.io/en/latest/examples/pypsa_ladder/)
43
+
44
+ <!-- --8<-- [end:benefits] -->
45
+
46
+ ## Example
47
+
48
+ <!-- --8<-- [start:model] -->
49
+ ```yaml
50
+ # dispatch.yaml
51
+ dimensions:
52
+ snapshot: {dtype: int}
53
+ generator: {dtype: str}
54
+ parameters:
55
+ p_max: {dims: [generator]}
56
+ load: {dims: [snapshot]}
57
+ cost: {dims: [generator]}
58
+ variables:
59
+ p:
60
+ dims: [snapshot, generator]
61
+ where: "p_max > 0"
62
+ bounds: {lower: 0, upper: p_max}
63
+ constraints:
64
+ power_balance:
65
+ dims: [snapshot]
66
+ expression: sum(p, over=generator) == load
67
+ objective:
68
+ sense: minimize
69
+ expression: sum(p * cost)
70
+ ```
71
+ <!-- --8<-- [end:model] -->
72
+
73
+ <!-- --8<-- [start:solve] -->
74
+
75
+ ```python
76
+ import specsolve as sps, polars as pl
77
+
78
+ generators = ['wind', 'solar', 'gas']
79
+ sources = { # (1)!
80
+ 'p_max': pl.DataFrame({'generator': generators, 'value': [100.0, 60.0, 200.0]}),
81
+ 'cost': pl.DataFrame({'generator': generators, 'value': [1.0, 2.0, 50.0]}),
82
+ 'load': pl.DataFrame({'snapshot': range(6), 'value': [80.0, 120.0, 150.0, 180.0, 140.0, 100.0]}),
83
+ 'snapshot': range(6),
84
+ 'generator': generators,
85
+ }
86
+
87
+ result = sps.solve('dispatch.yaml', sources, archive='runs/base/') # (2)!
88
+ print(result.objective) # 1920.0
89
+ print(result.primal('p')) # (3)!
90
+ print(result.dual('power_balance'))
91
+
92
+ base = sps.scan_archive('runs/base/') # (4)!
93
+ print(base.answer.primal('p').group_by('generator').agg(pl.col('value').sum()))
94
+ ```
95
+
96
+ 1. A source is any table: polars, pandas, pyarrow or DuckDB. It can also be a
97
+ parquet path, such as `'load': 'load.parquet'`.
98
+ 2. `archive=` writes the spec, the data and the answer to `runs/base/` as
99
+ parquet files.
100
+ 3. A tidy table, with one row per snapshot and generator.
101
+ 4. `scan_archive` reads the archive where it lies. `base.sources` are parquet
102
+ paths, so `sps.solve(base.spec, base.sources)` asks the same question again.
103
+
104
+ <!-- --8<-- [end:solve] -->
105
+
106
+ ## Documentation
107
+
108
+ The documentation is at <https://specsolve.readthedocs.io>. What a file may
109
+ contain is mathspec's
110
+ [language reference](https://mathspec.readthedocs.io/en/latest/reference/language/).
111
+
112
+ ## Installation
113
+
114
+ ```bash
115
+ pip install specsolve
116
+ ```
117
+
118
+ That brings polars, HiGHS and the language. Add the `[gurobi]` or `[xpress]`
119
+ extra for those solvers. The bridges out of a result, `to_pandas` and
120
+ `to_dataarray`, need pandas and xarray, which you install yourself. To work on
121
+ specsolve, see
122
+ [CONTRIBUTING.md](https://github.com/fluxopt/specsolve/blob/main/CONTRIBUTING.md).
123
+
124
+ ## Prior art
125
+
126
+ The YAML surface comes from [Calliope](https://github.com/calliope-project/calliope),
127
+ and [linopy](https://github.com/PyPSA/linopy) supplies the vocabulary, the
128
+ oracle and every benchmark denominator.
129
+ [Prior art and credit](https://specsolve.readthedocs.io/en/latest/about/prior-art/)
130
+ says what came from each.
131
+
132
+ ## Status
133
+
134
+ Alpha, pre-1.0.
135
+
136
+ <!-- --8<-- [start:status] -->
137
+
138
+ **Breaking changes land without a deprecation cycle.** Pin an exact version if
139
+ you depend on this, and read the
140
+ [changelog](https://github.com/fluxopt/specsolve/blob/main/CHANGELOG.md) before
141
+ upgrading. A retired spelling fails at load and names its rewrite. Real models
142
+ round-trip through solve and are tested against linopy. The accepted surface
143
+ is not yet frozen.
144
+
145
+ <!-- --8<-- [end:status] -->
146
+
147
+ ## Licence
148
+
149
+ [MIT](LICENSE).