Schedulers#

Schedulers deciding which nodes get a chance to be recolored at each step.

A scheduler is a numba jitclass exposing draw(config) -> ndarray of int64 that returns the indices of nodes to recolor at the current step. Returning an empty array is allowed and does not count as a productive step in tinto.runners.simple().

Four schedulers are shipped:

  • Shuffled: walks a random permutation of the node set and returns the current node iff it is enabled.

  • Ordered: same, but walks the node set in natural order.

  • Bernoulli: each enabled node is independently selected with probability p.

  • Independent: sequential greedy variant of Bernoulli that, visiting the nodes in random order, refuses to add a node whose neighbor has already been accepted, so that the returned batch is an independent set.

The lowercase factory functions shuffled(), ordered(), bernoulli() and independent() return a compiled factory(config) -> scheduler callable, which is the form expected by tinto.runners.mc().

class tinto.schedulers.Bernoulli(*args, **kwargs)[source]#

Bases: Bernoulli

Each enabled node is independently drawn with probability p.

Parameters:
  • n (int) – Number of nodes in the graph.

  • p (float) – Independent selection probability, in [0, 1].

Variables:
  • n (int64) – See the corresponding parameter.

  • p (float64) – See the corresponding parameter.

  • choices (ndarray of int64, shape (n,)) – Cached arange(n) used to slice out the drawn indices.

  • decisions (ndarray of bool, shape (n,)) – Scratch mask refilled at each draw() call. Reusing this buffer saves one allocation per call.

Examples

With p = 0.0, no node is ever selected:

>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> c = Config(line(4))
>>> Bernoulli(c.graph.n, 0.0).draw(c).tolist()
[]

With p = 1.0, every enabled node is selected:

>>> Bernoulli(c.graph.n, 1.0).draw(c).tolist()
[0, 1, 2, 3]
_ctor(p)#
_ctor_sig = <Signature (self, n, p)>#
class_type = jitclass.Bernoulli#7c3be7527110<n:int64,p:float64,choices:array(int64, 1d, A),decisions:array(bool, 1d, A)>#
class tinto.schedulers.Independent(*args, **kwargs)[source]#

Bases: Independent

Sequential greedy scheduler yielding an independent set of enabled nodes.

The nodes are visited in a fresh random order at every draw() call; at each visit, the current node goes through a Bernoulli trial (probability p) and is accepted only if none of its neighbors has already been accepted during this call. The returned batch is therefore an independent set of the graph, which lets the solver recolor every chosen node in parallel without conflict.

Compared to a batch Bernoulli draw followed by a conflict filter, this sequential scheme accepts more nodes (it never rejects a pair of adjacent draws outright) at the cost of an extra permutation per call.

Parameters:
  • n (int) – Number of nodes in the graph.

  • p (float) – Per-visit Bernoulli probability, in [0, 1].

Variables:
  • n (int64) – See the corresponding parameter.

  • p (float64) – See the corresponding parameter.

  • choices (ndarray of int64, shape (n,)) – Cached arange(n) used to slice out the accepted indices.

  • decisions (ndarray of bool, shape (n,)) – Scratch mask flipped to True for accepted nodes. Zeroed at the start of each draw() call so reusing the buffer saves one allocation per call.

Examples

With p = 1.0 on a 5-node line, every enabled node is considered and the sequential filter keeps exactly the even-indexed nodes once the random order is fixed by the seed:

>>> from tinto.io import set_seed
>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> set_seed(0)
>>> c = Config(line(5))
>>> Independent(c.graph.n, 1.0).draw(c).tolist()
[0, 2, 4]

With p = 0.0 no node passes the Bernoulli trial:

>>> Independent(c.graph.n, 0.0).draw(c).tolist()
[]

Disabling odd-indexed nodes on the same line makes the three even nodes mutually non-adjacent, so all of them survive regardless of the perm order:

>>> c.enabled[1::2] = False
>>> Independent(c.graph.n, 1.0).draw(c).tolist()
[0, 2, 4]
_ctor(p)#
_ctor_sig = <Signature (self, n, p)>#
choices: Array(int64, 1, 'A', False, aligned=True)#
class_type = jitclass.Independent#7c3be7527560<n:int64,p:float64,choices:array(int64, 1d, A),decisions:array(bool, 1d, A)>#
decisions: Array(bool, 1, 'A', False, aligned=True)#
n: int64#
p: float64#
class tinto.schedulers.Ordered(*args, **kwargs)[source]#

Bases: Ordered

Round-robin scheduler over the nodes in natural order.

Identical contract as Shuffled but visits the nodes in 0, 1, ..., n-1 order instead of a random permutation.

Parameters:

n (int) – Number of nodes in the graph.

Variables:
  • n (int64) – See the corresponding parameter.

  • i (int64) – Current cursor, initialized to n - 1 so that the first draw() call reads node 0.

  • empty (ndarray of int64, shape (0,)) – Cached empty return value.

  • one (ndarray of int64, shape (1,)) – Cached single-element return buffer.

Examples

>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> c = Config(line(4))
>>> o = Ordered(c.graph.n)
>>> o.draw(c).tolist()
[0]
>>> o.draw(c).tolist()
[1]

Disabling every node makes the next draw empty:

>>> c.colors[:] = [0, 1, 0, 1]
>>> c.check_all()
>>> o.draw(c).tolist()
[]
_ctor()#
_ctor_sig = <Signature (self, n)>#
class_type = jitclass.Ordered#7c3be75ec3b0<n:int64,i:int64,empty:array(int64, 1d, A),one:array(int64, 1d, A)>#
empty: Array(int64, 1, 'A', False, aligned=True)#
i: int64#
n: int64#
one: Array(int64, 1, 'A', False, aligned=True)#
class tinto.schedulers.Shuffled(*args, **kwargs)[source]#

Bases: Shuffled

Round-robin scheduler over a random permutation of the nodes.

At each call, the cursor i advances one position in perm; the pointed node is returned iff it is currently enabled.

Parameters:

n (int) – Number of nodes in the graph.

Variables:
  • n (int64) – See the corresponding parameter.

  • i (int64) – Current position in perm, initialized to n - 1 so that the first draw() call reads perm[0].

  • perm (ndarray of int64, shape (n,)) – Random permutation of range(n) fixed at construction time.

  • empty (ndarray of int64, shape (0,)) – Cached empty return value, reused to avoid per-call allocation.

  • one (ndarray of int64, shape (1,)) – Cached single-element return buffer, overwritten in place.

Examples

>>> from tinto.io import set_seed
>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> set_seed(0)
>>> Config = make()
>>> c = Config(line(4))
>>> s = Shuffled(c.graph.n)

On an all-enabled configuration, draw returns exactly one node:

>>> out = s.draw(c)
>>> len(out) == 1 and bool(c.enabled[out[0]])
True

Once a proper coloring disables every node, every draw is empty:

>>> c.colors[:] = [0, 1, 0, 1]
>>> c.check_all()
>>> all(len(s.draw(c)) == 0 for _ in range(10))
True
_ctor()#
_ctor_sig = <Signature (self, n)>#
class_type = jitclass.Shuffled#7c3be75ecd40<n:int64,i:int64,perm:array(int64, 1d, A),empty:array(int64, 1d, A),one:array(int64, 1d, A)>#
empty: Array(int64, 1, 'A', False, aligned=True)#
i: int64#
n: int64#
one: Array(int64, 1, 'A', False, aligned=True)#
perm: Array(int64, 1, 'A', False, aligned=True)#
tinto.schedulers.bernoulli(p=0.3333333333333333)[source]#

Build a Bernoulli factory suitable for tinto.runners.mc().

Parameters:

p (float, default=1/3) – Independent selection probability passed to Bernoulli.

Returns:

A numba.njit()-compiled factory(config) -> Bernoulli.

Return type:

callable

Notes

Memoized on p: two calls with the same float value hit the cache.

Examples

>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> c = Config(line(4))
>>> b = bernoulli(p=0.25)(c)
>>> b.p
0.25
>>> bernoulli(0.25) is bernoulli(0.25)
True
>>> bernoulli(0.25) is bernoulli(0.5)
False
tinto.schedulers.independent(p=0.3333333333333333)[source]#

Build an Independent factory suitable for tinto.runners.mc().

Parameters:

p (float, default=1/3) – Independent selection probability passed to Independent.

Returns:

A numba.njit()-compiled factory(config) -> Independent.

Return type:

callable

Notes

Memoized on p: two calls with the same float value hit the cache.

Examples

>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> c = Config(line(4))
>>> ind = independent(p=0.5)(c)
>>> ind.p
0.5
>>> independent(0.5) is independent(0.5)
True
tinto.schedulers.ordered()[source]#

Build an Ordered factory suitable for tinto.runners.mc().

Returns:

A numba.njit()-compiled factory(config) -> Ordered.

Return type:

callable

Notes

Memoized: repeated calls return the same compiled factory.

Examples

>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> c = Config(line(4))
>>> factory = ordered()
>>> o = factory(c)
>>> o.n
4
>>> ordered() is ordered()
True
tinto.schedulers.shuffled()[source]#

Build a Shuffled factory suitable for tinto.runners.mc().

The returned callable takes a Config and returns a fresh Shuffled instance sized for its graph.

Returns:

A numba.njit()-compiled factory(config) -> Shuffled.

Return type:

callable

Notes

Memoized: repeated calls return the same compiled factory.

Examples

>>> from tinto.graphs import line
>>> from tinto.configs import make
>>> Config = make()
>>> c = Config(line(4))
>>> factory = shuffled()
>>> s = factory(c)
>>> s.n
4
>>> shuffled() is shuffled()
True