An evolutionary optimization project for automatically tuning the gains of a PID controller applied to a simulated DC motor.
The project uses real-valued genetic algorithms to optimize the controller parameters (K_p), (K_i), and (K_d), evaluates multiple fitness formulations and genetic operators, compares the resulting controller with classical tuning methods, and performs extensive robustness and validation experiments.
The final experiments show that evolutionary tuning can produce a highly effective PI/PID controller with very low overshoot, negligible steady-state error, and fast settling time.
PID tuning is traditionally performed using analytical or empirical methods such as:
- Ziegler–Nichols
- Cohen–Coon
- Manual tuning
However, these approaches do not always produce the best controller for a particular performance objective.
This project formulates PID tuning as an optimization problem.
Each candidate controller is represented by a chromosome:
[ [K_p, K_i, K_d] ]
A genetic algorithm evolves these gains according to the simulated response of a DC motor.
The optimization considers several control-performance metrics:
- Overshoot
- Steady-state error
- Settling time
- Decay ratio
- Control effort
- Robustness
The project includes a discrete simulation of a DC motor.
The main physical parameters are:
| Parameter | Value |
|---|---|
| Resistance (R) | 1.11 Ω |
| Inductance (L) | 0.0002 H |
| Inertia (J) | (6.77 \times 10^{-6}) kg·m² |
| Viscous friction (f_v) | (1.66 \times 10^{-5}) Nm/(rad/s) |
| Motor constant (K) | 0.0364 |
The motor voltage is limited to:
[ [-24, 24] \text{ V} ]
to simulate actuator saturation.
The implemented controller follows the classical structure:
[ u(t) = K_p e(t) + K_i \int e(t),dt + K_d \frac{de(t)}{dt} ]
where:
- (K_p) controls the proportional response
- (K_i) eliminates persistent steady-state error
- (K_d) reacts to variations in the error
The main optimization experiments use a velocity reference of:
50 rad/s
and simulate:
320 controller cycles
for every candidate chromosome.
Each candidate solution is a real-valued chromosome:
[Kp, Ki, Kd]
The search bounds are:
Kp ∈ [0, 2]
Ki ∈ [0, 30]
Kd ∈ [0, 2]
This representation avoids binary encoding and allows evolutionary operators to work directly in the continuous parameter space.
The project implements a complete real-valued genetic algorithm including:
- Random population initialization
- Fitness evaluation
- Tournament selection
- BLX-α crossover
- Mutation
- Elitism
- Population replacement
The main algorithm is implemented in the Genetico class.
Selection is performed using tournament selection.
The default tournament size is:
T = 3
Several individuals are randomly sampled from the population and the one with the highest fitness becomes a parent.
This creates evolutionary pressure while maintaining population diversity.
The base implementation uses Blend Crossover (BLX-α) for real-valued chromosomes.
For two parent values:
[ x_1, x_2 ]
the offspring can be sampled from an expanded interval around the parents.
The implementation uses:
α = 0.5
allowing offspring to explore values both inside and slightly outside the interval defined by their parents.
Generated values are clipped to the permitted parameter bounds.
The original fitness combines several control-performance metrics.
The cost is approximately:
[ C = w_o \cdot Overshoot + w_e \cdot E_{ss} + w_t \cdot T_s + w_d \cdot d ]
and is converted into fitness using:
[ fitness = \frac{1}{1+C} ]
Therefore:
Lower control cost
↓
Higher fitness
The metrics considered include:
- Overshoot
- Steady-state error
- Settling time
- Decay ratio
The main experiment uses:
Population size: 100
Generations: 200
Mutation rate: 0.10
Crossover rate: 0.60
Chromosome length: 3
A representative optimization run found approximately:
Kp = 0.0927
Ki = 17.2257
Kd = 0.0000
The result behaves as a PI controller, indicating that the evolutionary process did not require derivative action for that experiment.
Reported performance:
| Metric | Result |
|---|---|
| Overshoot | 0% |
| Steady-state error | 0 |
| Settling time | 4 cycles |
| Decay ratio | 0 |
The project extends the original algorithm through the:
GeneticoMejorado
class.
The improved implementation records:
- Best fitness per generation
- Average population fitness
- Population diversity
- Best chromosome per generation
- Global best individual
It also introduces adaptive evolutionary parameters.
Instead of using constant probabilities throughout evolution, the improved algorithm modifies mutation and crossover rates over time.
Conceptually:
Early generations
│
├── Higher exploration
├── Higher mutation
└── Higher crossover
↓
Later generations
│
├── Lower exploration
└── Solution refinement
This encourages broad exploration at the beginning and exploitation of promising regions near the end.
The algorithm records several metrics during training.
The project visualizes:
- Best fitness
- Mean fitness
- Population diversity
- Evolution of (K_p), (K_i), and (K_d)
- Distribution of final fitness values
A representative improved run reported an increase from approximately:
Fitness 0.0085
at initialization to:
Fitness 0.8696
during later generations.
Population diversity decreased as the algorithm converged.
The project visualizes the fitness landscape over combinations of:
[ K_p, K_i ]
while fixing:
[ K_d = 0 ]
This provides a graphical representation of promising regions in the controller-parameter search space.
One reported local optimum appears approximately around:
Kp ≈ 0.069
Ki ≈ 13.966
Fitness ≈ 0.7407
Different optimization objectives are evaluated.
Balances:
- Overshoot
- Steady-state error
- Settling time
- Decay ratio
The project implements the:
Integral of Time-weighted Absolute Error
[ ITAE = \sum_t t |e(t)| ]
This penalizes errors that persist for long periods.
A normalized fitness is then derived from the ITAE score.
Another formulation penalizes aggressive actuator behavior.
It includes a control-effort term based on:
[ \sum u(t)^2 ]
This favors controllers that achieve good tracking without excessively large control signals.
A more restrictive fitness formulation favors solutions that remain stable and well behaved under changing operating conditions.
Representative results from the notebook include:
| Fitness Function | Value |
|---|---|
| Original weighted | 0.8333 |
| ITAE | 0.8579 |
| Control-effort penalty | 0.8325 |
| Robustness | 0.7143 |
In this experiment, the ITAE-based formulation achieved the strongest result.
The project evaluates the effect of several genetic-algorithm configurations.
Parameters include:
- Population size
- Mutation rate
- Crossover probability
Example tested configurations:
(50, 0.05, 0.5)
(50, 0.10, 0.7)
(100, 0.05, 0.7)
(100, 0.10, 0.6)
(100, 0.20, 0.5)
(200, 0.10, 0.7)
Each configuration is executed multiple times to estimate:
- Mean fitness
- Standard deviation
- Repeatability
Several configurations with population sizes of 100 or greater consistently achieved:
Fitness ≈ 0.8333
in the reported experiments.
The project implements multiple crossover and mutation strategies.
- BLX
- Uniform crossover
- Arithmetic crossover
- Differential Evolution-inspired operator
- Gaussian mutation
- Uniform mutation
- Non-uniform mutation
The combinations can be evaluated automatically to identify which operators work best for PID optimization.
Representative results:
| Crossover + Mutation | Fitness |
|---|---|
| BLX + Gaussian | 0.8333 |
| BLX + Uniform | 0.8333 |
| Uniform + Gaussian | 0.7407 |
| Arithmetic + Gaussian | 0.2817 |
BLX produced the strongest results among the tested configurations.
The genetically tuned controller is compared against classical PID tuning methods.
The notebook includes:
- Ziegler–Nichols
- Cohen–Coon
- Genetic Algorithm
Representative results:
| Method | Overshoot | (E_{ss}) | (T_s) |
|---|---|---|---|
| Ziegler–Nichols | 83.77% | 0.3699 | 320 |
| Cohen–Coon | 77.00% | 0.4099 | 320 |
| Genetic Algorithm | 0.00% | 0.0000 | 3 |
In this simulated system and under the selected objective function, evolutionary tuning produces significantly better performance than the two classical baselines.
The optimized controller is evaluated when motor parameters differ from their nominal values.
The tests include:
R +20%
J +20%
K -20%
fv +50%
Representative results:
| Scenario | Overshoot | (E_{ss}) | (T_s) |
|---|---|---|---|
| Nominal | 0.00% | 0.0000 | 3 |
| R +20% | 0.00% | 0.0000 | 3 |
| J +20% | 0.00% | 0.0000 | 3 |
| K -20% | 6.34% | 0.0000 | 14 |
| (f_v) +50% | 0.00% | 0.0000 | 3 |
The controller remains robust across most tested model variations.
The controller is also evaluated under noisy speed measurements.
Noise levels include:
σ = 0
σ = 0.5
σ = 1.0
σ = 2.0
Representative results show increasing sensitivity as measurement noise grows.
| Noise σ | Overshoot |
|---|---|
| 0 | 0.00% |
| 0.5 | 2.48% |
| 1.0 | 5.89% |
| 2.0 | 11.84% |
Settling performance also deteriorates significantly under stronger noise.
This reveals an important limitation of the optimized controller.
The controller is evaluated across several speed references:
10 rad/s
50 rad/s
100 rad/s
150 rad/s
200 rad/s
Representative results:
| Reference | Overshoot | (E_{ss}) | (T_s) |
|---|---|---|---|
| 10 | 0.00% | 0.0000 | 3 |
| 50 | 0.00% | 0.0000 | 3 |
| 100 | 0.00% | 0.0000 | 3 |
| 150 | 0.00% | 0.0000 | 3 |
| 200 | 1.13% | 0.0000 | 4 |
The tuned controller generalizes effectively across the tested reference range.
A PID variant with anti-windup is implemented.
The motor actuator is restricted to:
[ \pm 24V ]
When the calculated control output exceeds these bounds, the integral term stops accumulating.
Conceptually:
Compute integral candidate
│
▼
Would actuator saturate?
/ \
Yes No
│ │
▼ ▼
Keep old Accept new
integral integral
This prevents excessive integral accumulation during saturation.
The project also extends the original velocity-control problem to position control.
Motor position is estimated by integrating velocity:
[ x(t) = \int v(t),dt ]
A PD-style controller is then used to act on:
- Position error
- Current velocity
The objective is to reach the desired position while reducing speed as the target is approached.
This extension is experimental and is not part of the primary PID-tuning validation pipeline.
The function:
run_all_experiments()automates the full analysis.
It executes:
1. Improved genetic algorithm
2. Convergence visualization
3. Fitness landscape
4. Genetic hyperparameter sweep
5. Alternative fitness comparison
6. Classical tuning comparison
7. Robustness testing
8. Measurement-noise testing
9. Multiple-reference testing
10. Genetic-operator comparison
This provides a reproducible experimental framework rather than a single optimization run.
genetic-pid-optimization/
│
├── practica_geneticos_AnaYang_Junjing_YixuanLu.ipynb
└── README.md
The notebook contains the complete implementation, experimentation, visualization, and analysis.
A Python environment with Jupyter is recommended.
Install the main dependencies:
pip install numpy matplotlib seaborn jupyterClone the repository:
git clone https://github.com/YOUR_USERNAME/genetic-pid-optimization.git
cd genetic-pid-optimizationStart Jupyter:
jupyter notebookThen open:
practica_geneticos_AnaYang_Junjing_YixuanLu.ipynb
Run the notebook cells sequentially.
To execute the complete experimental suite, run:
run_all_experiments()after the required classes and functions have been defined.
- Python
- NumPy
- Matplotlib
- Seaborn
- Jupyter Notebook
This project demonstrates:
- PID control
- PI control
- DC motor simulation
- Genetic algorithms
- Real-valued chromosomes
- Evolutionary optimization
- Tournament selection
- BLX crossover
- Uniform crossover
- Arithmetic crossover
- Gaussian mutation
- Adaptive evolutionary parameters
- Elitism
- ITAE
- Control-effort minimization
- Robustness analysis
- Anti-windup
- Position control
- Parameter sensitivity
- Hyperparameter optimization
The experiments support several conclusions.
Under the simulated motor model and the selected objectives, the genetically optimized controller substantially outperforms the tested Ziegler–Nichols and Cohen–Coon configurations.
Several strong solutions converge toward:
Kd ≈ 0
effectively producing PI controllers.
Among the tested crossover strategies, BLX combined with Gaussian or uniform mutation produced the strongest results.
Population sizes of approximately 100 or more produced more repeatable high-quality solutions in the reported experiments.
Although the controller performs very well under parameter variations and different references, measurement noise significantly degrades settling behavior.
Future work could include:
- Multi-objective genetic optimization
- Pareto-front analysis
- NSGA-II
- Particle Swarm Optimization
- Differential Evolution comparison
- CMA-ES
- Bayesian optimization
- Automatic PID gain scheduling
- Kalman filtering for noisy measurements
- Frequency-domain validation
- Real hardware experiments
- Hardware-in-the-loop simulation
- Model Predictive Control comparison
This project was developed as an educational exercise in evolutionary optimization and control systems.
The goal was to extend a previously implemented PID controller by automatically optimizing its gains using a genetic algorithm and systematically evaluating the resulting controller.
- Ana Yang Rincon
- Junjing Wu
- Yixuan Lu
This repository contains a simulated control system intended for educational and experimental purposes.
The reported performance values depend on the motor model, simulation assumptions, fitness formulation, and random evolutionary process and should not be interpreted as guaranteed performance on physical hardware.
See the repository license for applicable terms.