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 ofBernoullithat, 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:
BernoulliEach enabled node is independently drawn with probability p.
- Parameters:
- Variables:
n (
int64) – See the corresponding parameter.p (
float64) – See the corresponding parameter.choices (
ndarrayofint64, shape (n,)) – Cachedarange(n)used to slice out the drawn indices.decisions (
ndarrayofbool, shape (n,)) – Scratch mask refilled at eachdraw()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:
IndependentSequential 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:
- Variables:
n (
int64) – See the corresponding parameter.p (
float64) – See the corresponding parameter.choices (
ndarrayofint64, shape (n,)) – Cachedarange(n)used to slice out the accepted indices.decisions (
ndarrayofbool, shape (n,)) – Scratch mask flipped toTruefor accepted nodes. Zeroed at the start of eachdraw()call so reusing the buffer saves one allocation per call.
Examples
With
p = 1.0on 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.0no 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)>#
- n: int64#
- p: float64#
- class tinto.schedulers.Ordered(*args, **kwargs)[source]#
Bases:
OrderedRound-robin scheduler over the nodes in natural order.
Identical contract as
Shuffledbut visits the nodes in0, 1, ..., n-1order instead of a random permutation.- Parameters:
n (
int) – Number of nodes in the graph.- Variables:
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:
ShuffledRound-robin scheduler over a random permutation of the nodes.
At each call, the cursor
iadvances one position inperm; 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 inperm, initialized ton - 1so that the firstdraw()call readsperm[0].perm (
ndarrayofint64, shape (n,)) – Random permutation ofrange(n)fixed at construction time.empty (
ndarrayofint64, shape (0,)) – Cached empty return value, reused to avoid per-call allocation.one (
ndarrayofint64, 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
Bernoullifactory suitable fortinto.runners.mc().- Parameters:
p (
float, default=1/3) – Independent selection probability passed toBernoulli.- Returns:
A
numba.njit()-compiledfactory(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
Independentfactory suitable fortinto.runners.mc().- Parameters:
p (
float, default=1/3) – Independent selection probability passed toIndependent.- Returns:
A
numba.njit()-compiledfactory(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
Orderedfactory suitable fortinto.runners.mc().- Returns:
A
numba.njit()-compiledfactory(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
Shuffledfactory suitable fortinto.runners.mc().The returned callable takes a
Configand returns a freshShuffledinstance sized for its graph.- Returns:
A
numba.njit()-compiledfactory(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