Metaheuristics

Invasive Weed Optimization (IWO) in MATLAB

Get the code
Comments

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

Illustration for Invasive Weed Optimization (IWO) in MATLAB
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 Smin⁡S_{\min} seeds and the best produces Smax⁡S_{\max}. In the code, for a weed with cost cc,

S=⌊Smin⁡+(Smax⁡−Smin⁡)c−cworstcbest−cworst⌋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 (Smin⁡=0S_{\min} = 0, Smax⁡=5S_{\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 σinitial\sigma_{\text{initial}} to σfinal\sigma_{\text{final}}, following a nonlinear rule with an exponent nn:

σit=(MaxIt−itMaxIt−1)n(σinitial−σfinal)+σfinal\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 σinitial\sigma_{\text{initial}} in the first iteration and σfinal\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)=∑i=1nxi2f(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, 1010
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 nn 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.

Downloads

How to cite

If you use this code or tutorial in your work, articles or publications, please cite it.

Plain text
Mostapha Kalami Heris, Invasive Weed Optimization (IWO) in MATLAB (YPEA119), Yarpiz, 2015. https://yarpiz.com/invasive-weed-optimization-ypea119
BibTeX
@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}
}

Comments

Sign in or create a free account to join the discussion. No password needed.