The GA and the function
Lax637351.Algorithm · concepts/Lax637351/Algorithm.lean · lax-637351
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
For , let be its number of ones. For a jump size , the function is
The genetic algorithm with crossover probability maintains a population, a multiset of individuals from . One iteration, maximizing a fitness function , proceeds as follows:
- With probability , choose two parents uniformly at random with replacement from the population. Create by uniform crossover of and , which takes each bit from or independently with probability . Create the offspring by standard bit mutation of , which flips each bit independently with probability .
- Otherwise, choose a parent uniformly at random and create by standard bit mutation of . With avoidance of replications, if , then is discarded and the iteration ends.
- Add to the population and remove an individual with the lowest fitness, breaking ties uniformly at random.
Concept map
Lean source view on GitHub
| 1 | import Mathlib.Data.PNat.Basic |
| 2 | import Mathlib.Probability.Distributions.Uniform |
| 3 | import Mathlib.Probability.ProbabilityMassFunction.Constructions |
| 4 | |
| 5 | /-! |
| 6 | --- |
| 7 | title: The GA and the function |
| 8 | type: definition |
| 9 | --- |
| 10 | For , let be its number of ones. For a jump size , |
| 11 | the function is |
| 12 | |
| 13 | |
| 14 | |
| 15 | The genetic algorithm with crossover probability maintains a |
| 16 | population, a multiset of individuals from . One iteration, maximizing a |
| 17 | fitness function , proceeds as follows: |
| 18 | |
| 19 | 1. With probability , choose two parents uniformly at random with replacement |
| 20 | from the population. Create by uniform crossover of and , which takes each |
| 21 | bit from or independently with probability . Create the offspring by |
| 22 | standard bit mutation of , which flips each bit independently with probability . |
| 23 | 2. Otherwise, choose a parent uniformly at random and create by standard bit mutation |
| 24 | of . With avoidance of replications, if , then is discarded and the iteration |
| 25 | ends. |
| 26 | 3. Add to the population and remove an individual with the lowest fitness, breaking ties |
| 27 | uniformly at random. |
| 28 | -/ |
| 29 | |
| 30 | namespace Lax637351.Algorithm |
| 31 | |
| 32 | open scoped ENNReal Classical |
| 33 | |
| 34 | noncomputable section |
| 35 | |
| 36 | /-- A bit string in . `Fin n` indexes positions ; |
| 37 | `Fin 2` consists of the values zero and one. -/ |
| 38 | abbrev BitString (n : ℕ) := Fin n → Fin 2 |
| 39 | |
| 40 | /-- A population of positive size , listed as individuals with indices, so that equal |
| 41 | individuals count separately. An iteration writes the offspring into the slot of the deleted |
| 42 | individual, or leaves the population unchanged if the offspring itself is deleted or discarded; |
| 43 | up to the order of the individuals, this is adding it and then deleting. -/ |
| 44 | abbrev Population (n : ℕ) (μ : ℕ+) := Fin μ → BitString n |
| 45 | |
| 46 | /-- The number of one-bits in a string. -/ |
| 47 | def ones {n : ℕ} (x : BitString n) : ℕ := |
| 48 | ∑ j : Fin n, (x j : ℕ) |
| 49 | |
| 50 | /-- The function with jump size `k`, usually considered for and |
| 51 | . Fitness values are real numbers; natural-number subtraction is truncated |
| 52 | at zero. -/ |
| 53 | def 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 , the unique optimum of when . -/ |
| 57 | def optimum (n : ℕ) : BitString n := fun _ ↦ 1 |
| 58 | |
| 59 | /-- A coin flip, `true` with probability for . |
| 60 | Outside this interval it always returns `false`; all algorithm claims use valid probabilities. -/ |
| 61 | def 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 : a string has |
| 69 | probability for . Here `coin p (x j = 1)` is if |
| 70 | and if . -/ |
| 71 | def 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. |
| 82 | Sampling the mask uniformly among all bit strings gives independent fair choices. -/ |
| 83 | def 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. -/ |
| 87 | def 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 individuals of |
| 91 | the population with the offspring added. Such an individual exists because `Q` is finite and |
| 92 | nonempty. -/ |
| 93 | def 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 GA, maximizing `fitness`. |
| 101 | Crossover parents are independent uniform draws with replacement; the parent in a |
| 102 | mutation-only iteration is uniform. Mutation flips each bit independently with probability . |
| 103 | The offspring is inserted even if it equals an existing individual, before a worst individual is |
| 104 | deleted. If `avoidReplications` is `true`, an offspring of a mutation-only iteration that equals |
| 105 | its parent is discarded instead. |
| 106 | Each `←` draws a sample from the distribution on its right. -/ |
| 107 | def 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 | |
| 131 | end |
| 132 | end Lax637351.Algorithm |
| 133 |
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments