morphogen.design.search

Search algorithms for discrete design problems, and the inverse design of automata.

simulated_annealing and genetic_algorithm minimize a cost over any representation (bit strings, tile weights, rule tables…), given the moves. fit_rule searches the outer-totalistic rules (birth and survival sets) of a cellular automaton whose structures, grown from noise, match target morphological descriptors: volume fraction, interface density and two-point correlation function, measured on an image (e.g. a real microstructure, at the resolution of the automaton’s cells) or given.

class morphogen.design.search.SearchResult(best: Any, cost: float, evaluations: int, history: dict[str, list]=<factory>)[source]

Bases: object

Best solution found, its cost and the history of the search.

morphogen.design.search.simulated_annealing(x0, cost: Callable[[Any], float], neighbour: Callable[[Any, Generator], Any], rng: Generator, *, steps: int = 1000, t_start: float | None = None, t_end: float | None = None, callback=None) → SearchResult[source]

Minimize cost by Metropolis moves x -> neighbour(x, rng) with a geometric cooling from t_start to t_end (default: from the spread of the costs of 20 random moves, down to a thousandth of it).

morphogen.design.search.genetic_algorithm(population: list, cost: Callable[[Any], float], rng: Generator, *, mutate: Callable[[Any, Generator], Any], crossover: Callable[[Any, Any, Generator], Any] | None = None, generations: int = 50, elite: int = 2, tournament: int = 3, callback=None) → SearchResult[source]

Minimize cost with a generational genetic algorithm: the elite best individuals are kept, the others are children of parents chosen by tournaments of tournament individuals, by crossover (if given) then mutate.

morphogen.design.search.structure_descriptors(state, max_lag: int = 4, periodic: bool = True) → ndarray[source]

Volume fraction, interface density (fraction of unlike neighbour pairs) and two-point correlation rho(h) = (C(h) - f^2) / (f - f^2) at lags 1..max_lag (mean over the axes of more than one cell).

morphogen.design.search.descriptor_cost(d: ndarray, target: ndarray) → float[source]

Squared errors: relative for the volume fraction and the interface density, absolute for the correlation function.

class morphogen.design.search.RuleFit(rule: str, initial_fraction: float, cost: float, descriptors: ndarray, target: ndarray, evaluations: int, history: dict[str, list])[source]

Bases: object

Result of fit_rule().

morphogen.design.search.fit_rule(target, *, neighbourhood: str = 'moore', radius: float = 1, dims: int = 3, lattice: str = 'square', shape=(24, 24, 24), steps: int = 15, initial_fraction: float | None = None, population: int = 24, generations: int = 15, max_lag: int = 4, rng: Generator | None = None, callback=None) → RuleFit[source]

Rule B.../S... (and initial noise density) whose structures best match target.

target is a phase image (phase 1 is the automaton’s state 1) or an array of descriptors (see structure_descriptors()). Each candidate rule is run for steps synchronous updates from noise of density initial_fraction (searched too when not given) on a periodic lattice of shape cells, and scored by descriptor_cost(); a genetic algorithm searches the birth and survival sets. All candidates start from the same noise (common random numbers). Other lattice values than square run the candidates on a tiling of the box of shape cell sizes (morphogen.automata.tilings; a free patch for quasi-periodic tilings), rasterized at one voxel per cell size before their descriptors are measured.