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