Petri.Config (petri v0.2.2)

Copy Markdown View Source

Typed config validation for Petri.run/2.

Uses Zoi to validate the config map against a typed schema. Invalid configs return {:error, reasons}.

Config fields

Fields marked Required must always be present. The available fields depend on which :encoding you choose.

:binary encoding

  • :length (integer/0) - Required. Number of bits in the chromosome

  • :encoding (:binary) - Required.

  • :seed (integer/0) - Random seed for reproducible runs

  • :population_size (integer/0) - Required. Number of individuals in each generation

  • :max_generations (integer/0) - Maximum number of generations before stopping

  • :fitness_threshold (float/0) - Stop when best fitness reaches or exceeds this value

  • :stagnation_generations (integer/0) - Stop if no improvement after this many generations

  • :time_budget_ms (integer/0) - Maximum wall-clock time in milliseconds

  • :selection (:tournament | :roulette | :rank | :sus) - Required. Selection strategy The default value is :sus.

  • :tournament_size (integer/0) - Required. Number of individuals in each tournament (only for :tournament selection) The default value is 3.

  • :elite_count (integer/0) - Required. Number of top individuals preserved unchanged each generation The default value is 2.

  • :crossover_rate (float/0) - Required. Probability of applying crossover to a selected pair The default value is 0.9.

  • :mutation_rate (float/0) - Required. Probability of applying mutation to an offspring The default value is 0.1.

  • :initialization (:random) - Required. Initialization strategy The default value is :random.

  • :crossover (:single_point | :two_point | :uniform) - Required. Crossover operator The default value is :single_point.

  • :mutation (:bit_flip) - Required. Mutation operator The default value is :bit_flip.

  • :mutation_per_gene_rate (float/0) - Probability of flipping each individual bit

:real encoding

  • :encoding (:real) - Required.

  • :seed (integer/0) - Random seed for reproducible runs

  • :bounds (list of {float/0, float/0}) - Required. List of {lo, hi} tuples, one per gene, defining the search space

  • :population_size (integer/0) - Required. Number of individuals in each generation

  • :max_generations (integer/0) - Maximum number of generations before stopping

  • :fitness_threshold (float/0) - Stop when best fitness reaches or exceeds this value

  • :stagnation_generations (integer/0) - Stop if no improvement after this many generations

  • :time_budget_ms (integer/0) - Maximum wall-clock time in milliseconds

  • :selection (:tournament | :roulette | :rank | :sus) - Required. Selection strategy The default value is :sus.

  • :tournament_size (integer/0) - Required. Number of individuals in each tournament (only for :tournament selection) The default value is 3.

  • :elite_count (integer/0) - Required. Number of top individuals preserved unchanged each generation The default value is 2.

  • :crossover_rate (float/0) - Required. Probability of applying crossover to a selected pair The default value is 0.9.

  • :mutation_rate (float/0) - Required. Probability of applying mutation to an offspring The default value is 0.1.

  • :initialization (:random | :lhs) - Required. Initialization strategy The default value is :random.

  • :crossover (:blx_alpha | :sbx) - Required. Crossover operator The default value is :blx_alpha.

  • :mutation (:gaussian | :uniform) - Required. Mutation operator The default value is :gaussian.

  • :blx_alpha_param (float/0) - Required. Alpha parameter for BLX-α crossover (0.0 = average, 0.5 = midpoint) The default value is 0.5.

  • :sbx_eta (float/0) - Required. Distribution index for SBX crossover (higher = closer to parents) The default value is 2.0.

  • :gaussian_sigma (float/0) - Required. Standard deviation for Gaussian mutation (as fraction of bound width) The default value is 0.1.

  • :mutation_per_gene_rate (float/0) - Required. Probability of mutating each individual gene The default value is 1.0.

:permutation encoding

  • :encoding (:permutation) - Required.

  • :seed (integer/0) - Random seed for reproducible runs

  • :n (integer/0) - Required. Number of elements in the permutation

  • :population_size (integer/0) - Required. Number of individuals in each generation

  • :max_generations (integer/0) - Maximum number of generations before stopping

  • :fitness_threshold (float/0) - Stop when best fitness reaches or exceeds this value

  • :stagnation_generations (integer/0) - Stop if no improvement after this many generations

  • :time_budget_ms (integer/0) - Maximum wall-clock time in milliseconds

  • :selection (:tournament | :roulette | :rank | :sus) - Required. Selection strategy The default value is :sus.

  • :tournament_size (integer/0) - Required. Number of individuals in each tournament (only for :tournament selection) The default value is 3.

  • :elite_count (integer/0) - Required. Number of top individuals preserved unchanged each generation The default value is 2.

  • :crossover_rate (float/0) - Required. Probability of applying crossover to a selected pair The default value is 0.9.

  • :mutation_rate (float/0) - Required. Probability of applying mutation to an offspring The default value is 0.1.

  • :initialization (:random) - Required. Initialization strategy The default value is :random.

  • :crossover (:ox | :pmx | :cx) - Required. Crossover operator The default value is :pmx.

  • :mutation (:swap | :insert | :inversion) - Required. Mutation operator The default value is :swap.

:integer encoding

  • :encoding (:integer) - Required.

  • :seed (integer/0) - Random seed for reproducible runs

  • :bounds (list of {integer/0, integer/0}) - Required. List of {lo, hi} tuples, one per gene, defining the integer search space

  • :population_size (integer/0) - Required. Number of individuals in each generation

  • :max_generations (integer/0) - Maximum number of generations before stopping

  • :fitness_threshold (float/0) - Stop when best fitness reaches or exceeds this value

  • :stagnation_generations (integer/0) - Stop if no improvement after this many generations

  • :time_budget_ms (integer/0) - Maximum wall-clock time in milliseconds

  • :selection (:tournament | :roulette | :rank | :sus) - Required. Selection strategy The default value is :sus.

  • :tournament_size (integer/0) - Required. Number of individuals in each tournament (only for :tournament selection) The default value is 3.

  • :elite_count (integer/0) - Required. Number of top individuals preserved unchanged each generation The default value is 2.

  • :crossover_rate (float/0) - Required. Probability of applying crossover to a selected pair The default value is 0.9.

  • :mutation_rate (float/0) - Required. Probability of applying mutation to an offspring The default value is 0.1.

  • :initialization (:random) - Required. Initialization strategy The default value is :random.

  • :crossover (:blx_alpha | :two_point | :sbx) - Required. Crossover operator The default value is :blx_alpha.

  • :mutation (:gaussian | :uniform) - Required. Mutation operator The default value is :gaussian.

  • :blx_alpha_param (float/0) - Required. Alpha parameter for BLX-α crossover The default value is 0.5.

  • :sbx_eta (float/0) - Required. Distribution index for SBX crossover The default value is 2.0.

  • :gaussian_sigma (float/0) - Required. Standard deviation for Gaussian mutation (as fraction of bound width) The default value is 0.1.

  • :mutation_per_gene_rate (float/0) - Required. Probability of mutating each individual gene The default value is 1.0.

Example

iex> {:ok, config} = Petri.Config.parse(%{
...>   encoding: :binary,
...>   length: 8,
...>   population_size: 20,
...>   max_generations: 10
...> })
iex> config.encoding
:binary

iex> {:error, err} = Petri.Config.parse(%{
...>   encoding: :binary,
...>   length: 8,
...>   population_size: 0,
...>   max_generations: 10
...> })
iex> is_list(err)
true

Summary

Functions

Parses and validates a map against the config schema.

Functions

parse(config)

Parses and validates a map against the config schema.

Returns {:ok, typed_map} on success or {:error, err} on failure.