Case Study: Nature-Inspired Feature Selection for Intrusion Detection
Genetic Algorithm, Particle Swarm and Ant Colony Optimisation compared under one fair protocol on two intrusion detection benchmarks — up to 80% fewer features with the same or better detection.
- cybersecurity
- optimisation
- feature selection
- swarm intelligence
An intrusion detection system watches network traffic and flags attacks. Every connection is described by dozens or hundreds of features, and many of them are redundant or noisy — they slow the model down and can even hurt detection. This project asks a simple question: which nature-inspired search algorithm is best at picking the features that matter? I built it for the Nature-Inspired Computing course in my M.Sc. at Canadian University Dubai.
- 3
- Algorithms written from scratch: GA, PSO, ACO
- 2
- Benchmarks: NSL-KDD and CIC-IDS-2017
- 80%
- Fewer features with ACO, on both datasets
- +9.5 pts
- Detection-rate gain from PSO on NSL-KDD
- 99.9%
- Accuracy on CIC-IDS-2017 (GA)
- 2¹²²
- Possible feature subsets on NSL-KDD
The problem
Choosing a subset of features is a search problem with an enormous space: with 122 features there are 2¹²² possible subsets, far too many to try. Nature-inspired algorithms explore that space the way evolution, bird flocks or ant colonies solve problems — keeping what works and gradually improving it. Many papers compare these methods, but often with different data, classifiers or settings, which makes the comparison unfair. The goal here was a clean, like-for-like test.
A fair comparison
All three algorithms share exactly the same data, preprocessing, classifier, fitness function and metrics. The only thing that changes is the search strategy, so any difference in the results comes from the algorithm alone.
- 1
Pre-process the traffic data
Impute missing values, one-hot encode categorical fields and min-max scale everything; labels become binary — normal or attack.
- 2
Search for a feature subset
GA, PSO or ACO proposes subsets; each is scored by the same wrapper fitness function using a Random Forest and k-fold cross-validation.
- 3
Stop when progress stalls
Each algorithm stops early if its best fitness has not improved for a set number of generations.
- 4
Evaluate on the full test set
Train a 100-tree Random Forest on the chosen features and measure accuracy, detection rate, F1, false-positive rate, feature reduction and runtime.
- 5
Test for significance
Repeat the runs and compare the algorithms with Wilcoxon and Friedman tests.
Fitness(C) = 0.9 · F1(C) − 0.1 · |C| / n
How each algorithm searches
| Algorithm | Inspired by | How a subset is represented | How the search moves |
|---|---|---|---|
| Genetic Algorithm (GA) | Evolution | Binary chromosome — 1 keeps a feature | Roulette-wheel selection, single-point crossover (p = 0.8), bit-flip mutation (p = 0.02) |
| Particle Swarm (PSO) | Bird flocks | Continuous position, thresholded with a sigmoid | Inertia decays from 0.9 to 0.4, c₁ = c₂ = 2.0 |
| Ant Colony (ACO) | Foraging ants | Pheromone level per feature | Sampling by pheromone and heuristic (α = 1, β = 2), evaporation ρ = 0.1 |
Results
Both benchmarks come from the Canadian Institute for Cybersecurity. NSL-KDD is the harder one, with 122 features after encoding; CIC-IDS-2017 has 78 features of modern traffic.
Features kept on NSL-KDD
Out of 122 features. Fewer features means a smaller, faster model.
- All features122
- PSO52
- GA43
- ACO25
Bar length = features
Detection rate on NSL-KDD
Share of attacks correctly detected on the full test set (%).
- PSO70.3
- ACO62.2
- GA60.8
- All features60.8
Bar length = % of attacks detected
The trade-off: features kept vs attacks detected (NSL-KDD)
Every algorithm moved left of the baseline (smaller model); PSO also moved clearly up (more attacks caught).
Up and to the left is better: more attacks detected with fewer features. Hover a point for details.
Features kept on CIC-IDS-2017
Out of 78 features — every subset matched or beat the baseline's 99.8% accuracy.
- All features78
- PSO44
- GA29
- ACO16
Bar length = features
Full results — NSL-KDD (122 features)
100-tree Random Forest on the full held-out test set.
| Method | Features | Accuracy | Detection rate | F1 | False-positive rate |
|---|---|---|---|---|---|
| Baseline (all) | 122 | 76.5% | 60.8% | 0.746 | 2.74% |
| GA | 43 (65% fewer) | 76.4% | 60.8% | 0.746 | 2.85% |
| PSO | 52 (57% fewer) | 81.9% | 70.3% | 0.816 | 2.63% |
| ACO | 25 (80% fewer) | 77.3% | 62.2% | 0.757 | 2.86% |
Full results — CIC-IDS-2017 (78 features)
| Method | Features | Accuracy | Detection rate | F1 | False-positive rate |
|---|---|---|---|---|---|
| Baseline (all) | 78 | 99.81% | 99.27% | 0.995 | 0.06% |
| GA | 29 (63% fewer) | 99.90% | 99.64% | 0.997 | 0.04% |
| PSO | 44 (44% fewer) | 99.82% | 99.29% | 0.995 | 0.05% |
| ACO | 16 (80% fewer) | 99.85% | 99.48% | 0.996 | 0.05% |
Every algorithm matched or beat the all-features baseline while using far fewer features. On CIC-IDS-2017, ACO kept just 16 of 78 features and still detected more attacks than the full model.
Which algorithm to choose
| If you need… | Choose | Why |
|---|---|---|
| The smallest model | ACO | About 80% of features removed on both datasets |
| The highest detection rate | PSO | +9.5 points on the harder NSL-KDD benchmark |
| The best all-round scores | GA | Top accuracy, F1 and lowest false-positive rate on CIC-IDS-2017 |
How the searches converged
The convergence curves track the best fitness found so far at each generation. ACO starts strong and settles within a few iterations; PSO begins lowest but climbs steadily as the swarm closes in; GA improves slowly and evenly.
Convergence of GA, PSO and ACO
Best-so-far fitness per generation on each dataset, from the pilot-scale run in the repository. Original figure from the project's code.

Swipe sideways to see the full figure.
Are the differences real?
Good results in one run can be luck, so the runs were repeated and tested statistically.
Statistical tests on F1 scores
p < 0.05 would indicate a significant difference.
| Test | NSL-KDD | CIC-IDS-2017 |
|---|---|---|
| Friedman (all three algorithms) | p = 0.25 | p = 0.55 |
| Wilcoxon GA vs PSO | p = 0.63 | p = 0.44 |
| Wilcoxon GA vs ACO | p = 0.81 | p = 0.81 |
| Wilcoxon PSO vs ACO | p = 0.13 | p = 0.63 |
Built to be reproduced
The algorithms are written in plain NumPy, following their published pseudocode, with no metaheuristic libraries. The project is packaged as a Python library with a command-line tool: one command re-runs every experiment and rebuilds every table and figure.
Engineering
| Area | Details |
|---|---|
| Algorithms | GA, PSO and ACO from scratch in NumPy |
| Modelling | scikit-learn Random Forest, stratified k-fold cross-validation |
| Statistics | SciPy Wilcoxon and Friedman tests |
| Reproducibility | Fixed seeds, YAML configs (fast and paper-scale), CLI: nifs run / nifs stats |
| Quality | pytest suite on synthetic data, Ruff linting, GitHub Actions CI on Python 3.10–3.12 |
Limitations
- Results come from a pilot-scale search (populations of 12, 8 generations, 6,000 training rows for fitness); the paper-scale configuration (50 × 100, all rows, 30 runs) takes several hours and would give stronger statistics.
- NSL-KDD is hard for every method — detection stays around 60–70% — while CIC-IDS-2017 is close to the ceiling, which leaves little room to separate the algorithms.
- Both datasets are binary (normal vs attack); multi-class attack types are a natural next step.
What I learned
A fair experiment is worth more than a flashy number. Holding everything constant except the search strategy, and then testing the results statistically, showed that the real gain came from selecting features at all — not from which algorithm did it. Writing the three algorithms from scratch also made their trade-offs concrete: ants prune aggressively, swarms explore widely, and evolution balances the two.