The (μ+1)(\mu+1) GA and the function Jumpk\mathrm{Jump}_k

Lax637351.Algorithm · concepts/Lax637351/Algorithm.lean · lax-637351

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural Language Statement

    Definition

    For x∈{0,1}nx\in\{0,1\}^n, let ∣x∣1|x|_1 be its number of ones. For a jump size k∈{1,…,n}k\in\{1,\dots,n\}, the function Jumpk ⁣:{0,1}n→N\mathrm{Jump}_k\colon\{0,1\}^n\to\mathbb N is

    Jumpk(x)={k+∣x∣1if ∣x∣1≤n−k or ∣x∣1=n,n−∣x∣1otherwise.\mathrm{Jump}_k(x)=\begin{cases}k+|x|_1 & \text{if } |x|_1\le n-k \text{ or } |x|_1=n,\\ n-|x|_1 & \text{otherwise.}\end{cases}

    The (μ+1)(\mu+1) genetic algorithm with crossover probability pc∈[0,1]p_c\in[0,1] maintains a population, a multiset of μ\mu individuals from {0,1}n\{0,1\}^n. One iteration, maximizing a fitness function ff, proceeds as follows:

    1. With probability pcp_c, choose two parents y1,y2y_1,y_2 uniformly at random with replacement from the population. Create yy by uniform crossover of y1y_1 and y2y_2, which takes each bit from y1y_1 or y2y_2 independently with probability 1/21/2. Create the offspring zz by standard bit mutation of yy, which flips each bit independently with probability 1/n1/n.
    2. Otherwise, choose a parent yy uniformly at random and create zz by standard bit mutation of yy. With avoidance of replications, if z=yz=y, then zz is discarded and the iteration ends.
    3. Add zz to the population and remove an individual with the lowest fitness, breaking ties uniformly at random.
    Concept map
    1 concept; 6 descendants hidden
    100%
    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A

    Lean source view on GitHub

    1import Mathlib.Data.PNat.Basic
    2import Mathlib.Probability.Distributions.Uniform
    3import Mathlib.Probability.ProbabilityMassFunction.Constructions
    4
    5/-!
    6---
    7title: The (μ+1)(\mu+1) GA and the function Jumpk\mathrm{Jump}_k
    8type: definition
    9---
    10For x∈{0,1}nx\in\{0,1\}^n, let ∣x∣1|x|_1 be its number of ones. For a jump size k∈{1,…,n}k\in\{1,\dots,n\},
    11the function Jumpk ⁣:{0,1}n→N\mathrm{Jump}_k\colon\{0,1\}^n\to\mathbb N is
    12Jumpk(x)={k+∣x∣1if ∣x∣1≤n−k or ∣x∣1=n,n−∣x∣1otherwise.\mathrm{Jump}_k(x)=\begin{cases}k+|x|_1 & \text{if } |x|_1\le n-k \text{ or } |x|_1=n,\\ n-|x|_1 & \text{otherwise.}\end{cases}
    13
    14
    15The (μ+1)(\mu+1) genetic algorithm with crossover probability pc∈[0,1]p_c\in[0,1] maintains a
    16population, a multiset of μ\mu individuals from {0,1}n\{0,1\}^n. One iteration, maximizing a
    17fitness function ff, proceeds as follows:
    18
    191. With probability pcp_c, choose two parents y1,y2y_1,y_2 uniformly at random with replacement
    20 from the population. Create yy by uniform crossover of y1y_1 and y2y_2, which takes each
    21 bit from y1y_1 or y2y_2 independently with probability 1/21/2. Create the offspring zz by
    22 standard bit mutation of yy, which flips each bit independently with probability 1/n1/n.
    232. Otherwise, choose a parent yy uniformly at random and create zz by standard bit mutation
    24 of yy. With avoidance of replications, if z=yz=y, then zz is discarded and the iteration
    25 ends.
    263. Add zz to the population and remove an individual with the lowest fitness, breaking ties
    27 uniformly at random.
    28-/
    29
    30namespace Lax637351.Algorithm
    31
    32open scoped ENNReal Classical
    33
    34noncomputable section
    35
    36/-- A bit string in {0,1}n\{0,1\}^n. `Fin n` indexes positions 0,…,n−10,\ldots,n-1;
    37`Fin 2` consists of the values zero and one. -/
    38abbrev BitString (n : ℕ) := Fin n → Fin 2
    39
    40/-- A population of positive size μ\mu, listed as μ\mu individuals with indices, so that equal
    41individuals count separately. An iteration writes the offspring into the slot of the deleted
    42individual, or leaves the population unchanged if the offspring itself is deleted or discarded;
    43up to the order of the individuals, this is adding it and then deleting. -/
    44abbrev Population (n : ℕ) (μ : ℕ+) := Fin μ → BitString n
    45
    46/-- The number of one-bits in a string. -/
    47def ones {n : ℕ} (x : BitString n) : ℕ :=
    48 ∑ j : Fin n, (x j : ℕ)
    49
    50/-- The function Jumpk\mathrm{Jump}_k with jump size `k`, usually considered for n≥2n\ge2 and
    511≤k≤n/21\le k\le n/2. Fitness values are real numbers; natural-number subtraction is truncated
    52at zero. -/
    53def jump {n : ℕ} (k : ℕ) (x : BitString n) : ℝ :=
    54 if ones x ≤ n - k ∨ ones x = n then (k + ones x : ℕ) else (n - ones x : ℕ)
    55
    56/-- The all-one string 1n1^n, the unique optimum of Jumpk\mathrm{Jump}_k when 1≤k≤n1\le k\le n. -/
    57def optimum (n : ℕ) : BitString n := fun _ ↦ 1
    58
    59/-- A coin flip, `true` with probability pp for 0≤p≤10\le p\le1.
    60Outside this interval it always returns `false`; all algorithm claims use valid probabilities. -/
    61def coin (p : ℝ) : PMF Bool :=
    62 if h : 0 ≤ p ∧ p ≤ 1 then
    63 PMF.ofFintype (fun b : Bool ↦ ENNReal.ofReal (if b then p else 1 - p))
    64 -- Lean checks this proof that the probabilities sum to one; readers can skip it.
    65 (by simp_all [← ENNReal.ofReal_add])
    66 else PMF.pure false
    67
    68/-- Independent random bits, each equal to one with probability pp: a string xx has
    69probability p∣x∣1(1−p)n−∣x∣1p^{|x|_1}(1-p)^{n-|x|_1} for 0≤p≤10\le p\le1. Here `coin p (x j = 1)` is pp if
    70xj=1x_j=1 and 1−p1-p if xj=0x_j=0. -/
    71def independentBits (n : ℕ) (p : ℝ) : PMF (BitString n) :=
    72 PMF.ofFintype (fun x ↦ ∏ j, coin p (x j = 1))
    73 -- Lean checks this proof that the probabilities sum to one; readers can skip it.
    74 (by
    75 have h := (coin p).tsum_coe
    76 rw [tsum_fintype] at h
    77 rw [← Fintype.prod_sum (fun (_ : Fin n) (b : Fin 2) ↦ coin p (b = 1))]
    78 simp [Fin.sum_univ_two, add_comm] at h ⊢
    79 simp [h])
    80
    81/-- Uniform crossover: at each position an independent fair mask bit selects one parent.
    82Sampling the mask uniformly among all bit strings gives independent fair choices. -/
    83def crossover {n : ℕ} (x y mask : BitString n) : BitString n :=
    84 fun j ↦ if mask j = 1 then x j else y j
    85
    86/-- Flip exactly the positions where the mutation mask is one. -/
    87def mutate {n : ℕ} (x mask : BitString n) : BitString n :=
    88 fun j ↦ if mask j = 1 then 1 - x j else x j
    89
    90/-- Uniform deletion among the individuals of minimal `fitness` in `Q`, the μ+1\mu+1 individuals of
    91the population with the offspring added. Such an individual exists because `Q` is finite and
    92nonempty. -/
    93def deletionIndex {n : ℕ} (fitness : BitString n → ℝ) {μ : ℕ+}
    94 (Q : Fin ((μ : ℕ) + 1) → BitString n) : PMF (Fin ((μ : ℕ) + 1)) :=
    95 PMF.uniformOfFinset
    96 (Finset.univ.filter fun i ↦ ∀ j, fitness (Q i) ≤ fitness (Q j))
    97 -- Lean checks this proof that a minimal individual exists; readers can skip it.
    98 (by simpa [Finset.filter_nonempty_iff] using Finite.exists_min fun j ↦ fitness (Q j))
    99
    100/-- One iteration of the (μ+1)(\mu+1) GA, maximizing `fitness`.
    101Crossover parents are independent uniform draws with replacement; the parent in a
    102mutation-only iteration is uniform. Mutation flips each bit independently with probability 1/n1/n.
    103The offspring is inserted even if it equals an existing individual, before a worst individual is
    104deleted. If `avoidReplications` is `true`, an offspring of a mutation-only iteration that equals
    105its parent is discarded instead.
    106Each `←` draws a sample from the distribution on its right. -/
    107def step {n : ℕ} (fitness : BitString n → ℝ) {μ : ℕ+} (p_c : ℝ) (avoidReplications : Bool)
    108 (P : Population n μ) : PMF (Population n μ) := do
    109 let useCrossover ← coin p_c
    110 -- `y` is the uniform crossover of two parents (if branch) or a single parent (else branch).
    111 let y ← if useCrossover then do
    112 let (u, v) ← PMF.uniformOfFintype (Fin μ × Fin μ)
    113 let crossMask ← PMF.uniformOfFintype (BitString n)
    114 pure (crossover (P u) (P v) crossMask)
    115 else do
    116 let u ← PMF.uniformOfFintype (Fin μ)
    117 pure (P u)
    118 let flipMask ← independentBits n (1 / (n : ℝ))
    119 let z := mutate y flipMask
    120 -- Avoidance of replications: discard a mutation-only offspring equal to its parent y.
    121 if avoidReplications ∧ ¬useCrossover ∧ z = y then
    122 pure P
    123 else
    124 -- `Q` lists the μ + 1 individuals: the population `P`, followed by the offspring `z`.
    125 let Q := Fin.lastCases z P
    126 let d ← deletionIndex fitness Q
    127 -- Deleting an old individual puts z into its slot; deleting the offspring (the last
    128 -- individual in `Q`) leaves P unchanged.
    129 pure (if h : d.val < (μ : ℕ) then Function.update P ⟨d.val, h⟩ z else P)
    130
    131end
    132end Lax637351.Algorithm
    133

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…