beacon-attraction 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 (64) hide show
  1. beacon_attraction-0.1.0/LICENSE +21 -0
  2. beacon_attraction-0.1.0/PKG-INFO +102 -0
  3. beacon_attraction-0.1.0/README.md +83 -0
  4. beacon_attraction-0.1.0/pyproject.toml +78 -0
  5. beacon_attraction-0.1.0/pyproject.toml.orig +55 -0
  6. beacon_attraction-0.1.0/src/beacon_attraction/__init__.py +63 -0
  7. beacon_attraction-0.1.0/src/beacon_attraction/_attraction/__init__.py +2 -0
  8. beacon_attraction-0.1.0/src/beacon_attraction/_attraction/queries.py +202 -0
  9. beacon_attraction-0.1.0/src/beacon_attraction/_attraction/results.py +148 -0
  10. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/.gitignore +2 -0
  11. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.0.db +0 -0
  12. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.1.db +0 -0
  13. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.10.db +0 -0
  14. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.11.db +0 -0
  15. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.12.db +0 -0
  16. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.13.db +0 -0
  17. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.14.db +0 -0
  18. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.15.db +0 -0
  19. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.2.db +0 -0
  20. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.3.db +0 -0
  21. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.4.db +0 -0
  22. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.5.db +0 -0
  23. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.6.db +0 -0
  24. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.7.db +0 -0
  25. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.8.db +0 -0
  26. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/3.14/cache.9.db +0 -0
  27. beacon_attraction-0.1.0/src/beacon_attraction/_cover/.mypy_cache/CACHEDIR.TAG +3 -0
  28. beacon_attraction-0.1.0/src/beacon_attraction/_cover/__init__.py +2 -0
  29. beacon_attraction-0.1.0/src/beacon_attraction/_cover/bits.py +21 -0
  30. beacon_attraction-0.1.0/src/beacon_attraction/_cover/cover.py +38 -0
  31. beacon_attraction-0.1.0/src/beacon_attraction/_cover/cover_cleanup.py +133 -0
  32. beacon_attraction-0.1.0/src/beacon_attraction/_cover/cover_search.py +126 -0
  33. beacon_attraction-0.1.0/src/beacon_attraction/_cover/covers.py +51 -0
  34. beacon_attraction-0.1.0/src/beacon_attraction/_cover/greedy_covers.py +71 -0
  35. beacon_attraction-0.1.0/src/beacon_attraction/_cover/greedy_set_cover.py +126 -0
  36. beacon_attraction-0.1.0/src/beacon_attraction/_cover/minimum_covers.py +61 -0
  37. beacon_attraction-0.1.0/src/beacon_attraction/_cover/set_cover.py +151 -0
  38. beacon_attraction-0.1.0/src/beacon_attraction/_cover/splitmix64.py +25 -0
  39. beacon_attraction-0.1.0/src/beacon_attraction/_graph/__init__.py +2 -0
  40. beacon_attraction-0.1.0/src/beacon_attraction/_graph/component_table.py +156 -0
  41. beacon_attraction-0.1.0/src/beacon_attraction/_graph/components.py +98 -0
  42. beacon_attraction-0.1.0/src/beacon_attraction/_graph/frozen.py +85 -0
  43. beacon_attraction-0.1.0/src/beacon_attraction/_graph/graphs.py +436 -0
  44. beacon_attraction-0.1.0/src/beacon_attraction/_graph/index_kind.py +15 -0
  45. beacon_attraction-0.1.0/src/beacon_attraction/_graph/mutable.py +220 -0
  46. beacon_attraction-0.1.0/src/beacon_attraction/_graph/validation.py +200 -0
  47. beacon_attraction-0.1.0/src/beacon_attraction/_graph/view.py +101 -0
  48. beacon_attraction-0.1.0/src/beacon_attraction/_graph/violations.py +52 -0
  49. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/__init__.py +2 -0
  50. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/circular.py +87 -0
  51. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/geometric.py +97 -0
  52. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/kd_tree.py +249 -0
  53. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/linear.py +26 -0
  54. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/precomputed.py +77 -0
  55. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/search.py +27 -0
  56. beacon_attraction-0.1.0/src/beacon_attraction/_indexes/steps.py +27 -0
  57. beacon_attraction-0.1.0/src/beacon_attraction/_kernel/__init__.py +1 -0
  58. beacon_attraction-0.1.0/src/beacon_attraction/_kernel/kernels.py +127 -0
  59. beacon_attraction-0.1.0/src/beacon_attraction/_kernel/witness_order.py +51 -0
  60. beacon_attraction-0.1.0/src/beacon_attraction/_numerics/__init__.py +2 -0
  61. beacon_attraction-0.1.0/src/beacon_attraction/_numerics/precision.py +87 -0
  62. beacon_attraction-0.1.0/src/beacon_attraction/_numerics/predicates.py +181 -0
  63. beacon_attraction-0.1.0/src/beacon_attraction/_numerics/vectors.py +26 -0
  64. beacon_attraction-0.1.0/src/beacon_attraction/py.typed +0 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Leandro Casuso Montero
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,102 @@
1
+ Metadata-Version: 2.4
2
+ Name: beacon-attraction
3
+ Version: 0.1.0
4
+ Summary: Beacon attraction in geometric graphs: attracted sets, kernels and covers, with exact integer predicates.
5
+ Keywords: beacon attraction,geometric graphs,computational geometry,exact predicates
6
+ Author: Leandro Casuso Montero
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
14
+ Classifier: Typing :: Typed
15
+ Requires-Python: >=3.14
16
+ Project-URL: Repository, https://github.com/Casuso/beacon-attraction-geometric-graphs
17
+ Project-URL: Issues, https://github.com/Casuso/beacon-attraction-geometric-graphs/issues
18
+ Description-Content-Type: text/markdown
19
+
20
+ # beacon-attraction
21
+
22
+ Beacon attraction in geometric graphs, in pure Python. Put a beacon on a vertex: an object at
23
+ any other vertex moves along the edge that brings it closer to the beacon fastest, and keeps
24
+ moving while its distance shrinks. It reaches the beacon, or it stops at a vertex or inside an
25
+ edge. This package computes where every object ends, the vertices a beacon attracts, the
26
+ kernel (the vertices that attract every vertex) and covers (sets of beacons that together
27
+ attract every vertex). Every decision is exact: coordinates are decimals, and every comparison
28
+ is made in integers.
29
+
30
+ ## Install
31
+
32
+ ```sh
33
+ pip install beacon-attraction
34
+ ```
35
+
36
+ Python 3.14 or later. No dependencies.
37
+
38
+ ## Example
39
+
40
+ ![A graph of five vertices: beacon 0 attracts 1 and 3; the objects at 2 and 4 slide toward each other along their edge and stop at the dead point (28/5, 11/5).](https://raw.githubusercontent.com/Casuso/beacon-attraction-geometric-graphs/main/docs/images/readme-example.svg)
41
+
42
+ Five vertices and five edges, with the beacon at vertex 0. The objects at 1 and 3 reach it.
43
+ The objects at 2 and 4 slide toward each other along their edge and stop where it passes
44
+ closest to the beacon, at the dead point (28/5, 11/5), exactly.
45
+
46
+ ```pycon
47
+ >>> from beacon_attraction import GeometricGraph
48
+ >>> graph = GeometricGraph.from_points(
49
+ ... [(2, 4), (8, 4), (5, 1), (7, 6), (7, 5)], # vertices 0 to 4
50
+ ... [(0, 1), (0, 3), (1, 3), (1, 4), (2, 4)], # edges
51
+ ... decimals=0,
52
+ ... )
53
+ >>> graph.attraction_beacon_tree(0).vertices # the vertices beacon 0 attracts
54
+ (0, 1, 3)
55
+ >>> end = graph.attraction_path(0, 4).end # where the object at 4 stops
56
+ >>> end.segment, end.point
57
+ ((2, 4), (Fraction(28, 5), Fraction(11, 5)))
58
+ >>> graph.kernel() # no single beacon attracts every vertex
59
+ ()
60
+ >>> cover = graph.minimum_cover() # but two do, and no fewer can
61
+ >>> cover.beacons, cover.is_optimal
62
+ ((1, 4), True)
63
+ ```
64
+
65
+ ## What it has
66
+
67
+ - **Graphs:** `GeometricGraph` and `DirectedGeometricGraph` to build and edit, in any
68
+ dimension; `to_frozen()` gives an immutable copy with neighbor indexes, for repeated
69
+ queries.
70
+ Every edit is validated: no vertex may lie inside an edge, and no two may coincide.
71
+ - **Attraction:** `attraction_path`, `attraction_tree`, `attraction_beacon_tree` (the attracted
72
+ set) and `attraction_forest` (where every object ends).
73
+ - **Components** (`components()`), the **kernel** (`kernel()`), and the **covers**:
74
+ `minimum_cover()`, exact, and `greedy_cover()`, fast, each with a lower bound.
75
+
76
+ The design and its reasons are in
77
+ [docs/PYTHON-API.md](https://github.com/Casuso/beacon-attraction-geometric-graphs/blob/main/docs/PYTHON-API.md).
78
+
79
+ ## Exactness
80
+
81
+ Coordinates come in as integers, decimal strings, `Decimal`s or floats, and are stored as
82
+ integers scaled to the graph's fixed number of decimals. Every decision, which edge is
83
+ steepest, whether two are tied, where an object stops, is made with exact integer arithmetic,
84
+ and dead points come back as `Fraction`s. Ties are real: two equally steep edges stop the
85
+ object, as the model says. The same language-neutral conformance suite checks this package and
86
+ its C# and Rust siblings.
87
+
88
+ ## Links
89
+
90
+ - [The model](https://github.com/Casuso/beacon-attraction-geometric-graphs/blob/main/docs/MODEL.md):
91
+ the rules and the exact predicates.
92
+ - [The explainer](https://casuso.github.io/beacon-attraction-geometric-graphs/explainer/attraction-predicates.html)
93
+ and [the playground](https://casuso.github.io/beacon-attraction-geometric-graphs/playground/),
94
+ where you draw a graph and watch it.
95
+ - [Performance](https://github.com/Casuso/beacon-attraction-geometric-graphs/blob/main/docs/PERFORMANCE.md),
96
+ measured on large scenarios.
97
+ - [The repository](https://github.com/Casuso/beacon-attraction-geometric-graphs), with the C#
98
+ and Rust versions.
99
+
100
+ ## License
101
+
102
+ MIT.
@@ -0,0 +1,83 @@
1
+ # beacon-attraction
2
+
3
+ Beacon attraction in geometric graphs, in pure Python. Put a beacon on a vertex: an object at
4
+ any other vertex moves along the edge that brings it closer to the beacon fastest, and keeps
5
+ moving while its distance shrinks. It reaches the beacon, or it stops at a vertex or inside an
6
+ edge. This package computes where every object ends, the vertices a beacon attracts, the
7
+ kernel (the vertices that attract every vertex) and covers (sets of beacons that together
8
+ attract every vertex). Every decision is exact: coordinates are decimals, and every comparison
9
+ is made in integers.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pip install beacon-attraction
15
+ ```
16
+
17
+ Python 3.14 or later. No dependencies.
18
+
19
+ ## Example
20
+
21
+ ![A graph of five vertices: beacon 0 attracts 1 and 3; the objects at 2 and 4 slide toward each other along their edge and stop at the dead point (28/5, 11/5).](https://raw.githubusercontent.com/Casuso/beacon-attraction-geometric-graphs/main/docs/images/readme-example.svg)
22
+
23
+ Five vertices and five edges, with the beacon at vertex 0. The objects at 1 and 3 reach it.
24
+ The objects at 2 and 4 slide toward each other along their edge and stop where it passes
25
+ closest to the beacon, at the dead point (28/5, 11/5), exactly.
26
+
27
+ ```pycon
28
+ >>> from beacon_attraction import GeometricGraph
29
+ >>> graph = GeometricGraph.from_points(
30
+ ... [(2, 4), (8, 4), (5, 1), (7, 6), (7, 5)], # vertices 0 to 4
31
+ ... [(0, 1), (0, 3), (1, 3), (1, 4), (2, 4)], # edges
32
+ ... decimals=0,
33
+ ... )
34
+ >>> graph.attraction_beacon_tree(0).vertices # the vertices beacon 0 attracts
35
+ (0, 1, 3)
36
+ >>> end = graph.attraction_path(0, 4).end # where the object at 4 stops
37
+ >>> end.segment, end.point
38
+ ((2, 4), (Fraction(28, 5), Fraction(11, 5)))
39
+ >>> graph.kernel() # no single beacon attracts every vertex
40
+ ()
41
+ >>> cover = graph.minimum_cover() # but two do, and no fewer can
42
+ >>> cover.beacons, cover.is_optimal
43
+ ((1, 4), True)
44
+ ```
45
+
46
+ ## What it has
47
+
48
+ - **Graphs:** `GeometricGraph` and `DirectedGeometricGraph` to build and edit, in any
49
+ dimension; `to_frozen()` gives an immutable copy with neighbor indexes, for repeated
50
+ queries.
51
+ Every edit is validated: no vertex may lie inside an edge, and no two may coincide.
52
+ - **Attraction:** `attraction_path`, `attraction_tree`, `attraction_beacon_tree` (the attracted
53
+ set) and `attraction_forest` (where every object ends).
54
+ - **Components** (`components()`), the **kernel** (`kernel()`), and the **covers**:
55
+ `minimum_cover()`, exact, and `greedy_cover()`, fast, each with a lower bound.
56
+
57
+ The design and its reasons are in
58
+ [docs/PYTHON-API.md](https://github.com/Casuso/beacon-attraction-geometric-graphs/blob/main/docs/PYTHON-API.md).
59
+
60
+ ## Exactness
61
+
62
+ Coordinates come in as integers, decimal strings, `Decimal`s or floats, and are stored as
63
+ integers scaled to the graph's fixed number of decimals. Every decision, which edge is
64
+ steepest, whether two are tied, where an object stops, is made with exact integer arithmetic,
65
+ and dead points come back as `Fraction`s. Ties are real: two equally steep edges stop the
66
+ object, as the model says. The same language-neutral conformance suite checks this package and
67
+ its C# and Rust siblings.
68
+
69
+ ## Links
70
+
71
+ - [The model](https://github.com/Casuso/beacon-attraction-geometric-graphs/blob/main/docs/MODEL.md):
72
+ the rules and the exact predicates.
73
+ - [The explainer](https://casuso.github.io/beacon-attraction-geometric-graphs/explainer/attraction-predicates.html)
74
+ and [the playground](https://casuso.github.io/beacon-attraction-geometric-graphs/playground/),
75
+ where you draw a graph and watch it.
76
+ - [Performance](https://github.com/Casuso/beacon-attraction-geometric-graphs/blob/main/docs/PERFORMANCE.md),
77
+ measured on large scenarios.
78
+ - [The repository](https://github.com/Casuso/beacon-attraction-geometric-graphs), with the C#
79
+ and Rust versions.
80
+
81
+ ## License
82
+
83
+ MIT.
@@ -0,0 +1,78 @@
1
+ [project]
2
+ name = "beacon-attraction"
3
+ version = "0.1.0"
4
+ description = "Beacon attraction in geometric graphs: attracted sets, kernels and covers, with exact integer predicates."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ requires-python = ">=3.14"
9
+ keywords = [
10
+ "beacon attraction",
11
+ "geometric graphs",
12
+ "computational geometry",
13
+ "exact predicates",
14
+ ]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Intended Audience :: Science/Research",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.14",
20
+ "Topic :: Scientific/Engineering :: Mathematics",
21
+ "Typing :: Typed",
22
+ ]
23
+ dependencies = []
24
+
25
+ [[project.authors]]
26
+ name = "Leandro Casuso Montero"
27
+
28
+ [project.urls]
29
+ Repository = "https://github.com/Casuso/beacon-attraction-geometric-graphs"
30
+ Issues = "https://github.com/Casuso/beacon-attraction-geometric-graphs/issues"
31
+
32
+ [build-system]
33
+ requires = ["uv_build>=0.12.5,<0.13.0"]
34
+ build-backend = "uv_build"
35
+
36
+ [dependency-groups]
37
+ dev = [
38
+ "hypothesis",
39
+ "mypy",
40
+ "pytest",
41
+ "ruff",
42
+ ]
43
+
44
+ [tool.pytest.ini_options]
45
+ testpaths = ["tests"]
46
+ pythonpath = ["."]
47
+ addopts = ["--import-mode=importlib"]
48
+ markers = ["conformance: a case of the language-neutral suite in conformance/"]
49
+
50
+ [tool.ruff]
51
+ line-length = 100
52
+ target-version = "py314"
53
+
54
+ [tool.ruff.lint]
55
+ select = [
56
+ "E",
57
+ "F",
58
+ "W",
59
+ "I",
60
+ "B",
61
+ "UP",
62
+ "SIM",
63
+ "RUF",
64
+ ]
65
+ ignore = [
66
+ "RUF001",
67
+ "RUF002",
68
+ "RUF003",
69
+ "SIM108",
70
+ ]
71
+
72
+ [tool.mypy]
73
+ strict = true
74
+ files = [
75
+ "src",
76
+ "tests",
77
+ "benchmarks",
78
+ ]
@@ -0,0 +1,55 @@
1
+ [project]
2
+ name = "beacon-attraction"
3
+ version = "0.1.0"
4
+ description = "Beacon attraction in geometric graphs: attracted sets, kernels and covers, with exact integer predicates."
5
+ readme = "README.md"
6
+ authors = [{ name = "Leandro Casuso Montero" }]
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ requires-python = ">=3.14"
10
+ keywords = ["beacon attraction", "geometric graphs", "computational geometry", "exact predicates"]
11
+ classifiers = [
12
+ "Development Status :: 3 - Alpha",
13
+ "Intended Audience :: Science/Research",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3.14",
16
+ "Topic :: Scientific/Engineering :: Mathematics",
17
+ "Typing :: Typed",
18
+ ]
19
+ dependencies = []
20
+
21
+ [project.urls]
22
+ Repository = "https://github.com/Casuso/beacon-attraction-geometric-graphs"
23
+ Issues = "https://github.com/Casuso/beacon-attraction-geometric-graphs/issues"
24
+
25
+ [build-system]
26
+ requires = ["uv_build>=0.12.5,<0.13.0"]
27
+ build-backend = "uv_build"
28
+
29
+ [dependency-groups]
30
+ dev = [
31
+ "hypothesis",
32
+ "mypy",
33
+ "pytest",
34
+ "ruff",
35
+ ]
36
+
37
+ [tool.pytest.ini_options]
38
+ testpaths = ["tests"]
39
+ pythonpath = ["."]
40
+ addopts = ["--import-mode=importlib"]
41
+ markers = ["conformance: a case of the language-neutral suite in conformance/"]
42
+
43
+ [tool.ruff]
44
+ line-length = 100
45
+ target-version = "py314"
46
+
47
+ [tool.ruff.lint]
48
+ select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF"]
49
+ # The docstrings use the notation of docs/MODEL.md (×, −, ·, ≤), which RUF001–003 flag.
50
+ # SIM108 asks for ternaries where the code prefers if statements.
51
+ ignore = ["RUF001", "RUF002", "RUF003", "SIM108"]
52
+
53
+ [tool.mypy]
54
+ strict = true
55
+ files = ["src", "tests", "benchmarks"]
@@ -0,0 +1,63 @@
1
+ """Beacon attraction in geometric graphs: attracted sets, kernels and covers, with exact integer
2
+ predicates (docs/MODEL.md; the Python API is in docs/PYTHON-API.md).
3
+
4
+ Everything public is imported from here; the modules beneath are internal.
5
+ """
6
+
7
+ from beacon_attraction._attraction.results import (
8
+ AttractionEnd,
9
+ AttractionForest,
10
+ AttractionPath,
11
+ AttractionTree,
12
+ BeaconEnd,
13
+ DeadPointEnd,
14
+ DeadVertexEnd,
15
+ StuckReason,
16
+ )
17
+ from beacon_attraction._cover.cover import Cover
18
+ from beacon_attraction._graph.component_table import ComponentTable
19
+ from beacon_attraction._graph.graphs import (
20
+ AttractionGraph,
21
+ DirectedGeometricGraph,
22
+ DirectedGraph,
23
+ FrozenDirectedGeometricGraph,
24
+ FrozenGeometricGraph,
25
+ GeometricGraph,
26
+ UndirectedGraph,
27
+ )
28
+ from beacon_attraction._graph.index_kind import NeighborIndexKind
29
+ from beacon_attraction._graph.violations import (
30
+ CoincidentVertices,
31
+ GraphValidationError,
32
+ PrecisionExceeded,
33
+ SelfLoop,
34
+ VertexInsideSegment,
35
+ Violation,
36
+ )
37
+
38
+ __all__ = [
39
+ "AttractionEnd",
40
+ "AttractionForest",
41
+ "AttractionGraph",
42
+ "AttractionPath",
43
+ "AttractionTree",
44
+ "BeaconEnd",
45
+ "CoincidentVertices",
46
+ "ComponentTable",
47
+ "Cover",
48
+ "DeadPointEnd",
49
+ "DeadVertexEnd",
50
+ "DirectedGeometricGraph",
51
+ "DirectedGraph",
52
+ "FrozenDirectedGeometricGraph",
53
+ "FrozenGeometricGraph",
54
+ "GeometricGraph",
55
+ "GraphValidationError",
56
+ "NeighborIndexKind",
57
+ "PrecisionExceeded",
58
+ "SelfLoop",
59
+ "StuckReason",
60
+ "UndirectedGraph",
61
+ "VertexInsideSegment",
62
+ "Violation",
63
+ ]
@@ -0,0 +1,2 @@
1
+ """Attraction (MODEL.md §6): where every vertex ends up under one beacon. The results are
2
+ public, re-exported by ``beacon_attraction``; the algorithms are internal."""
@@ -0,0 +1,202 @@
1
+ """The four attraction queries (MODEL.md §6): a path, the tree of a vertex, the beacon's tree, and
2
+ the forest. They share one steepest step, and their results agree: every path is the branch of
3
+ its vertex's tree, and every tree is a tree of the forest."""
4
+
5
+ from collections.abc import Callable, MutableMapping
6
+
7
+ from beacon_attraction._attraction.results import (
8
+ AttractionEnd,
9
+ AttractionForest,
10
+ AttractionPath,
11
+ AttractionTree,
12
+ BeaconEnd,
13
+ DeadPointEnd,
14
+ DeadVertexEnd,
15
+ StuckReason,
16
+ )
17
+ from beacon_attraction._graph.view import GraphView
18
+ from beacon_attraction._indexes.precomputed import PrecomputedIndex
19
+ from beacon_attraction._indexes.search import NeighborSearch
20
+ from beacon_attraction._indexes.steps import Step, StepKind
21
+ from beacon_attraction._numerics.vectors import direction, dot
22
+
23
+
24
+ def path(view: GraphView, beacon: int, vertex: int) -> AttractionPath:
25
+ """The path of ``vertex``: its steepest steps, from it to its end."""
26
+ search = view.index.search(beacon)
27
+ motion, step = _follow(search, vertex, known=None)
28
+ end = _end_for(view, beacon, motion[-1], step)
29
+ return AttractionPath(beacon, tuple(motion), end)
30
+
31
+
32
+ def tree(view: GraphView, beacon: int, vertex: int) -> AttractionTree:
33
+ """The tree ``vertex`` belongs to: up its path to the end, then a reverse search down from
34
+ that end. The steps computed on the way up are kept, so the search reuses them."""
35
+ search = view.index.search(beacon)
36
+ known = _step_cache(view)
37
+ motion, step = _follow(search, vertex, known)
38
+ end = _end_for(view, beacon, motion[-1], step)
39
+ return _reverse_search(view, search, beacon, end, known)
40
+
41
+
42
+ def beacon_tree(view: GraphView, beacon: int) -> AttractionTree:
43
+ """The beacon's tree, whose vertices are the attracted set ``A_V(b)``: a reverse search from
44
+ the beacon, at most ``O(E)``."""
45
+ search = view.index.search(beacon)
46
+ return _reverse_search(view, search, beacon, BeaconEnd(beacon), _step_cache(view))
47
+
48
+
49
+ def forest(view: GraphView, beacon: int) -> AttractionForest:
50
+ """Every vertex's next vertex and end: one steepest step per vertex, ``O(E)``."""
51
+ search = view.index.search(beacon)
52
+ steps = [search.steepest(v) for v in range(view.vertex_count)]
53
+ nexts = [next_vertex if kind is StepKind.MOVES else -1 for kind, next_vertex in steps]
54
+
55
+ # Each vertex's end: follow its path to a vertex whose end is known, or to where it ends, and
56
+ # give every vertex passed the same end. The beacon's end is index 0. Going up the ids, the
57
+ # first vertex met in a tree is its smallest, so the other ends get their indices in the
58
+ # order of their trees' smallest vertices.
59
+ ends: list[AttractionEnd] = [BeaconEnd(beacon)]
60
+ end_index = [-1] * view.vertex_count
61
+ end_index[beacon] = 0
62
+ dead_points: dict[tuple[int, int], int] = {} # both endpoints of a segment share its dead point
63
+
64
+ def new_end(u: int) -> int:
65
+ kind, next_vertex = steps[u]
66
+ if kind is StepKind.DEAD_POINT:
67
+ segment = (min(u, next_vertex), max(u, next_vertex))
68
+ shared = dead_points.get(segment)
69
+ if shared is not None:
70
+ return shared
71
+ dead_points[segment] = len(ends)
72
+ ends.append(_end_for(view, beacon, u, steps[u]))
73
+ return len(ends) - 1
74
+
75
+ for v in range(view.vertex_count):
76
+ if end_index[v] >= 0:
77
+ continue
78
+ passed = []
79
+ u = v
80
+ while end_index[u] < 0 and nexts[u] >= 0:
81
+ passed.append(u)
82
+ u = nexts[u]
83
+ if end_index[u] < 0:
84
+ end_index[u] = new_end(u) # u is where the path ends, and its end is new
85
+ for w in passed:
86
+ end_index[w] = end_index[u]
87
+
88
+ return AttractionForest(beacon, tuple(ends), tuple(nexts), tuple(end_index))
89
+
90
+
91
+ def _step_cache(view: GraphView) -> dict[int, Step] | None:
92
+ """Where a tree keeps the steps it computes, so that it computes each once; ``None`` for a
93
+ precomputed copy, whose steps are lookups already, cheaper than the cache."""
94
+ if isinstance(view.index, PrecomputedIndex):
95
+ return None
96
+ return {}
97
+
98
+
99
+ def _follow(
100
+ search: NeighborSearch, vertex: int, known: MutableMapping[int, Step] | None
101
+ ) -> tuple[list[int], Step]:
102
+ """Follows the steps from ``vertex`` while it moves: the vertices in the order visited, and
103
+ the step that ends the motion. The distance to the beacon strictly decreases, so no vertex
104
+ repeats. ``known``, if given, keeps each step computed."""
105
+ motion = [vertex]
106
+ step = search.steepest(vertex)
107
+ if known is not None:
108
+ known[vertex] = step
109
+ while step[0] is StepKind.MOVES:
110
+ motion.append(step[1])
111
+ step = search.steepest(step[1])
112
+ if known is not None:
113
+ known[motion[-1]] = step
114
+ return motion, step
115
+
116
+
117
+ def _end_for(view: GraphView, beacon: int, last: int, step: Step) -> AttractionEnd:
118
+ """The end of a motion whose last vertex is ``last``, which stops with ``step``."""
119
+ kind, next_vertex = step
120
+ match kind:
121
+ case StepKind.AT_BEACON:
122
+ return BeaconEnd(last)
123
+ case StepKind.TIE:
124
+ return DeadVertexEnd(last, StuckReason.TIE)
125
+ case StepKind.NO_DESCENT:
126
+ return DeadVertexEnd(last, StuckReason.NO_DESCENT)
127
+ case StepKind.DEAD_POINT:
128
+ return _dead_point_end(view, beacon, last, next_vertex)
129
+ raise ValueError("A step that moves does not end a motion.")
130
+
131
+
132
+ def _dead_point_end(view: GraphView, beacon: int, u: int, v: int) -> DeadPointEnd:
133
+ """The dead point on the segment from ``u`` toward ``v``: the foot of the perpendicular from
134
+ the beacon, ``f = (m·u + n·uv) / m`` with P3's integers ``n = ub·uv`` and ``m = |uv|²``."""
135
+ pu = view.coordinates[u]
136
+ uv = direction(pu, view.coordinates[v])
137
+ ub = direction(pu, view.coordinates[beacon])
138
+ n, m = dot(ub, uv), dot(uv, uv)
139
+ numerators = [m * x + n * component for x, component in zip(pu, uv, strict=True)]
140
+ return DeadPointEnd((min(u, v), max(u, v)), view.precision.to_fractions(numerators, m))
141
+
142
+
143
+ def _reverse_search(
144
+ view: GraphView,
145
+ search: NeighborSearch,
146
+ beacon: int,
147
+ end: AttractionEnd,
148
+ known: dict[int, Step] | None,
149
+ ) -> AttractionTree:
150
+ """Every vertex that ends at ``end``, with its next vertex: a search down from the end, level
151
+ by level. An in-neighbor ``v`` of a tree vertex ``u`` joins the tree with next vertex ``u``
152
+ when its step moves to ``u``. A vertex's step never changes, so it joins at most once.
153
+ ``known`` holds the steps already computed, and the search adds the ones it computes; with
154
+ ``None`` it looks every step up in the search instead."""
155
+ step_of: Callable[[int], Step] = search.steepest
156
+ if known is not None:
157
+ cache = known
158
+
159
+ def step_of(v: int) -> Step: # computed once, then kept
160
+ step = cache.get(v)
161
+ if step is None:
162
+ step = search.steepest(v)
163
+ cache[v] = step
164
+ return step
165
+
166
+ # The tree's vertices with their next vertices, in the order found; the first level is the
167
+ # vertices directly under the end, whose paths end there (-1).
168
+ level = _under_end(end, step_of)
169
+ vertices = list(level)
170
+ nexts = [-1] * len(level)
171
+ in_neighbors = view.in_neighbors
172
+ while level:
173
+ children = []
174
+ for u in level:
175
+ for v in in_neighbors[u]:
176
+ kind, next_vertex = step_of(v)
177
+ if kind is StepKind.MOVES and next_vertex == u:
178
+ children.append(v)
179
+ vertices.append(v)
180
+ nexts.append(u)
181
+ level = children
182
+
183
+ order = sorted(range(len(vertices)), key=vertices.__getitem__)
184
+ return AttractionTree(
185
+ beacon, end, tuple(vertices[i] for i in order), tuple(nexts[i] for i in order)
186
+ )
187
+
188
+
189
+ def _under_end(end: AttractionEnd, step_of: Callable[[int], Step]) -> list[int]:
190
+ """The vertices directly under ``end``: the beacon or the dead vertex itself, or the
191
+ endpoints that slide into the dead point (one or two)."""
192
+ match end:
193
+ case BeaconEnd(vertex) | DeadVertexEnd(vertex, _):
194
+ return [vertex]
195
+ case DeadPointEnd((u, v), _):
196
+ under = []
197
+ for p, q in ((u, v), (v, u)):
198
+ kind, next_vertex = step_of(p)
199
+ if kind is StepKind.DEAD_POINT and next_vertex == q:
200
+ under.append(p)
201
+ return under
202
+ raise ValueError(f"Unknown end {end!r}.")