Neuroevolution for Graph Neural Networks (Truss Design)
Evolutionary optimisation of Graph Neural Networks (GCN/GAT/GraphSAGE, PyTorch-Geometric) for truss design.
A genetic algorithm (GA) evolves network weights using Simulated Binary Crossover (SBX) and Polynomial Mutation (PM), while a GNN controller iteratively "develops" a seed truss by adjusting node coordinates and member cross-sectional areas under fixed loads/reactions.
- Backbones: GCN (Graph Convolutional Network), GAT (Graph Attention Network), GraphSAGE (PyTorch-Geometric).
- Physics Loop: strain energy, volume, stresses computed per step; fitness = normalised strain energy + volume.
- EA Operators: SBX + PM.
- Provenance: Environment/organism logic adapted from the RIED project; this repo adds PyG models (GCN/GAT/SAGE), vectorised EA ops, and extra reward/visualisation utilities.
Reference codebase (RIED EvoDevo): https://gitlab.com/riedproject
Earlier NumPy GCN ideas were tested there. My repo re-implements the GNN controller in PyTorch-Geometric and extends it.
Quick Outputs
Training writes a timestamped folder under ./data/ with:
reward_plot.csv-- best & average reward per generationbest_network.pkl-- pickled best controllerresults/best_devo.gif-- replayed developmental process (fromreplay.py)results/gnn_evo_plot.jpg,results/gnn_devo_plot.jpg-- plots
Repository Structure
.
+- main.py # run evolutionary training
+- replay.py # visualise best controller; export GIF/CSVs/plots
+- src/
| +- genetic_algorithm.py # GA + tournament selection
| +- ea_utils.py # SBX + PM (vectorised, bound-aware)
| +- gnn_model.py # GCN/GAT/GraphSAGE heads (node D, edge D)
| +- network.py # wrapper: init, eval loop per organism
| +- organism.py # truss state, graph features, fitness, updates
| +- environment.py # statics: equilibrium, forces, strain energy, volume
| +- utils.py # run-dir helper, feature normaliser
+- data/ # autogenerated per run (plots, logs, pickles)
Installation
- Create a virtual environment and install dependencies (ensure your PyTorch & PyG wheels match CPU/CUDA):
source .venv/bin/activate # (Windows: .venv\Scripts\activate)
pip install numpy matplotlib imageio torch torch_geometric
If PyG fails, first install a matching PyTorch (CPU or CUDA), then
torch_geometricper your platform's instructions.
How to Run
1) Train (evolutionary loop)
Defaults live in main.py:
hidden_dim = 120
num_layers = 3
dropout_rate = 0.1
num_generations = 150
population_size = 512
num_devo_steps = 10
crossover_prob = 0.9
crossover_eta = 20.0
mutation_prob = 0.1
mutation_eta = 20.0
WEIGHT_MIN, WEIGHT_MAX = -5.0, 5.0
2) Replay the best controller
- Set
run_dirat the bottom ofreplay.pyto your latestdata/./ - Produces
results/best_devo.gif, CSVs for node positions/edge areas, and plots of strain energy/volume/total cost per developmental step.
Configuration Notes
- Model backbone: switch
model_nameamonggcn,gat,sageinmain.py. - Reward (fitness): sum of normalised strain energy + normalised volume (see
Organism.get_fitness). - EA operators: implemented on flattened parameter vectors for speed and consistent per-weight behaviour (
src/ea_utils.py). - Weight bounds: defaults to
[-5, 5]; tune viaWEIGHT_MIN/MAXinmain.py. - Seedling & loads: geometry, supports, and loads are defined in helpers within
main.py/replay.py.
Acknowledgements
This repository re-implements the controller in PyTorch-Geometric (GCN/GAT/GraphSAGE), adds vectorised SBX/Mutation, and expands logging/visualisation for truss development. Environment modelling concepts originate from the RIED project (GitLab): https://gitlab.com/riedproject
Troubleshooting
- PyG install issues: ensure your PyTorch and CUDA/CPU versions match PyG wheels.
- Singular matrix / unstable truss: large penalties are applied when the equilibrium system is ill-posed; adjust seedling geometry, supports, or loads.
- No replay output: confirm
best_network.pklexists under your chosenrun_dir.