stochastic-matching 0.3.0__py3-none-any.whl
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.
- stochastic_matching/__init__.py +24 -0
- stochastic_matching/common.py +253 -0
- stochastic_matching/display.py +633 -0
- stochastic_matching/graphs.py +838 -0
- stochastic_matching/model.py +1653 -0
- stochastic_matching/simulator/__init__.py +0 -0
- stochastic_matching/simulator/arrivals.py +103 -0
- stochastic_matching/simulator/e_filtering.py +196 -0
- stochastic_matching/simulator/fcfm.py +142 -0
- stochastic_matching/simulator/graph.py +72 -0
- stochastic_matching/simulator/logs.py +28 -0
- stochastic_matching/simulator/longest.py +307 -0
- stochastic_matching/simulator/multiqueue.py +271 -0
- stochastic_matching/simulator/priority.py +111 -0
- stochastic_matching/simulator/random_edge.py +136 -0
- stochastic_matching/simulator/random_item.py +95 -0
- stochastic_matching/simulator/simulator.py +332 -0
- stochastic_matching/simulator/virtual_queue.py +346 -0
- stochastic_matching-0.3.0.dist-info/AUTHORS.rst +13 -0
- stochastic_matching-0.3.0.dist-info/LICENSE +33 -0
- stochastic_matching-0.3.0.dist-info/METADATA +102 -0
- stochastic_matching-0.3.0.dist-info/RECORD +23 -0
- stochastic_matching-0.3.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Top-level package for Stochastic Matching."""
|
|
2
|
+
|
|
3
|
+
__author__ = """Fabien Mathieu"""
|
|
4
|
+
__email__ = 'fabien.mathieu@normalesup.org'
|
|
5
|
+
__version__ = '0.3.0'
|
|
6
|
+
|
|
7
|
+
from stochastic_matching.model import Model
|
|
8
|
+
from stochastic_matching.graphs import Path, Star, Cycle, Codomino, CycleChain, Complete, Pyramid, HyperPaddle, \
|
|
9
|
+
KayakPaddle, Lollipop, Tadpole, Barbell, Fan, concatenate
|
|
10
|
+
from stochastic_matching.simulator.simulator import Simulator
|
|
11
|
+
from stochastic_matching.simulator.fcfm import FCFM
|
|
12
|
+
from stochastic_matching.simulator.longest import Longest
|
|
13
|
+
from stochastic_matching.simulator.priority import Priority
|
|
14
|
+
from stochastic_matching.simulator.random_edge import RandomEdge
|
|
15
|
+
from stochastic_matching.simulator.random_item import RandomItem
|
|
16
|
+
from stochastic_matching.simulator.virtual_queue import VirtualQueue
|
|
17
|
+
from stochastic_matching.simulator.e_filtering import EFiltering
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# from stochastic_matching.old_simulator.age_based import FCFM
|
|
21
|
+
# from stochastic_matching.old_simulator.virtual_queue import VQSimulator
|
|
22
|
+
# from stochastic_matching.old_simulator.size_based import LongestSimulator, PrioritySimulator, QueueSizeSimulator, \
|
|
23
|
+
# RandomItemSimulator, \
|
|
24
|
+
# RandomNodeSimulator, FilteringGreedy
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
from inspect import isclass
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from numba import njit
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
if os.environ.get("NUMBA_DISABLE_JIT") == "1":
|
|
8
|
+
List = lambda x: x
|
|
9
|
+
else:
|
|
10
|
+
from numba.typed import List
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def pseudo_inverse_scalar(x):
|
|
14
|
+
"""
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
x: :class:`float`
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
:class:`float`
|
|
22
|
+
Inverse of x if it is not 0.
|
|
23
|
+
|
|
24
|
+
Examples
|
|
25
|
+
--------
|
|
26
|
+
|
|
27
|
+
>>> pseudo_inverse_scalar(2.0)
|
|
28
|
+
0.5
|
|
29
|
+
>>> pseudo_inverse_scalar(0)
|
|
30
|
+
0.0
|
|
31
|
+
"""
|
|
32
|
+
return 0.0 if x == 0 else 1 / x
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def clean_zeros(matrix, tol=1e-10):
|
|
36
|
+
"""
|
|
37
|
+
Replace in-place all small values of a matrix by 0.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
matrix: :class:`~numpy.ndarray`
|
|
42
|
+
Matrix to clean.
|
|
43
|
+
tol: :class:`float`, optional
|
|
44
|
+
Threshold. All entries with absolute value lower than `tol` are put to zero.
|
|
45
|
+
|
|
46
|
+
Returns
|
|
47
|
+
-------
|
|
48
|
+
None
|
|
49
|
+
|
|
50
|
+
Examples
|
|
51
|
+
--------
|
|
52
|
+
|
|
53
|
+
>>> import numpy as np
|
|
54
|
+
>>> mat = np.array([[1e-12, -.3], [.8, -1e-13]])
|
|
55
|
+
>>> clean_zeros(mat)
|
|
56
|
+
>>> mat # doctest: +NORMALIZE_WHITESPACE
|
|
57
|
+
array([[ 0. , -0.3],
|
|
58
|
+
[ 0.8, 0. ]])
|
|
59
|
+
"""
|
|
60
|
+
matrix[abs(matrix[:]) < tol] = 0
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class CharMaker:
|
|
64
|
+
"""
|
|
65
|
+
Class that acts as an infinite list of letters. Used to provide letter-labels to nodes
|
|
66
|
+
|
|
67
|
+
Examples
|
|
68
|
+
--------
|
|
69
|
+
|
|
70
|
+
>>> names = CharMaker()
|
|
71
|
+
>>> names[0]
|
|
72
|
+
'A'
|
|
73
|
+
>>> names[7]
|
|
74
|
+
'H'
|
|
75
|
+
>>> names[26]
|
|
76
|
+
'AA'
|
|
77
|
+
>>> names[107458610947716]
|
|
78
|
+
'STOCHASTIC'
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(self):
|
|
82
|
+
pass
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def to_char(i):
|
|
86
|
+
return chr(ord('A') + (i % 26))
|
|
87
|
+
|
|
88
|
+
def __getitem__(self, i):
|
|
89
|
+
res = self.to_char(i)
|
|
90
|
+
while i > 25:
|
|
91
|
+
i = i // 26 - 1
|
|
92
|
+
res = f"{self.to_char(i)}{res}"
|
|
93
|
+
return res
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def get_classes(root):
|
|
97
|
+
"""
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
root: :class:`class`
|
|
101
|
+
Starting class (can be abstract).
|
|
102
|
+
|
|
103
|
+
Returns
|
|
104
|
+
-------
|
|
105
|
+
:class:`dict`
|
|
106
|
+
Dictionaries of all subclasses that have a name (as in class attribute `name`).
|
|
107
|
+
|
|
108
|
+
Examples
|
|
109
|
+
--------
|
|
110
|
+
>>> import stochastic_matching as sm
|
|
111
|
+
>>> get_classes(sm.Model) # doctest: +NORMALIZE_WHITESPACE
|
|
112
|
+
{'Path': <class 'stochastic_matching.graphs.Path'>,
|
|
113
|
+
'Star': <class 'stochastic_matching.graphs.Star'>,
|
|
114
|
+
'Cycle': <class 'stochastic_matching.graphs.Cycle'>,
|
|
115
|
+
'Complete': <class 'stochastic_matching.graphs.Complete'>,
|
|
116
|
+
'Codomino': <class 'stochastic_matching.graphs.Codomino'>,
|
|
117
|
+
'Pyramid': <class 'stochastic_matching.graphs.Pyramid'>,
|
|
118
|
+
'Tadpole': <class 'stochastic_matching.graphs.Tadpole'>,
|
|
119
|
+
'Lollipop': <class 'stochastic_matching.graphs.Lollipop'>,
|
|
120
|
+
'Kayak Paddle': <class 'stochastic_matching.graphs.KayakPaddle'>,
|
|
121
|
+
'Barbell': <class 'stochastic_matching.graphs.Barbell'>,
|
|
122
|
+
'Cycle Chain': <class 'stochastic_matching.graphs.CycleChain'>,
|
|
123
|
+
'Hyper Kayak Paddle': <class 'stochastic_matching.graphs.HyperPaddle'>,
|
|
124
|
+
'Fan': <class 'stochastic_matching.graphs.Fan'>}
|
|
125
|
+
"""
|
|
126
|
+
result = {c.name: c for c in root.__subclasses__() if c.name}
|
|
127
|
+
for c in root.__subclasses__():
|
|
128
|
+
result.update(get_classes(c))
|
|
129
|
+
return result
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def class_converter(subclass, motherclass):
|
|
133
|
+
"""
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
subclass: :class:`str` or :class:`class`
|
|
137
|
+
Required subclass, or its name.
|
|
138
|
+
motherclass: :class:`class`
|
|
139
|
+
Ancestor of the subclass.
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
:class:`class`
|
|
144
|
+
The subclass.
|
|
145
|
+
|
|
146
|
+
Examples
|
|
147
|
+
--------
|
|
148
|
+
|
|
149
|
+
>>> import stochastic_matching as sm
|
|
150
|
+
>>> from stochastic_matching.simulator.simulator import Simulator
|
|
151
|
+
>>> class_converter('random_edge', Simulator)
|
|
152
|
+
<class 'stochastic_matching.simulator.random_edge.RandomEdge'>
|
|
153
|
+
>>> class_converter(sm.FCFM, Simulator)
|
|
154
|
+
<class 'stochastic_matching.simulator.fcfm.FCFM'>
|
|
155
|
+
>>> class_converter('anything', Simulator) # doctest: +NORMALIZE_WHITESPACE
|
|
156
|
+
Traceback (most recent call last):
|
|
157
|
+
...
|
|
158
|
+
ValueError: anything is not a known name for a subclass of Simulator.
|
|
159
|
+
Known names: fcfm, longest, priority, random_edge, random_item, virtual_queue, e_filtering.
|
|
160
|
+
|
|
161
|
+
>>> class_converter(2, Simulator)
|
|
162
|
+
Traceback (most recent call last):
|
|
163
|
+
...
|
|
164
|
+
TypeError: Subclass must be string or Simulator subclass (not instance).
|
|
165
|
+
"""
|
|
166
|
+
if isinstance(subclass, str):
|
|
167
|
+
class_dict = get_classes(motherclass)
|
|
168
|
+
if subclass in class_dict:
|
|
169
|
+
return class_dict[subclass]
|
|
170
|
+
else:
|
|
171
|
+
raise ValueError(f"{subclass} is not a known name for a subclass of {motherclass.__name__}. "
|
|
172
|
+
f"Known names: {', '.join(class_dict.keys())}.")
|
|
173
|
+
elif isclass(subclass) and issubclass(subclass, motherclass):
|
|
174
|
+
return subclass
|
|
175
|
+
else:
|
|
176
|
+
raise TypeError(f"Subclass must be string or {motherclass.__name__} subclass (not instance).")
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def neighbors(i, compressed_incidence):
|
|
180
|
+
"""
|
|
181
|
+
Return neighbors of a node/edge with respect to an incident matrix.
|
|
182
|
+
Neighborhood is defined on hypergraph level, not on adjacency level:
|
|
183
|
+
neighbors of a node are edges, neighbors of an edge are nodes.
|
|
184
|
+
|
|
185
|
+
Parameters
|
|
186
|
+
----------
|
|
187
|
+
i: :class:`int`
|
|
188
|
+
Index of the node/edge to probe.
|
|
189
|
+
compressed_incidence: :class:`~scipy.sparse.csr_matrix` or :class:`~scipy.sparse.csc_matrix`
|
|
190
|
+
Compressed sparse incidence matrix on rows (for nodes) or columns (for edges).
|
|
191
|
+
|
|
192
|
+
Returns
|
|
193
|
+
-------
|
|
194
|
+
:class:`~numpy.ndarray`
|
|
195
|
+
Neighbors of *i*.
|
|
196
|
+
|
|
197
|
+
Examples
|
|
198
|
+
--------
|
|
199
|
+
|
|
200
|
+
A hypergraph with 4 nodes, 2 regular edges (0, 1) and (0, 2) and one 4-edge (0, 1, 2, 3).
|
|
201
|
+
|
|
202
|
+
>>> import numpy as np
|
|
203
|
+
>>> from scipy.sparse import csr_matrix, csc_matrix
|
|
204
|
+
>>> incidence = np.array([[1, 1, 1],
|
|
205
|
+
... [1, 0, 1],
|
|
206
|
+
... [0, 1, 1],
|
|
207
|
+
... [0, 0, 1]])
|
|
208
|
+
|
|
209
|
+
Edges of node 0:
|
|
210
|
+
|
|
211
|
+
>>> neighbors(0, csr_matrix(incidence)) # doctest: +ELLIPSIS
|
|
212
|
+
array([0, 1, 2]...)
|
|
213
|
+
|
|
214
|
+
Egde of node 3:
|
|
215
|
+
|
|
216
|
+
>>> neighbors(3, csr_matrix(incidence)) # doctest: +ELLIPSIS
|
|
217
|
+
array([2]...)
|
|
218
|
+
|
|
219
|
+
Nodes of edge 0:
|
|
220
|
+
|
|
221
|
+
>>> neighbors(0, csc_matrix(incidence)) # doctest: +ELLIPSIS
|
|
222
|
+
array([0, 1]...)
|
|
223
|
+
|
|
224
|
+
Nodes of hyperedge 2:
|
|
225
|
+
|
|
226
|
+
>>> neighbors(2, csc_matrix(incidence)) # doctest: +ELLIPSIS
|
|
227
|
+
array([0, 1, 2, 3]...)
|
|
228
|
+
"""
|
|
229
|
+
return compressed_incidence.indices[compressed_incidence.indptr[i]:compressed_incidence.indptr[i + 1]]
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
@njit
|
|
233
|
+
def set_seed(value):
|
|
234
|
+
"""
|
|
235
|
+
Change the random generator seed inside numba jitted scope.
|
|
236
|
+
|
|
237
|
+
Parameters
|
|
238
|
+
----------
|
|
239
|
+
value: :class:`int`
|
|
240
|
+
Seed.
|
|
241
|
+
|
|
242
|
+
Returns
|
|
243
|
+
-------
|
|
244
|
+
None
|
|
245
|
+
|
|
246
|
+
Examples
|
|
247
|
+
--------
|
|
248
|
+
|
|
249
|
+
>>> set_seed(42)
|
|
250
|
+
>>> np.random.rand()
|
|
251
|
+
0.3745401188473625
|
|
252
|
+
"""
|
|
253
|
+
np.random.seed(value)
|