solvor 0.4.2__tar.gz → 0.4.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.4.4/.envrc +8 -0
- {solvor-0.4.2 → solvor-0.4.4}/.github/workflows/ci.yml +3 -1
- solvor-0.4.4/.pre-commit-config.yaml +31 -0
- {solvor-0.4.2 → solvor-0.4.4}/CONTRIBUTING.md +2 -1
- {solvor-0.4.2 → solvor-0.4.4}/LICENSE +1 -1
- {solvor-0.4.2 → solvor-0.4.4}/PKG-INFO +12 -7
- {solvor-0.4.2 → solvor-0.4.4}/README.md +9 -5
- {solvor-0.4.2 → solvor-0.4.4}/pyproject.toml +8 -17
- {solvor-0.4.2 → solvor-0.4.4}/solvor/__init__.py +32 -9
- {solvor-0.4.2 → solvor-0.4.4}/solvor/a_star.py +12 -6
- {solvor-0.4.2 → solvor-0.4.4}/solvor/anneal.py +8 -3
- {solvor-0.4.2 → solvor-0.4.4}/solvor/bayesian.py +6 -3
- {solvor-0.4.2 → solvor-0.4.4}/solvor/bellman_ford.py +12 -11
- {solvor-0.4.2 → solvor-0.4.4}/solvor/bfs.py +10 -15
- {solvor-0.4.2 → solvor-0.4.4}/solvor/cp.py +30 -28
- {solvor-0.4.2 → solvor-0.4.4}/solvor/dijkstra.py +8 -7
- {solvor-0.4.2 → solvor-0.4.4}/solvor/dlx.py +28 -17
- {solvor-0.4.2 → solvor-0.4.4}/solvor/flow.py +21 -18
- {solvor-0.4.2 → solvor-0.4.4}/solvor/floyd_warshall.py +5 -5
- {solvor-0.4.2 → solvor-0.4.4}/solvor/genetic.py +6 -6
- {solvor-0.4.2 → solvor-0.4.4}/solvor/gradient.py +9 -9
- {solvor-0.4.2 → solvor-0.4.4}/solvor/hungarian.py +2 -2
- {solvor-0.4.2 → solvor-0.4.4}/solvor/milp.py +12 -8
- {solvor-0.4.2 → solvor-0.4.4}/solvor/mst.py +5 -5
- {solvor-0.4.2 → solvor-0.4.4}/solvor/network_simplex.py +8 -6
- {solvor-0.4.2 → solvor-0.4.4}/solvor/sat.py +4 -2
- {solvor-0.4.2 → solvor-0.4.4}/solvor/simplex.py +13 -9
- {solvor-0.4.2 → solvor-0.4.4}/solvor/tabu.py +13 -7
- {solvor-0.4.2 → solvor-0.4.4}/solvor/types.py +5 -2
- {solvor-0.4.2 → solvor-0.4.4}/solvor/utils.py +9 -1
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_a_star.py +37 -83
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_anneal.py +73 -88
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_bayesian.py +10 -50
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_bellman_ford.py +5 -27
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_bfs.py +34 -43
- solvor-0.4.4/tests/test_cp.py +267 -0
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_dijkstra.py +24 -38
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_dlx.py +3 -2
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_flow.py +54 -107
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_floyd_warshall.py +5 -5
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_genetic.py +45 -20
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_gradient.py +131 -2
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_hungarian.py +13 -57
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_milp.py +4 -20
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_mst.py +8 -22
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_network_simplex.py +7 -34
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_sat.py +7 -6
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_simplex.py +2 -10
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_tabu.py +19 -18
- solvor-0.4.4/tests/test_types.py +97 -0
- {solvor-0.4.2 → solvor-0.4.4}/tests/test_utils.py +7 -5
- solvor-0.4.2/tests/test_cp.py +0 -181
- {solvor-0.4.2 → solvor-0.4.4}/.github/workflows/publish.yml +0 -0
- {solvor-0.4.2 → solvor-0.4.4}/.gitignore +0 -0
- {solvor-0.4.2 → solvor-0.4.4}/tests/__init__.py +0 -0
solvor-0.4.4/.envrc
ADDED
|
@@ -38,7 +38,7 @@ jobs:
|
|
|
38
38
|
run: uv sync --extra dev
|
|
39
39
|
|
|
40
40
|
- name: Run type checker
|
|
41
|
-
run: uv run
|
|
41
|
+
run: uv run ty check solvor/
|
|
42
42
|
|
|
43
43
|
# Detect which solvers have changed
|
|
44
44
|
changes:
|
|
@@ -68,6 +68,8 @@ jobs:
|
|
|
68
68
|
infra: ${{ steps.filter.outputs.infra }}
|
|
69
69
|
steps:
|
|
70
70
|
- uses: actions/checkout@v4
|
|
71
|
+
with:
|
|
72
|
+
fetch-depth: 0
|
|
71
73
|
- uses: dorny/paths-filter@v3
|
|
72
74
|
id: filter
|
|
73
75
|
with:
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
default_language_version:
|
|
2
|
+
python: python3.13
|
|
3
|
+
|
|
4
|
+
repos:
|
|
5
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
6
|
+
rev: v5.0.0
|
|
7
|
+
hooks:
|
|
8
|
+
- id: check-added-large-files
|
|
9
|
+
- id: check-case-conflict
|
|
10
|
+
- id: check-merge-conflict
|
|
11
|
+
- id: check-toml
|
|
12
|
+
- id: check-yaml
|
|
13
|
+
- id: debug-statements
|
|
14
|
+
- id: end-of-file-fixer
|
|
15
|
+
- id: trailing-whitespace
|
|
16
|
+
|
|
17
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
18
|
+
rev: v0.14.10
|
|
19
|
+
hooks:
|
|
20
|
+
- id: ruff
|
|
21
|
+
args: [--fix]
|
|
22
|
+
- id: ruff-format
|
|
23
|
+
|
|
24
|
+
- repo: local
|
|
25
|
+
hooks:
|
|
26
|
+
- id: ty
|
|
27
|
+
name: ty type checker
|
|
28
|
+
entry: uv run ty check solvor/
|
|
29
|
+
language: system
|
|
30
|
+
types: [python]
|
|
31
|
+
pass_filenames: false
|
|
@@ -148,9 +148,10 @@ Type hints are required on public APIs but optional for internal helpers. Some s
|
|
|
148
148
|
1. Working > perfect
|
|
149
149
|
2. Readable > clever
|
|
150
150
|
3. Simple > general
|
|
151
|
+
|
|
151
152
|
Any performance optimization is welcome, but not at the cost of significant complexity.
|
|
152
153
|
|
|
153
154
|
```python
|
|
154
155
|
model.maximize(readability + performance)
|
|
155
156
|
model.add(complexity <= maintainable)
|
|
156
|
-
```
|
|
157
|
+
```
|
|
@@ -198,4 +198,4 @@
|
|
|
198
198
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
199
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
200
|
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|
|
201
|
+
limitations under the License.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: solvOR
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.4
|
|
4
4
|
Summary: Solvor all your optimization needs.
|
|
5
5
|
Project-URL: Repository, https://github.com/StevenBtw/solvOR
|
|
6
6
|
Project-URL: PyPI, https://pypi.org/project/solvor
|
|
@@ -17,19 +17,24 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
17
17
|
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
18
18
|
Requires-Python: >=3.12
|
|
19
19
|
Provides-Extra: dev
|
|
20
|
-
Requires-Dist:
|
|
20
|
+
Requires-Dist: pre-commit>=4.5.1; extra == 'dev'
|
|
21
21
|
Requires-Dist: pytest-cov>=7.0.0; extra == 'dev'
|
|
22
22
|
Requires-Dist: pytest>=9.0.2; extra == 'dev'
|
|
23
23
|
Requires-Dist: ruff>=0.14.10; extra == 'dev'
|
|
24
|
+
Requires-Dist: ty>=0.0.5; extra == 'dev'
|
|
24
25
|
Description-Content-Type: text/markdown
|
|
25
26
|
|
|
26
27
|
# solvOR
|
|
27
28
|
|
|
28
|
-
[](LICENSE)
|
|
30
|
-
[](https://github.com/StevenBtw/solvOR/actions/workflows/ci.yml)
|
|
29
|
+
[](https://github.com/StevenBtw/solvOR/actions/workflows/ci.yml)
|
|
31
30
|
[](https://codecov.io/gh/StevenBtw/solvOR)
|
|
32
31
|
[](https://pypi.org/project/solvor/)
|
|
32
|
+
[](LICENSE)
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/solvor/)
|
|
35
|
+
[](https://github.com/astral-sh/uv)
|
|
36
|
+
[](https://github.com/astral-sh/ruff)
|
|
37
|
+
[](https://github.com/astral-sh/ty)
|
|
33
38
|
|
|
34
39
|
Solvor all your optimization needs.
|
|
35
40
|
|
|
@@ -385,7 +390,7 @@ Result(
|
|
|
385
390
|
1. **Pure Python:** no numpy, no scipy, no compiled extensions
|
|
386
391
|
2. **Readable:** each solvor fits in one file you can actually read
|
|
387
392
|
3. **Consistent:** same Result format, same minimize/maximize convention
|
|
388
|
-
4. **Practical:** solves real problems (or AoC puzzles)
|
|
393
|
+
4. **Practical:** solves real problems (or AoC puzzles)
|
|
389
394
|
|
|
390
395
|
---
|
|
391
396
|
|
|
@@ -406,4 +411,4 @@ Over time this toolbox got larger and larger, so I decided to publish it on GitH
|
|
|
406
411
|
|
|
407
412
|
Disclaimer; I am not a professional software engineer, so there are probably some obvious improvements possible. If so let me know, or create a PR!
|
|
408
413
|
|
|
409
|
-
</details>
|
|
414
|
+
</details>
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
# solvOR
|
|
2
2
|
|
|
3
|
-
[](LICENSE)
|
|
5
|
-
[](https://github.com/StevenBtw/solvOR/actions/workflows/ci.yml)
|
|
3
|
+
[](https://github.com/StevenBtw/solvOR/actions/workflows/ci.yml)
|
|
6
4
|
[](https://codecov.io/gh/StevenBtw/solvOR)
|
|
7
5
|
[](https://pypi.org/project/solvor/)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
[](https://pypi.org/project/solvor/)
|
|
9
|
+
[](https://github.com/astral-sh/uv)
|
|
10
|
+
[](https://github.com/astral-sh/ruff)
|
|
11
|
+
[](https://github.com/astral-sh/ty)
|
|
8
12
|
|
|
9
13
|
Solvor all your optimization needs.
|
|
10
14
|
|
|
@@ -360,7 +364,7 @@ Result(
|
|
|
360
364
|
1. **Pure Python:** no numpy, no scipy, no compiled extensions
|
|
361
365
|
2. **Readable:** each solvor fits in one file you can actually read
|
|
362
366
|
3. **Consistent:** same Result format, same minimize/maximize convention
|
|
363
|
-
4. **Practical:** solves real problems (or AoC puzzles)
|
|
367
|
+
4. **Practical:** solves real problems (or AoC puzzles)
|
|
364
368
|
|
|
365
369
|
---
|
|
366
370
|
|
|
@@ -381,4 +385,4 @@ Over time this toolbox got larger and larger, so I decided to publish it on GitH
|
|
|
381
385
|
|
|
382
386
|
Disclaimer; I am not a professional software engineer, so there are probably some obvious improvements possible. If so let me know, or create a PR!
|
|
383
387
|
|
|
384
|
-
</details>
|
|
388
|
+
</details>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "solvOR"
|
|
3
|
-
version = "0.4.
|
|
3
|
+
version = "0.4.4"
|
|
4
4
|
description = "Solvor all your optimization needs."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
@@ -22,14 +22,15 @@ classifiers = [
|
|
|
22
22
|
|
|
23
23
|
[project.urls]
|
|
24
24
|
Repository = "https://github.com/StevenBtw/solvOR"
|
|
25
|
-
PyPI = "https://pypi.org/project/solvor"
|
|
25
|
+
PyPI = "https://pypi.org/project/solvor"
|
|
26
26
|
|
|
27
27
|
[project.optional-dependencies]
|
|
28
28
|
dev = [
|
|
29
|
-
"mypy>=1.19.1",
|
|
30
29
|
"pytest>=9.0.2",
|
|
31
30
|
"pytest-cov>=7.0.0",
|
|
32
31
|
"ruff>=0.14.10",
|
|
32
|
+
"pre-commit>=4.5.1",
|
|
33
|
+
"ty>=0.0.5",
|
|
33
34
|
]
|
|
34
35
|
|
|
35
36
|
[build-system]
|
|
@@ -57,18 +58,8 @@ exclude_lines = [
|
|
|
57
58
|
"raise NotImplementedError",
|
|
58
59
|
]
|
|
59
60
|
|
|
60
|
-
[tool.
|
|
61
|
-
|
|
62
|
-
strict = false
|
|
63
|
-
warn_return_any = false
|
|
64
|
-
warn_unused_ignores = false
|
|
65
|
-
disallow_untyped_defs = false
|
|
66
|
-
check_untyped_defs = true
|
|
67
|
-
no_implicit_optional = true
|
|
68
|
-
show_error_codes = true
|
|
69
|
-
files = ["solvor"]
|
|
70
|
-
disable_error_code = ["var-annotated", "union-attr"]
|
|
61
|
+
[tool.ty.environment]
|
|
62
|
+
python-version = "3.12"
|
|
71
63
|
|
|
72
|
-
[
|
|
73
|
-
|
|
74
|
-
disable_error_code = ["var-annotated", "union-attr", "assignment", "misc", "arg-type", "index", "attr-defined"]
|
|
64
|
+
[tool.ty.rules]
|
|
65
|
+
invalid-argument-type = "ignore"
|
|
@@ -22,14 +22,37 @@ from solvor.tabu import solve_tsp, tabu_search
|
|
|
22
22
|
from solvor.types import Result, Status
|
|
23
23
|
|
|
24
24
|
__all__ = [
|
|
25
|
-
"solve_lp",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
25
|
+
"solve_lp",
|
|
26
|
+
"solve_milp",
|
|
27
|
+
"tabu_search",
|
|
28
|
+
"solve_tsp",
|
|
29
|
+
"anneal",
|
|
30
|
+
"exponential_cooling",
|
|
31
|
+
"linear_cooling",
|
|
32
|
+
"logarithmic_cooling",
|
|
33
|
+
"solve_sat",
|
|
34
|
+
"Model",
|
|
35
|
+
"bayesian_opt",
|
|
36
|
+
"evolve",
|
|
37
|
+
"max_flow",
|
|
38
|
+
"min_cost_flow",
|
|
39
|
+
"solve_assignment",
|
|
40
|
+
"gradient_descent",
|
|
41
|
+
"momentum",
|
|
42
|
+
"rmsprop",
|
|
43
|
+
"adam",
|
|
30
44
|
"solve_exact_cover",
|
|
31
|
-
"bfs",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
45
|
+
"bfs",
|
|
46
|
+
"dfs",
|
|
47
|
+
"dijkstra",
|
|
48
|
+
"astar",
|
|
49
|
+
"astar_grid",
|
|
50
|
+
"bellman_ford",
|
|
51
|
+
"floyd_warshall",
|
|
52
|
+
"kruskal",
|
|
53
|
+
"prim",
|
|
54
|
+
"network_simplex",
|
|
55
|
+
"hungarian",
|
|
56
|
+
"Status",
|
|
57
|
+
"Result",
|
|
35
58
|
]
|
|
@@ -36,6 +36,7 @@ _DIRS_8 = tuple((dx, dy) for dx, dy in product((-1, 0, 1), repeat=2) if (dx, dy)
|
|
|
36
36
|
# 4-directions: N, S, E, W only
|
|
37
37
|
_DIRS_4 = tuple((dx, dy) for dx, dy in _DIRS_8 if dx == 0 or dy == 0)
|
|
38
38
|
|
|
39
|
+
|
|
39
40
|
def astar[S](
|
|
40
41
|
start: S,
|
|
41
42
|
goal: S | Callable[[S], bool],
|
|
@@ -46,7 +47,6 @@ def astar[S](
|
|
|
46
47
|
max_iter: int = 1_000_000,
|
|
47
48
|
max_cost: float | None = None,
|
|
48
49
|
) -> Result:
|
|
49
|
-
|
|
50
50
|
is_goal = goal if callable(goal) else lambda s: s == goal
|
|
51
51
|
|
|
52
52
|
g: dict[S, float] = {start: 0.0}
|
|
@@ -82,7 +82,7 @@ def astar[S](
|
|
|
82
82
|
|
|
83
83
|
tentative_g = g[current] + edge_cost
|
|
84
84
|
|
|
85
|
-
if tentative_g < g.get(neighbor, float(
|
|
85
|
+
if tentative_g < g.get(neighbor, float("inf")):
|
|
86
86
|
g[neighbor] = tentative_g
|
|
87
87
|
parent[neighbor] = current
|
|
88
88
|
f_new = tentative_g + weight * heuristic(neighbor)
|
|
@@ -91,8 +91,9 @@ def astar[S](
|
|
|
91
91
|
evaluations += 1
|
|
92
92
|
|
|
93
93
|
if iterations >= max_iter:
|
|
94
|
-
return Result(None, float(
|
|
95
|
-
return Result(None, float(
|
|
94
|
+
return Result(None, float("inf"), iterations, evaluations, Status.MAX_ITER)
|
|
95
|
+
return Result(None, float("inf"), iterations, evaluations, Status.INFEASIBLE)
|
|
96
|
+
|
|
96
97
|
|
|
97
98
|
def astar_grid(
|
|
98
99
|
grid: Sequence[Sequence[int]],
|
|
@@ -106,7 +107,6 @@ def astar_grid(
|
|
|
106
107
|
weight: float = 1.0,
|
|
107
108
|
max_iter: int = 1_000_000,
|
|
108
109
|
) -> Result:
|
|
109
|
-
|
|
110
110
|
rows = len(grid)
|
|
111
111
|
cols = len(grid[0]) if rows else 0
|
|
112
112
|
blocked_set = {blocked} if isinstance(blocked, int) else set(blocked)
|
|
@@ -120,19 +120,24 @@ def astar_grid(
|
|
|
120
120
|
|
|
121
121
|
match h_name:
|
|
122
122
|
case "manhattan":
|
|
123
|
+
|
|
123
124
|
def h(s):
|
|
124
125
|
return abs(s[0] - gr) + abs(s[1] - gc)
|
|
125
126
|
case "euclidean":
|
|
127
|
+
|
|
126
128
|
def h(s):
|
|
127
129
|
return ((s[0] - gr) ** 2 + (s[1] - gc) ** 2) ** 0.5
|
|
128
130
|
case "octile":
|
|
131
|
+
|
|
129
132
|
def h(s):
|
|
130
133
|
dr, dc = abs(s[0] - gr), abs(s[1] - gc)
|
|
131
134
|
return max(dr, dc) + _SQRT2_MINUS_1 * min(dr, dc)
|
|
132
135
|
case "chebyshev":
|
|
136
|
+
|
|
133
137
|
def h(s):
|
|
134
138
|
return max(abs(s[0] - gr), abs(s[1] - gc))
|
|
135
139
|
case _:
|
|
140
|
+
|
|
136
141
|
def h(s):
|
|
137
142
|
return abs(s[0] - gr) + abs(s[1] - gc)
|
|
138
143
|
|
|
@@ -150,10 +155,11 @@ def astar_grid(
|
|
|
150
155
|
|
|
151
156
|
return astar(start, goal, neighbors, h, weight=weight, max_iter=max_iter)
|
|
152
157
|
|
|
158
|
+
|
|
153
159
|
def _reconstruct(parent, current):
|
|
154
160
|
path = [current]
|
|
155
161
|
while current in parent:
|
|
156
162
|
current = parent[current]
|
|
157
163
|
path.append(current)
|
|
158
164
|
path.reverse()
|
|
159
|
-
return path
|
|
165
|
+
return path
|
|
@@ -44,22 +44,28 @@ CoolingSchedule = Callable[[float, int, int], float]
|
|
|
44
44
|
|
|
45
45
|
def exponential_cooling(rate: float = 0.9995) -> CoolingSchedule:
|
|
46
46
|
"""Geometric cooling: temp = initial * rate^iteration."""
|
|
47
|
+
|
|
47
48
|
def schedule(initial_temp: float, iteration: int, max_iter: int) -> float:
|
|
48
|
-
return initial_temp * (rate
|
|
49
|
+
return initial_temp * (rate**iteration)
|
|
50
|
+
|
|
49
51
|
return schedule
|
|
50
52
|
|
|
51
53
|
|
|
52
54
|
def linear_cooling(min_temp: float = 1e-8) -> CoolingSchedule:
|
|
53
55
|
"""Linear cooling: temp decreases linearly from initial to min_temp."""
|
|
56
|
+
|
|
54
57
|
def schedule(initial_temp: float, iteration: int, max_iter: int) -> float:
|
|
55
58
|
return initial_temp - (initial_temp - min_temp) * iteration / max_iter
|
|
59
|
+
|
|
56
60
|
return schedule
|
|
57
61
|
|
|
58
62
|
|
|
59
63
|
def logarithmic_cooling(c: float = 1.0) -> CoolingSchedule:
|
|
60
64
|
"""Logarithmic cooling: temp = initial / (1 + c * log(1 + iteration))."""
|
|
65
|
+
|
|
61
66
|
def schedule(initial_temp: float, iteration: int, max_iter: int) -> float:
|
|
62
67
|
return initial_temp / (1 + c * log(1 + iteration))
|
|
68
|
+
|
|
63
69
|
return schedule
|
|
64
70
|
|
|
65
71
|
|
|
@@ -77,7 +83,6 @@ def anneal[T](
|
|
|
77
83
|
on_progress: ProgressCallback | None = None,
|
|
78
84
|
progress_interval: int = 0,
|
|
79
85
|
) -> Result:
|
|
80
|
-
|
|
81
86
|
rng = Random(seed)
|
|
82
87
|
sign = 1 if minimize else -1
|
|
83
88
|
evals = 0
|
|
@@ -121,4 +126,4 @@ def anneal[T](
|
|
|
121
126
|
|
|
122
127
|
final_obj = best_obj * sign
|
|
123
128
|
status = Status.MAX_ITER if iteration == max_iter else Status.FEASIBLE
|
|
124
|
-
return Result(best_solution, final_obj, iteration, evals, status)
|
|
129
|
+
return Result(best_solution, final_obj, iteration, evals, status)
|
|
@@ -27,6 +27,7 @@ from solvor.types import Result, Status
|
|
|
27
27
|
|
|
28
28
|
__all__ = ["bayesian_opt"]
|
|
29
29
|
|
|
30
|
+
|
|
30
31
|
def bayesian_opt(
|
|
31
32
|
objective_fn: Callable[[Sequence[float]], float],
|
|
32
33
|
bounds: Sequence[tuple[float, float]],
|
|
@@ -36,7 +37,6 @@ def bayesian_opt(
|
|
|
36
37
|
n_initial: int = 5,
|
|
37
38
|
seed: int | None = None,
|
|
38
39
|
) -> Result:
|
|
39
|
-
|
|
40
40
|
rng = Random(seed)
|
|
41
41
|
sign = 1 if minimize else -1
|
|
42
42
|
n_dims = len(bounds)
|
|
@@ -89,7 +89,7 @@ def bayesian_opt(
|
|
|
89
89
|
return ei
|
|
90
90
|
|
|
91
91
|
for iteration in range(n_initial, max_iter):
|
|
92
|
-
best_ei, best_candidate = -float(
|
|
92
|
+
best_ei, best_candidate = -float("inf"), None
|
|
93
93
|
|
|
94
94
|
for _ in range(100 * n_dims):
|
|
95
95
|
candidate = random_point()
|
|
@@ -110,6 +110,7 @@ def bayesian_opt(
|
|
|
110
110
|
final_obj = best_y * sign
|
|
111
111
|
return Result(best_x, final_obj, max_iter, evals, Status.FEASIBLE)
|
|
112
112
|
|
|
113
|
+
|
|
113
114
|
def _solve_linear(A, b):
|
|
114
115
|
n = len(A)
|
|
115
116
|
aug = [row[:] + [b[i]] for i, row in enumerate(A)]
|
|
@@ -133,8 +134,10 @@ def _solve_linear(A, b):
|
|
|
133
134
|
|
|
134
135
|
return [aug[i][n] for i in range(n)]
|
|
135
136
|
|
|
137
|
+
|
|
136
138
|
def _norm_pdf(x):
|
|
137
139
|
return exp(-0.5 * x * x) / sqrt(2 * pi)
|
|
138
140
|
|
|
141
|
+
|
|
139
142
|
def _norm_cdf(x):
|
|
140
|
-
return 0.5 * (1 + erf(x / sqrt(2)))
|
|
143
|
+
return 0.5 * (1 + erf(x / sqrt(2)))
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Bellman-Ford for shortest paths with negative edge weights.
|
|
3
3
|
|
|
4
|
-
Use this when edges can have negative weights. Slower than Dijkstra, checks
|
|
5
|
-
every edge V-1 times. The true edgelord. Only use when you actually have
|
|
4
|
+
Use this when edges can have negative weights. Slower than Dijkstra, checks
|
|
5
|
+
every edge V-1 times. The true edgelord. Only use when you actually have
|
|
6
6
|
negative edges.
|
|
7
7
|
|
|
8
8
|
from solvor.bellman_ford import bellman_ford
|
|
@@ -30,6 +30,7 @@ from solvor.types import Result, Status
|
|
|
30
30
|
|
|
31
31
|
__all__ = ["bellman_ford"]
|
|
32
32
|
|
|
33
|
+
|
|
33
34
|
def bellman_ford(
|
|
34
35
|
start: int,
|
|
35
36
|
edges: list[tuple[int, int, float]],
|
|
@@ -37,8 +38,7 @@ def bellman_ford(
|
|
|
37
38
|
*,
|
|
38
39
|
target: int | None = None,
|
|
39
40
|
) -> Result:
|
|
40
|
-
|
|
41
|
-
dist = [float('inf')] * n_nodes
|
|
41
|
+
dist = [float("inf")] * n_nodes
|
|
42
42
|
parent = [-1] * n_nodes
|
|
43
43
|
dist[start] = 0.0
|
|
44
44
|
iterations = 0
|
|
@@ -47,7 +47,7 @@ def bellman_ford(
|
|
|
47
47
|
updated = False
|
|
48
48
|
for u, v, w in edges:
|
|
49
49
|
iterations += 1
|
|
50
|
-
if dist[u] != float(
|
|
50
|
+
if dist[u] != float("inf") and dist[u] + w < dist[v]:
|
|
51
51
|
dist[v] = dist[u] + w
|
|
52
52
|
parent[v] = u
|
|
53
53
|
updated = True
|
|
@@ -56,21 +56,22 @@ def bellman_ford(
|
|
|
56
56
|
|
|
57
57
|
for u, v, w in edges:
|
|
58
58
|
iterations += 1
|
|
59
|
-
if dist[u] != float(
|
|
60
|
-
return Result(None, float(
|
|
59
|
+
if dist[u] != float("inf") and dist[u] + w < dist[v]:
|
|
60
|
+
return Result(None, float("-inf"), iterations, len(edges), Status.UNBOUNDED)
|
|
61
61
|
|
|
62
62
|
if target is not None:
|
|
63
|
-
if dist[target] == float(
|
|
64
|
-
return Result(None, float(
|
|
63
|
+
if dist[target] == float("inf"):
|
|
64
|
+
return Result(None, float("inf"), iterations, len(edges), Status.INFEASIBLE)
|
|
65
65
|
path = _reconstruct_indexed(parent, target)
|
|
66
66
|
return Result(path, dist[target], iterations, len(edges))
|
|
67
67
|
|
|
68
|
-
distances = {i: dist[i] for i in range(n_nodes) if dist[i] < float(
|
|
68
|
+
distances = {i: dist[i] for i in range(n_nodes) if dist[i] < float("inf")}
|
|
69
69
|
return Result(distances, 0, iterations, len(edges))
|
|
70
70
|
|
|
71
|
+
|
|
71
72
|
def _reconstruct_indexed(parent, target):
|
|
72
73
|
path = [target]
|
|
73
74
|
while parent[path[-1]] != -1:
|
|
74
75
|
path.append(parent[path[-1]])
|
|
75
76
|
path.reverse()
|
|
76
|
-
return path
|
|
77
|
+
return path
|
|
@@ -26,6 +26,7 @@ from solvor.types import Result, Status
|
|
|
26
26
|
|
|
27
27
|
__all__ = ["bfs", "dfs"]
|
|
28
28
|
|
|
29
|
+
|
|
29
30
|
def bfs[S](
|
|
30
31
|
start: S,
|
|
31
32
|
goal: S | Callable[[S], bool] | None,
|
|
@@ -33,11 +34,7 @@ def bfs[S](
|
|
|
33
34
|
*,
|
|
34
35
|
max_iter: int = 1_000_000,
|
|
35
36
|
) -> Result:
|
|
36
|
-
|
|
37
|
-
is_goal = (
|
|
38
|
-
(lambda s: s == goal) if not callable(goal) and goal is not None
|
|
39
|
-
else goal
|
|
40
|
-
)
|
|
37
|
+
is_goal = (lambda s: s == goal) if not callable(goal) and goal is not None else goal
|
|
41
38
|
|
|
42
39
|
parent: dict[S, S] = {}
|
|
43
40
|
visited: set[S] = {start}
|
|
@@ -60,11 +57,12 @@ def bfs[S](
|
|
|
60
57
|
|
|
61
58
|
if is_goal:
|
|
62
59
|
if iterations >= max_iter:
|
|
63
|
-
return Result(None, float(
|
|
64
|
-
return Result(None, float(
|
|
60
|
+
return Result(None, float("inf"), iterations, len(visited), Status.MAX_ITER)
|
|
61
|
+
return Result(None, float("inf"), iterations, len(visited), Status.INFEASIBLE)
|
|
65
62
|
|
|
66
63
|
return Result(visited, len(visited), iterations, len(visited))
|
|
67
64
|
|
|
65
|
+
|
|
68
66
|
def dfs[S](
|
|
69
67
|
start: S,
|
|
70
68
|
goal: S | Callable[[S], bool] | None,
|
|
@@ -72,11 +70,7 @@ def dfs[S](
|
|
|
72
70
|
*,
|
|
73
71
|
max_iter: int = 1_000_000,
|
|
74
72
|
) -> Result:
|
|
75
|
-
|
|
76
|
-
is_goal = (
|
|
77
|
-
(lambda s: s == goal) if not callable(goal) and goal is not None
|
|
78
|
-
else goal
|
|
79
|
-
)
|
|
73
|
+
is_goal = (lambda s: s == goal) if not callable(goal) and goal is not None else goal
|
|
80
74
|
|
|
81
75
|
parent: dict[S, S] = {}
|
|
82
76
|
visited: set[S] = {start}
|
|
@@ -99,15 +93,16 @@ def dfs[S](
|
|
|
99
93
|
|
|
100
94
|
if is_goal:
|
|
101
95
|
if iterations >= max_iter:
|
|
102
|
-
return Result(None, float(
|
|
103
|
-
return Result(None, float(
|
|
96
|
+
return Result(None, float("inf"), iterations, len(visited), Status.MAX_ITER)
|
|
97
|
+
return Result(None, float("inf"), iterations, len(visited), Status.INFEASIBLE)
|
|
104
98
|
|
|
105
99
|
return Result(visited, len(visited), iterations, len(visited))
|
|
106
100
|
|
|
101
|
+
|
|
107
102
|
def _reconstruct(parent, current):
|
|
108
103
|
path = [current]
|
|
109
104
|
while current in parent:
|
|
110
105
|
current = parent[current]
|
|
111
106
|
path.append(current)
|
|
112
107
|
path.reverse()
|
|
113
|
-
return path
|
|
108
|
+
return path
|