stochastic-matching 0.3.0__tar.gz → 0.3.2__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.
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/PKG-INFO +3 -1
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/pyproject.toml +3 -1
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/__init__.py +2 -8
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/common.py +2 -2
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/model.py +3 -3
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/arrivals.py +5 -10
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/e_filtering.py +68 -63
- stochastic_matching-0.3.2/stochastic_matching/simulator/extended.py +56 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/fcfm.py +30 -37
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/graph.py +17 -6
- stochastic_matching-0.3.2/stochastic_matching/simulator/logs.py +96 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/longest.py +90 -98
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/multiqueue.py +5 -11
- stochastic_matching-0.3.2/stochastic_matching/simulator/parallel.py +359 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/priority.py +2 -3
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/random_edge.py +33 -28
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/random_item.py +21 -18
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/simulator.py +54 -42
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/virtual_queue.py +70 -101
- stochastic_matching-0.3.0/stochastic_matching/simulator/logs.py +0 -28
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/AUTHORS.rst +0 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/LICENSE +0 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/README.rst +0 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/display.py +0 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/graphs.py +0 -0
- {stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: stochastic-matching
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
4
4
|
Summary: Stochastic Matching provides tools to analyze the behavior of stochastic matching problems.
|
|
5
5
|
License: GPLv3
|
|
6
6
|
Author: Fabien Mathieu
|
|
@@ -14,8 +14,10 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
14
14
|
Requires-Dist: cached-property (>=1.5.2,<2.0.0)
|
|
15
15
|
Requires-Dist: ipython (>=8.24.0,<9.0.0)
|
|
16
16
|
Requires-Dist: matplotlib (>=3.9.0,<4.0.0)
|
|
17
|
+
Requires-Dist: multiprocess (>=0.70.16,<0.71.0)
|
|
17
18
|
Requires-Dist: numba (>=0.59.1,<0.60.0)
|
|
18
19
|
Requires-Dist: scipy (>=1.13.0,<2.0.0)
|
|
20
|
+
Requires-Dist: tqdm (>=4.66.4,<5.0.0)
|
|
19
21
|
Description-Content-Type: text/x-rst
|
|
20
22
|
|
|
21
23
|
.. image:: https://github.com/balouf/stochastic_matching/raw/master/docs/sm_logo.png
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "stochastic-matching"
|
|
3
|
-
version = "v0.3.
|
|
3
|
+
version = "v0.3.2"
|
|
4
4
|
description = "Stochastic Matching provides tools to analyze the behavior of stochastic matching problems."
|
|
5
5
|
authors = ["Fabien Mathieu <fabien.mathieu@normalesup.org>"]
|
|
6
6
|
license = "GPLv3"
|
|
@@ -13,6 +13,8 @@ scipy = "^1.13.0"
|
|
|
13
13
|
cached-property = "^1.5.2"
|
|
14
14
|
ipython = "^8.24.0"
|
|
15
15
|
matplotlib = "^3.9.0"
|
|
16
|
+
tqdm = "^4.66.4"
|
|
17
|
+
multiprocess = "^0.70.16"
|
|
16
18
|
|
|
17
19
|
[tool.poetry.group.dev.dependencies]
|
|
18
20
|
pytest-cov = "^5.0.0"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
__author__ = """Fabien Mathieu"""
|
|
4
4
|
__email__ = 'fabien.mathieu@normalesup.org'
|
|
5
|
-
__version__ = '0.3.
|
|
5
|
+
__version__ = '0.3.2'
|
|
6
6
|
|
|
7
7
|
from stochastic_matching.model import Model
|
|
8
8
|
from stochastic_matching.graphs import Path, Star, Cycle, Codomino, CycleChain, Complete, Pyramid, HyperPaddle, \
|
|
@@ -15,10 +15,4 @@ from stochastic_matching.simulator.random_edge import RandomEdge
|
|
|
15
15
|
from stochastic_matching.simulator.random_item import RandomItem
|
|
16
16
|
from stochastic_matching.simulator.virtual_queue import VirtualQueue
|
|
17
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
|
|
18
|
+
from stochastic_matching.simulator.parallel import XP, Runner, Iterator, evaluate
|
|
@@ -156,7 +156,7 @@ def class_converter(subclass, motherclass):
|
|
|
156
156
|
Traceback (most recent call last):
|
|
157
157
|
...
|
|
158
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
|
|
159
|
+
Known names: e_filtering, fcfm, longest, priority, random_edge, random_item, virtual_queue.
|
|
160
160
|
|
|
161
161
|
>>> class_converter(2, Simulator)
|
|
162
162
|
Traceback (most recent call last):
|
|
@@ -169,7 +169,7 @@ def class_converter(subclass, motherclass):
|
|
|
169
169
|
return class_dict[subclass]
|
|
170
170
|
else:
|
|
171
171
|
raise ValueError(f"{subclass} is not a known name for a subclass of {motherclass.__name__}. "
|
|
172
|
-
f"Known names: {', '.join(class_dict.keys())}.")
|
|
172
|
+
f"Known names: {', '.join(sorted(class_dict.keys()))}.")
|
|
173
173
|
elif isclass(subclass) and issubclass(subclass, motherclass):
|
|
174
174
|
return subclass
|
|
175
175
|
else:
|
|
@@ -1631,10 +1631,10 @@ class Model:
|
|
|
1631
1631
|
>>> candy.run('longest', seed=42, n_steps=20000)
|
|
1632
1632
|
False
|
|
1633
1633
|
>>> candy.simulator.logs.steps_done
|
|
1634
|
-
|
|
1634
|
+
10458
|
|
1635
1635
|
>>> candy.simulation # doctest: +NORMALIZE_WHITESPACE
|
|
1636
|
-
array([0.
|
|
1637
|
-
0.
|
|
1636
|
+
array([0.64234079, 0.37590361, 0.38760757, 0.40757315, 0.40895009,
|
|
1637
|
+
0.59208262, 0.2939759 ])
|
|
1638
1638
|
|
|
1639
1639
|
A greedy simulator performs poorly on the hyperedge.
|
|
1640
1640
|
|
{stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/arrivals.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import numpy as np
|
|
2
|
-
from numba import njit,
|
|
2
|
+
from numba import njit, int64, float64
|
|
3
3
|
from numba.experimental import jitclass
|
|
4
4
|
|
|
5
5
|
|
|
@@ -28,7 +28,7 @@ def create_prob_alias(mu):
|
|
|
28
28
|
>>> probas, aliases = create_prob_alias([2 ,2, 3, 1])
|
|
29
29
|
>>> probas
|
|
30
30
|
array([1. , 1. , 1. , 0.5])
|
|
31
|
-
>>> aliases
|
|
31
|
+
>>> aliases.astype(int)
|
|
32
32
|
array([0, 0, 0, 2])
|
|
33
33
|
"""
|
|
34
34
|
if isinstance(mu, list):
|
|
@@ -36,7 +36,7 @@ def create_prob_alias(mu):
|
|
|
36
36
|
else:
|
|
37
37
|
cmu = mu
|
|
38
38
|
n = len(cmu)
|
|
39
|
-
alias = np.zeros(n, dtype=np.
|
|
39
|
+
alias = np.zeros(n, dtype=np.int64)
|
|
40
40
|
prob = np.zeros(n)
|
|
41
41
|
# noinspection PyUnresolvedReferences
|
|
42
42
|
normalized_intensities = cmu * n / np.sum(cmu)
|
|
@@ -55,10 +55,6 @@ def create_prob_alias(mu):
|
|
|
55
55
|
return prob, alias
|
|
56
56
|
|
|
57
57
|
|
|
58
|
-
alias_type = typeof(np.zeros(1, dtype=int))
|
|
59
|
-
prob_type = typeof(np.zeros(1))
|
|
60
|
-
|
|
61
|
-
|
|
62
58
|
@jitclass
|
|
63
59
|
class Arrivals:
|
|
64
60
|
"""
|
|
@@ -81,8 +77,8 @@ class Arrivals:
|
|
|
81
77
|
>>> Counter([arrivals.draw() for _ in range(800)])
|
|
82
78
|
Counter({2: 291, 1: 210, 0: 208, 3: 91})
|
|
83
79
|
"""
|
|
84
|
-
prob:
|
|
85
|
-
alias:
|
|
80
|
+
prob: float64[:]
|
|
81
|
+
alias: int64[:]
|
|
86
82
|
n: int
|
|
87
83
|
|
|
88
84
|
def __init__(self, mu, seed=None):
|
|
@@ -100,4 +96,3 @@ class Arrivals:
|
|
|
100
96
|
if np.random.rand() > self.prob[node]:
|
|
101
97
|
node = self.alias[node]
|
|
102
98
|
return node
|
|
103
|
-
|
{stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/e_filtering.py
RENAMED
|
@@ -5,6 +5,7 @@ from stochastic_matching.simulator.graph import make_jit_graph
|
|
|
5
5
|
from stochastic_matching.simulator.simulator import Simulator
|
|
6
6
|
from stochastic_matching.model import Model
|
|
7
7
|
from stochastic_matching.common import class_converter
|
|
8
|
+
from stochastic_matching.simulator.logs import PhantomLogs
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
def expand_model(model, forbidden_edges, epsilon):
|
|
@@ -41,7 +42,7 @@ def expand_model(model, forbidden_edges, epsilon):
|
|
|
41
42
|
for node in nodes[1]:
|
|
42
43
|
new_inc[node, i] = 1
|
|
43
44
|
|
|
44
|
-
return Model(incidence=new_inc, rates=new_rates), [e[0] for e in edge_codex]
|
|
45
|
+
return Model(incidence=new_inc, rates=new_rates), np.array([e[0] for e in edge_codex]).astype(np.int64)
|
|
45
46
|
|
|
46
47
|
|
|
47
48
|
class EFiltering(Simulator):
|
|
@@ -63,17 +64,8 @@ class EFiltering(Simulator):
|
|
|
63
64
|
(overrides forbidden_edges argument).
|
|
64
65
|
epsilon: :class:`float`, default=.01
|
|
65
66
|
Proportion of "+" arrivals (lower means less odds to select a forbidden edge).
|
|
66
|
-
**
|
|
67
|
-
Keyword arguments.
|
|
68
|
-
|
|
69
|
-
Notes
|
|
70
|
-
-----
|
|
71
|
-
|
|
72
|
-
Due to technical constraints, the queue_log does not contain the actual queue_log of the true model but the
|
|
73
|
-
sum of the queue_log of the "+" and "-" items.
|
|
74
|
-
For example, queue_log[0, 0] contains the sum of the times where 0+ is null and when 0- is null.
|
|
75
|
-
This does not impact the computation of flows and average queues but the CCDF is not accurate
|
|
76
|
-
and should not be used.
|
|
67
|
+
**base_policy_kwargs:
|
|
68
|
+
Keyword arguments. Only universal parameters should be used (seed, max_queue, n_steps).
|
|
77
69
|
|
|
78
70
|
Examples
|
|
79
71
|
--------
|
|
@@ -83,43 +75,57 @@ class EFiltering(Simulator):
|
|
|
83
75
|
>>> import stochastic_matching as sm
|
|
84
76
|
>>> diamond = sm.CycleChain(rates=[1, 2, 2, 1])
|
|
85
77
|
|
|
86
|
-
|
|
78
|
+
|
|
79
|
+
Without any specific argument, epsilon-filtering acts as a regular longest policy:
|
|
80
|
+
|
|
81
|
+
>>> sim = EFiltering(diamond, n_steps=1000, seed=42)
|
|
82
|
+
>>> sim.run()
|
|
83
|
+
>>> sim.plogs
|
|
84
|
+
Arrivals: [162 342 348 148]
|
|
85
|
+
Traffic: [ 76 86 190 76 72]
|
|
86
|
+
Queues: [[882 91 23 ... 0 0 0]
|
|
87
|
+
[661 109 72 ... 0 0 0]
|
|
88
|
+
[737 111 63 ... 0 0 0]
|
|
89
|
+
[870 96 27 ... 0 0 0]]
|
|
90
|
+
Steps done: 1000
|
|
91
|
+
|
|
92
|
+
Let us use epsilon-filtering by specifying forbidden_edges:
|
|
87
93
|
|
|
88
94
|
>>> sim = EFiltering(diamond, forbidden_edges=[0, 4], n_steps=1000, seed=42)
|
|
89
95
|
>>> sim.run()
|
|
90
|
-
>>> sim.
|
|
96
|
+
>>> sim.plogs
|
|
97
|
+
Arrivals: [162 342 348 148]
|
|
91
98
|
Traffic: [ 1 158 189 147 1]
|
|
92
|
-
Queues: [[
|
|
93
|
-
[
|
|
94
|
-
[
|
|
95
|
-
[
|
|
99
|
+
Queues: [[571 44 61 ... 0 0 0]
|
|
100
|
+
[560 37 52 ... 0 0 0]
|
|
101
|
+
[529 66 67 ... 0 0 0]
|
|
102
|
+
[537 62 59 ... 0 0 0]]
|
|
96
103
|
Steps done: 1000
|
|
97
104
|
|
|
98
|
-
Notice the big queue_log. Each row sums to 2000 instead of 1000 because of state aggregation. It does not
|
|
99
|
-
impact any computed metric apart from the CCDF, which is faulty.
|
|
100
|
-
|
|
101
105
|
Switch to a FCFM policy:
|
|
102
106
|
|
|
103
107
|
>>> sim = EFiltering(diamond, base_policy='fcfm', forbidden_edges=[0, 4], n_steps=1000, seed=42)
|
|
104
108
|
>>> sim.run()
|
|
105
|
-
>>> sim.
|
|
109
|
+
>>> sim.plogs
|
|
110
|
+
Arrivals: [162 342 348 148]
|
|
106
111
|
Traffic: [ 1 158 189 147 1]
|
|
107
|
-
Queues: [[
|
|
108
|
-
[
|
|
109
|
-
[
|
|
110
|
-
[
|
|
112
|
+
Queues: [[569 99 118 ... 0 0 0]
|
|
113
|
+
[569 35 37 ... 0 0 0]
|
|
114
|
+
[528 58 57 ... 0 0 0]
|
|
115
|
+
[551 84 98 ... 0 0 0]]
|
|
111
116
|
Steps done: 1000
|
|
112
117
|
|
|
113
118
|
Switch to virtual queue:
|
|
114
119
|
|
|
115
120
|
>>> sim = EFiltering(diamond, base_policy='virtual_queue', forbidden_edges=[0, 4], n_steps=1000, seed=42)
|
|
116
121
|
>>> sim.run()
|
|
117
|
-
>>> sim.
|
|
122
|
+
>>> sim.plogs
|
|
123
|
+
Arrivals: [162 342 348 148]
|
|
118
124
|
Traffic: [ 0 157 186 144 1]
|
|
119
|
-
Queues: [[
|
|
120
|
-
[
|
|
121
|
-
[
|
|
122
|
-
[
|
|
125
|
+
Queues: [[311 93 110 ... 0 0 0]
|
|
126
|
+
[108 120 111 ... 0 0 0]
|
|
127
|
+
[ 67 113 76 ... 0 0 0]
|
|
128
|
+
[174 142 137 ... 0 0 0]]
|
|
123
129
|
Steps done: 1000
|
|
124
130
|
|
|
125
131
|
Stolyar's example to see the behavior on hypergraph:
|
|
@@ -130,67 +136,66 @@ class EFiltering(Simulator):
|
|
|
130
136
|
... [0, 0, 0, 1, 0, 0, 1]], rates=[1.2, 1.5, 2, .8])
|
|
131
137
|
>>> rewards = [-1, -1, 1, 2, 5, 4, 7]
|
|
132
138
|
|
|
133
|
-
>>> sim =
|
|
139
|
+
>>> sim = EFiltering(stol, base_policy='virtual_queue', rewards=rewards, n_steps=1000, epsilon=.0001, seed=42)
|
|
134
140
|
>>> sim.run()
|
|
135
|
-
>>> sim.
|
|
141
|
+
>>> sim.plogs
|
|
142
|
+
Arrivals: [216 282 371 131]
|
|
136
143
|
Traffic: [ 0 0 311 76 213 0 55]
|
|
137
|
-
Queues: [[
|
|
138
|
-
[
|
|
139
|
-
[
|
|
140
|
-
[
|
|
144
|
+
Queues: [[ 30 114 181 ... 0 0 0]
|
|
145
|
+
[ 22 64 117 ... 0 0 0]
|
|
146
|
+
[ 24 590 217 ... 0 0 0]
|
|
147
|
+
[995 5 0 ... 0 0 0]]
|
|
141
148
|
Steps done: 1000
|
|
142
149
|
>>> sim.logs.traffic @ rewards
|
|
143
150
|
1913
|
|
144
151
|
>>> sim.compute_average_queues()
|
|
145
|
-
array([3.
|
|
152
|
+
array([3.577, 4.856, 1.65 , 0.005])
|
|
146
153
|
|
|
147
154
|
To compare with, the original EGPD policy:
|
|
148
155
|
|
|
149
|
-
>>> sim = sm.VirtualQueue(stol,
|
|
156
|
+
>>> sim = sm.VirtualQueue(stol, rewards=rewards, n_steps=1000, seed=42, beta=.01)
|
|
150
157
|
>>> sim.run()
|
|
151
|
-
>>> sim.
|
|
158
|
+
>>> sim.plogs
|
|
159
|
+
Arrivals: [236 279 342 143]
|
|
152
160
|
Traffic: [ 0 0 100 3 139 0 140]
|
|
153
|
-
Queues: [[
|
|
161
|
+
Queues: [[ 2 3 5 ... 0 0 0]
|
|
154
162
|
[844 8 6 ... 0 0 0]
|
|
155
|
-
[
|
|
163
|
+
[ 0 1 3 ... 0 0 0]
|
|
156
164
|
[597 230 96 ... 0 0 0]]
|
|
157
165
|
Steps done: 1000
|
|
158
166
|
>>> sim.logs.traffic @ rewards
|
|
159
167
|
1781
|
|
160
168
|
>>> sim.compute_average_queues()
|
|
161
|
-
array([54.
|
|
169
|
+
array([54.439, 1.597, 78.51 , 0.691])
|
|
162
170
|
"""
|
|
163
171
|
name = 'e_filtering'
|
|
164
172
|
|
|
165
|
-
def __init__(self, model, base_policy='longest', forbidden_edges=None,
|
|
166
|
-
**
|
|
167
|
-
if
|
|
168
|
-
|
|
169
|
-
flow = model.optimize_rates(
|
|
173
|
+
def __init__(self, model, base_policy='longest', forbidden_edges=None, rewards=None, epsilon=.01,
|
|
174
|
+
**base_policy_kwargs):
|
|
175
|
+
if rewards is not None:
|
|
176
|
+
rewards = np.array(rewards)
|
|
177
|
+
flow = model.optimize_rates(rewards)
|
|
170
178
|
forbidden_edges = [i for i in range(model.m) if flow[i] == 0]
|
|
179
|
+
self.rewards = rewards
|
|
171
180
|
else:
|
|
181
|
+
self.rewards = np.ones(model.m, dtype=int)
|
|
172
182
|
if forbidden_edges is None:
|
|
173
183
|
forbidden_edges = []
|
|
184
|
+
else:
|
|
185
|
+
self.rewards[forbidden_edges] = -1
|
|
174
186
|
self.base_policy = class_converter(base_policy, Simulator)
|
|
187
|
+
self.base_policy_kwargs = base_policy_kwargs
|
|
175
188
|
self.forbidden_edges = forbidden_edges
|
|
176
189
|
self.epsilon = epsilon
|
|
177
|
-
super().__init__(model, **
|
|
190
|
+
super().__init__(model, **base_policy_kwargs)
|
|
178
191
|
|
|
179
192
|
def set_internal(self):
|
|
180
193
|
expanded_model, edges = expand_model(model=self.model, forbidden_edges=self.forbidden_edges,
|
|
181
|
-
|
|
182
|
-
expanded_simu = self.base_policy(model=expanded_model,
|
|
183
|
-
|
|
184
|
-
self.internal = {'simu': expanded_simu, '
|
|
194
|
+
epsilon=self.epsilon)
|
|
195
|
+
expanded_simu = self.base_policy(model=expanded_model, **self.base_policy_kwargs)
|
|
196
|
+
expanded_simu.logs = PhantomLogs(n=self.model.n, m=self.model.m, max_queue=self.max_queue, edges=edges)
|
|
197
|
+
self.internal = {'simu': expanded_simu, 'n_steps': self.n_steps}
|
|
185
198
|
|
|
186
199
|
def run(self):
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
edges = self.internal['edges']
|
|
190
|
-
xlogs = simu.logs
|
|
191
|
-
logs = self.logs
|
|
192
|
-
simu.run()
|
|
193
|
-
logs.steps_done = xlogs.steps_done
|
|
194
|
-
logs.queue_log += (xlogs.queue_log[:n, :] + xlogs.queue_log[n:, :])
|
|
195
|
-
for i, t in enumerate(xlogs.traffic):
|
|
196
|
-
logs.traffic[edges[i]] += t
|
|
200
|
+
self.internal['simu'].run()
|
|
201
|
+
self.logs = self.internal['simu'].logs
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from stochastic_matching.simulator.simulator import Simulator
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ExtendedSimulator(Simulator):
|
|
7
|
+
"""
|
|
8
|
+
This extended (abstract) class of :class:`~stochastic_matching.simulator.simulator.Simulator` accepts
|
|
9
|
+
additional parameters designed to reach vertices of the stability polytope.
|
|
10
|
+
|
|
11
|
+
How these parameters are exactly used is determined by its sub-classes.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
model: :class:`~stochastic_matching.model.Model`
|
|
16
|
+
Model to simulate.
|
|
17
|
+
rewards: :class:`~numpy.ndarray` or :class:`list`, optional
|
|
18
|
+
Rewards associated to edges.
|
|
19
|
+
beta: :class:`float`, optional
|
|
20
|
+
Reward factor: each edge is given a default score 1/beta (small beta means higher reward impact).
|
|
21
|
+
Setting beta triggers reward-based scoring.
|
|
22
|
+
forbidden_edges: :class:`list` or :class:`~numpy.ndarray`, or :class:`bool`, optional
|
|
23
|
+
Edges that should not be used. If set to True, the list of forbidden edges is computed from the rewards.
|
|
24
|
+
k: :class:`int`, optional
|
|
25
|
+
Limit on queue size to apply edge interdiction (enforce stability on injective-only vertices).
|
|
26
|
+
kwargs: :class:`dict`
|
|
27
|
+
Keyword parameters of :class:`~stochastic_matching.simulator.simulator.Simulator`
|
|
28
|
+
"""
|
|
29
|
+
def __init__(self, model, rewards=None, beta=None,
|
|
30
|
+
forbidden_edges=None, k=None,
|
|
31
|
+
**kwargs):
|
|
32
|
+
if rewards is not None:
|
|
33
|
+
self.rewards = np.array(rewards)
|
|
34
|
+
else:
|
|
35
|
+
self.rewards = np.ones(model.m, dtype=int)
|
|
36
|
+
self.beta = beta
|
|
37
|
+
if forbidden_edges is True:
|
|
38
|
+
flow = model.optimize_rates(self.rewards)
|
|
39
|
+
forbidden_edges = [i for i in range(model.m) if flow[i] == 0]
|
|
40
|
+
if len(forbidden_edges) == 0:
|
|
41
|
+
forbidden_edges = None
|
|
42
|
+
self.forbidden_edges = forbidden_edges
|
|
43
|
+
if forbidden_edges and rewards is None:
|
|
44
|
+
self.rewards[forbidden_edges] = -1
|
|
45
|
+
self.k = k
|
|
46
|
+
super().__init__(model, **kwargs)
|
|
47
|
+
|
|
48
|
+
def set_internal(self):
|
|
49
|
+
super().set_internal()
|
|
50
|
+
if self.beta is None:
|
|
51
|
+
scores = np.zeros(self.model.m, dtype=int)
|
|
52
|
+
else:
|
|
53
|
+
scores = self.rewards / self.beta
|
|
54
|
+
self.internal['scores'] = scores
|
|
55
|
+
self.internal['forbidden_edges'] = self.forbidden_edges
|
|
56
|
+
self.internal['k'] = self.k
|
{stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/fcfm.py
RENAMED
|
@@ -5,14 +5,14 @@ from stochastic_matching.simulator.multiqueue import MultiQueue
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
@njit
|
|
8
|
-
def fcfm_core(arrivals, graph, n_steps, queue_size,
|
|
9
|
-
queues, # FCFM specific argument
|
|
10
|
-
traffic, queue_log, steps_done): # Monitored variables
|
|
8
|
+
def fcfm_core(logs, arrivals, graph, n_steps, queue_size, queues):
|
|
11
9
|
"""
|
|
12
10
|
Jitted function for first-come, first-matched policy.
|
|
13
11
|
|
|
14
12
|
Parameters
|
|
15
13
|
----------
|
|
14
|
+
logs: :class:`~stochastic_matching.simulator.logs.Logs`
|
|
15
|
+
Monitored variables.
|
|
16
16
|
arrivals: :class:`~stochastic_matching.simulator.arrivals.Arrivals`
|
|
17
17
|
Item arrival process.
|
|
18
18
|
graph: :class:`~stochastic_matching.simulator.graph.JitHyperGraph`
|
|
@@ -23,36 +23,26 @@ def fcfm_core(arrivals, graph, n_steps, queue_size, # Generic arguments
|
|
|
23
23
|
Number of waiting items of each class.
|
|
24
24
|
queues: :class:`~stochastic_matching.simulator.multiqueue.MultiQueue`
|
|
25
25
|
Waiting items of each class.
|
|
26
|
-
traffic: :class:`~numpy.ndarray`
|
|
27
|
-
Monitor traffic on edges.
|
|
28
|
-
queue_log: :class:`~numpy.ndarray`
|
|
29
|
-
Monitor queue sizes.
|
|
30
|
-
steps_done: :class:`int`
|
|
31
|
-
Number of arrivals processed so far.
|
|
32
26
|
|
|
33
27
|
Returns
|
|
34
28
|
-------
|
|
35
|
-
|
|
36
|
-
Total number of steps performed, possibly including previous runs.
|
|
29
|
+
None
|
|
37
30
|
"""
|
|
38
31
|
|
|
39
|
-
n, max_queue = queue_log.shape
|
|
32
|
+
n, max_queue = logs.queue_log.shape
|
|
40
33
|
inf = queues.infinity
|
|
41
34
|
|
|
42
35
|
for age in range(n_steps):
|
|
43
36
|
|
|
44
|
-
for j in range(n):
|
|
45
|
-
queue_log[j, queue_size[j]] += 1
|
|
46
|
-
|
|
47
37
|
node = arrivals.draw()
|
|
48
38
|
queue_size[node] += 1
|
|
49
39
|
if queue_size[node] == max_queue:
|
|
50
|
-
return
|
|
40
|
+
return None
|
|
51
41
|
queues.add(node, age)
|
|
52
42
|
|
|
53
43
|
# Test if an actionable edge may be present
|
|
44
|
+
best_edge = -1
|
|
54
45
|
if queue_size[node] == 1:
|
|
55
|
-
best_edge = -1
|
|
56
46
|
best_age = inf
|
|
57
47
|
# check oldest edge node
|
|
58
48
|
for e in graph.edges(node):
|
|
@@ -68,12 +58,12 @@ def fcfm_core(arrivals, graph, n_steps, queue_size, # Generic arguments
|
|
|
68
58
|
best_age = edge_age
|
|
69
59
|
|
|
70
60
|
if best_edge > -1:
|
|
71
|
-
traffic[best_edge] += 1
|
|
61
|
+
# logs.traffic[best_edge] += 1
|
|
72
62
|
queue_size[graph.nodes(best_edge)] -= 1
|
|
73
63
|
for v in graph.nodes(best_edge):
|
|
74
64
|
queues.pop(v)
|
|
75
65
|
|
|
76
|
-
|
|
66
|
+
logs.update(queue_size=queue_size, node=node, edge=best_edge)
|
|
77
67
|
|
|
78
68
|
|
|
79
69
|
class FCFM(Simulator):
|
|
@@ -84,13 +74,14 @@ class FCFM(Simulator):
|
|
|
84
74
|
Examples
|
|
85
75
|
--------
|
|
86
76
|
|
|
87
|
-
Let start with a working triangle. One can notice the results are the same for all greedy
|
|
77
|
+
Let start with a working triangle. One can notice the results are the same for all greedy simulator because
|
|
88
78
|
there are no multiple choices in a triangle (always one non-empty queue at most under a greedy policy).
|
|
89
79
|
|
|
90
80
|
>>> import stochastic_matching as sm
|
|
91
81
|
>>> sim = FCFM(sm.Cycle(rates=[3, 4, 5]), n_steps=1000, seed=42, max_queue=10)
|
|
92
82
|
>>> sim.run()
|
|
93
|
-
>>> sim.
|
|
83
|
+
>>> sim.plogs # doctest: +NORMALIZE_WHITESPACE
|
|
84
|
+
Arrivals: [287 338 375]
|
|
94
85
|
Traffic: [125 162 213]
|
|
95
86
|
Queues: [[838 104 41 13 3 1 0 0 0 0]
|
|
96
87
|
[796 119 53 22 8 2 0 0 0 0]
|
|
@@ -101,36 +92,38 @@ class FCFM(Simulator):
|
|
|
101
92
|
|
|
102
93
|
>>> sim = FCFM(sm.CycleChain(rates=[1, 1, 1, 1]), n_steps=1000, seed=42, max_queue=10)
|
|
103
94
|
>>> sim.run()
|
|
104
|
-
>>> sim.
|
|
95
|
+
>>> sim.plogs # doctest: +NORMALIZE_WHITESPACE
|
|
96
|
+
Arrivals: [85 82 85 86]
|
|
105
97
|
Traffic: [34 42 7 41 36]
|
|
106
|
-
Queues: [[
|
|
107
|
-
[
|
|
108
|
-
[
|
|
109
|
-
[
|
|
110
|
-
Steps done:
|
|
98
|
+
Queues: [[126 70 22 26 29 12 23 15 10 5]
|
|
99
|
+
[326 8 3 1 0 0 0 0 0 0]
|
|
100
|
+
[321 12 4 1 0 0 0 0 0 0]
|
|
101
|
+
[105 80 65 28 31 15 4 2 6 2]]
|
|
102
|
+
Steps done: 338
|
|
111
103
|
|
|
112
104
|
A stable candy (but candies are not good for greedy policies).
|
|
113
105
|
|
|
114
106
|
>>> sim = FCFM(sm.HyperPaddle(rates=[1, 1, 1.5, 1, 1.5, 1, 1]),
|
|
115
107
|
... n_steps=1000, seed=42, max_queue=25)
|
|
116
108
|
>>> sim.run()
|
|
117
|
-
>>> sim.
|
|
109
|
+
>>> sim.plogs # doctest: +NORMALIZE_WHITESPACE
|
|
110
|
+
Arrivals: [46 26 32 37 70 35 45]
|
|
118
111
|
Traffic: [24 17 2 23 33 12 13]
|
|
119
|
-
Queues: [[
|
|
112
|
+
Queues: [[ 23 32 45 38 22 43 31 34 20 3 0 0 0 0 0 0 0 0
|
|
120
113
|
0 0 0 0 0 0 0]
|
|
121
|
-
[
|
|
114
|
+
[290 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
|
122
115
|
0 0 0 0 0 0 0]
|
|
123
|
-
[
|
|
116
|
+
[290 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
|
124
117
|
0 0 0 0 0 0 0]
|
|
125
|
-
[
|
|
118
|
+
[ 9 1 7 9 3 3 26 37 4 8 10 9 2 10 40 11 2 16
|
|
126
119
|
3 3 21 27 22 1 7]
|
|
127
|
-
[
|
|
120
|
+
[212 49 22 5 3 0 0 0 0 0 0 0 0 0 0 0 0 0
|
|
128
121
|
0 0 0 0 0 0 0]
|
|
129
|
-
[
|
|
122
|
+
[233 41 6 7 4 0 0 0 0 0 0 0 0 0 0 0 0 0
|
|
130
123
|
0 0 0 0 0 0 0]
|
|
131
|
-
[
|
|
124
|
+
[231 33 16 4 6 1 0 0 0 0 0 0 0 0 0 0 0 0
|
|
132
125
|
0 0 0 0 0 0 0]]
|
|
133
|
-
Steps done:
|
|
126
|
+
Steps done: 291
|
|
134
127
|
"""
|
|
135
128
|
name = 'fcfm'
|
|
136
129
|
|
|
@@ -139,4 +132,4 @@ class FCFM(Simulator):
|
|
|
139
132
|
self.internal['queues'] = MultiQueue(self.model.n, max_queue=self.max_queue + 1)
|
|
140
133
|
|
|
141
134
|
def run(self):
|
|
142
|
-
|
|
135
|
+
fcfm_core(logs=self.logs, **self.internal)
|
{stochastic_matching-0.3.0 → stochastic_matching-0.3.2}/stochastic_matching/simulator/graph.py
RENAMED
|
@@ -2,9 +2,6 @@ from numba import int32
|
|
|
2
2
|
from numba.experimental import jitclass
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
specs = [('incid_ind', int32[:]), ('incid_ptr', int32[:]), ('coinc_ind', int32[:]), ('coinc_ptr', int32[:])]
|
|
6
|
-
|
|
7
|
-
|
|
8
5
|
def make_jit_graph(model):
|
|
9
6
|
"""
|
|
10
7
|
Parameters
|
|
@@ -18,10 +15,11 @@ def make_jit_graph(model):
|
|
|
18
15
|
Jitted graph.
|
|
19
16
|
"""
|
|
20
17
|
return JitHyperGraph(incid_ind=model.incidence_csr.indices, incid_ptr=model.incidence_csr.indptr,
|
|
21
|
-
coinc_ind=model.incidence_csc.indices, coinc_ptr=model.incidence_csc.indptr
|
|
18
|
+
coinc_ind=model.incidence_csc.indices, coinc_ptr=model.incidence_csc.indptr,
|
|
19
|
+
n=model.n, m=model.m)
|
|
22
20
|
|
|
23
21
|
|
|
24
|
-
@jitclass
|
|
22
|
+
@jitclass
|
|
25
23
|
class JitHyperGraph:
|
|
26
24
|
"""
|
|
27
25
|
Jit compatible view of a (hyper)graph.
|
|
@@ -36,12 +34,25 @@ class JitHyperGraph:
|
|
|
36
34
|
Indices of the co-incidence matrix.
|
|
37
35
|
coinc_ptr: :class:`~numpy.ndarray`
|
|
38
36
|
Pointers of the co-incidence matrix.
|
|
37
|
+
n: :class:`int`
|
|
38
|
+
Number of nodes.
|
|
39
|
+
m: :class:`int`
|
|
40
|
+
Number of (hyper)edges.
|
|
39
41
|
"""
|
|
40
|
-
|
|
42
|
+
incid_ind: int32[:]
|
|
43
|
+
incid_ptr: int32[:]
|
|
44
|
+
coinc_ind: int32[:]
|
|
45
|
+
coinc_ptr: int32[:]
|
|
46
|
+
n: int
|
|
47
|
+
m: int
|
|
48
|
+
|
|
49
|
+
def __init__(self, incid_ind, incid_ptr, coinc_ind, coinc_ptr, n, m):
|
|
41
50
|
self.incid_ind = incid_ind
|
|
42
51
|
self.incid_ptr = incid_ptr
|
|
43
52
|
self.coinc_ind = coinc_ind
|
|
44
53
|
self.coinc_ptr = coinc_ptr
|
|
54
|
+
self.n = n
|
|
55
|
+
self.m = m
|
|
45
56
|
|
|
46
57
|
def edges(self, node):
|
|
47
58
|
"""
|