File size: 4,133 Bytes
05b9f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
evolution_engine/belel_device.py

Quantum-inspired evolutionary search (practical GA with ingenuity bias).

Public API:
    belel_evolve(fitness_fn, genome_length, pop_size=..., generations=..., ingenuity_boost=..., seed=...)
Returns:
    (best_genome, best_score)
"""

from __future__ import annotations

import math
import random
from dataclasses import dataclass
from typing import Callable, List, Tuple, Optional


Genome = List[int]
FitnessFn = Callable[[Genome], float]


@dataclass
class EvoConfig:
    genome_length: int
    pop_size: int = 32
    generations: int = 60
    mutation_rate: float = 0.12
    crossover_rate: float = 0.65
    tournament_k: int = 3
    ingenuity_boost: float = 1.5
    allele_min: int = 0
    allele_max: int = 9
    seed: Optional[int] = None


def _novelty_score(genome: Genome) -> float:
    # Simple novelty heuristic: diversity within the genome.
    # Scales gently to avoid dwarfing performance fitness.
    if not genome:
        return 0.0
    uniq = len(set(genome))
    return math.log1p(uniq) * 2.0


def _init_pop(cfg: EvoConfig) -> List[Genome]:
    r = random.Random(cfg.seed)
    return [
        [r.randint(cfg.allele_min, cfg.allele_max) for _ in range(cfg.genome_length)]
        for _ in range(cfg.pop_size)
    ]


def _tournament_select(r: random.Random, pop: List[Genome], scores: List[float], k: int) -> Genome:
    idxs = [r.randrange(len(pop)) for _ in range(k)]
    best_i = max(idxs, key=lambda i: scores[i])
    return pop[best_i]


def _crossover(r: random.Random, a: Genome, b: Genome) -> Tuple[Genome, Genome]:
    if len(a) != len(b) or len(a) < 2:
        return a[:], b[:]
    cut = r.randint(1, len(a) - 1)
    return a[:cut] + b[cut:], b[:cut] + a[cut:]


def _mutate(r: random.Random, cfg: EvoConfig, g: Genome) -> Genome:
    out = g[:]
    for i in range(len(out)):
        if r.random() < cfg.mutation_rate:
            # Slightly "quantum-ish" mutation: either random jump or local tweak
            if r.random() < 0.55:
                out[i] = r.randint(cfg.allele_min, cfg.allele_max)
            else:
                step = r.choice([-2, -1, 1, 2])
                out[i] = max(cfg.allele_min, min(cfg.allele_max, out[i] + step))
    return out


def _ingenuity_adjusted_fitness(fitness_fn: FitnessFn, genome: Genome, ingenuity_boost: float) -> float:
    base = float(fitness_fn(genome))
    novelty = _novelty_score(genome)
    return base + (novelty * float(ingenuity_boost))


def belel_evolve(
    fitness_fn: FitnessFn,
    genome_length: int,
    pop_size: int = 32,
    generations: int = 60,
    ingenuity_boost: float = 1.5,
    seed: Optional[int] = None,
) -> Tuple[Genome, float]:
    cfg = EvoConfig(
        genome_length=genome_length,
        pop_size=pop_size,
        generations=generations,
        ingenuity_boost=ingenuity_boost,
        seed=seed,
    )
    r = random.Random(cfg.seed)

    pop = _init_pop(cfg)

    best_g: Genome = pop[0][:]
    best_s: float = float("-inf")

    for _gen in range(cfg.generations):
        scores = [
            _ingenuity_adjusted_fitness(fitness_fn, g, cfg.ingenuity_boost) for g in pop
        ]

        # Track best
        gen_best_i = max(range(len(pop)), key=lambda i: scores[i])
        if scores[gen_best_i] > best_s:
            best_s = scores[gen_best_i]
            best_g = pop[gen_best_i][:]

        # Elitism: keep top 2
        elite_pairs = sorted(zip(pop, scores), key=lambda x: x[1], reverse=True)[:2]
        next_pop: List[Genome] = [elite_pairs[0][0][:], elite_pairs[1][0][:]]

        # Breed the rest
        while len(next_pop) < cfg.pop_size:
            p1 = _tournament_select(r, pop, scores, cfg.tournament_k)
            p2 = _tournament_select(r, pop, scores, cfg.tournament_k)

            c1, c2 = p1[:], p2[:]
            if r.random() < cfg.crossover_rate:
                c1, c2 = _crossover(r, p1, p2)

            c1 = _mutate(r, cfg, c1)
            c2 = _mutate(r, cfg, c2)

            next_pop.append(c1)
            if len(next_pop) < cfg.pop_size:
                next_pop.append(c2)

        pop = next_pop

    return best_g, best_s