An advanced, self-adaptive variant of the Whale Optimization Algorithm (WOA) that integrates Multi-Armed Bandit (UCB1) reinforcement learning, diversity-aware parameter adaptation, and LΓ©vy-flight enhanced exploration.
This repository contains the core implementation of RA-WOA, benchmarking scripts comparing it against standard WOA on standard optimization landscapes, and a practical application optimizing Cluster Head Selection in Wireless Sensor Networks (WSNs).
- Key Features
- How it Works
- Repository Structure
- Getting Started
- Experimental Results
- Visualizations
- License
- Intelligent Phase Selection: Replaces the standard blind coin-flip (
p = 0.5) with a UCB1 bandit that adapts to which exploration/exploitation strategies yield the highest fitness rewards. - Diversity Monitoring: Automatically adjusts the convergence search parameter
abased on population diversity to prevent premature convergence and escape local minima. - LΓ©vy Flight Exploratory Jumps: Integrates heavy-tailed probability jumps to provide "escape velocity" when trapped in sub-optimal basins of attraction.
- Practical WSN Application: Evaluates the algorithms on NP-hard Cluster Head Selection in a simulated 100-node Wireless Sensor Network.
RA-WOA upgrades the standard biological model of bubble-net hunting (encircling and spiral pathing) with a cognitive feedback loop:
graph TD
A["π Initialize Whales & Compute Centroid Diversity"] --> B["For each Whale in Population"]
B --> C{"Select Strategy using UCB1"}
C -->|Arm 0| D["Encircling Prey (Exploitation)"]
C -->|Arm 1| E["Spiral Bubble-Net (Exploitation)"]
C -->|Arm 2| F["LΓ©vy Flight (Exploration)"]
D --> G["Calculate Fitness & Feedback Reward"]
E --> G
F --> G
G --> H["Update UCB1 Q-Values & Arm Counts"]
H --> I["Adapt Parameter 'a' based on Diversity Ratio"]
I --> J{"Max Iterations Reached?"}
J -->|No| B
J -->|Yes| K["Output Best Global Solution"]
Standard WOA selects between encircling/hunting and spiral updates randomly. RA-WOA frames strategy selection as a Multi-Armed Bandit problem. Using the Upper Confidence Bound (UCB1) algorithm, it chooses the action
Where
The parameter
The adaptation parameter
When the bandit identifies exploitation stagnation, it pulls the LΓ©vy Flight arm. Generating heavy-tailed random walk steps via Mantegna's algorithm, whales perform sudden wide-ranging jumps to discover new search regions.
βββ src/
β βββ ra_woa.py # Core implementations of Standard WOA, RA-WOA, and Random Search
βββ experiments/
β βββ benchmark_experiment.py # Benchmark script running Sphere, Rastrigin, and Ackley tests
β βββ wsn_experiment.py # Practical simulation & optimization for WSN Cluster Head selection
βββ notebooks/
β βββ gen_notebook.py # Script to programmatically construct the project notebook
β βββ RA_WOA_Notebook.ipynb # Clean notebook (unexecuted)
β βββ RA_WOA_Executed.ipynb # Pre-executed Jupyter Notebook with rendered plots
βββ docs/
β βββ project_explanation.md # Comprehensive project details and mathematical formulations
β βββ wsn_explanation.md # In-depth analysis of why RA-WOA outperforms WOA in WSN domains
β βββ benchmark_vs_wsn_explanation.md # Comparative landscape analysis
β βββ research_paper.md # Draft detailing the formulation and theoretical background
β βββ explanation.pdf # Compiled document containing project insights
βββ assets/
β βββ *.png # Result plots (convergence, boxplots, trajectories, WSN layouts)
βββ .gitignore # Python/Jupyter/IDE Git exclusion file
βββ LICENSE # MIT License file
βββ README.md # Main repository readme
βββ requirements.txt # Project dependencies list
Make sure you have Python 3.8+ and install the dependencies:
pip install -r requirements.txtTo run the optimization benchmarking on Sphere, Rastrigin, and Ackley functions:
python experiments/experiment.pyThis script will execute 20 independent runs for each algorithm and output statistics along with saving the following plots in the assets/ directory:
assets/rawoa_convergence.png: Mean fitness trajectory over iterations.assets/rawoa_boxplot.png: Statistical variance of the algorithms.
To run the Wireless Sensor Network Cluster Head Selection simulation:
python experiments/wsn_experiment.pyThis script places 100 sensors across a
- Outputs the statistical comparison over 20 runs.
- Saves
assets/wsn_rawoa_result.pngshowing the node distribution, optimized cluster heads, and connectivity lines.
Under 10 independent runs, Standard WOA performs better on these theoretical landscapes because they either favor direct exploitation (Sphere) or have an artificial symmetry/origin bias (Schwefel, Shifted Rastrigin) that standard WOA's mathematical formulation naturally collapses towards. RA-WOA maintains diversity and actively explores, which makes it perform slightly worse on these highly-biased theoretical functions but prevents premature convergence in real-world asymmetric problems.
| Function | Algorithm | Mean Fitness | Std Dev | Winner |
|---|---|---|---|---|
| Sphere (Unimodal) | Standard WOA | WOA | ||
| RA-WOA | ||||
| Schwefel (Deceptive Multimodal) | Standard WOA | WOA | ||
| RA-WOA | ||||
| Shifted Rastrigin (Multimodal) | Standard WOA | WOA | ||
| RA-WOA |
In the practical, unbiased, and asymmetric WSN cluster head assignment, Standard WOA frequently stagnates in sub-optimal configurations where some nodes are isolated or left far from their cluster heads due to its origin bias. RA-WOAβs LΓ©vy exploration and UCB1 action scheduling allow it to find superior configurations consistently:
- Mean Fitness Improvement: 16.0% reduction in transmission distances.
- Standard Deviation: 0.55 (RA-WOA) vs 0.81 (Standard WOA).
- Winner: RA-WOA (won 20 out of 20 runs)
Here are some of the key output figures generated during the experiments:
Shows the final 100-node network with the 6 optimized cluster heads and node-head cluster memberships.

Demonstrates how the UCB1 algorithm dynamically learns to favor exploitation (Encircling/Spiral) or exploration (LΓ©vy Flight) as iterations progress.

Comparison of convergence speed and stability on benchmark functions.

Visualizing search agent pathing in 2D space.
