An open API service indexing awesome lists of open source software.

https://github.com/tahernezhad/neuroevolution-graph-neural-network

A neuroevolutionary framework for structural optimization that trains Graph Neural Networks (GNNs) with a genetic algorithm.
https://github.com/tahernezhad/neuroevolution-graph-neural-network

evolutionary-algorithms genetic-algorithm gnn gnn-model graph-attention-networks graph-convolutional-networks graph-neural-networks graph-sage neuroevolution pytorch torch-geometric truss-optimization

Last synced: about 1 month ago
JSON representation

A neuroevolutionary framework for structural optimization that trains Graph Neural Networks (GNNs) with a genetic algorithm.

Awesome Lists containing this project

README

          

# 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 generation
- `best_network.pkl` — pickled best controller
- `results/best_devo.gif` — replayed developmental process (from `replay.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 Δ, edge Δ)
│ ├─ 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

1) Create a virtual environment and install dependencies (ensure your PyTorch & PyG wheels match CPU/CUDA):
```bash
python -m venv .venv
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_geometric` per your platform’s instructions.

---

## How to Run

### 1) Train (evolutionary loop)
```bash
python main.py
```
Defaults live in `main.py`:
```python
model_name = "gcn" # "gcn" | "gat" | "sage"
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
```bash
python replay.py
```
- Set `run_dir` at the bottom of `replay.py` to your latest `data//`.
- 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_name` among `gcn`, `gat`, `sage` in `main.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 via `WEIGHT_MIN/MAX` in `main.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

- [PyTorch](https://pytorch.org/)
- [TorchGeometric](https://pytorch-geometric.readthedocs.io/en/latest/)
---

## 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.pkl` exists under your chosen `run_dir`.