solvor 0.3.3__tar.gz → 0.3.4__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.
- solvor-0.3.4/.github/workflows/ci.yml +209 -0
- {solvor-0.3.3 → solvor-0.3.4}/CONTRIBUTING.md +19 -10
- {solvor-0.3.3 → solvor-0.3.4}/PKG-INFO +8 -7
- {solvor-0.3.3 → solvor-0.3.4}/README.md +6 -6
- {solvor-0.3.3 → solvor-0.3.4}/pyproject.toml +3 -2
- solvor-0.3.4/tests/test_anneal.py +177 -0
- solvor-0.3.4/tests/test_bayesian.py +182 -0
- solvor-0.3.4/tests/test_cp.py +181 -0
- solvor-0.3.4/tests/test_dlx.py +282 -0
- solvor-0.3.4/tests/test_flow.py +244 -0
- solvor-0.3.4/tests/test_genetic.py +207 -0
- solvor-0.3.4/tests/test_gradient.py +207 -0
- solvor-0.3.4/tests/test_milp.py +120 -0
- solvor-0.3.4/tests/test_sat.py +187 -0
- solvor-0.3.4/tests/test_simplex.py +131 -0
- solvor-0.3.4/tests/test_tabu.py +191 -0
- solvor-0.3.3/.github/workflows/ci.yml +0 -25
- solvor-0.3.3/tests/test_solvers.py +0 -411
- {solvor-0.3.3 → solvor-0.3.4}/.github/workflows/publish.yml +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/.gitignore +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/LICENSE +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/__init__.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/anneal.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/bayesian.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/cp.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/dlx.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/flow.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/genetic.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/gradient.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/milp.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/sat.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/simplex.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/tabu.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/solvor/types.py +0 -0
- {solvor-0.3.3 → solvor-0.3.4}/tests/__init__.py +0 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, dev]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
lint:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Install uv
|
|
16
|
+
uses: astral-sh/setup-uv@v4
|
|
17
|
+
|
|
18
|
+
- name: Install dependencies
|
|
19
|
+
run: uv sync --extra dev
|
|
20
|
+
|
|
21
|
+
- name: Run linter
|
|
22
|
+
run: uv run ruff check solvor/
|
|
23
|
+
|
|
24
|
+
# Detect which solvers have changed
|
|
25
|
+
changes:
|
|
26
|
+
runs-on: ubuntu-latest
|
|
27
|
+
outputs:
|
|
28
|
+
simplex: ${{ steps.filter.outputs.simplex }}
|
|
29
|
+
milp: ${{ steps.filter.outputs.milp }}
|
|
30
|
+
anneal: ${{ steps.filter.outputs.anneal }}
|
|
31
|
+
tabu: ${{ steps.filter.outputs.tabu }}
|
|
32
|
+
sat: ${{ steps.filter.outputs.sat }}
|
|
33
|
+
cp: ${{ steps.filter.outputs.cp }}
|
|
34
|
+
bayesian: ${{ steps.filter.outputs.bayesian }}
|
|
35
|
+
genetic: ${{ steps.filter.outputs.genetic }}
|
|
36
|
+
flow: ${{ steps.filter.outputs.flow }}
|
|
37
|
+
gradient: ${{ steps.filter.outputs.gradient }}
|
|
38
|
+
dlx: ${{ steps.filter.outputs.dlx }}
|
|
39
|
+
types: ${{ steps.filter.outputs.types }}
|
|
40
|
+
tests: ${{ steps.filter.outputs.tests }}
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/checkout@v4
|
|
43
|
+
- uses: dorny/paths-filter@v3
|
|
44
|
+
id: filter
|
|
45
|
+
with:
|
|
46
|
+
filters: |
|
|
47
|
+
simplex:
|
|
48
|
+
- 'solvor/simplex.py'
|
|
49
|
+
- 'tests/test_simplex.py'
|
|
50
|
+
milp:
|
|
51
|
+
- 'solvor/milp.py'
|
|
52
|
+
- 'solvor/simplex.py'
|
|
53
|
+
- 'tests/test_milp.py'
|
|
54
|
+
anneal:
|
|
55
|
+
- 'solvor/anneal.py'
|
|
56
|
+
- 'tests/test_anneal.py'
|
|
57
|
+
tabu:
|
|
58
|
+
- 'solvor/tabu.py'
|
|
59
|
+
- 'tests/test_tabu.py'
|
|
60
|
+
sat:
|
|
61
|
+
- 'solvor/sat.py'
|
|
62
|
+
- 'tests/test_sat.py'
|
|
63
|
+
cp:
|
|
64
|
+
- 'solvor/cp.py'
|
|
65
|
+
- 'solvor/sat.py'
|
|
66
|
+
- 'tests/test_cp.py'
|
|
67
|
+
bayesian:
|
|
68
|
+
- 'solvor/bayesian.py'
|
|
69
|
+
- 'tests/test_bayesian.py'
|
|
70
|
+
genetic:
|
|
71
|
+
- 'solvor/genetic.py'
|
|
72
|
+
- 'tests/test_genetic.py'
|
|
73
|
+
flow:
|
|
74
|
+
- 'solvor/flow.py'
|
|
75
|
+
- 'tests/test_flow.py'
|
|
76
|
+
gradient:
|
|
77
|
+
- 'solvor/gradient.py'
|
|
78
|
+
- 'tests/test_gradient.py'
|
|
79
|
+
dlx:
|
|
80
|
+
- 'solvor/dlx.py'
|
|
81
|
+
- 'tests/test_dlx.py'
|
|
82
|
+
types:
|
|
83
|
+
- 'solvor/types.py'
|
|
84
|
+
- 'solvor/__init__.py'
|
|
85
|
+
tests:
|
|
86
|
+
- 'tests/**'
|
|
87
|
+
- 'pyproject.toml'
|
|
88
|
+
|
|
89
|
+
# Run tests for changed solvers only
|
|
90
|
+
test-simplex:
|
|
91
|
+
needs: changes
|
|
92
|
+
if: ${{ needs.changes.outputs.simplex == 'true' || needs.changes.outputs.types == 'true' }}
|
|
93
|
+
runs-on: ubuntu-latest
|
|
94
|
+
steps:
|
|
95
|
+
- uses: actions/checkout@v4
|
|
96
|
+
- uses: astral-sh/setup-uv@v4
|
|
97
|
+
- run: uv sync --extra dev
|
|
98
|
+
- run: uv run pytest tests/test_simplex.py -v
|
|
99
|
+
|
|
100
|
+
test-milp:
|
|
101
|
+
needs: changes
|
|
102
|
+
if: ${{ needs.changes.outputs.milp == 'true' || needs.changes.outputs.types == 'true' }}
|
|
103
|
+
runs-on: ubuntu-latest
|
|
104
|
+
steps:
|
|
105
|
+
- uses: actions/checkout@v4
|
|
106
|
+
- uses: astral-sh/setup-uv@v4
|
|
107
|
+
- run: uv sync --extra dev
|
|
108
|
+
- run: uv run pytest tests/test_milp.py -v
|
|
109
|
+
|
|
110
|
+
test-anneal:
|
|
111
|
+
needs: changes
|
|
112
|
+
if: ${{ needs.changes.outputs.anneal == 'true' || needs.changes.outputs.types == 'true' }}
|
|
113
|
+
runs-on: ubuntu-latest
|
|
114
|
+
steps:
|
|
115
|
+
- uses: actions/checkout@v4
|
|
116
|
+
- uses: astral-sh/setup-uv@v4
|
|
117
|
+
- run: uv sync --extra dev
|
|
118
|
+
- run: uv run pytest tests/test_anneal.py -v
|
|
119
|
+
|
|
120
|
+
test-tabu:
|
|
121
|
+
needs: changes
|
|
122
|
+
if: ${{ needs.changes.outputs.tabu == 'true' || needs.changes.outputs.types == 'true' }}
|
|
123
|
+
runs-on: ubuntu-latest
|
|
124
|
+
steps:
|
|
125
|
+
- uses: actions/checkout@v4
|
|
126
|
+
- uses: astral-sh/setup-uv@v4
|
|
127
|
+
- run: uv sync --extra dev
|
|
128
|
+
- run: uv run pytest tests/test_tabu.py -v
|
|
129
|
+
|
|
130
|
+
test-sat:
|
|
131
|
+
needs: changes
|
|
132
|
+
if: ${{ needs.changes.outputs.sat == 'true' || needs.changes.outputs.types == 'true' }}
|
|
133
|
+
runs-on: ubuntu-latest
|
|
134
|
+
steps:
|
|
135
|
+
- uses: actions/checkout@v4
|
|
136
|
+
- uses: astral-sh/setup-uv@v4
|
|
137
|
+
- run: uv sync --extra dev
|
|
138
|
+
- run: uv run pytest tests/test_sat.py -v
|
|
139
|
+
|
|
140
|
+
test-cp:
|
|
141
|
+
needs: changes
|
|
142
|
+
if: ${{ needs.changes.outputs.cp == 'true' || needs.changes.outputs.types == 'true' }}
|
|
143
|
+
runs-on: ubuntu-latest
|
|
144
|
+
steps:
|
|
145
|
+
- uses: actions/checkout@v4
|
|
146
|
+
- uses: astral-sh/setup-uv@v4
|
|
147
|
+
- run: uv sync --extra dev
|
|
148
|
+
- run: uv run pytest tests/test_cp.py -v
|
|
149
|
+
|
|
150
|
+
test-bayesian:
|
|
151
|
+
needs: changes
|
|
152
|
+
if: ${{ needs.changes.outputs.bayesian == 'true' || needs.changes.outputs.types == 'true' }}
|
|
153
|
+
runs-on: ubuntu-latest
|
|
154
|
+
steps:
|
|
155
|
+
- uses: actions/checkout@v4
|
|
156
|
+
- uses: astral-sh/setup-uv@v4
|
|
157
|
+
- run: uv sync --extra dev
|
|
158
|
+
- run: uv run pytest tests/test_bayesian.py -v
|
|
159
|
+
|
|
160
|
+
test-genetic:
|
|
161
|
+
needs: changes
|
|
162
|
+
if: ${{ needs.changes.outputs.genetic == 'true' || needs.changes.outputs.types == 'true' }}
|
|
163
|
+
runs-on: ubuntu-latest
|
|
164
|
+
steps:
|
|
165
|
+
- uses: actions/checkout@v4
|
|
166
|
+
- uses: astral-sh/setup-uv@v4
|
|
167
|
+
- run: uv sync --extra dev
|
|
168
|
+
- run: uv run pytest tests/test_genetic.py -v
|
|
169
|
+
|
|
170
|
+
test-flow:
|
|
171
|
+
needs: changes
|
|
172
|
+
if: ${{ needs.changes.outputs.flow == 'true' || needs.changes.outputs.types == 'true' }}
|
|
173
|
+
runs-on: ubuntu-latest
|
|
174
|
+
steps:
|
|
175
|
+
- uses: actions/checkout@v4
|
|
176
|
+
- uses: astral-sh/setup-uv@v4
|
|
177
|
+
- run: uv sync --extra dev
|
|
178
|
+
- run: uv run pytest tests/test_flow.py -v
|
|
179
|
+
|
|
180
|
+
test-gradient:
|
|
181
|
+
needs: changes
|
|
182
|
+
if: ${{ needs.changes.outputs.gradient == 'true' || needs.changes.outputs.types == 'true' }}
|
|
183
|
+
runs-on: ubuntu-latest
|
|
184
|
+
steps:
|
|
185
|
+
- uses: actions/checkout@v4
|
|
186
|
+
- uses: astral-sh/setup-uv@v4
|
|
187
|
+
- run: uv sync --extra dev
|
|
188
|
+
- run: uv run pytest tests/test_gradient.py -v
|
|
189
|
+
|
|
190
|
+
test-dlx:
|
|
191
|
+
needs: changes
|
|
192
|
+
if: ${{ needs.changes.outputs.dlx == 'true' || needs.changes.outputs.types == 'true' }}
|
|
193
|
+
runs-on: ubuntu-latest
|
|
194
|
+
steps:
|
|
195
|
+
- uses: actions/checkout@v4
|
|
196
|
+
- uses: astral-sh/setup-uv@v4
|
|
197
|
+
- run: uv sync --extra dev
|
|
198
|
+
- run: uv run pytest tests/test_dlx.py -v
|
|
199
|
+
|
|
200
|
+
# Full test suite on main branch or when test infrastructure changes
|
|
201
|
+
test-all:
|
|
202
|
+
needs: changes
|
|
203
|
+
if: ${{ github.ref == 'refs/heads/main' || needs.changes.outputs.tests == 'true' }}
|
|
204
|
+
runs-on: ubuntu-latest
|
|
205
|
+
steps:
|
|
206
|
+
- uses: actions/checkout@v4
|
|
207
|
+
- uses: astral-sh/setup-uv@v4
|
|
208
|
+
- run: uv sync --extra dev
|
|
209
|
+
- run: uv run pytest tests/ -v
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# Contributing to
|
|
1
|
+
# Contributing to solvOR
|
|
2
2
|
|
|
3
|
-
Thanks for your interest in contributing to
|
|
3
|
+
Thanks for your interest in contributing to solvor!
|
|
4
4
|
|
|
5
5
|
## Development Setup
|
|
6
6
|
|
|
@@ -33,7 +33,7 @@ Follow the project's style:
|
|
|
33
33
|
|
|
34
34
|
## Terminology
|
|
35
35
|
|
|
36
|
-
Use consistent naming across all
|
|
36
|
+
Use consistent naming across all solvors:
|
|
37
37
|
|
|
38
38
|
| Term | Usage |
|
|
39
39
|
|------|-------|
|
|
@@ -44,26 +44,35 @@ Use consistent naming across all solvers:
|
|
|
44
44
|
| `neighbors` | Adjacent solutions (metaheuristics) |
|
|
45
45
|
| `minimize` | Boolean flag (default `True`) |
|
|
46
46
|
|
|
47
|
-
## Adding a New
|
|
47
|
+
## Adding a New Solvor
|
|
48
48
|
|
|
49
|
-
1. Create `
|
|
49
|
+
1. Create `solvor/<solver_name>.py`
|
|
50
50
|
2. Import shared types: `from solvor.types import Status, Result`
|
|
51
51
|
3. Export `Status`, `Result`, and main solver function in `__all__`
|
|
52
|
-
4. Add exports to `
|
|
53
|
-
5.
|
|
52
|
+
4. Add exports to `solvor/__init__.py`
|
|
53
|
+
5. Create `tests/test_<solver_name>.py` with comprehensive tests
|
|
54
54
|
6. Update `README.md` with usage examples
|
|
55
|
+
7. Add the solver to `.github/workflows/ci.yml` path filters
|
|
55
56
|
|
|
56
57
|
## Testing
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
Each solver has its own test file (`tests/test_<solver_name>.py`). Tests should cover:
|
|
59
60
|
- Basic functionality
|
|
60
|
-
- Edge cases (empty input, infeasible, etc.)
|
|
61
|
+
- Edge cases (empty input, infeasible, single variable, etc.)
|
|
61
62
|
- Minimize and maximize modes
|
|
63
|
+
- Parameter variations
|
|
64
|
+
- Stress tests with larger inputs
|
|
62
65
|
|
|
63
66
|
```bash
|
|
67
|
+
# Run all tests
|
|
64
68
|
uv run pytest tests/ -v
|
|
69
|
+
|
|
70
|
+
# Run tests for a specific solver
|
|
71
|
+
uv run pytest tests/test_simplex.py -v
|
|
65
72
|
```
|
|
66
73
|
|
|
74
|
+
The CI runs tests selectively based on which files changed - only the affected solver's tests run.
|
|
75
|
+
|
|
67
76
|
## Pull Requests
|
|
68
77
|
|
|
69
78
|
1. Fork the repo
|
|
@@ -77,4 +86,4 @@ uv run pytest tests/ -v
|
|
|
77
86
|
1. Usable > perfect
|
|
78
87
|
2. Readable > clever
|
|
79
88
|
3. Simple > general
|
|
80
|
-
4. Fast, but understandable
|
|
89
|
+
4. Fast, but understandable
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: solvor
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.4
|
|
4
4
|
Summary: Solvor your optimization needs.
|
|
5
5
|
Project-URL: Repository, https://github.com/StevenBtw/solvor
|
|
6
|
+
Project-URL: PyPI, https://pypi.org/project/solvor
|
|
6
7
|
Author: Steven Bytheway
|
|
7
8
|
License: Apache-2.0
|
|
8
9
|
License-File: LICENSE
|
|
@@ -18,7 +19,7 @@ Requires-Dist: pytest>=9.0.2; extra == 'dev'
|
|
|
18
19
|
Requires-Dist: ruff>=0.14.10; extra == 'dev'
|
|
19
20
|
Description-Content-Type: text/markdown
|
|
20
21
|
|
|
21
|
-
#
|
|
22
|
+
# solvOR
|
|
22
23
|
|
|
23
24
|

|
|
24
25
|
[](LICENSE)
|
|
@@ -27,7 +28,7 @@ Solvor your optimization needs..
|
|
|
27
28
|
|
|
28
29
|
## What's in the box?
|
|
29
30
|
|
|
30
|
-
| Category |
|
|
31
|
+
| Category | Solvors | Use Case |
|
|
31
32
|
|----------|---------|----------|
|
|
32
33
|
| **Linear/Integer** | `solve_lp`, `solve_milp` | Resource allocation, scheduling |
|
|
33
34
|
| **Constraint** | `solve_sat` | Sudoku, configuration, puzzles |
|
|
@@ -70,7 +71,7 @@ print(result.solution) # {'x': 3, 'y': 7}
|
|
|
70
71
|
|
|
71
72
|
---
|
|
72
73
|
|
|
73
|
-
##
|
|
74
|
+
## Solvors
|
|
74
75
|
|
|
75
76
|
<details>
|
|
76
77
|
<summary><strong>Linear & Integer Programming</strong></summary>
|
|
@@ -254,7 +255,7 @@ result = solve_exact_cover(matrix)
|
|
|
254
255
|
|
|
255
256
|
## Result Format
|
|
256
257
|
|
|
257
|
-
All
|
|
258
|
+
All solvors return a consistent `Result` namedtuple:
|
|
258
259
|
|
|
259
260
|
```python
|
|
260
261
|
Result(
|
|
@@ -270,7 +271,7 @@ Result(
|
|
|
270
271
|
|
|
271
272
|
## When to use what?
|
|
272
273
|
|
|
273
|
-
| Problem |
|
|
274
|
+
| Problem | Solvor |
|
|
274
275
|
|---------|--------|
|
|
275
276
|
| Linear constraints, continuous variables | `solve_lp` |
|
|
276
277
|
| Linear constraints, some integers | `solve_milp` |
|
|
@@ -300,4 +301,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
|
|
|
300
301
|
|
|
301
302
|
## License
|
|
302
303
|
|
|
303
|
-
[Apache 2.0 License](LICENSE) - free for personal and commercial use.
|
|
304
|
+
[Apache 2.0 License](LICENSE) - free for personal and commercial use.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# solvOR
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
[](LICENSE)
|
|
@@ -7,7 +7,7 @@ Solvor your optimization needs..
|
|
|
7
7
|
|
|
8
8
|
## What's in the box?
|
|
9
9
|
|
|
10
|
-
| Category |
|
|
10
|
+
| Category | Solvors | Use Case |
|
|
11
11
|
|----------|---------|----------|
|
|
12
12
|
| **Linear/Integer** | `solve_lp`, `solve_milp` | Resource allocation, scheduling |
|
|
13
13
|
| **Constraint** | `solve_sat` | Sudoku, configuration, puzzles |
|
|
@@ -50,7 +50,7 @@ print(result.solution) # {'x': 3, 'y': 7}
|
|
|
50
50
|
|
|
51
51
|
---
|
|
52
52
|
|
|
53
|
-
##
|
|
53
|
+
## Solvors
|
|
54
54
|
|
|
55
55
|
<details>
|
|
56
56
|
<summary><strong>Linear & Integer Programming</strong></summary>
|
|
@@ -234,7 +234,7 @@ result = solve_exact_cover(matrix)
|
|
|
234
234
|
|
|
235
235
|
## Result Format
|
|
236
236
|
|
|
237
|
-
All
|
|
237
|
+
All solvors return a consistent `Result` namedtuple:
|
|
238
238
|
|
|
239
239
|
```python
|
|
240
240
|
Result(
|
|
@@ -250,7 +250,7 @@ Result(
|
|
|
250
250
|
|
|
251
251
|
## When to use what?
|
|
252
252
|
|
|
253
|
-
| Problem |
|
|
253
|
+
| Problem | Solvor |
|
|
254
254
|
|---------|--------|
|
|
255
255
|
| Linear constraints, continuous variables | `solve_lp` |
|
|
256
256
|
| Linear constraints, some integers | `solve_milp` |
|
|
@@ -280,4 +280,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
|
|
|
280
280
|
|
|
281
281
|
## License
|
|
282
282
|
|
|
283
|
-
[Apache 2.0 License](LICENSE) - free for personal and commercial use.
|
|
283
|
+
[Apache 2.0 License](LICENSE) - free for personal and commercial use.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "solvor"
|
|
3
|
-
version = "0.3.
|
|
3
|
+
version = "0.3.4"
|
|
4
4
|
description = "Solvor your optimization needs."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.14"
|
|
@@ -20,6 +20,7 @@ classifiers = [
|
|
|
20
20
|
|
|
21
21
|
[project.urls]
|
|
22
22
|
Repository = "https://github.com/StevenBtw/solvor"
|
|
23
|
+
PyPI = "https://pypi.org/project/solvor"
|
|
23
24
|
|
|
24
25
|
[project.optional-dependencies]
|
|
25
26
|
dev = [
|
|
@@ -32,4 +33,4 @@ requires = ["hatchling"]
|
|
|
32
33
|
build-backend = "hatchling.build"
|
|
33
34
|
|
|
34
35
|
[tool.ruff]
|
|
35
|
-
line-length = 100
|
|
36
|
+
line-length = 100
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Tests for the simulated annealing solver."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from random import gauss, seed
|
|
5
|
+
from solvor.anneal import anneal, Status
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def make_neighbor_fn(std=0.5):
|
|
9
|
+
"""Create a neighbor function with gaussian perturbation."""
|
|
10
|
+
def neighbor(x):
|
|
11
|
+
return [xi + gauss(0, std) for xi in x]
|
|
12
|
+
return neighbor
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TestBasicAnneal:
|
|
16
|
+
def test_minimize_quadratic(self):
|
|
17
|
+
# Minimize x^2, starting from x=5
|
|
18
|
+
seed(42)
|
|
19
|
+
result = anneal(
|
|
20
|
+
[5.0],
|
|
21
|
+
lambda x: x[0] ** 2,
|
|
22
|
+
make_neighbor_fn(0.5),
|
|
23
|
+
max_iter=5000
|
|
24
|
+
)
|
|
25
|
+
assert result.status in (Status.FEASIBLE, Status.MAX_ITER)
|
|
26
|
+
assert abs(result.solution[0]) < 1.0
|
|
27
|
+
|
|
28
|
+
def test_maximize_quadratic(self):
|
|
29
|
+
# Maximize -x^2 (peak at 0)
|
|
30
|
+
seed(42)
|
|
31
|
+
result = anneal(
|
|
32
|
+
[5.0],
|
|
33
|
+
lambda x: -x[0] ** 2,
|
|
34
|
+
make_neighbor_fn(0.5),
|
|
35
|
+
minimize=False,
|
|
36
|
+
max_iter=5000
|
|
37
|
+
)
|
|
38
|
+
assert abs(result.solution[0]) < 1.0
|
|
39
|
+
|
|
40
|
+
def test_2d_optimization(self):
|
|
41
|
+
# Minimize x^2 + y^2
|
|
42
|
+
seed(42)
|
|
43
|
+
result = anneal(
|
|
44
|
+
[3.0, 4.0],
|
|
45
|
+
lambda x: x[0] ** 2 + x[1] ** 2,
|
|
46
|
+
make_neighbor_fn(0.3),
|
|
47
|
+
max_iter=10000
|
|
48
|
+
)
|
|
49
|
+
assert result.status in (Status.FEASIBLE, Status.MAX_ITER)
|
|
50
|
+
assert abs(result.solution[0]) < 1.5
|
|
51
|
+
assert abs(result.solution[1]) < 1.5
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TestMultiModal:
|
|
55
|
+
def test_rastrigin_like(self):
|
|
56
|
+
# Simple multimodal function: x^2 + sin(4*x)
|
|
57
|
+
# Has local minima but global at x~0
|
|
58
|
+
seed(42)
|
|
59
|
+
result = anneal(
|
|
60
|
+
[3.0],
|
|
61
|
+
lambda x: x[0] ** 2 + 0.5 * (1 - __import__('math').cos(4 * x[0])),
|
|
62
|
+
make_neighbor_fn(0.3),
|
|
63
|
+
max_iter=5000,
|
|
64
|
+
temperature=100.0
|
|
65
|
+
)
|
|
66
|
+
# Should find something near the global minimum
|
|
67
|
+
assert result.objective < 2.0
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class TestParameters:
|
|
71
|
+
def test_high_temperature(self):
|
|
72
|
+
# High temperature allows more exploration
|
|
73
|
+
seed(42)
|
|
74
|
+
result = anneal(
|
|
75
|
+
[10.0],
|
|
76
|
+
lambda x: x[0] ** 2,
|
|
77
|
+
make_neighbor_fn(1.0),
|
|
78
|
+
temperature=10000.0,
|
|
79
|
+
max_iter=3000
|
|
80
|
+
)
|
|
81
|
+
assert result.status in (Status.FEASIBLE, Status.MAX_ITER)
|
|
82
|
+
|
|
83
|
+
def test_fast_cooling(self):
|
|
84
|
+
# Fast cooling converges quickly
|
|
85
|
+
seed(42)
|
|
86
|
+
result = anneal(
|
|
87
|
+
[5.0],
|
|
88
|
+
lambda x: x[0] ** 2,
|
|
89
|
+
make_neighbor_fn(0.5),
|
|
90
|
+
cooling=0.99,
|
|
91
|
+
max_iter=1000
|
|
92
|
+
)
|
|
93
|
+
assert result.status in (Status.FEASIBLE, Status.MAX_ITER)
|
|
94
|
+
|
|
95
|
+
def test_min_temp_stop(self):
|
|
96
|
+
# Should stop when temperature drops below min_temp
|
|
97
|
+
seed(42)
|
|
98
|
+
result = anneal(
|
|
99
|
+
[5.0],
|
|
100
|
+
lambda x: x[0] ** 2,
|
|
101
|
+
make_neighbor_fn(0.5),
|
|
102
|
+
min_temp=1e-4,
|
|
103
|
+
cooling=0.99,
|
|
104
|
+
max_iter=100000
|
|
105
|
+
)
|
|
106
|
+
assert result.status == Status.FEASIBLE # Stopped by min_temp, not max_iter
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class TestEdgeCases:
|
|
110
|
+
def test_already_optimal(self):
|
|
111
|
+
# Start at optimal
|
|
112
|
+
seed(42)
|
|
113
|
+
result = anneal(
|
|
114
|
+
[0.0],
|
|
115
|
+
lambda x: x[0] ** 2,
|
|
116
|
+
make_neighbor_fn(0.1),
|
|
117
|
+
max_iter=1000
|
|
118
|
+
)
|
|
119
|
+
# Should stay near optimal
|
|
120
|
+
assert abs(result.solution[0]) < 0.5
|
|
121
|
+
|
|
122
|
+
def test_discrete_neighbor(self):
|
|
123
|
+
# Discrete optimization
|
|
124
|
+
seed(42)
|
|
125
|
+
def discrete_objective(x):
|
|
126
|
+
return abs(x[0] - 7)
|
|
127
|
+
|
|
128
|
+
def discrete_neighbor(x):
|
|
129
|
+
from random import choice
|
|
130
|
+
delta = choice([-1, 1])
|
|
131
|
+
return [x[0] + delta]
|
|
132
|
+
|
|
133
|
+
result = anneal(
|
|
134
|
+
[0],
|
|
135
|
+
discrete_objective,
|
|
136
|
+
discrete_neighbor,
|
|
137
|
+
max_iter=1000
|
|
138
|
+
)
|
|
139
|
+
assert abs(result.solution[0] - 7) < 3
|
|
140
|
+
|
|
141
|
+
def test_high_dimensional(self):
|
|
142
|
+
# Higher dimensional problem
|
|
143
|
+
seed(42)
|
|
144
|
+
n = 10
|
|
145
|
+
result = anneal(
|
|
146
|
+
[5.0] * n,
|
|
147
|
+
lambda x: sum(xi ** 2 for xi in x),
|
|
148
|
+
make_neighbor_fn(0.2),
|
|
149
|
+
max_iter=20000
|
|
150
|
+
)
|
|
151
|
+
# Should reduce the objective significantly
|
|
152
|
+
assert result.objective < sum(5.0 ** 2 for _ in range(n)) * 0.5
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class TestStress:
|
|
156
|
+
def test_many_iterations(self):
|
|
157
|
+
# Long run for better convergence
|
|
158
|
+
seed(42)
|
|
159
|
+
result = anneal(
|
|
160
|
+
[10.0],
|
|
161
|
+
lambda x: x[0] ** 2,
|
|
162
|
+
make_neighbor_fn(0.3),
|
|
163
|
+
max_iter=50000
|
|
164
|
+
)
|
|
165
|
+
assert abs(result.solution[0]) < 0.5
|
|
166
|
+
|
|
167
|
+
def test_evaluations_counted(self):
|
|
168
|
+
# Verify evaluations are tracked
|
|
169
|
+
seed(42)
|
|
170
|
+
result = anneal(
|
|
171
|
+
[5.0],
|
|
172
|
+
lambda x: x[0] ** 2,
|
|
173
|
+
make_neighbor_fn(0.5),
|
|
174
|
+
max_iter=100
|
|
175
|
+
)
|
|
176
|
+
# Should have at least initial + max_iter evaluations
|
|
177
|
+
assert result.evaluations >= 100
|