{ "cells": [ { "cell_type": "markdown", "id": "08569366-fbf4-4e4b-a537-02865737eee5", "metadata": {}, "source": [ "# Coloring concepts" ] }, { "cell_type": "markdown", "id": "8cdf2413-34f6-475e-a560-f0d09f2a0a82", "metadata": {}, "source": [ ":::note\n", "Tinto uses a lot of words:\n", "\n", "- configs,\n", "- graphs,\n", "- schedulers,\n", "- colorers,\n", "- solvers,\n", "- starters,\n", "- runners,\n", "- experiments,\n", "- estimators,\n", "- ...\n", "\n", "At first it can be hard to navigate between all these concepts. In particular:\n", "\n", "- The vocabulary comes from the theory of self-stabilizing coloring;\n", "- The way these concepts are implemented is designed for practical flexibility and efficiency.\n", "\n", "This notebook proposes a gentle tour.\n", "\n", "If you find the notebook too slow for your taste, don't hesitate to skip to the other ones.\n", ":::" ] }, { "cell_type": "markdown", "id": "5014dc75-5d7f-4040-b65a-29f15cf1906c", "metadata": {}, "source": [ "## TL;DR" ] }, { "cell_type": "markdown", "id": "800d6d83-60e2-48d3-850c-a13e07bce127", "metadata": {}, "source": [ "- A `Config` **class** is defined from a **guard**: `tinto.configs.make(guard=tinto.guards.color_clash) -> Config (jitclass)`.\n", "- `Config(graph) -> config instance`: manages colors, guarded nodes, and ids (if required).\n", "- A *scheduler* selects a subset of enabled nodes. First build a factory (`facto=shuffled()`, `facto=bernoulli(p=2/3)`, ...), then apply it to a config (`sched=facto(config)`), then draw (`selection=sched.draw(config)`).\n", "- A *solver* is a function `solver(config, decisions)` that modifies the configuration in place. It is built with `solvers.make(coloring_function)`. A few coloring functions are provided.\n", "- `runners.simple(config, scheduler, solver)` runs one specific scenario (# steps & # activations).\n", "- `runners.mc(config_factory, scheduler_factory, solver, n_trials=1000)` runs many scenarios (first moments of steps and activations). `starters.from_graph(Config, color=None)(graph)` is a typical factory.\n", "- An `Experiment` (built from one or more `Iterator` sweeps) runs `runners.mc` across a parameter grid and gathers the results: `Experiment(name, iterator=Iterator(\"starter\", values, process=...), scheduler=..., solver=..., n_trials=...).run()`, then `.series()` / `.plot()` / `.to_csv()`.\n", "- *Estimators* read a behavior law off a sweep: `Experiment.fit` (affine or custom basis, with $\\chi^2$ diagnostics) and `Experiment.fit_fractions` (exact fractions). They wrap the `estimate` module." ] }, { "cell_type": "markdown", "id": "6a87ca27-dde5-484a-aaea-e6afc72adda6", "metadata": {}, "source": [ "## Graphs" ] }, { "cell_type": "markdown", "id": "2ab5125f-4d21-4dbb-b648-7c10857b6f41", "metadata": {}, "source": [ "Tinto graphs are jitted. The one method you really need is `neighbors(i)`; a graph also knows its size `n` and its `max_degree`, and can spell out its full adjacency matrix with `to_adja()`.\n", "\n", "Graphs are handled by the `graphs` module. Beyond the deterministic `line` and `ring`, it ships two **random** generators, `cayley` and `erdos_renyi`." ] }, { "cell_type": "code", "execution_count": 1, "id": "9b39a18e-a222-4a06-a633-7b0fb689fbf0", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:36.655642Z", "iopub.status.busy": "2026-06-23T16:33:36.655467Z", "iopub.status.idle": "2026-06-23T16:33:36.966599Z", "shell.execute_reply": "2026-06-23T16:33:36.965373Z" } }, "outputs": [], "source": [ "from tinto import graphs" ] }, { "cell_type": "markdown", "id": "e3b6bb24-635b-4cac-bc68-93634d859bd9", "metadata": {}, "source": [ "### Line" ] }, { "cell_type": "code", "execution_count": 2, "id": "743fbe3f-86b4-4a09-b750-d8a7483d6f8e", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:36.969843Z", "iopub.status.busy": "2026-06-23T16:33:36.969468Z", "iopub.status.idle": "2026-06-23T16:33:39.341571Z", "shell.execute_reply": "2026-06-23T16:33:39.340857Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Neighbors of 0: [1]\n", "Neighbors of 1: [0 2]\n", "Neighbors of 2: [1 3]\n", "Neighbors of 3: [2 4]\n", "Neighbors of 4: [3]\n" ] } ], "source": [ "n = 5\n", "line = graphs.line(n=n)\n", "for i in range(n):\n", " print(f\"Neighbors of {i}: {line.neighbors(i)}\")" ] }, { "cell_type": "markdown", "id": "f64a58be-e5ff-4c79-825e-9bb14537f3a7", "metadata": {}, "source": [ "### Ring" ] }, { "cell_type": "code", "execution_count": 3, "id": "58c4b4cb-c073-42b0-9c03-940ec4baf27b", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:39.344031Z", "iopub.status.busy": "2026-06-23T16:33:39.343692Z", "iopub.status.idle": "2026-06-23T16:33:39.961026Z", "shell.execute_reply": "2026-06-23T16:33:39.959459Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Neighbors of 0: [1 4]\n", "Neighbors of 1: [0 2]\n", "Neighbors of 2: [1 3]\n", "Neighbors of 3: [2 4]\n", "Neighbors of 4: [0 3]\n" ] } ], "source": [ "n = 5\n", "ring = graphs.ring(n=n)\n", "for i in range(n):\n", " print(f\"Neighbors of {i}: {ring.neighbors(i)}\")" ] }, { "cell_type": "markdown", "id": "graph-cayley-md", "metadata": {}, "source": [ "### Random rooted tree (Cayley)\n", "\n", "`graphs.cayley(n)` draws a **uniform random labelled tree** on `n` nodes, via the bijection behind [Cayley's formula](https://en.wikipedia.org/wiki/Cayley%27s_formula) ($n^{n-2}$ labelled trees, each equally likely). A tree is connected and acyclic, so it has exactly `n - 1` edges.\n", "\n", "The random generators honor the global seed (`set_seed`), which we fix for reproducibility." ] }, { "cell_type": "code", "execution_count": 4, "id": "graph-cayley-code", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:39.964650Z", "iopub.status.busy": "2026-06-23T16:33:39.964174Z", "iopub.status.idle": "2026-06-23T16:33:41.614475Z", "shell.execute_reply": "2026-06-23T16:33:41.613191Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Neighbors of 0: [4]\n", "Neighbors of 1: [2]\n", "Neighbors of 2: [1 6]\n", "Neighbors of 3: [4 6]\n", "Neighbors of 4: [0 3 5]\n", "Neighbors of 5: [4]\n", "Neighbors of 6: [2 3]\n", "\n", "7 nodes, 6 edges (a tree always has n - 1)\n" ] } ], "source": [ "from tinto.io import set_seed\n", "\n", "set_seed(42)\n", "tree = graphs.cayley(n=7)\n", "for i in range(tree.n):\n", " print(f\"Neighbors of {i}: {tree.neighbors(i)}\")\n", "\n", "n_edges = sum(len(tree.neighbors(i)) for i in range(tree.n)) // 2\n", "print(f\"\\n{tree.n} nodes, {n_edges} edges (a tree always has n - 1)\")" ] }, { "cell_type": "markdown", "id": "graph-erdos-md", "metadata": {}, "source": [ "### Erdős-Rényi\n", "\n", "`graphs.erdos_renyi(n, p)` is the classic $G(n, p)$ model: each of the $\\binom{n}{2}$ possible undirected edges appears independently with probability `p`. Its `max_degree` is therefore random -- and since a `Config` derives its default number of colors from the max degree (see below), that palette size varies from one draw to the next." ] }, { "cell_type": "code", "execution_count": 5, "id": "graph-erdos-code", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:41.617325Z", "iopub.status.busy": "2026-06-23T16:33:41.617048Z", "iopub.status.idle": "2026-06-23T16:33:42.057269Z", "shell.execute_reply": "2026-06-23T16:33:42.055997Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Neighbors of 0: [1 5 6]\n", "Neighbors of 1: [0 2 6]\n", "Neighbors of 2: [1 5 6]\n", "Neighbors of 3: [4 5]\n", "Neighbors of 4: [3 6]\n", "Neighbors of 5: [0 2 3]\n", "Neighbors of 6: [0 1 2 4]\n", "\n", "max_degree = 4 (random, unlike line/ring/tree)\n" ] } ], "source": [ "set_seed(42)\n", "er = graphs.erdos_renyi(n=7, p=0.4)\n", "for i in range(er.n):\n", " print(f\"Neighbors of {i}: {er.neighbors(i)}\")\n", "\n", "print(f\"\\nmax_degree = {er.max_degree} (random, unlike line/ring/tree)\")" ] }, { "cell_type": "markdown", "id": "eb6e4739-e883-407a-9bc5-07e691ecb5ad", "metadata": {}, "source": [ "## Guards" ] }, { "cell_type": "markdown", "id": "37170e24-3767-4346-8364-5e8e15dfea3b", "metadata": {}, "source": [ "A guard is a function `guard(config, i) -> bool` that takes a *configuration* (see below) and a node index, and tells whether the considered node can be selected. Some guards may require node ids. Examples of guards:\n", "\n", "- `color_clash` (no node id required): at least one neighbor has my color.\n", "- `color_clash_tie` (node id required): at least one neighbor has my color, and my ID is lower than the IDs of the clashing nodes I see.\n", "- `color_min` (no node id required): my color index is not the lowest one absent from my neighborhood.\n", "- `color_min_tie` (node id required): my color index is not the lowest one absent from my neighborhood, and neighbors that share my color have a higher ID than me." ] }, { "cell_type": "markdown", "id": "4966a254-7a09-4b19-a787-e1ef8c63ff23", "metadata": {}, "source": [ "Guards are handled by the `guards` module." ] }, { "cell_type": "code", "execution_count": 6, "id": "7c975da7-7f00-438e-8e7e-ba7c630724c6", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:42.060461Z", "iopub.status.busy": "2026-06-23T16:33:42.060173Z", "iopub.status.idle": "2026-06-23T16:33:42.071523Z", "shell.execute_reply": "2026-06-23T16:33:42.069882Z" } }, "outputs": [ { "data": { "text/plain": [ "{'njit': ,\n", " 'color_clash': CPUDispatcher(),\n", " 'color_clash_tie': CPUDispatcher(),\n", " 'color_min': CPUDispatcher(),\n", " 'color_min_tie': CPUDispatcher()}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from tinto import guards\n", "\n", "{k: v for k, v in guards.__dict__.items() if not k.startswith(\"_\")}" ] }, { "cell_type": "markdown", "id": "33244cd8-10f1-4ee0-81a1-2c5f59281d99", "metadata": {}, "source": [ "## Configurations" ] }, { "cell_type": "markdown", "id": "66f48a4f-b606-428c-bf78-c309129de5a1", "metadata": {}, "source": [ "For a given graph, a coloring associates to each node a color (represented by its index).\n", "\n", "A Configuration is a numba `jitclass` that glues together a graph, a guard, and a coloring. In particular, it keeps track of the guarded nodes.\n", "\n", "In practice, to avoid redundant JIT compilation, the building process is as follows:\n", "\n", "- Using the `tinto.configs.make` function (input: a `guard`, defaults to `color_clash`), one creates a `Config` **class**.\n", "- Actual configurations are instantiated by providing a `graph` (and optionally a number of colors).\n", "- Post-processing functions are available to alter colors or ids." ] }, { "cell_type": "markdown", "id": "fed49800-fceb-439b-a211-1d51c02b0458", "metadata": {}, "source": [ "### Color clash" ] }, { "cell_type": "markdown", "id": "804d8868-abdf-4d66-a9ee-7884a0fd278e", "metadata": {}, "source": [ "Example: we start with the default `color_clash`. We also set up a seed for reproducibility." ] }, { "cell_type": "code", "execution_count": 7, "id": "92bc7c06-8e55-44f7-8481-6209c57d05cf", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:42.076186Z", "iopub.status.busy": "2026-06-23T16:33:42.075841Z", "iopub.status.idle": "2026-06-23T16:33:43.751488Z", "shell.execute_reply": "2026-06-23T16:33:43.749824Z" } }, "outputs": [], "source": [ "from tinto.configs import make\n", "from tinto.io import set_seed\n", "\n", "set_seed(42)\n", "ClashConfig = make()\n", "clashconfig = ClashConfig(ring)" ] }, { "cell_type": "markdown", "id": "6d0dcbc4-73e4-452c-b4a7-0b6d388b654c", "metadata": {}, "source": [ "No ID." ] }, { "cell_type": "code", "execution_count": 8, "id": "4bd05560-2edd-436e-a96a-3b2ee70548f1", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:43.754816Z", "iopub.status.busy": "2026-06-23T16:33:43.754536Z", "iopub.status.idle": "2026-06-23T16:33:43.760018Z", "shell.execute_reply": "2026-06-23T16:33:43.758912Z" } }, "outputs": [ { "data": { "text/plain": [ "array([], dtype=int64)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.ids" ] }, { "cell_type": "markdown", "id": "23dd9c6a-6645-4d87-abae-21ee6928228d", "metadata": {}, "source": [ "For a max-degree-2 graph, the configuration defaults to 3 colors." ] }, { "cell_type": "code", "execution_count": 9, "id": "1594fac3-ca6b-48d0-853b-a01a0c91f01c", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:43.762903Z", "iopub.status.busy": "2026-06-23T16:33:43.762620Z", "iopub.status.idle": "2026-06-23T16:33:43.769511Z", "shell.execute_reply": "2026-06-23T16:33:43.768058Z" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.k" ] }, { "cell_type": "markdown", "id": "1b3f79db-6306-494f-922f-536e1fb9887b", "metadata": {}, "source": [ "By default, everyone has color 0." ] }, { "cell_type": "code", "execution_count": 10, "id": "19b86f9f-dfa0-40bb-9092-96f8e94a3081", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:43.772649Z", "iopub.status.busy": "2026-06-23T16:33:43.772334Z", "iopub.status.idle": "2026-06-23T16:33:43.778626Z", "shell.execute_reply": "2026-06-23T16:33:43.777132Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.colors" ] }, { "cell_type": "markdown", "id": "bcf6a56f-92b5-4b58-948d-8d4f5331b259", "metadata": {}, "source": [ "Meaning that every connected node is enabled by the guard." ] }, { "cell_type": "code", "execution_count": 11, "id": "c963e743-63d0-4cdf-8e42-d051fec0d09b", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:43.781969Z", "iopub.status.busy": "2026-06-23T16:33:43.781669Z", "iopub.status.idle": "2026-06-23T16:33:43.787839Z", "shell.execute_reply": "2026-06-23T16:33:43.786630Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True, True, True])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.enabled" ] }, { "cell_type": "code", "execution_count": 12, "id": "bd18b0f1-543b-40d3-80fb-f0c4949b1853", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:43.790893Z", "iopub.status.busy": "2026-06-23T16:33:43.790603Z", "iopub.status.idle": "2026-06-23T16:33:43.795819Z", "shell.execute_reply": "2026-06-23T16:33:43.794807Z" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.total" ] }, { "cell_type": "markdown", "id": "ad158bba-17be-47af-8240-b773abd64ebf", "metadata": {}, "source": [ "If one shuffles the colors, the guarded nodes are not the same." ] }, { "cell_type": "code", "execution_count": 13, "id": "75958d08-8080-4667-b3e3-9332cf6d1e4a", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:43.798924Z", "iopub.status.busy": "2026-06-23T16:33:43.798640Z", "iopub.status.idle": "2026-06-23T16:33:44.131521Z", "shell.execute_reply": "2026-06-23T16:33:44.130331Z" } }, "outputs": [], "source": [ "clashconfig.color_randomize()" ] }, { "cell_type": "code", "execution_count": 14, "id": "c718690d-e928-4b1c-845e-ab0a09b0184b", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:44.134697Z", "iopub.status.busy": "2026-06-23T16:33:44.134325Z", "iopub.status.idle": "2026-06-23T16:33:44.140891Z", "shell.execute_reply": "2026-06-23T16:33:44.139627Z" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 0, 2, 2, 0])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.colors" ] }, { "cell_type": "code", "execution_count": 15, "id": "5969a606-e3f9-4e73-bed6-de6dcad8fe75", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:44.144345Z", "iopub.status.busy": "2026-06-23T16:33:44.143737Z", "iopub.status.idle": "2026-06-23T16:33:44.151188Z", "shell.execute_reply": "2026-06-23T16:33:44.149824Z" } }, "outputs": [ { "data": { "text/plain": [ "array([False, False, True, True, False])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.enabled" ] }, { "cell_type": "markdown", "id": "4eba866b-2033-455a-9290-e760bed37947", "metadata": {}, "source": [ "In the example above, nodes 2 and 3 clash with each other." ] }, { "cell_type": "code", "execution_count": 16, "id": "d54b737b-3871-44bc-be43-f39cf6ed82e4", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:44.154866Z", "iopub.status.busy": "2026-06-23T16:33:44.154499Z", "iopub.status.idle": "2026-06-23T16:33:44.160336Z", "shell.execute_reply": "2026-06-23T16:33:44.158910Z" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clashconfig.total" ] }, { "cell_type": "markdown", "id": "1a22f68d-7a76-40c8-bac1-b54c23b0ff41", "metadata": {}, "source": [ "### Min Tie" ] }, { "cell_type": "markdown", "id": "3a79f0ee-1db7-407a-bfef-7384187f1b16", "metadata": {}, "source": [ "One could play with all the guards, but let's just pick another one." ] }, { "cell_type": "code", "execution_count": 17, "id": "bc006b9f-3778-4fe4-ae30-942fa59b875c", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:44.163306Z", "iopub.status.busy": "2026-06-23T16:33:44.163017Z", "iopub.status.idle": "2026-06-23T16:33:47.152213Z", "shell.execute_reply": "2026-06-23T16:33:47.150584Z" } }, "outputs": [], "source": [ "MinTieConfig = make(guards.color_min_tie)\n", "config = MinTieConfig(ring, k=4)" ] }, { "cell_type": "markdown", "id": "94f6717d-5f50-4b4c-b28c-44a74338ab67", "metadata": {}, "source": [ "IDs are required by the guard." ] }, { "cell_type": "code", "execution_count": 18, "id": "d5c781ed-8a94-4f48-8af9-8c3400222b07", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.156025Z", "iopub.status.busy": "2026-06-23T16:33:47.155696Z", "iopub.status.idle": "2026-06-23T16:33:47.162669Z", "shell.execute_reply": "2026-06-23T16:33:47.161389Z" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 0, 1, 2, 4])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.ids" ] }, { "cell_type": "markdown", "id": "ec6c2698-7984-4bee-88ff-8975b4c59938", "metadata": {}, "source": [ "For a max-degree-2 graph, the configuration defaults to 3 colors, but we set up 4 colors." ] }, { "cell_type": "code", "execution_count": 19, "id": "70938fcd-f555-437e-b7e8-59b3b0dd4cd3", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.166340Z", "iopub.status.busy": "2026-06-23T16:33:47.166011Z", "iopub.status.idle": "2026-06-23T16:33:47.171909Z", "shell.execute_reply": "2026-06-23T16:33:47.170711Z" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.k" ] }, { "cell_type": "markdown", "id": "afafb25c-f7ca-4c21-b789-e9897bdc66f2", "metadata": {}, "source": [ "By default, everyone has color 0." ] }, { "cell_type": "code", "execution_count": 20, "id": "c71d2fed-5747-4c5e-892d-1dc5c0e9c89f", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.175109Z", "iopub.status.busy": "2026-06-23T16:33:47.174699Z", "iopub.status.idle": "2026-06-23T16:33:47.180530Z", "shell.execute_reply": "2026-06-23T16:33:47.179128Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.colors" ] }, { "cell_type": "markdown", "id": "5deab349-a7e1-4bcb-a687-d97e608aef9b", "metadata": {}, "source": [ "Meaning that every node with locally minimal ID is enabled by the guard." ] }, { "cell_type": "code", "execution_count": 21, "id": "3bd7d94e-33c1-446e-98c4-a9918ddc6ebd", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.184031Z", "iopub.status.busy": "2026-06-23T16:33:47.183648Z", "iopub.status.idle": "2026-06-23T16:33:47.190175Z", "shell.execute_reply": "2026-06-23T16:33:47.188797Z" } }, "outputs": [ { "data": { "text/plain": [ "array([False, True, False, False, False])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.enabled" ] }, { "cell_type": "code", "execution_count": 22, "id": "f04c3c03-a803-4d9b-8202-0d83846e6700", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.193208Z", "iopub.status.busy": "2026-06-23T16:33:47.192786Z", "iopub.status.idle": "2026-06-23T16:33:47.198672Z", "shell.execute_reply": "2026-06-23T16:33:47.197301Z" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.total" ] }, { "cell_type": "markdown", "id": "bc5e8ca5-0103-4921-a15b-dce173bfb00c", "metadata": {}, "source": [ "In the example above, 1 is the only local minimum (all other nodes have a neighbor with lower ID)." ] }, { "cell_type": "markdown", "id": "7a6ce334-8114-475f-8c8d-4026776431a0", "metadata": {}, "source": [ "If one shuffles the colors, the guarded nodes are not the same." ] }, { "cell_type": "code", "execution_count": 23, "id": "e5cf9964-8930-4a3d-9660-9cc8c8b60217", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.201865Z", "iopub.status.busy": "2026-06-23T16:33:47.201546Z", "iopub.status.idle": "2026-06-23T16:33:47.689315Z", "shell.execute_reply": "2026-06-23T16:33:47.687718Z" } }, "outputs": [], "source": [ "config.color_randomize()" ] }, { "cell_type": "code", "execution_count": 24, "id": "27ed0203-b7ab-467c-a58d-f23728580d95", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.692414Z", "iopub.status.busy": "2026-06-23T16:33:47.692110Z", "iopub.status.idle": "2026-06-23T16:33:47.698510Z", "shell.execute_reply": "2026-06-23T16:33:47.696990Z" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 2, 2, 3, 0])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.colors" ] }, { "cell_type": "code", "execution_count": 25, "id": "a9221a66-2775-46c8-acb7-afc12db8209e", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.701415Z", "iopub.status.busy": "2026-06-23T16:33:47.701126Z", "iopub.status.idle": "2026-06-23T16:33:47.706661Z", "shell.execute_reply": "2026-06-23T16:33:47.705494Z" } }, "outputs": [ { "data": { "text/plain": [ "array([False, True, False, True, False])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.enabled" ] }, { "cell_type": "code", "execution_count": 26, "id": "f0a0e8d5-a280-4616-970b-3cf6529f3991", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.710113Z", "iopub.status.busy": "2026-06-23T16:33:47.709800Z", "iopub.status.idle": "2026-06-23T16:33:47.716396Z", "shell.execute_reply": "2026-06-23T16:33:47.714599Z" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.total" ] }, { "cell_type": "markdown", "id": "e5308f73-397e-42e3-9ad2-5e67b1910459", "metadata": {}, "source": [ "Node 1 is still enabled (it can switch to 0 and is a local minimum among its neighbors of color 2). Node 3 is enabled (current color is 3; can go to 1; no neighbor with color 3)." ] }, { "cell_type": "markdown", "id": "55997b0d-3c38-446f-be0a-a1aee06ea878", "metadata": {}, "source": [ "## Starters" ] }, { "cell_type": "markdown", "id": "ff7ec7a2-0da4-4e11-ab75-3b203fffd312", "metadata": {}, "source": [ "Starters are objects with a `draw()` method that returns configurations. They define the initial state (possibly randomized) of a simulation.\n", "\n", "Currently, the main entry point is `from_graph`, which, as the name says, will use a fixed graph. As for `Configuration`, the starter is built in multiple stages:\n", "\n", "- Using a `Config` **class** (not an instance), one creates a `Starter` class; the initial coloring can be random (default) or uniform if one gives a `color`;\n", "- The starter is instantiated with a `graph`;\n", "- The instance can use its method `draw()`." ] }, { "cell_type": "code", "execution_count": 27, "id": "8fe89074-68a6-4434-86bc-b8cbbc158cac", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:47.720214Z", "iopub.status.busy": "2026-06-23T16:33:47.719884Z", "iopub.status.idle": "2026-06-23T16:33:48.024474Z", "shell.execute_reply": "2026-06-23T16:33:48.022801Z" } }, "outputs": [], "source": [ "from tinto.starters import from_graph\n", "\n", "Starter = from_graph(make())\n", "starter = Starter(ring)" ] }, { "cell_type": "code", "execution_count": 28, "id": "a87b51fa-da16-48f4-a495-d8317b09ab6b", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:48.027899Z", "iopub.status.busy": "2026-06-23T16:33:48.027592Z", "iopub.status.idle": "2026-06-23T16:33:48.400026Z", "shell.execute_reply": "2026-06-23T16:33:48.398543Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 1 0 1 1]\n", "[1 1 0 0 1]\n", "[1 0 0 0 2]\n" ] } ], "source": [ "for _ in range(3):\n", " c = starter.draw()\n", " print(c.colors)" ] }, { "cell_type": "code", "execution_count": 29, "id": "5bdaeb6d-a422-4e3b-bfdb-b3cb96258d07", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:48.403051Z", "iopub.status.busy": "2026-06-23T16:33:48.402768Z", "iopub.status.idle": "2026-06-23T16:33:48.697648Z", "shell.execute_reply": "2026-06-23T16:33:48.696329Z" } }, "outputs": [], "source": [ "Starter = from_graph(make(guard=guards.color_clash_tie), color=2)\n", "starter = Starter(ring)" ] }, { "cell_type": "code", "execution_count": 30, "id": "69d8015c-f3d2-477c-a8e3-24b43e95662b", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:48.700556Z", "iopub.status.busy": "2026-06-23T16:33:48.700280Z", "iopub.status.idle": "2026-06-23T16:33:50.923377Z", "shell.execute_reply": "2026-06-23T16:33:50.922201Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ids: [0 3 1 4 2]\n", "Cls: [2 2 2 2 2]\n", "Guards: [ True False True False False]\n", "Ids: [0 1 2 4 3]\n", "Cls: [2 2 2 2 2]\n", "Guards: [ True False False False False]\n", "Ids: [0 1 4 3 2]\n", "Cls: [2 2 2 2 2]\n", "Guards: [ True False False False False]\n" ] } ], "source": [ "for _ in range(3):\n", " c = starter.draw()\n", " print(f\"Ids: {c.ids}\")\n", " print(f\"Cls: {c.colors}\")\n", " print(f\"Guards: {c.enabled}\")" ] }, { "cell_type": "markdown", "id": "e61389f1-989c-404d-bfa1-e0f28c4dda32", "metadata": {}, "source": [ "## Schedulers (a.k.a. *Daemons*)" ] }, { "cell_type": "markdown", "id": "891f5123-cd77-4855-b2e8-3b781d252141", "metadata": {}, "source": [ "Schedulers are functions that, for a given config, return a subset of the enabled nodes through a `draw(config)` method. The current schedulers are:" ] }, { "cell_type": "markdown", "id": "ceb0e5d6-b544-4507-a010-191583ae9006", "metadata": {}, "source": [ ":::important\n", "\n", "By convention, we allow a scheduler to return nothing even if some nodes are enabled, but this does not count as an actual step.\n", "\n", ":::" ] }, { "cell_type": "markdown", "id": "5dc31cbb-b53c-4e47-a484-29e11733908d", "metadata": {}, "source": [ "### Shuffled" ] }, { "cell_type": "markdown", "id": "6cf097f6-b65b-4c13-a8f4-061a55d394ca", "metadata": {}, "source": [ "A random permutation is drawn and a cursor cycles through it. Yields the current node if it is enabled. Shuffled is a *sequential*, a.k.a. *central* scheduler (one node at a time)." ] }, { "cell_type": "markdown", "id": "3ee93ce4-4a7b-4793-a5c4-e1e6178b3908", "metadata": {}, "source": [ ":::note\n", "\n", "Currently, the permutation is static. This is 100% OK for one-pass algorithms.\n", "\n", ":::" ] }, { "cell_type": "code", "execution_count": 31, "id": "2b883d1b-9765-44ff-bcd4-0df20fecc7ca", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:50.926662Z", "iopub.status.busy": "2026-06-23T16:33:50.926391Z", "iopub.status.idle": "2026-06-23T16:33:52.331155Z", "shell.execute_reply": "2026-06-23T16:33:52.330103Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1], [3], [4], [2], [0], [1], [3], [4], [2], [0]\n" ] } ], "source": [ "from tinto import schedulers\n", "\n", "config = make()(ring)\n", "s = schedulers.shuffled()(config)\n", "print(\", \".join(str(s.draw(config)) for _ in range(10)))" ] }, { "cell_type": "markdown", "id": "a4f78792-ae84-4f53-8392-98ab75b11cca", "metadata": {}, "source": [ "### Ordered" ] }, { "cell_type": "markdown", "id": "3468df51-7e4c-4020-b582-91b91a3513f7", "metadata": {}, "source": [ "Exactly like `shuffled` but uses `np.arange` as its unique permutation. Designed for controlled edge-case exploration." ] }, { "cell_type": "code", "execution_count": 32, "id": "2d79240e-f978-4630-953b-8617abc9ee86", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:52.334751Z", "iopub.status.busy": "2026-06-23T16:33:52.334454Z", "iopub.status.idle": "2026-06-23T16:33:52.901713Z", "shell.execute_reply": "2026-06-23T16:33:52.899873Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0], [1], [2], [3], [4], [0], [1], [2], [3], [4]\n" ] } ], "source": [ "s = schedulers.ordered()(config)\n", "print(\", \".join(str(s.draw(config)) for _ in range(10)))" ] }, { "cell_type": "markdown", "id": "72f96cc5-1df0-4d1b-8de4-95413aca1e57", "metadata": {}, "source": [ "### Bernoulli" ] }, { "cell_type": "markdown", "id": "9699dad9-e804-4b7b-a1aa-51af231d36a9", "metadata": {}, "source": [ "Selects each enabled node with probability $p$." ] }, { "cell_type": "code", "execution_count": 33, "id": "50957e5c-2176-47ee-b919-70b7ff6b96c4", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:52.909397Z", "iopub.status.busy": "2026-06-23T16:33:52.908771Z", "iopub.status.idle": "2026-06-23T16:33:54.096711Z", "shell.execute_reply": "2026-06-23T16:33:54.094964Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2]\n", "[1 2 3]\n", "[2 4]\n", "[2 4]\n", "[]\n" ] } ], "source": [ "s = schedulers.bernoulli(p=1 / 3)(config)\n", "print(\"\\n\".join(str(s.draw(config)) for _ in range(5)))" ] }, { "cell_type": "code", "execution_count": 34, "id": "1d6ab200-0b22-4577-bd18-0907ca196b99", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:54.101105Z", "iopub.status.busy": "2026-06-23T16:33:54.100406Z", "iopub.status.idle": "2026-06-23T16:33:54.319984Z", "shell.execute_reply": "2026-06-23T16:33:54.318610Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4]\n", "[0 1 2 3 4]\n", "[0 1 2 3 4]\n", "[0 1 2 3 4]\n", "[0 1 2 3 4]\n" ] } ], "source": [ "s = schedulers.bernoulli(p=1)(config)\n", "print(\"\\n\".join(str(s.draw(config)) for _ in range(5)))" ] }, { "cell_type": "code", "execution_count": 35, "id": "b970d19b-9579-41b8-9af8-aba4c777cafd", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:54.322639Z", "iopub.status.busy": "2026-06-23T16:33:54.322376Z", "iopub.status.idle": "2026-06-23T16:33:54.430375Z", "shell.execute_reply": "2026-06-23T16:33:54.429149Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2]\n", "[]\n", "[]\n", "[0 2]\n", "[4]\n" ] } ], "source": [ "s = schedulers.bernoulli(p=1 / 5)(config)\n", "print(\"\\n\".join(str(s.draw(config)) for _ in range(5)))" ] }, { "cell_type": "markdown", "id": "1ec7afd0-014a-445c-8b67-05b95abbf1cf", "metadata": {}, "source": [ "### Independent / local" ] }, { "cell_type": "markdown", "id": "4a621f54-a566-4a64-9b50-c11d5a0627fe", "metadata": {}, "source": [ "Same as Bernoulli with a twist: you build your selection one node at a time, and whenever you pick up a node adjacent to another node already selected, you skip the selection. This ensures that the result is always an independent set, avoiding the re-creation of conflicts." ] }, { "cell_type": "code", "execution_count": 36, "id": "9a7314fa-7157-447f-b9bd-b4029502f248", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:54.433128Z", "iopub.status.busy": "2026-06-23T16:33:54.432869Z", "iopub.status.idle": "2026-06-23T16:33:55.382466Z", "shell.execute_reply": "2026-06-23T16:33:55.381387Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 3]\n", "[0]\n", "[3]\n", "[2 4]\n", "[1]\n" ] } ], "source": [ "s = schedulers.independent(p=1 / 3)(config)\n", "print(\"\\n\".join(str(s.draw(config)) for _ in range(5)))" ] }, { "cell_type": "code", "execution_count": 37, "id": "c2f03180-530e-493a-828d-48ec6a9578d0", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:55.384933Z", "iopub.status.busy": "2026-06-23T16:33:55.384686Z", "iopub.status.idle": "2026-06-23T16:33:55.591527Z", "shell.execute_reply": "2026-06-23T16:33:55.590484Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 4]\n", "[1 3]\n", "[1 4]\n", "[1 3]\n", "[0 3]\n" ] } ], "source": [ "s = schedulers.independent(p=1)(config)\n", "print(\"\\n\".join(str(s.draw(config)) for _ in range(5)))" ] }, { "cell_type": "code", "execution_count": 38, "id": "dbf19f22-5b22-415c-be98-0ffcbea0cdbb", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:55.594083Z", "iopub.status.busy": "2026-06-23T16:33:55.593835Z", "iopub.status.idle": "2026-06-23T16:33:55.694375Z", "shell.execute_reply": "2026-06-23T16:33:55.693197Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4]\n", "[3]\n", "[]\n", "[0 2]\n", "[]\n" ] } ], "source": [ "s = schedulers.independent(p=1 / 5)(config)\n", "print(\"\\n\".join(str(s.draw(config)) for _ in range(5)))" ] }, { "cell_type": "markdown", "id": "f3b9069e-d036-43ac-9283-75a32420afe2", "metadata": {}, "source": [ "## Solvers / Coloring" ] }, { "cell_type": "markdown", "id": "532529a4-f97f-418d-9e15-0da81ea7fcf4", "metadata": {}, "source": [ "A solver takes as input a *configuration* and a set of nodes (typically the output of a scheduler) and applies a coloring function to each of the nodes.\n", "\n", "- All colors are decided as a function of the input configuration (not taking into account the possible changes from other nodes);\n", "- You instantiate a solver through the `solvers.make(colorer)` function, where the `colorer` is a `(config, node) -> color` function.\n", "\n", "A few colorers are proposed by default." ] }, { "cell_type": "markdown", "id": "595636c2-9fc5-4ecf-9d07-65a354f90918", "metadata": {}, "source": [ "### Lowest" ] }, { "cell_type": "markdown", "id": "63b604ca-bc01-41c9-8763-eb8d96caf563", "metadata": {}, "source": [ "Selects the lowest available color. Can cause trouble for non-independent schedulers. To see that, let us write a function that shows the evolution of a configuration." ] }, { "cell_type": "code", "execution_count": 39, "id": "a036ce6d-cb7c-4cd7-b5e9-d2e490d199e9", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:55.697423Z", "iopub.status.busy": "2026-06-23T16:33:55.697175Z", "iopub.status.idle": "2026-06-23T16:33:55.704490Z", "shell.execute_reply": "2026-06-23T16:33:55.703226Z" } }, "outputs": [], "source": [ "from tinto import solvers\n", "\n", "lowest = solvers.make(solvers.colorer_lowest)\n", "\n", "\n", "def verbose_run(config, scheduler, solver, max_steps=5):\n", " print(f\"Colors: {config.colors}\")\n", " for _ in range(max_steps):\n", " if config.total == 0:\n", " print(\"Done!\")\n", " break\n", " select = scheduler.draw(config)\n", " print(f\"Selection: {select}\")\n", " solver(config, select)\n", " print(f\"Colors: {config.colors}\")" ] }, { "cell_type": "code", "execution_count": 40, "id": "62ac8382-e9cb-4d38-953f-5e723ac20bae", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:55.707336Z", "iopub.status.busy": "2026-06-23T16:33:55.707058Z", "iopub.status.idle": "2026-06-23T16:33:56.819766Z", "shell.execute_reply": "2026-06-23T16:33:56.818568Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Colors: [1 1 1 1 1]\n", "Selection: [0 1 2 3 4]\n", "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n", "Colors: [1 1 1 1 1]\n", "Selection: [0 1 2 3 4]\n", "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n", "Colors: [1 1 1 1 1]\n" ] } ], "source": [ "config = make()(ring)\n", "s = schedulers.bernoulli(p=1)(config)\n", "verbose_run(config, s, lowest)" ] }, { "cell_type": "code", "execution_count": 41, "id": "a30dbfc4-1ba1-4b2d-865b-771e66ec580e", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:56.823043Z", "iopub.status.busy": "2026-06-23T16:33:56.822725Z", "iopub.status.idle": "2026-06-23T16:33:56.829076Z", "shell.execute_reply": "2026-06-23T16:33:56.827977Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [0 3]\n", "Colors: [1 0 0 1 0]\n", "Selection: [1]\n", "Colors: [1 2 0 1 0]\n", "Done!\n" ] } ], "source": [ "config = make()(ring)\n", "s = schedulers.independent(p=1)(config)\n", "verbose_run(config, s, lowest)" ] }, { "cell_type": "markdown", "id": "ba8aba56-05f7-4f22-8f00-6b177e575584", "metadata": {}, "source": [ "### Next" ] }, { "cell_type": "markdown", "id": "75fce56d-0985-4f46-ac6f-abfb3b1c024f", "metadata": {}, "source": [ "Selects the next free color (cyclic). Avoids too much concentration around the lowest color(s), but stays sensitive to non-independent schedulers." ] }, { "cell_type": "code", "execution_count": 42, "id": "f7d13719-1f72-40e9-b8f2-336f7e49d369", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:56.832826Z", "iopub.status.busy": "2026-06-23T16:33:56.832495Z", "iopub.status.idle": "2026-06-23T16:33:57.287497Z", "shell.execute_reply": "2026-06-23T16:33:57.286393Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Colors: [1 1 1 1 1]\n", "Selection: [0 1 2 3 4]\n", "Colors: [2 2 2 2 2]\n", "Selection: [0 1 2 3 4]\n", "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n", "Colors: [1 1 1 1 1]\n", "Selection: [0 1 2 3 4]\n", "Colors: [2 2 2 2 2]\n" ] } ], "source": [ "nexter = solvers.make(solvers.colorer_next)\n", "config = make()(ring)\n", "s = schedulers.bernoulli(p=1)(config)\n", "verbose_run(config, s, nexter)" ] }, { "cell_type": "code", "execution_count": 43, "id": "116daf72-672e-4cc1-9961-139f910a7547", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:57.290418Z", "iopub.status.busy": "2026-06-23T16:33:57.290141Z", "iopub.status.idle": "2026-06-23T16:33:57.295883Z", "shell.execute_reply": "2026-06-23T16:33:57.294741Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [1 4]\n", "Colors: [0 1 0 0 1]\n", "Selection: [2]\n", "Colors: [0 1 2 0 1]\n", "Done!\n" ] } ], "source": [ "config = make()(ring)\n", "s = schedulers.independent(p=1)(config)\n", "verbose_run(config, s, nexter)" ] }, { "cell_type": "markdown", "id": "c6aee47d-d9d6-43d0-a028-22d09e3d31a3", "metadata": {}, "source": [ "### Next blind" ] }, { "cell_type": "markdown", "id": "e3f93a2e-13b6-4817-bcb1-e025f5bbe2ef", "metadata": {}, "source": [ "Returns the next color (cyclic) without checking availability. Models scenarios where you can detect a conflict without a proper view of what's available. Combining a deterministic step with the absence of a check is a recipe for failure unless a properly randomized scheduler is used." ] }, { "cell_type": "code", "execution_count": 44, "id": "f9cfa57d-05c2-42b2-9440-f34b68042d4b", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:57.299338Z", "iopub.status.busy": "2026-06-23T16:33:57.299035Z", "iopub.status.idle": "2026-06-23T16:33:57.637813Z", "shell.execute_reply": "2026-06-23T16:33:57.636952Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [1 4]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 1 0 0 1]\n", "Selection: [2]\n", "Colors: [0 1 1 0 1]\n", "Selection: [2]\n", "Colors: [0 1 2 0 1]\n", "Done!\n" ] } ], "source": [ "nexter = solvers.make(solvers.colorer_next_blind)\n", "config = make()(ring)\n", "s = schedulers.independent(p=1)(config)\n", "verbose_run(config, s, nexter)" ] }, { "cell_type": "markdown", "id": "69f814c5-211a-4bd2-ba08-f21ca1e810fc", "metadata": {}, "source": [ "### Random" ] }, { "cell_type": "markdown", "id": "4a586dfe-8d75-4419-baa4-ee3eaf88fa1a", "metadata": {}, "source": [ "Selects a random available color. Designed to destroy unwanted synchrony, but the design can fail. In particular, it can happen that two adjacent nodes always see a single free color, making them loop forever." ] }, { "cell_type": "code", "execution_count": 45, "id": "03a71aa0-b39a-446a-9e58-828ede9bd571", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:57.640581Z", "iopub.status.busy": "2026-06-23T16:33:57.640320Z", "iopub.status.idle": "2026-06-23T16:33:58.343975Z", "shell.execute_reply": "2026-06-23T16:33:58.342691Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Colors: [1 1 2 1 1]\n", "Selection: [0 1 3 4]\n", "Colors: [2 0 2 0 0]\n", "Selection: [3 4]\n", "Colors: [2 0 2 1 1]\n", "Selection: [3 4]\n", "Colors: [2 0 2 0 0]\n", "Selection: [3 4]\n", "Colors: [2 0 2 1 1]\n" ] } ], "source": [ "rand = solvers.make(solvers.colorer_random)\n", "config = make()(ring)\n", "s = schedulers.bernoulli(p=1)(config)\n", "verbose_run(config, s, rand)" ] }, { "cell_type": "markdown", "id": "704f8ac7-1b97-42ed-8634-8d11f5ac915e", "metadata": {}, "source": [ "### Random blind" ] }, { "cell_type": "markdown", "id": "5def4c7d-a877-49c0-9567-fd2615c97105", "metadata": {}, "source": [ "Selects another random color, free or not. Models scenarios where you can detect a conflict without a proper view of what's available. Chaotic by nature." ] }, { "cell_type": "code", "execution_count": 46, "id": "20de4804-81b4-45da-b873-5313412b4ef3", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:58.346876Z", "iopub.status.busy": "2026-06-23T16:33:58.346613Z", "iopub.status.idle": "2026-06-23T16:33:58.739341Z", "shell.execute_reply": "2026-06-23T16:33:58.738038Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colors: [0 0 0 0 0]\n", "Selection: [0 1 2 3 4]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Colors: [2 2 1 1 1]\n", "Selection: [0 1 2 3 4]\n", "Colors: [0 0 2 0 2]\n", "Selection: [0 1]\n", "Colors: [2 2 2 0 2]\n", "Selection: [0 1 2 4]\n", "Colors: [1 1 0 0 1]\n", "Selection: [0 1 2 3 4]\n", "Colors: [2 2 1 2 2]\n" ] } ], "source": [ "rand = solvers.make(solvers.colorer_random_blind)\n", "config = make()(ring)\n", "s = schedulers.bernoulli(p=1)(config)\n", "verbose_run(config, s, rand)" ] }, { "cell_type": "markdown", "id": "fd594708-b8ae-429d-b884-5f0afed6b226", "metadata": {}, "source": [ "## Runners" ] }, { "cell_type": "markdown", "id": "5f1d50a7-f903-4658-8422-ff7f3b33defa", "metadata": {}, "source": [ "Runners are there to compute the convergence of a coloring problem. Two runners are provided: simple and Monte-Carlo." ] }, { "cell_type": "markdown", "id": "1d259bf1-f99e-4804-bc44-e1f9f700d3d8", "metadata": {}, "source": [ "### Simple runner" ] }, { "cell_type": "markdown", "id": "a800fdec-cbbb-4c6f-beca-16ca36959ca6", "metadata": {}, "source": [ "Remember the `verbose_run` function we defined to monitor the evolution of a configuration? The `simple` runner is similar, taking as input a configuration, a scheduler, and a solver. The main differences:\n", "\n", "- `simple` is very fast (JIT);\n", "- it outputs two integers: the number of (true) steps and the number of activations to convergence." ] }, { "cell_type": "code", "execution_count": 47, "id": "70ca519e-acdc-4f34-8430-2a8463f1b30e", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:58.743268Z", "iopub.status.busy": "2026-06-23T16:33:58.742819Z", "iopub.status.idle": "2026-06-23T16:33:59.134606Z", "shell.execute_reply": "2026-06-23T16:33:59.133166Z" } }, "outputs": [ { "data": { "text/plain": [ "(10, 37)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from tinto import runners\n", "\n", "rand = solvers.make(solvers.colorer_random_blind)\n", "config = make()(ring)\n", "s = schedulers.bernoulli(p=1)(config)\n", "runners.simple(config, s, rand)" ] }, { "cell_type": "markdown", "id": "ecdfcd50-ed70-402f-8638-e01e7bd56b87", "metadata": {}, "source": [ "### Monte-Carlo" ] }, { "cell_type": "markdown", "id": "b53bf079-f27a-442e-ade4-92297bab5d40", "metadata": {}, "source": [ "Runs a bunch of colorings. Compared to `simple`:\n", "\n", "- You need a `starter` (e.g. a producer of starting configurations) instead of a single configuration;\n", "- You can define the number of trials (defaults to 1000 but you should aim higher);\n", "- Out-of-the-box parallelism;\n", "- Output is a dictionary with the first 3 moments of # of steps and # of activations." ] }, { "cell_type": "code", "execution_count": 48, "id": "da90f7a8-1c75-4849-806a-d221fa78cd94", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:33:59.137934Z", "iopub.status.busy": "2026-06-23T16:33:59.137630Z", "iopub.status.idle": "2026-06-23T16:34:53.154487Z", "shell.execute_reply": "2026-06-23T16:34:53.151143Z" } }, "outputs": [ { "data": { "text/plain": [ "{'steps': (137.42364, 5352.781929150398, 585202.3897595722),\n", " 'activations': (2444.13777, 1178516.4887694279, 1479517014.6305733)}" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "starter = from_graph(make())(graphs.ring(n=100))\n", "rand = solvers.make(solvers.colorer_random_blind)\n", "dict(runners.mc(starter, schedulers.bernoulli(p=1), rand, n_trials=100000))" ] }, { "cell_type": "markdown", "id": "01c79a1c-3fc1-4d2e-8e6e-6e933b9a0700", "metadata": {}, "source": [ ":::important\n", "\n", "The runners currently have no time-out whatsoever. That means that if you enter a combination that does not converge, the program will run forever.\n", "\n", ":::" ] }, { "cell_type": "markdown", "id": "exp-intro-md", "metadata": {}, "source": [ "## Experiments & estimators\n", "\n", "The pieces above answer one question at a time: *given* this graph, scheduler and solver, how long until convergence? To **study** a coloring rule you sweep a parameter, average over many random trials, and read off a trend. Tinto wraps that loop in two layers, both exposed at the top level (`from tinto import Experiment, Iterator`):\n", "\n", "- **`Experiment`** (with `Iterator`): a parameter sweep glued to `runners.mc`. It stores the first moments of the # of steps / activations in a tidy structure and offers `.series()`, `.plot()`, `.to_csv()`.\n", "- the **`estimate`** module, reached through `Experiment.fit` and `Experiment.fit_fractions`: *estimators of behavior*. From the swept means and their error bars they fit a law, test for a hidden term, and even recover the **exact fractions** behind the coefficients.\n", "\n", "Here is the whole pipeline at a glance -- sweep the size `n` of a uniformly-started line under the sequential `shuffled` daemon and the `lowest` colorer." ] }, { "cell_type": "code", "execution_count": 49, "id": "exp-build-code", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:34:53.163094Z", "iopub.status.busy": "2026-06-23T16:34:53.162214Z", "iopub.status.idle": "2026-06-23T16:35:16.553094Z", "shell.execute_reply": "2026-06-23T16:35:16.550818Z" } }, "outputs": [ { "data": { "text/plain": [ "{'line': ([np.int64(5),\n", " np.int64(10),\n", " np.int64(15),\n", " np.int64(20),\n", " np.int64(25),\n", " np.int64(30),\n", " np.int64(35),\n", " np.int64(40),\n", " np.int64(45),\n", " np.int64(50)],\n", " [3.001085,\n", " 6.33106,\n", " 9.66647,\n", " 12.99707,\n", " 16.33331,\n", " 19.669665,\n", " 23.000415,\n", " 26.333035,\n", " 29.67011,\n", " 33.00306])}" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "from tinto import Experiment, Iterator\n", "\n", "set_seed(42)\n", "uniform = from_graph(make(), color=0) # everyone starts at color 0\n", "\n", "exp = Experiment(\n", " \"line\",\n", " iterator=Iterator(\n", " \"starter\",\n", " np.arange(5, 55, 5),\n", " name=\"n\",\n", " process=lambda n: uniform(graphs.line(n=n)),\n", " ),\n", " scheduler=schedulers.shuffled(),\n", " solver=solvers.make(), # lowest\n", " n_trials=200000,\n", ")\n", "exp.run(progress=False)\n", "exp.series(metric=\"steps\")" ] }, { "cell_type": "markdown", "id": "exp-fit-md", "metadata": {}, "source": [ "`series` returns the measured means; `fit` turns them into an affine law `a*n + b` together with goodness-of-fit diagnostics." ] }, { "cell_type": "code", "execution_count": 50, "id": "exp-fit-code", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:35:16.567340Z", "iopub.status.busy": "2026-06-23T16:35:16.566441Z", "iopub.status.idle": "2026-06-23T16:35:19.021839Z", "shell.execute_reply": "2026-06-23T16:35:19.019321Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "steps(n) ~= 0.6667 * n + -0.3340\n", "reduced chi2 = 0.92 (p = 0.50)\n" ] } ], "source": [ "result = exp.fit(metric=\"steps\")[\"line\"]\n", "print(f\"steps(n) ~= {result['beta'][1]:.4f} * n + {result['beta'][0]:.4f}\")\n", "print(f\"reduced chi2 = {result['chi2_red']:.2f} (p = {result['pvalue']:.2f})\")" ] }, { "cell_type": "markdown", "id": "exp-frac-md", "metadata": {}, "source": [ "A reduced $\\chi^2$ near 1 says the affine law fits. The final step asks for the *exact* fractions hiding in those coefficients:" ] }, { "cell_type": "code", "execution_count": 51, "id": "exp-frac-code", "metadata": { "execution": { "iopub.execute_input": "2026-06-23T16:35:19.027245Z", "iopub.status.busy": "2026-06-23T16:35:19.026352Z", "iopub.status.idle": "2026-06-23T16:35:19.038649Z", "shell.execute_reply": "2026-06-23T16:35:19.037022Z" } }, "outputs": [ { "data": { "text/plain": [ "[Fraction(-1, 3), Fraction(2, 3)]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp.fit_fractions(metric=\"steps\")[\"line\"][\"fractions\"]" ] }, { "cell_type": "markdown", "id": "exp-closing-md", "metadata": {}, "source": [ "The estimator recovers `steps(n) = (2n - 1) / 3` -- slope `2/3`, intercept `-1/3` -- directly from the simulated data, closing the loop from a coloring rule to its exact convergence law.\n", "\n", "That is the end of the tour. From here:\n", "\n", "- **basics** -- your first campaigns, comparing simulation with theory, saving results;\n", "- **advanced** -- multi-dimensional sweeps, alternative topologies, varying the palette;\n", "- **estimation** -- hidden-term tests, inverse-power laws, and the fraction machinery in depth." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 5 }