-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
79 lines (64 loc) · 2.5 KB
/
example.py
File metadata and controls
79 lines (64 loc) · 2.5 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
"""
Minimal Python example for the Genetic Algorithm framework.
Optimises the Rastrigin function (minimisation → maximisation via 1000/(1+f)).
Global minimum is at origin where f=0, giving fitness ≈ 1000.
"""
from __future__ import annotations
import math
import sys
import os
try:
import genetic_algorithm_lib as ga
except ImportError:
# Allow running from the source tree after a local CMake build.
repo_root = os.path.join(os.path.dirname(__file__), "..")
sys.path.insert(0, os.path.join(repo_root, "python"))
sys.path.insert(0, os.path.join(repo_root, "build", "python"))
import genetic_algorithm_lib as ga
def rastrigin_fitness(x: list[float]) -> float:
"""Rastrigin function converted to a maximisation objective."""
A = 10.0
val = A * len(x) + sum(xi**2 - A * math.cos(2 * math.pi * xi) for xi in x)
return 1000.0 / (1.0 + val)
def main():
cfg = ga.Config()
cfg.population_size = 60
cfg.generations = 200
cfg.dimension = 10
cfg.bounds = ga.Bounds(-5.12, 5.12)
cfg.mutation_rate = 0.05
cfg.crossover_rate = 0.8
cfg.elite_ratio = 0.05
engine = ga.GeneticAlgorithm(cfg)
print("Running Genetic Algorithm on the Rastrigin function...")
result = engine.run(rastrigin_fitness)
print(f"Best fitness : {result.best_fitness:.4f}")
print(f"Best genes : {[f'{g:.4f}' for g in result.best_genes]}")
print(f"Generations : {len(result.best_history)}")
# Show convergence trend (every 50 generations)
print("\nConvergence (best fitness per 50 generations):")
for i, f in enumerate(result.best_history):
if i % 50 == 0 or i == len(result.best_history) - 1:
print(f" gen {i:3d}: {f:.4f}")
# NSGA-III utility demo in objective space
objectives = [
[0.02, 1.20],
[0.10, 0.80],
[0.35, 0.45],
[0.90, 0.10],
]
refs = ga.nsga3_reference_points(2, 4)
picked = ga.nsga3_environmental_select_indices(objectives, 2, refs)
print(f"\nNSGA-III selected objective indices: {picked}")
# Checkpoint JSON demo
state = ga.CheckpointState()
state.config = cfg
state.result = result
state.generation = len(result.best_history) - 1
state.rng_state = "python-demo"
cp_path = os.path.join(os.path.dirname(__file__), "..", "build", "python_checkpoint.json")
ga.checkpoint_save_json(cp_path, state)
loaded = ga.checkpoint_load_json(cp_path)
print(f"Checkpoint JSON roundtrip generation: {loaded.generation}")
if __name__ == "__main__":
main()