stochastic-matching 0.3.2__tar.gz → 0.3.3__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.2 → stochastic_matching-0.3.3}/PKG-INFO +1 -1
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/pyproject.toml +1 -1
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/__init__.py +1 -1
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/common.py +0 -30
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/model.py +1 -1
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/e_filtering.py +2 -2
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/longest.py +5 -5
- stochastic_matching-0.3.3/stochastic_matching/simulator/metrics.py +160 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/parallel.py +35 -22
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/priority.py +3 -3
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/random_edge.py +1 -1
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/simulator.py +38 -50
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/virtual_queue.py +3 -3
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/AUTHORS.rst +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/LICENSE +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/README.rst +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/display.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/graphs.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/__init__.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/arrivals.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/extended.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/fcfm.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/graph.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/logs.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/multiqueue.py +0 -0
- {stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/random_item.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "stochastic-matching"
|
|
3
|
-
version = "v0.3.
|
|
3
|
+
version = "v0.3.3"
|
|
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"
|
|
@@ -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.3'
|
|
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, \
|
|
@@ -3,12 +3,6 @@ from inspect import isclass
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
from numba import njit
|
|
5
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
6
|
|
|
13
7
|
def pseudo_inverse_scalar(x):
|
|
14
8
|
"""
|
|
@@ -227,27 +221,3 @@ def neighbors(i, compressed_incidence):
|
|
|
227
221
|
array([0, 1, 2, 3]...)
|
|
228
222
|
"""
|
|
229
223
|
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)
|
|
@@ -1648,6 +1648,6 @@ class Model:
|
|
|
1648
1648
|
simulator = class_converter(simulator, Simulator)
|
|
1649
1649
|
self.simulator = simulator(self, n_steps=n_steps, seed=seed, max_queue=max_queue, **kwargs)
|
|
1650
1650
|
self.simulator.run()
|
|
1651
|
-
self.simulation = self.simulator.
|
|
1651
|
+
self.simulation = self.simulator.flow
|
|
1652
1652
|
self.base_flow = self.simulation
|
|
1653
1653
|
return self.simulator.internal['n_steps'] == self.simulator.logs.steps_done
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/e_filtering.py
RENAMED
|
@@ -148,7 +148,7 @@ class EFiltering(Simulator):
|
|
|
148
148
|
Steps done: 1000
|
|
149
149
|
>>> sim.logs.traffic @ rewards
|
|
150
150
|
1913
|
|
151
|
-
>>> sim.
|
|
151
|
+
>>> sim.avg_queues
|
|
152
152
|
array([3.577, 4.856, 1.65 , 0.005])
|
|
153
153
|
|
|
154
154
|
To compare with, the original EGPD policy:
|
|
@@ -165,7 +165,7 @@ class EFiltering(Simulator):
|
|
|
165
165
|
Steps done: 1000
|
|
166
166
|
>>> sim.logs.traffic @ rewards
|
|
167
167
|
1781
|
|
168
|
-
>>> sim.
|
|
168
|
+
>>> sim.avg_queues
|
|
169
169
|
array([54.439, 1.597, 78.51 , 0.691])
|
|
170
170
|
"""
|
|
171
171
|
name = 'e_filtering'
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/longest.py
RENAMED
|
@@ -174,12 +174,12 @@ class Longest(ExtendedSimulator):
|
|
|
174
174
|
|
|
175
175
|
The flow avoids one edge:
|
|
176
176
|
|
|
177
|
-
>>> sim.
|
|
177
|
+
>>> sim.flow
|
|
178
178
|
array([2.8764, 1.089 , 1.0008, 0. , 0.8532, 2.079 , 1.0062])
|
|
179
179
|
|
|
180
180
|
The price is one node having a big queue:
|
|
181
181
|
|
|
182
|
-
>>> avg_queues = sim.
|
|
182
|
+
>>> avg_queues = sim.avg_queues
|
|
183
183
|
>>> avg_queues[-1]
|
|
184
184
|
61.1767
|
|
185
185
|
|
|
@@ -192,9 +192,9 @@ class Longest(ExtendedSimulator):
|
|
|
192
192
|
|
|
193
193
|
>>> sim = Longest(fish, rewards=[0, 2, 2, 0, 1, 1, 0], beta=.1, n_steps=10000, seed=42)
|
|
194
194
|
>>> sim.run()
|
|
195
|
-
>>> sim.
|
|
195
|
+
>>> sim.flow
|
|
196
196
|
array([2.9574, 1.008 , 0.9198, 0.018 , 0.9972, 2.061 , 1.0242])
|
|
197
|
-
>>> sim.
|
|
197
|
+
>>> sim.avg_queues[-1]
|
|
198
198
|
8.4628
|
|
199
199
|
|
|
200
200
|
Alternatively, one can use the k-filtering techniques:
|
|
@@ -228,7 +228,7 @@ class Longest(ExtendedSimulator):
|
|
|
228
228
|
>>> diamond.run('longest', rewards=[1, 2, 2, 2, 1], seed=42, forbidden_edges=True,
|
|
229
229
|
... n_steps=1000, max_queue=100)
|
|
230
230
|
True
|
|
231
|
-
>>> diamond.simulator.
|
|
231
|
+
>>> diamond.simulator.avg_queues
|
|
232
232
|
array([7.515, 7.819, 1.067, 1.363])
|
|
233
233
|
>>> diamond.simulation
|
|
234
234
|
array([0. , 0.954, 0.966, 0.954, 0. ])
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from stochastic_matching.common import class_converter
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Metric:
|
|
6
|
+
"""
|
|
7
|
+
Blueprint for extracting a metric from the simulator.
|
|
8
|
+
"""
|
|
9
|
+
name = None
|
|
10
|
+
|
|
11
|
+
@staticmethod
|
|
12
|
+
def get(simu):
|
|
13
|
+
"""
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
simu: :class:`~stochastic_matching.simulator.simulator.Simulator`
|
|
17
|
+
Simulator to use.
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
Object
|
|
22
|
+
A metric.
|
|
23
|
+
"""
|
|
24
|
+
raise NotImplementedError
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Traffic(Metric):
|
|
28
|
+
"""
|
|
29
|
+
Matched edges.
|
|
30
|
+
"""
|
|
31
|
+
name = 'traffic'
|
|
32
|
+
|
|
33
|
+
@staticmethod
|
|
34
|
+
def get(simu):
|
|
35
|
+
return simu.logs.traffic
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class StepsDone(Metric):
|
|
39
|
+
"""
|
|
40
|
+
Simulation duration.
|
|
41
|
+
"""
|
|
42
|
+
name = 'steps_done'
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def get(simu):
|
|
46
|
+
return simu.logs.steps_done
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Income(Metric):
|
|
50
|
+
"""
|
|
51
|
+
Number of arrived items per class.
|
|
52
|
+
"""
|
|
53
|
+
name = 'income'
|
|
54
|
+
|
|
55
|
+
@staticmethod
|
|
56
|
+
def get(simu):
|
|
57
|
+
return simu.logs.income
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class Regret(Metric):
|
|
61
|
+
"""
|
|
62
|
+
Given the matched items observed, difference between the best possible match
|
|
63
|
+
between these items and the actual match.
|
|
64
|
+
|
|
65
|
+
Notes
|
|
66
|
+
-----
|
|
67
|
+
|
|
68
|
+
* Regret depends on the assigned rewards.
|
|
69
|
+
* Unmatched items are ignored to avoid side effects (e.g. if the optimal allocation is
|
|
70
|
+
unstable, for example because of negative weights).
|
|
71
|
+
"""
|
|
72
|
+
name = 'regret'
|
|
73
|
+
|
|
74
|
+
@staticmethod
|
|
75
|
+
def get(simu):
|
|
76
|
+
rewards = getattr(simu, 'rewards', np.ones(simu.model.m, dtype=int))
|
|
77
|
+
original_rates = simu.model.rates
|
|
78
|
+
flow = Flow.get(simu)
|
|
79
|
+
simu.model.rates = simu.model.incidence @ flow
|
|
80
|
+
best_flow = simu.model.optimize_rates(rewards)
|
|
81
|
+
simu.model.rates = original_rates
|
|
82
|
+
return rewards @ (best_flow - flow)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class AvgQueues(Metric):
|
|
86
|
+
"""
|
|
87
|
+
Average queue size per item class.
|
|
88
|
+
"""
|
|
89
|
+
name = 'avg_queues'
|
|
90
|
+
|
|
91
|
+
@staticmethod
|
|
92
|
+
def get(simu):
|
|
93
|
+
return (simu.logs.queue_log @ np.arange(simu.max_queue)) / simu.logs.steps_done
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class Delay(Metric):
|
|
97
|
+
"""Average waiting time (i.e. average total queue size divided by global arrival rate)."""
|
|
98
|
+
name = 'delay'
|
|
99
|
+
|
|
100
|
+
@staticmethod
|
|
101
|
+
def get(simu):
|
|
102
|
+
return np.sum(AvgQueues.get(simu)) / np.sum(simu.model.rates)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class CCDF(Metric):
|
|
106
|
+
"""
|
|
107
|
+
Complementary Cumulative Distribution Function of queue size for each item class.
|
|
108
|
+
"""
|
|
109
|
+
name = 'ccdf'
|
|
110
|
+
|
|
111
|
+
@staticmethod
|
|
112
|
+
def get(simu):
|
|
113
|
+
steps = simu.logs.steps_done
|
|
114
|
+
n = simu.model.n
|
|
115
|
+
return (steps - np.cumsum(np.hstack([np.zeros((n, 1)), simu.logs.queue_log]), axis=1)) / steps
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class Flow(Metric):
|
|
119
|
+
"""
|
|
120
|
+
Edge matching rate.
|
|
121
|
+
"""
|
|
122
|
+
name = 'flow'
|
|
123
|
+
|
|
124
|
+
@staticmethod
|
|
125
|
+
def get(simu):
|
|
126
|
+
tot_mu = np.sum(simu.model.rates)
|
|
127
|
+
steps = simu.logs.steps_done
|
|
128
|
+
return simu.logs.traffic * tot_mu / steps
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def get_metrics(simu, metrics):
|
|
132
|
+
"""
|
|
133
|
+
Extraction of metrics from a simulator. Metrics can be:
|
|
134
|
+
|
|
135
|
+
* Name of a :class:`~stochastic_matching.simulator.metrics.Metric` subclass
|
|
136
|
+
* A :class:`~stochastic_matching.simulator.metrics.Metric` subclass
|
|
137
|
+
* A callable that takes a :class:`~stochastic_matching.simulator.simulator.Simulator` as input
|
|
138
|
+
(the name will be the name of the callable)
|
|
139
|
+
* A list of metrics defined as above
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
simu: :class:`~stochastic_matching.simulator.simulator.Simulator`
|
|
144
|
+
Simulator to use.
|
|
145
|
+
metrics: :class:`str` or :class:`~stochastic_matching.simulator.metrics.Metric` or :class:`list` or callable.
|
|
146
|
+
Metric(s) to extract.
|
|
147
|
+
Returns
|
|
148
|
+
-------
|
|
149
|
+
:class:`dict`
|
|
150
|
+
Metric(s).
|
|
151
|
+
|
|
152
|
+
"""
|
|
153
|
+
if isinstance(metrics, list):
|
|
154
|
+
return {k: v for metric in metrics
|
|
155
|
+
for k, v in get_metrics(simu, metric).items()}
|
|
156
|
+
try:
|
|
157
|
+
getter = class_converter(metrics, Metric)
|
|
158
|
+
return {getter.name: getter.get(simu)}
|
|
159
|
+
except TypeError:
|
|
160
|
+
return {metrics.__name__: metrics(simu)}
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/parallel.py
RENAMED
|
@@ -4,6 +4,8 @@ import gzip
|
|
|
4
4
|
import dill as pickle
|
|
5
5
|
import functools
|
|
6
6
|
|
|
7
|
+
from stochastic_matching.simulator.metrics import get_metrics
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
def do_nothing(x):
|
|
9
11
|
"""
|
|
@@ -180,19 +182,24 @@ class Runner:
|
|
|
180
182
|
"""
|
|
181
183
|
Parameters
|
|
182
184
|
----------
|
|
183
|
-
|
|
184
|
-
|
|
185
|
+
metrics: :class:`list`
|
|
186
|
+
Metrics to extract.
|
|
185
187
|
"""
|
|
186
188
|
|
|
187
|
-
def __init__(self,
|
|
188
|
-
self.
|
|
189
|
+
def __init__(self, metrics):
|
|
190
|
+
self.metrics = metrics
|
|
189
191
|
|
|
190
192
|
def __call__(self, tup):
|
|
191
193
|
name, kv, params = tup
|
|
192
194
|
params = {**params}
|
|
193
195
|
model = params.pop('model')
|
|
194
196
|
model.run(**params)
|
|
195
|
-
|
|
197
|
+
out = get_metrics(model.simulator, self.metrics)
|
|
198
|
+
# logs = model.simulator.logs
|
|
199
|
+
# out = {k: getattr(logs, k) for k in XP_LOGS}
|
|
200
|
+
# if self.callback:
|
|
201
|
+
# out.update(self.callback(model))
|
|
202
|
+
return name, kv, out
|
|
196
203
|
|
|
197
204
|
|
|
198
205
|
def aggregate(results):
|
|
@@ -281,7 +288,7 @@ def cached(func):
|
|
|
281
288
|
|
|
282
289
|
|
|
283
290
|
@cached
|
|
284
|
-
def evaluate(xps,
|
|
291
|
+
def evaluate(xps, metrics=None, pool=None):
|
|
285
292
|
"""
|
|
286
293
|
Evaluate some experiments. Results can be cached.
|
|
287
294
|
|
|
@@ -289,9 +296,8 @@ def evaluate(xps, extractor=None, pool=None):
|
|
|
289
296
|
----------
|
|
290
297
|
xps: :class:`~stochastic_matching.simulator.parallel.XP`
|
|
291
298
|
Experiment(s) to run.
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
Default to :meth:`~stochastic_matching.simulator.parallel.regret_delay`.
|
|
299
|
+
metrics: :class:`list`, optional.
|
|
300
|
+
Metrics to get.
|
|
295
301
|
pool: :class:`~multiprocess.pool.Pool`, optional.
|
|
296
302
|
Existing pool of workers.
|
|
297
303
|
|
|
@@ -308,18 +314,25 @@ def evaluate(xps, extractor=None, pool=None):
|
|
|
308
314
|
>>> diamond = sm.CycleChain()
|
|
309
315
|
>>> base = {'model': diamond, 'n_steps': 1000, 'seed': 42, 'rewards': [1, 2.9, 1, -1, 1]}
|
|
310
316
|
>>> xp = XP('Diamond', simulator='longest', **base)
|
|
311
|
-
>>> res = evaluate(xp)
|
|
317
|
+
>>> res = evaluate(xp, ['income', 'steps_done', 'regret', 'delay'])
|
|
312
318
|
>>> for name, r in res.items():
|
|
313
319
|
... print(name)
|
|
314
320
|
... for k, v in r.items():
|
|
315
321
|
... print(f"{k}: {np.round(v, 4)}")
|
|
316
322
|
Diamond
|
|
323
|
+
income: [216 304 293 187]
|
|
324
|
+
steps_done: 1000
|
|
317
325
|
regret: 0.088
|
|
318
|
-
delay:
|
|
319
|
-
>>>
|
|
320
|
-
...
|
|
321
|
-
|
|
322
|
-
|
|
326
|
+
delay: 0.1634
|
|
327
|
+
>>> def q0(simu):
|
|
328
|
+
... return simu.avg_queues[0]
|
|
329
|
+
>>> res = evaluate(xp, ['flow', 'avg_queues', q0])
|
|
330
|
+
>>> res['Diamond']['flow']
|
|
331
|
+
array([1.19, 0.97, 0.97, 0.88, 0.99])
|
|
332
|
+
>>> res['Diamond']['avg_queues']
|
|
333
|
+
array([0.531, 0.214, 0.273, 0.616])
|
|
334
|
+
>>> res['Diamond']['q0']
|
|
335
|
+
0.531
|
|
323
336
|
>>> xp1 = XP('e-filtering', simulator='e_filtering', **base,
|
|
324
337
|
... iterator=Iterator('epsilon', [.01, .1, 1], name='e'))
|
|
325
338
|
>>> xp2 = XP(name='k-filtering', simulator='longest', forbidden_edges=True,
|
|
@@ -331,7 +344,7 @@ def evaluate(xps, extractor=None, pool=None):
|
|
|
331
344
|
9
|
|
332
345
|
>>> import multiprocess as mp
|
|
333
346
|
>>> with mp.Pool(processes=2) as p:
|
|
334
|
-
... res = evaluate(xp, pool=p)
|
|
347
|
+
... res = evaluate(xp, ['regret', 'delay'], pool=p)
|
|
335
348
|
>>> for name, r in res.items():
|
|
336
349
|
... print(name)
|
|
337
350
|
... for k, v in r.items():
|
|
@@ -339,19 +352,19 @@ def evaluate(xps, extractor=None, pool=None):
|
|
|
339
352
|
e-filtering
|
|
340
353
|
e: [0.01 0.1 1. ]
|
|
341
354
|
regret: [0.002 0.017 0.103]
|
|
342
|
-
delay: [
|
|
355
|
+
delay: [1.0538 0.695 0.1952]
|
|
343
356
|
k-filtering
|
|
344
357
|
k: [ 0 10 100]
|
|
345
358
|
regret: [ 8.8000000e-02 2.0000000e-03 -8.8817842e-16]
|
|
346
|
-
delay: [
|
|
359
|
+
delay: [0.1634 0.7342 1.3542]
|
|
347
360
|
egpd
|
|
348
361
|
beta: [0.01 0.1 1. ]
|
|
349
362
|
regret: [0.003 0.043 0.076]
|
|
350
|
-
delay: [
|
|
363
|
+
delay: [9.2024 1.013 0.1888]
|
|
351
364
|
"""
|
|
352
|
-
if
|
|
353
|
-
|
|
354
|
-
runner = Runner(
|
|
365
|
+
if metrics is None:
|
|
366
|
+
metrics = ['regret', 'delay']
|
|
367
|
+
runner = Runner(metrics=metrics)
|
|
355
368
|
if pool is None:
|
|
356
369
|
res = [runner(p) for p in tqdm(xps)]
|
|
357
370
|
else:
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/priority.py
RENAMED
|
@@ -71,9 +71,9 @@ class Priority(Simulator):
|
|
|
71
71
|
|
|
72
72
|
The last node is the pseudo-instable node.
|
|
73
73
|
|
|
74
|
-
>>> fish.simulator.
|
|
74
|
+
>>> fish.simulator.avg_queues[-1]
|
|
75
75
|
38.346
|
|
76
|
-
>>> np.round(np.mean(fish.simulator.
|
|
76
|
+
>>> np.round(np.mean(fish.simulator.avg_queues[:-1]), decimals=2)
|
|
77
77
|
0.75
|
|
78
78
|
|
|
79
79
|
Choosing proper counter-weights is important.
|
|
@@ -84,7 +84,7 @@ class Priority(Simulator):
|
|
|
84
84
|
True
|
|
85
85
|
>>> fish.simulation
|
|
86
86
|
array([2.9232, 1.0422, 0.9504, 0.216 , 0.7344, 1.8666, 1.2186])
|
|
87
|
-
>>> fish.simulator.
|
|
87
|
+
>>> fish.simulator.avg_queues[-1]
|
|
88
88
|
38.6016
|
|
89
89
|
"""
|
|
90
90
|
name = "priority"
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/random_edge.py
RENAMED
|
@@ -80,7 +80,7 @@ class RandomEdge(Simulator):
|
|
|
80
80
|
[76 4 3 0 0 0 0 0 0 0]
|
|
81
81
|
[13 8 15 19 14 8 2 1 2 1]]
|
|
82
82
|
Steps done: 83
|
|
83
|
-
>>> sim.
|
|
83
|
+
>>> sim.flow
|
|
84
84
|
array([0.48192771, 0.38554217, 0.09638554, 0.38554217, 0.28915663])
|
|
85
85
|
|
|
86
86
|
A working candy (but candies are not good for greedy policies).
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/simulator.py
RENAMED
|
@@ -7,6 +7,7 @@ from stochastic_matching.simulator.graph import make_jit_graph
|
|
|
7
7
|
from stochastic_matching.simulator.logs import Logs
|
|
8
8
|
from stochastic_matching.display import int_2_str
|
|
9
9
|
from stochastic_matching.simulator.logs import repr_logs
|
|
10
|
+
from stochastic_matching.simulator.metrics import AvgQueues, Regret, Delay, CCDF, Flow
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
@njit
|
|
@@ -101,13 +102,13 @@ class Simulator:
|
|
|
101
102
|
|
|
102
103
|
Different methods are proposed to provide various indicators.
|
|
103
104
|
|
|
104
|
-
>>> sim.
|
|
105
|
+
>>> sim.avg_queues
|
|
105
106
|
array([1.08296943, 0.26200873, 0.07423581, 0.8558952 ])
|
|
106
107
|
|
|
107
|
-
>>> sim.
|
|
108
|
+
>>> sim.delay
|
|
108
109
|
0.3669530919847866
|
|
109
110
|
|
|
110
|
-
>>> sim.
|
|
111
|
+
>>> sim.ccdf #doctest: +NORMALIZE_WHITESPACE
|
|
111
112
|
array([[1. , 0.48471616, 0.27947598, 0.16593886, 0.10043668,
|
|
112
113
|
0.03930131, 0.00873362, 0.00436681, 0. ],
|
|
113
114
|
[1. , 0.1790393 , 0.069869 , 0.01310044, 0. ,
|
|
@@ -116,7 +117,7 @@ class Simulator:
|
|
|
116
117
|
0. , 0. , 0. , 0. ],
|
|
117
118
|
[1. , 0.45414847, 0.23580786, 0.10043668, 0.05240175,
|
|
118
119
|
0.01310044, 0. , 0. , 0. ]])
|
|
119
|
-
>>> sim.
|
|
120
|
+
>>> sim.flow
|
|
120
121
|
array([1.16419214, 0.46026201, 0.3790393 , 0.62270742, 0.32489083])
|
|
121
122
|
|
|
122
123
|
You can also draw the average or CCDF of the queues.
|
|
@@ -201,23 +202,45 @@ class Simulator:
|
|
|
201
202
|
"""
|
|
202
203
|
core_simulator(logs=self.logs, **self.internal)
|
|
203
204
|
|
|
204
|
-
|
|
205
|
+
@property
|
|
206
|
+
def avg_queues(self):
|
|
205
207
|
"""
|
|
206
|
-
Returns
|
|
207
|
-
-------
|
|
208
208
|
:class:`~numpy.ndarray`
|
|
209
209
|
Average queue sizes.
|
|
210
210
|
"""
|
|
211
|
-
return
|
|
211
|
+
return AvgQueues.get(self)
|
|
212
212
|
|
|
213
|
-
|
|
213
|
+
@property
|
|
214
|
+
def delay(self):
|
|
214
215
|
"""
|
|
215
|
-
Returns
|
|
216
|
-
-------
|
|
217
216
|
:class:`float`
|
|
218
217
|
Average waiting time
|
|
219
218
|
"""
|
|
220
|
-
return
|
|
219
|
+
return Delay.get(self)
|
|
220
|
+
|
|
221
|
+
@property
|
|
222
|
+
def ccdf(self):
|
|
223
|
+
"""
|
|
224
|
+
:class:`~numpy.ndarray`
|
|
225
|
+
CCDFs of the queues.
|
|
226
|
+
"""
|
|
227
|
+
return CCDF.get(self)
|
|
228
|
+
|
|
229
|
+
@property
|
|
230
|
+
def flow(self):
|
|
231
|
+
"""
|
|
232
|
+
Normalize the simulated flow.
|
|
233
|
+
|
|
234
|
+
Returns
|
|
235
|
+
-------
|
|
236
|
+
:class:`~numpy.ndarray`
|
|
237
|
+
Flow on edges.
|
|
238
|
+
"""
|
|
239
|
+
return Flow.get(self)
|
|
240
|
+
|
|
241
|
+
@property
|
|
242
|
+
def regret(self):
|
|
243
|
+
return Regret.get(self)
|
|
221
244
|
|
|
222
245
|
def show_average_queues(self, indices=None, sort=False, as_time=False):
|
|
223
246
|
"""
|
|
@@ -235,7 +258,7 @@ class Simulator:
|
|
|
235
258
|
:class:`~matplotlib.figure.Figure`
|
|
236
259
|
A figure of the CCDFs of the queues.
|
|
237
260
|
"""
|
|
238
|
-
averages = self.
|
|
261
|
+
averages = self.avg_queues
|
|
239
262
|
if as_time:
|
|
240
263
|
averages = averages / self.model.rates
|
|
241
264
|
if indices is not None:
|
|
@@ -255,41 +278,6 @@ class Simulator:
|
|
|
255
278
|
plt.xlabel("Node")
|
|
256
279
|
return plt.gcf()
|
|
257
280
|
|
|
258
|
-
def compute_ccdf(self):
|
|
259
|
-
"""
|
|
260
|
-
Returns
|
|
261
|
-
-------
|
|
262
|
-
:class:`~numpy.ndarray`
|
|
263
|
-
CCDFs of the queues.
|
|
264
|
-
"""
|
|
265
|
-
events = self.logs.steps_done
|
|
266
|
-
n = self.model.n
|
|
267
|
-
# noinspection PyUnresolvedReferences
|
|
268
|
-
return (events - np.cumsum(np.hstack([np.zeros((n, 1)), self.logs.queue_log]), axis=1)) / events
|
|
269
|
-
|
|
270
|
-
def compute_flow(self):
|
|
271
|
-
"""
|
|
272
|
-
Normalize the simulated flow.
|
|
273
|
-
|
|
274
|
-
Returns
|
|
275
|
-
-------
|
|
276
|
-
:class:`~numpy.ndarray`
|
|
277
|
-
Flow on edges.
|
|
278
|
-
"""
|
|
279
|
-
# noinspection PyUnresolvedReferences
|
|
280
|
-
tot_mu = np.sum(self.model.rates)
|
|
281
|
-
steps = self.logs.steps_done
|
|
282
|
-
return self.logs.traffic * tot_mu / steps
|
|
283
|
-
|
|
284
|
-
def compute_regret(self):
|
|
285
|
-
rewards = getattr(self, 'rewards', np.ones(self.model.m, dtype=int))
|
|
286
|
-
original_rates = self.model.rates
|
|
287
|
-
flow = self.compute_flow()
|
|
288
|
-
self.model.rates = self.model.incidence @ flow
|
|
289
|
-
best_flow = self.model.optimize_rates(rewards)
|
|
290
|
-
self.model.rates = original_rates
|
|
291
|
-
return rewards @ (best_flow - flow)
|
|
292
|
-
|
|
293
281
|
def show_ccdf(self, indices=None, sort=None, strict=False):
|
|
294
282
|
"""
|
|
295
283
|
Parameters
|
|
@@ -306,7 +294,7 @@ class Simulator:
|
|
|
306
294
|
:class:`~matplotlib.figure.Figure`
|
|
307
295
|
A figure of the CCDFs of the queues.
|
|
308
296
|
"""
|
|
309
|
-
ccdf = self.
|
|
297
|
+
ccdf = self.ccdf
|
|
310
298
|
|
|
311
299
|
if indices is not None:
|
|
312
300
|
ccdf = ccdf[indices, :]
|
|
@@ -314,7 +302,7 @@ class Simulator:
|
|
|
314
302
|
else:
|
|
315
303
|
names = [int_2_str(self.model, i) for i in range(self.model.n)]
|
|
316
304
|
if sort is True:
|
|
317
|
-
averages = self.
|
|
305
|
+
averages = self.avg_queues
|
|
318
306
|
if indices is not None:
|
|
319
307
|
averages = averages[indices]
|
|
320
308
|
ind = np.argsort(-averages)
|
|
@@ -274,7 +274,7 @@ class VirtualQueue(ExtendedSimulator):
|
|
|
274
274
|
>>> sim.run()
|
|
275
275
|
>>> sim.logs.traffic.astype(int)
|
|
276
276
|
array([236, 279, 342, 143, 0, 0, 0])
|
|
277
|
-
>>> sim.
|
|
277
|
+
>>> sim.avg_queues
|
|
278
278
|
array([0., 0., 0., 0.])
|
|
279
279
|
|
|
280
280
|
>>> rewards = [-1, -1, 1, 2, 5, 4, 7]
|
|
@@ -285,7 +285,7 @@ class VirtualQueue(ExtendedSimulator):
|
|
|
285
285
|
>>> sim.run()
|
|
286
286
|
>>> sim.logs.traffic.astype(int)
|
|
287
287
|
array([ 0, 0, 761, 242, 591, 0, 205])
|
|
288
|
-
>>> sim.
|
|
288
|
+
>>> sim.avg_queues
|
|
289
289
|
array([86.17266667, 0.53233333, 93.03 , 0.33533333])
|
|
290
290
|
|
|
291
291
|
Same traffic could be achieved with much lower queues by enforcing forbidden edges:
|
|
@@ -294,7 +294,7 @@ class VirtualQueue(ExtendedSimulator):
|
|
|
294
294
|
>>> sim.run()
|
|
295
295
|
>>> sim.logs.traffic.astype(int)
|
|
296
296
|
array([ 0, 0, 961, 342, 691, 0, 105])
|
|
297
|
-
>>> sim.
|
|
297
|
+
>>> sim.avg_queues
|
|
298
298
|
array([2.731 , 0.69633333, 0.22266667, 0.052 ])
|
|
299
299
|
"""
|
|
300
300
|
name = 'virtual_queue'
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/__init__.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/arrivals.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/extended.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/fcfm.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/graph.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/logs.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/multiqueue.py
RENAMED
|
File without changes
|
{stochastic_matching-0.3.2 → stochastic_matching-0.3.3}/stochastic_matching/simulator/random_item.py
RENAMED
|
File without changes
|