Solvers#
Solvers that recolor a batch of nodes and refresh the enabled cache.
A solver is a callable solver(config, decisions) -> None that, given
the indices returned by a scheduler, picks a new color for each decision
through a colorer function, writes the colors back into
colors, and then invokes
check() on each touched node and its neighbors
to refresh the enabled mask.
A few colorers are provided:
colorer_lowest()always picks the smallest color absent from the neighborhood.colorer_next()starts the search atme + 1and wraps around, favoring a change over a “canonical” choice.colorer_random()picks a random color absent from the neighborhood.colorer_random_blind()picks a random color different from the current one, without checking the neighborhood.colorer_next_blind()picks the next color (cyclic) after the current one, without checking the neighborhood.
colorer_lowest(), colorer_next() and colorer_random()
raise ValueError if no free color exists, which can only happen
when the current configuration uses fewer colors than needed. The
_blind variants never raise but may leave clashes behind.
Use make() to wrap a colorer into a compiled solver.
- tinto.solvers.colorer_lowest(config, i)[source]#
Return the smallest color of
range(k)absent from the neighborhood of i.- Parameters:
config (
Config) – Current coloring configuration.i (
int) – Node whose new color is requested.
- Returns:
Smallest
csuch that no neighbor of i is currently colored c.- Return type:
int64- Raises:
ValueError – If every color in
range(k)is represented in the neighborhood of i.
Examples
On a 3-node line with every node at color 0, node 1 has two neighbors of color 0 and k=3 colors available; the lowest free color is 1:
>>> from tinto.graphs import line >>> from tinto.configs import make as make_config >>> Config = make_config() >>> c = Config(line(3)) >>> int(colorer_lowest(c, 1)) 1
On the 3-node complete graph with only 2 colors, every color is occupied in the neighborhood and the call raises:
>>> import numpy as np >>> from tinto.graphs import Graph >>> ptr = np.array([0, 2, 4, 6], dtype=np.int32) >>> ind = np.array([1, 2, 0, 2, 0, 1], dtype=np.int32) >>> c = Config(Graph(ptr, ind), k=2) >>> c.colors[:] = [0, 0, 1] >>> c.check_all() >>> colorer_lowest(c, 0) Traceback (most recent call last): ... ValueError: Not Enough Colors!!!
- tinto.solvers.colorer_next(config, i)[source]#
Return the next free color after
config.colors[i](cyclic).Scans
(me + 1) % k, (me + 2) % k, ...and returns the first free value. Favors a change relative to the current color, which matters for rules that must avoid self-fixed points.- Parameters:
config (
Config) – Current coloring configuration.i (
int) – Node whose new color is requested.
- Returns:
First free color encountered when iterating
(me + 1 + c) % kforcinrange(k).- Return type:
int64- Raises:
ValueError – If no free color exists.
Examples
On a 3-node line with every node at color 0, node 1’s first candidate is
(0 + 1) % 3 = 1, which is free:>>> from tinto.graphs import line >>> from tinto.configs import make as make_config >>> Config = make_config() >>> c = Config(line(3)) >>> int(colorer_next(c, 1)) 1
The cyclic search wraps around. On a 3-node line with
colors = [0, 2, 0], node 1’s search starts at color 0 (taken) and only finds 1 on the next iteration:>>> c.colors[:] = [0, 2, 0] >>> c.check_all() >>> int(colorer_next(c, 1)) 1
The saturated K3-with-k=2 case raises:
>>> import numpy as np >>> from tinto.graphs import Graph >>> ptr = np.array([0, 2, 4, 6], dtype=np.int32) >>> ind = np.array([1, 2, 0, 2, 0, 1], dtype=np.int32) >>> c = Config(Graph(ptr, ind), k=2) >>> c.colors[:] = [0, 0, 1] >>> c.check_all() >>> colorer_next(c, 0) Traceback (most recent call last): ... ValueError: Not Enough Colors!!!
- tinto.solvers.colorer_next_blind(config, i)[source]#
Return the next color after
config.colors[i](cyclic) without checking the neighborhood.- Parameters:
config (
Config) – Current coloring configuration.i (
int) – Node whose new color is requested.
- Returns:
The color
(config.colors[i] + 1) % config.k.- Return type:
int64
Notes
The returned color may clash with a neighbor: this colorer skips the neighborhood scan and trades correctness for speed. Useful for modelling agents that can detect a conflict locally but cannot enumerate which colors are free.
Examples
>>> from tinto.graphs import line >>> from tinto.configs import make as make_config >>> from tinto.io import set_seed >>> set_seed(51) >>> Config = make_config() >>> c = Config(line(3)) >>> c.color_randomize() >>> c.colors.tolist() [2, 1, 1] >>> c.total 2 >>> solver = make(colorer=colorer_next_blind) >>> decisions = np.array([0, 1, 2], dtype=np.int64) >>> solver(c, decisions) >>> c.colors.tolist() [0, 2, 2] >>> c.total 2
- tinto.solvers.colorer_random(config, i)[source]#
Return a random free color for node i.
- Parameters:
config (
Config) – Current coloring configuration.i (
int) – Node whose new color is requested.
- Returns:
A random color in
range(k)that is absent from the neighborhood of i.- Return type:
int64- Raises:
ValueError – If no free color exists.
Examples
>>> from tinto.graphs import line >>> from tinto.configs import make as make_config >>> from tinto.io import set_seed >>> set_seed(42) >>> Config = make_config() >>> c = Config(line(3)) >>> solver = make(colorer=colorer_random) >>> decisions = np.array([0, 1, 2], dtype=np.int64) >>> solver(c, decisions) >>> c.colors.tolist() [1, 2, 1] >>> c.total 0
On the 3-node complete graph with only 2 colors, no color is free:
>>> from tinto.graphs import Graph >>> ptr = np.array([0, 2, 4, 6], dtype=np.int32) >>> ind = np.array([1, 2, 0, 2, 0, 1], dtype=np.int32) >>> c = Config(Graph(ptr, ind), k=2) >>> c.colors[:] = [0, 0, 1] >>> c.check_all() >>> colorer_random(c, 0) Traceback (most recent call last): ... ValueError: Not Enough Colors!!!
- tinto.solvers.colorer_random_blind(config, i)[source]#
Return a random color different from
config.colors[i]without checking the neighborhood.- Parameters:
config (
Config) – Current coloring configuration.i (
int) – Node whose new color is requested.
- Returns:
A color drawn uniformly from
range(k)excluding the current colorconfig.colors[i]. Always guarantees a change.- Return type:
int64
Notes
The returned color may still clash with a neighbor: this colorer skips the neighborhood scan and trades correctness for speed. Useful when modelling agents that detect a conflict locally but cannot enumerate which colors are free.
Examples
>>> from tinto.graphs import line >>> from tinto.configs import make as make_config >>> from tinto.io import set_seed >>> set_seed(40) >>> Config = make_config() >>> c = Config(line(3)) >>> c.color_randomize() >>> c.colors.tolist() [2, 1, 0] >>> solver = make(colorer=colorer_random_blind) >>> decisions = np.array([0, 1, 2], dtype=np.int64) >>> solver(c, decisions) >>> c.colors.tolist() [0, 0, 2] >>> c.total 2
- tinto.solvers.make(colorer=None)[source]#
Build a compiled solver wired to colorer.
The returned callable applies colorer to every decision, writes the new colors into
colorsat once, then callscheck()on every touched node and its neighbors to refresh the enabled mask.- Parameters:
colorer (callable or
None, default=``None``) – Acolorer(config, i) -> int64picking the new color for node i. WhenNone,colorer_lowest()is used.- Returns:
A
numba.njit()-compiledsolver(config, decisions) -> None.- Return type:
callable
Examples
With the default colorer, recoloring node 1 of a 3-node line reaches a stable coloring in one step:
>>> from tinto.graphs import line >>> from tinto.configs import make as make_config >>> Config = make_config() >>> c = Config(line(3)) >>> solver = make() >>> decisions = np.array([1], dtype=np.int64) >>> solver(c, decisions) >>> c.colors.tolist() [0, 1, 0] >>> c.total 0
A custom colorer is accepted:
>>> solver_next = make(colorer=colorer_next) >>> c = Config(line(3)) >>> solver_next(c, decisions) >>> c.colors.tolist() [0, 1, 0]
Repeated calls with the same (normalized) colorer hit the cache:
>>> make() is make() True >>> make() is make(colorer_lowest) True >>> make(colorer_lowest) is make(colorer_next) False
Notes
Two calls with the same (normalized) colorer return the same compiled solver. Colorers are keyed by identity;
Noneandcolorer_lowest()are treated as the same key.