-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevolution_utils.py
More file actions
157 lines (117 loc) Β· 4.54 KB
/
evolution_utils.py
File metadata and controls
157 lines (117 loc) Β· 4.54 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
"""
Evolutionary AI utilities.
Usage:
python evolution_utils.py <command> [options]
"""
import sys
from pathlib import Path
try:
import codomyrmex
except ImportError:
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
import argparse
import random
def create_population(size: int, genome_length: int = 10) -> list:
"""Create initial population."""
return [[random.randint(0, 1) for _ in range(genome_length)] for _ in range(size)]
def evaluate_fitness(genome: list) -> float:
"""Simple fitness function (count ones)."""
return sum(genome) / len(genome)
def select_parents(population: list, fitnesses: list, count: int = 2) -> list:
"""Tournament selection."""
parents = []
for _ in range(count):
candidates = random.sample(
list(zip(population, fitnesses, strict=False)), min(3, len(population))
)
winner = max(candidates, key=lambda x: x[1])
parents.append(winner[0])
return parents
def crossover(parent1: list, parent2: list) -> list:
"""Single-point crossover."""
point = random.randint(1, len(parent1) - 1)
return parent1[:point] + parent2[point:]
def mutate(genome: list, rate: float = 0.1) -> list:
"""Bit-flip mutation."""
return [1 - g if random.random() < rate else g for g in genome]
def run_evolution(generations: int = 10, pop_size: int = 20) -> dict:
"""Run evolutionary algorithm."""
population = create_population(pop_size)
history = []
for gen in range(generations):
fitnesses = [evaluate_fitness(g) for g in population]
best_fitness = max(fitnesses)
avg_fitness = sum(fitnesses) / len(fitnesses)
history.append({"gen": gen, "best": best_fitness, "avg": avg_fitness})
# Create new population
new_pop = []
while len(new_pop) < pop_size:
parents = select_parents(population, fitnesses)
child = crossover(parents[0], parents[1])
child = mutate(child)
new_pop.append(child)
population = new_pop
return {
"generations": generations,
"history": history,
"final_best": max(fitnesses),
}
def main():
# Auto-injected: Load configuration
from pathlib import Path
import yaml
config_path = (
Path(__file__).resolve().parent.parent.parent
/ "config"
/ "evolutionary_ai"
/ "config.yaml"
)
if config_path.exists():
with open(config_path) as f:
yaml.safe_load(f) or {}
print("Loaded config from config/evolutionary_ai/config.yaml")
parser = argparse.ArgumentParser(description="Evolutionary AI utilities")
subparsers = parser.add_subparsers(dest="command")
# Run command
run = subparsers.add_parser("run", help="Run evolution")
run.add_argument("--generations", "-g", type=int, default=10)
run.add_argument("--population", "-p", type=int, default=20)
# Demo command
subparsers.add_parser("demo", help="Demo evolution operators")
args = parser.parse_args()
if not args.command:
print("𧬠Evolutionary AI Utilities\n")
print("Commands:")
print(" run - Run evolutionary algorithm")
print(" demo - Demo evolution operators")
return 0
if args.command == "run":
print("𧬠Running Evolution\n")
print(f" Generations: {args.generations}")
print(f" Population: {args.population}\n")
result = run_evolution(args.generations, args.population)
print(" Progress:")
for h in result["history"][:: max(1, len(result["history"]) // 5)]:
bar = "β" * int(h["best"] * 20)
print(f" Gen {h['gen']:3d}: {bar} {h['best']:.2f}")
print(f"\n Final best: {result['final_best']:.2f}")
elif args.command == "demo":
print("𧬠Evolution Demo\n")
print(" Create population:")
pop = create_population(3, 8)
for i, g in enumerate(pop):
print(f" {i}: {''.join(map(str, g))}")
print("\n Crossover:")
child = crossover(pop[0], pop[1])
print(f" P1: {''.join(map(str, pop[0]))}")
print(f" P2: {''.join(map(str, pop[1]))}")
print(f" C: {''.join(map(str, child))}")
print("\n Mutation:")
mutated = mutate(child, 0.3)
print(f" Before: {''.join(map(str, child))}")
print(f" After: {''.join(map(str, mutated))}")
return 0
if __name__ == "__main__":
sys.exit(main())