Invasive Weed Optimization (IWO) in MATLAB
Implement Invasive Weed Optimization in MATLAB with cost-based seed counts, shrinking Gaussian dispersal, and a bounded Sphere test problem.

On this page
Invasive Weed Optimization (IWO) is a nature-inspired metaheuristic based on the spreading strategy of weeds. Alireza Mehrabian and Caro Lucas proposed it in 2006. It follows the r/K selection theory (opens in a new tab). The artificial weeds (solutions) use the r-selection strategy at the beginning of the algorithm and gradually switch to the K-selection strategy as the algorithm runs. For more information, see the paper that proposed IWO (opens in a new tab).
In ecology, r-selected species have many offspring and spread them widely. K-selected species have fewer offspring and compete well in a crowded place. In IWO, this means that the seeds spread far from their parents in the early iterations, which explores the search space. Later, the seeds fall close to their parents, which refines the best solutions.
How IWO works
The algorithm starts with a small number of weeds at random positions. Each iteration then has three steps.
Reproduction
Each weed produces seeds. The number of seeds grows linearly with the fitness of the weed: the worst weed produces seeds and the best produces . In the code, for a weed with cost ,
So good weeds have more offspring. Some weaker weeds can still produce seeds, although rounding down may give several weeds no offspring. With the values in the code (, and rounding down), a weed needs to be at least one fifth of the way from the worst cost to the best cost to produce any seed, and only weeds tied at the best cost produce five.
Spatial dispersal
Each seed lands at a random point near its parent. The offset is drawn from a normal distribution with mean zero and standard deviation . This standard deviation shrinks over the iterations, from to , following a nonlinear rule with an exponent :
This is the formula of the code. It gives in the first iteration and in the last. The shrinking is the move from the r-selection strategy to the K-selection strategy.
Competitive exclusion
The seeds join the population as new weeds. When the number of weeds grows beyond the maximum population size, the weeds are sorted by cost and only the best ones are kept. The others are removed. Because parents and seeds compete together, the best weed is never lost.
Implementation in MATLAB
This post shares a structured, open-source MATLAB implementation of IWO. If you know MATLAB, you will find it easy to use the source code in your research or projects.
What is in the download
The IWO folder holds three files:
iwo.m: the IWO script, with the problem definition, the parameters, the main loop and the results.Sphere.m: the sample cost function, .main.m: a one line entry point that runsiwo.m.
To run the code, open the IWO folder in MATLAB and run main.m. The program prints the best cost of every iteration and then plots the best cost against the iteration number on a logarithmic scale.
The script creates the first weeds with unifrnd, a function of the Statistics and Machine Learning Toolbox, so it probably needs that toolbox.
Parameters in the code
| Parameter | Meaning | Value |
|---|---|---|
nVar |
Number of decision variables | 5 |
VarMin, VarMax |
Lower and upper bounds of the variables | , |
MaxIt |
Maximum number of iterations | 200 |
nPop0 |
Initial population size | 10 |
nPop |
Maximum population size | 25 |
Smin |
Minimum number of seeds | 0 |
Smax |
Maximum number of seeds | 5 |
Exponent |
Exponent of the reduction | 2 |
sigma_initial |
Initial standard deviation | 0.5 |
sigma_final |
Final standard deviation | 0.001 |
Positions of new seeds outside the bounds are set back to the nearest bound.
To solve your own problem, write a cost function that takes a row vector and returns a number, and set CostFunction, nVar, VarMin and VarMax in the problem definition section of iwo.m. The values of sigma_initial and sigma_final are absolute, so you may need to scale them to the range of your variables.
Check the result
The Sphere test has a known minimum of 0 at the origin. The seed-count formula divides by the difference between the best and worst costs, so equal costs across the population need explicit handling. The dispersal schedule also divides by MaxIt - 1, so it is not defined for a single iteration. These cases matter when using a constant, rounded, or already-converged objective. Check the final bounds and repeat runs before comparing dispersal settings.
Related posts
- YPEA is a MATLAB toolbox that includes IWO and 14 other algorithms that share one problem definition.
- Imperialist Competitive Algorithm in MATLAB is another metaheuristic proposed by Caro Lucas and a coauthor.
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Invasive Weed Optimization (IWO) in MATLAB (YPEA119), Yarpiz, 2015. https://yarpiz.com/invasive-weed-optimization-ypea119@misc{yarpiz-ypea119,
author = {{Mostapha Kalami Heris}},
title = {{Invasive Weed Optimization (IWO) in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPEA119},
url = {https://yarpiz.com/invasive-weed-optimization-ypea119}
}Published Updated
Comments