Common#

Multipurpose methods and classes.

class stochastic_matching.common.CharMaker[source]#

Class that acts as an infinite list of letters. Used to provide letter-labels to nodes

Examples

>>> names = CharMaker()
>>> names[0]
'A'
>>> names[7]
'H'
>>> names[26]
'AA'
>>> names[107458610947716]
'STOCHASTIC'
stochastic_matching.common.class_converter(subclass, motherclass)[source]#
Parameters:
  • subclass (str or class) – Required subclass, or its name.

  • motherclass (class) – Ancestor of the subclass.

Returns:

The subclass.

Return type:

class

Examples

>>> import stochastic_matching as sm
>>> from stochastic_matching.simulator.simulator import Simulator
>>> class_converter('random_edge', Simulator)
<class 'stochastic_matching.simulator.random_edge.RandomEdge'>
>>> class_converter(sm.FCFM, Simulator)
<class 'stochastic_matching.simulator.fcfm.FCFM'>
>>> class_converter('anything', Simulator)  
Traceback (most recent call last):
...
ValueError: anything is not a known name for a subclass of Simulator.
Known names: constant_regret, e_filtering, fcfm, longest, priority, random_edge, random_item, virtual_queue.
>>> class_converter(2, Simulator)
Traceback (most recent call last):
...
TypeError: Subclass must be string or Simulator subclass (not instance).
stochastic_matching.common.clean_zeros(matrix, tol=1e-10)[source]#

Replace in-place all small values of a matrix by 0.

Parameters:
  • matrix (ndarray) – Matrix to clean.

  • tol (float, optional) – Threshold. All entries with absolute value lower than tol are put to zero.

Return type:

None

Examples

>>> import numpy as np
>>> mat = np.array([[1e-12, -.3], [.8, -1e-13]])
>>> clean_zeros(mat)
>>> mat 
array([[ 0. , -0.3],
       [ 0.8,  0. ]])
stochastic_matching.common.get_classes(root)[source]#
Parameters:

root (class) – Starting class (can be abstract).

Returns:

Dictionaries of all subclasses that have a name (as in class attribute name).

Return type:

dict

Examples

>>> import stochastic_matching as sm
>>> get_classes(sm.Model) 
{'Path': <class 'stochastic_matching.graphs.Path'>,
 'Star': <class 'stochastic_matching.graphs.Star'>,
 'Cycle': <class 'stochastic_matching.graphs.Cycle'>,
 'Complete': <class 'stochastic_matching.graphs.Complete'>,
 'Codomino': <class 'stochastic_matching.graphs.Codomino'>,
 'Erdös-Rényi': <class 'stochastic_matching.graphs.ErdosRenyi'>,
 'Nazari-Stolyar': <class 'stochastic_matching.graphs.NS19'>,
 'Pyramid': <class 'stochastic_matching.graphs.Pyramid'>,
 'Tadpole': <class 'stochastic_matching.graphs.Tadpole'>,
 'Lollipop': <class 'stochastic_matching.graphs.Lollipop'>,
 'Kayak Paddle': <class 'stochastic_matching.graphs.KayakPaddle'>,
 'Barbell': <class 'stochastic_matching.graphs.Barbell'>,
 'Cycle Chain': <class 'stochastic_matching.graphs.CycleChain'>,
 'Hyper Kayak Paddle': <class 'stochastic_matching.graphs.HyperPaddle'>,
 'Fan': <class 'stochastic_matching.graphs.Fan'>}
stochastic_matching.common.neighbors(i, compressed_incidence)[source]#

Return neighbors of a node/edge with respect to an incident matrix. Neighborhood is defined on hypergraph level, not on adjacency level: neighbors of a node are edges, neighbors of an edge are nodes.

Parameters:
  • i (int) – Index of the node/edge to probe.

  • compressed_incidence (csr_matrix or csc_matrix) – Compressed sparse incidence matrix on rows (for nodes) or columns (for edges).

Returns:

Neighbors of i.

Return type:

ndarray

Examples

A hypergraph with 4 nodes, 2 regular edges (0, 1) and (0, 2) and one 4-edge (0, 1, 2, 3).

>>> import numpy as np
>>> from scipy.sparse import csr_matrix, csc_matrix
>>> incidence = np.array([[1, 1, 1],
...                       [1, 0, 1],
...                       [0, 1, 1],
...                       [0, 0, 1]])

Edges of node 0:

>>> neighbors(0, csr_matrix(incidence)) 
array([0, 1, 2]...)

Egde of node 3:

>>> neighbors(3, csr_matrix(incidence)) 
array([2]...)

Nodes of edge 0:

>>> neighbors(0, csc_matrix(incidence)) 
array([0, 1]...)

Nodes of hyperedge 2:

>>> neighbors(2, csc_matrix(incidence)) 
array([0, 1, 2, 3]...)
stochastic_matching.common.pseudo_inverse_scalar(x)[source]#
Parameters:

x (float)

Returns:

Inverse of x if it is not 0.

Return type:

float

Examples

>>> pseudo_inverse_scalar(2.0)
0.5
>>> pseudo_inverse_scalar(0)
0.0