# Invasive Weed Optimization (IWO) in MATLAB

URL: https://yarpiz.com/invasive-weed-optimization-ypea119
Author: Mostapha Kalami Heris
Published: 2015-09-06
Updated: 2026-10-30
Topics: Metaheuristics

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

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](https://en.wikipedia.org/wiki/R/K_selection_theory). 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](http://www.sciencedirect.com/science/article/pii/S1574954106000665).

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 $S_{\min}$ seeds and the best produces $S_{\max}$. In the code, for a weed with cost $c$,

$$
S = \left\lfloor S_{\min} + \left(S_{\max} - S_{\min}\right) \frac{c - c_{\text{worst}}}{c_{\text{best}} - c_{\text{worst}}} \right\rfloor
$$

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 ($S_{\min} = 0$, $S_{\max} = 5$ 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 $\sigma$. This standard deviation shrinks over the iterations, from $\sigma_{\text{initial}}$ to $\sigma_{\text{final}}$, following a nonlinear rule with an exponent $n$:

$$
\sigma_{\text{it}} = \left(\frac{\text{MaxIt} - \text{it}}{\text{MaxIt} - 1}\right)^{n} \left(\sigma_{\text{initial}} - \sigma_{\text{final}}\right) + \sigma_{\text{final}}
$$

This is the formula of the code. It gives $\sigma_{\text{initial}}$ in the first iteration and $\sigma_{\text{final}}$ in the last. The shrinking $\sigma$ 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, $f(x) = \sum_{i=1}^{n} x_i^2$.
- `main.m`: a one line entry point that runs `iwo.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 | $-10$, $10$ |
| `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 $n$ of the $\sigma$ 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](https://yarpiz.com/yarpiz-evolutionary-algorithms-ypea) is a MATLAB toolbox that includes IWO and 14 other algorithms that share one problem definition.
- [Imperialist Competitive Algorithm in MATLAB](https://yarpiz.com/imperialist-competitive-algorithm-ypea118) is another metaheuristic proposed by Caro Lucas and a coauthor.

## How to cite

Mostapha Kalami Heris, Invasive Weed Optimization (IWO) in MATLAB (YPEA119), Yarpiz, 2015. https://yarpiz.com/invasive-weed-optimization-ypea119
