morphogen.design.csp

Constraint solvers for tile assemblies.

wave_function_collapse

Random compatible assemblies of any size: arc consistency (each tile left in the domain of a cell has a compatible tile in every neighbouring domain), then cells of least entropy are given a tile at random (with the tile weights) and the choice is propagated, backtracking on contradictions (domains are bit sets, Numba).

TransferMatrix

Exact counting and exact uniform sampling of the compatible assemblies of a box, with a transfer matrix between the admissible cross-sections along x (statistical physics). It needs few admissible cross-sections: small sections, or constrained tile sets.

count_polarized_blocks

Exact number of compatible assemblies of polarized blocks, for larger boxes: the strain sign along every line of blocks alternates, which reduces the problem to sign variables on the faces of the box (a gauge), summed by dynamic programming.

morphogen.design.csp.initial_domains(tileset: TileSet, shape, *, periodic: bool = True, boundary=None, allowed=None) → ndarray[source]

Boolean domains (n0, n1, n2, n_tiles): tiles of positive weight, restricted by allowed (same shape) and, on the faces of non-periodic boxes, to tiles whose outer faces match the label boundary.

morphogen.design.csp.wave_function_collapse(tileset: TileSet, shape, rng: Generator, *, periodic: bool = True, boundary: int | None = None, allowed=None, max_backtracks: int | None = None, restarts: int = 5) → ndarray[source]

A random compatible assembly of shape tiles ((n0, n1, 1) for planar sets).

allowed (boolean, shape + (n_tiles,)) restricts the tiles of each cell, e.g. to grade a design in space; boundary is the label the outer faces of a non-periodic box must match. Raises ValueError when no assembly exists, or when the search gave up after restarts attempts of max_backtracks backtracks.

class morphogen.design.csp.TransferMatrix(tileset: TileSet, section, *, periodic: bool = False, max_states: int = 20000)[source]

Bases: object

Admissible cross-sections (n1, n2) of a tile set and the transfer matrix between consecutive sections along x (T[s, s'] = 1 when section s’ may follow section s).

Counting and sampling use exact integer arithmetic. Periodic boxes need the closed walks of the transfer matrix (one sum per section): keep the number of sections modest (max_states).

property n_states: int
count(length: int) → int[source]

Number of compatible assemblies of length sections (exact).

sample(length: int, rng: Generator) → ndarray[source]

An assembly (length, n1, n2) drawn uniformly among the compatible ones.

morphogen.design.csp.count_assemblies(tileset: TileSet, shape, *, periodic: bool = False, max_states: int = 20000) → int[source]

Exact number of compatible assemblies of a box of shape tiles.

morphogen.design.csp.sample_uniform(tileset: TileSet, shape, rng: Generator, *, periodic: bool = False, max_states: int = 20000) → ndarray[source]

A compatible assembly drawn uniformly at random (exact, by transfer matrix).

morphogen.design.csp.count_polarized_blocks(shape, *, periodic: bool = False) → int[source]

Number of compatible assemblies of polarized blocks in a box of shape blocks.

Writing the strain signs of the block (i, j, k) as e_x = F_x(j, k) (-1)^i, e_y = F_y(i, k) (-1)^j, e_z = F_z(i, j) (-1)^k (alternation along every line), an assembly is a choice of the sign arrays such that no block has three equal signs. The sum over F_z factorizes over the columns, then over F_y over the rows i, which leaves a sum over F_x (2^(ny nz) terms, the smallest section is chosen). Periodic boxes need even numbers of blocks. Exact up to 2^62.