A Python implementation of networked genetic algorithms that use genotypic and phenotypic similarity to form scale-free inter-chromosomal links, helping balance exploration and exploitation during evolution.
Animated evolution of node rankings across generations. Node colors indicate fitness ranking: green (top 2), teal (next 10), blue-green (next 15), and dark blue (others). Labels show node scores.
This repository contains several GA variants (including ENGA, NGA, GA, CRGA, DCGA) and a small benchmarking harness to compare them on common test functions.
- Clone the repository:
git clone https://github.com/yazid-hoblos/ENGA.git
cd ENGA- Install requirements:
pip install -r requirements.txt- Run the main benchmark script:
python src/main.py --run allCommand-line options
-
--run— select which benchmark(s) to execute. Accepts one or more of the following values:enhanced,networked,ga,crga,dcga, orall. -
--save-metrics— optional flag. When present, the benchmarks will save metrics (fitness, diversity, selection pressure, etc.) intologs/metrics/. This flag is off by default. -
--generations N— set the number of generations (overrides per-benchmark default). Default (if omitted): 2000. -
--population-size N— set the population size for all benchmarks. Default (if omitted): 125 (124 for CRGA in original config). -
--number-elites N— number of elites to keep. Default (per-benchmark): 0 for most algorithms, 10 for the plain GA. -
--crossover-prob P— crossover probability (0.0–1.0). Default: 0.95. -
--mutation-prob P— mutation probability (0.0–1.0). Default: 0.10. -
--nruns N— number of independent runs to perform. Default: 1. -
--no-verbose— disable verbose output. Default: verbose on.
Examples:
# run all benchmarks
python src/main.py --run all
# run only the enhanced networked GA
python src/main.py --run enhanced
# run multiple specified benchmarks (enhanced and plain ga)
python src/main.py --run enhanced gaKey files (located in src/):
main.py— example entry point and benchmark orchestration.genetic_algorithm.py— base GA class and shared utilities.networked_genetic_algorithm.py,enhanced_networked_genetic_algorithm.py,networked_assignment.py— networked GA variants and helpers.benchmark.py,benchmark_functions.py— benchmarking harness and test functions.crossover.py,mutation.py,selection.py,networked_selection.py— GA operators.utils/— small utility modules (math.py,drawable.py).
Output and logs:
logs/metrics/— saved metrics (fitness, diversity, selection pressure, ...)logs/results/— benchmark run outputs and archived results
This project can save a rich set of metrics and visualizations from each benchmark run. Metrics are written under logs/metrics/ when enabled (see below). Keep in mind metrics (especially animated GIFs) can consume disk and I/O during long runs — enable them only when you need the outputs.
logs/metrics/fitness/— PNG plots of average and best fitness over generations.logs/metrics/phenotype_diversity/— PNG plots showing phenotype diversity over time.logs/metrics/genotype_diversity/— PNG plots for genotype-space diversity.logs/metrics/selection_pressure/— PNG plots tracking selection pressure each generation.logs/metrics/degree/,logs/metrics/degree_exponent/,logs/metrics/clustering_coefficient/,logs/metrics/path_length/— network-related PNG visualizations.logs/metrics/number_of_nodes_to_remove/— PNG plots for node-removal statistics.logs/metrics/animated_networks/— optional animated network visualizations saved as GIF files.
- From the command line: pass
--save-metricstosrc/main.pyto enable metric saving for benchmark runs (this toggles thesave_metricsflag whenBenchmarkobjects are created). - Programmatically: set
save_metrics=Truewhen constructing aBenchmark(...)or callDrawManager.draw_animated_network(history, save=True)to save an animation explicitly.
As highlighted in the animation above, in the network architecture, high‑fitness individuals migrate from the periphery to the center as evolution creates hubs — node color shows fitness and centrality reflects accumulated connections. This highlights selection pressure, hub formation, and convergence.
from benchmark import Benchmark
from benchmark_functions import get_all_functions
from enhanced_networked_genetic_algorithm import EnhancedNetworkGeneticAlgorithm
benchmark_functions = get_all_functions()
enhanced_nga_benchmark = Benchmark(
genetic_algorithm=EnhancedNetworkGeneticAlgorithm,
number_of_runs=1,
benchmark_functions=benchmark_functions,
number_of_generations=2000,
population_size=125,
number_elites=0,
probability_crossover=0.95,
probability_mutation=0.1,
verbose=True,
save_metrics=True,
)
enhanced_nga_benchmark.run()See requirements.txt for Python package dependencies. The project expects Python 3.8+ (recommend 3.10+).
Contributions are welcome. If you'd like to contribute:
- Fork the repo and create a feature branch.
- Add tests or examples for new behaviour when applicable.
- Open a pull request describing your change.
If you plan to work on an issue, comment on it to avoid duplicated effort.
