
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
There are various optimization algorithms in computer science, and the Grasshopper Optimization Algorithm (GOA) is one of them. In this tutorial, we’ll take a look at what this algorithm means and what it does.
Then, we’ll go through the steps of the GOA algorithm. In addition to a numerical example demonstrating the solution of an optimization issue using GOA, we’ll discuss its benefits and drawbacks.
GOA is a meta-heuristics algorithm and a population-based algorithm. As implied by the name, this algorithm was proposed for solving numerical optimization issues by Saremi and Mirjalili, 2017. It’s a recent swarm intelligence algorithm. The inspiration for GOA comes, in particular, from the behavior and the interaction of grasshopper swarms in nature.
Grasshoppers are harmful and destructive insects because of the extensive damage they cause to crops. Let’s take a look at the life cycle of grasshoppers, which has three phases:
In short, the Grasshopper life cycle may resemble the image below:
Let’s now investigate the main characteristic of the grasshopper swarm:
In the exploration phase, the swarm search for food sources. Thus, in this stage, we update all the value positions of the grasshopper and compute the fitness value. In the exploration phase, we find the best solution among all solutions (search for better food sources).
The GOA algorithm mimics the social behavior and hunting method of grasshoppers in nature. It’s a population-based algorithm, with each grasshopper representing a solution in the population. So, how do we determine the position of each solution in the swarm?
It’s simple, we only need to calculate three forces: the social interaction between the solution and the other grasshoppers, the wind advection, and the gravity force on the solution. Let’s now have a look at the mathematical model used to calculate the position of each solution:
(1)
where represents the social interaction between the solution and the other grasshoppers,
represents the gravity force on the solution, and
represents the wind advection. The equation below represents the position of each solution after adding random behavior to it:
(2)
where and
are random numbers in [0, 1]. Let’s now have a look at the model of each force used in Equation 1.
Let’s firstly start with the force of social interaction . The equation below represents the social interaction between the solution and the other grasshoppers:
(3)
(4)
where represents the distance between
grasshopper and the
grasshopper,
represents the unit vector. In addition,
represents the strength of two social forces (repulsion and attraction between grasshoppers), where
is the attractive length scale and
is the intensity of attraction. In fact, these cofficients
and
have a great impact in the social behavior of grasshoppers.
Let’s dig a little deeper into the strength of these social forces. When the distance between two grasshoppers is between , repulsion occurs, and when the distance between two grasshoppers is
, neither attraction nor repulsion occurs, which form a comfort zone. When the distance exceeds
, the attraction force increases, then progressively decreases until it reaches
.
The function fails to apply forces between grasshoppers when the distance between them is larger than
. In order to solve this problem, we map the distance of grasshoppers in the interval
. The graph below explains the social interaction in more detail:
Let’s now move forward to the second force, which is the force of gravity. The equation below shows how to calculate the force of gravity :
(5)
where represents the gravitational constant and
is unit vector toward center of earth.
Afterward, we investigate the force of wind direction, which greatly impacts the nymphs and adulthood grasshoppers. As a result, the movements of nymph and adulthood grasshoppers are correlated with the wind direction . The equation below shows how to compute
:
(6)
where represents the drift constant and
is the unit vector in the wind direction.
Let’s reconstruct Equation 1 using Equations 3, 4, 5 and 6:
(7)
In order to solve optimization issues and prevent grasshoppers from quickly reaching their comfort zone and the swarm from failing to converge to the target location (global optimum), we’ll make some modifications in Equation 7:
(8)
where ,
is the best solution in the
dimension, and
and
are the upper and lower bounds in the
dimension, respectively.
The parameter represents the decreasing coefficient, and it is in charge of decreasing the comfort zone, repulsion zone, and attraction zone. In order to balance the exploration and the exploitation phases using the grasshopper approach, the coefficient
decreases according to the number of iterations. Here’s the model for
:
(9)
where and
are the maximum and minimum values of
respectively,
is the current iteration, and
is the maximum number of iterations.
Let’s now take a look at the implementation of the GOA optimizer:
algorithm GrasshopperOptimizer(iter, c_max, c_min, l, f, n, Max_iter):
// INPUT
// c_max = maximum value of the coefficient c
// c_min = minimum value of the coefficient c
// l = attractive length scale
// f = intensity of attraction
// n = number of grasshoppers (search agents)
// Max_iter = maximum number of iterations
// OUTPUT
// The best found solution
// Initialize the swarm
grasshoppers <- initialize n solutions (X_1, X_2, ..., X_n)
Compute the fitness of each grasshopper
Find the best solution
iter <- 0
while iter < Max_iter:
Update c using Equation 9
for grasshopper in grasshoppers:
Normalize distance between grasshopper in the range [1, 4]
Update the position of grasshopper using Equation 8
Bring current grasshopper back if it goes outside the boundaries
Update the best solution if there is a better one
iter <- iter + 1
return the best solution
As we can see in the algorithm, we start by initializing the parameters {} and the grasshopper population
. Then, we evaluate each solution by calculating its value using the fitness function. After evaluating each solution in the population, we assign the best solution according to its value. Afterward, we update the coefficient parameter
to shrink the attraction, repulsion, and comfort zones (Equation 9).
The function (Equation 4) divides the search space into repulsion, comfort, and attraction zones. As explained in Section 4.1, when the function
fails to apply forces between the grasshoppers, we map the distance in the interval
.
Then, we update each solution in the population based on the distance between the grasshopper (the solution) and the other grasshopper (the other solution). We also update the best solution in the population and the decreasing coefficient parameter using Equation 8.
In the case of a grasshopper violating the boundaries after updating the solution, we bring the grasshopper back to its domain. Then, we repeat the previous three steps for each solution in the population.
Afterward, we update and evaluate each solution to assign the best one in the population. We repeat the overall operations until we reach the maximum number of iterations to return the best global solution.
It is worth noting that the time complexity of the grasshopper algorithm is generally related to the number of iterations and the number of agents. So, represents the time complexity of this algorithm with
is the number of agents and
is the number of iterations.
To understand the grasshopper optimizer algorithm, we use a numerical example.
Firstly, we randomly initialize the population of grasshopper
, where
. Afterward, we initialize all the parameters
, and
using a random set of values:
Here’s the fitness function in order to calculate the fitness value for each grasshopper:
Let’s calculate the fitness value for each grasshopper in the current population using the fitness function:
Sr. No | X(1) | X(2) | Fitness Value |
---|---|---|---|
1. | |||
2. | |||
3. | |||
4. |
After calculating all the fitness values, we have to select the optimal solution among all. The optimal solution is the one that has the smallest fitness value across all grasshoppers. As a result, is the best solution for the first iteration. Let’s move forward to check the
condition. Notice that the number of iteration
and the
, which means condition true
. As a result, we move to the next step.
We need to compute in order to normalize the distance between grasshoppers using the equations below:
(10)
(11)
Here’s the result of normalizing the distance between grasshoppers:
Sr. No | Distance(1) | Distance(2) |
---|---|---|
1. | 0.0058 | -0.1533 |
2. | -0.0756 | 0.13648 |
3. | 0.1565 | -0.0275 |
4. | -0.0867 | -0.0106 |
To update the position for each grasshopper using Equation 8, we require the current position of the grasshopper (calculated in section 6.2), the best solution found (calculated in section 6.3), and other grasshopper positions (calculated using the distance in section 6.4). Here’s the new position for grasshoppers:
Sr. No | New Position X(1) | New Position X(2) |
---|---|---|
1. | 3.1519 | 1.2009 |
2. | 3.0867 | 1.4328 |
3. | 3.1472 | 1.3236 |
4. | 3.0778 | 1.3151 |
We got the values of the new position by adding the best solution found to
.
Before computing the new fitness value, we bring the current grasshopper back if it goes outside boundaries . The table in section 6.5 shows that all grasshoppers are within the boundary. So, let’s now compute the new fitness values for each grasshopper in the current population:
Sr. No | New Fitness Value |
---|---|
1. | 165.6341 |
2. | 148.8582 |
3. | 221.6412 |
4. | 141.9027 |
We check if there’s any new best solution to update the best solution. To do that, we compare the old fitness value with the new fitness value for each grasshopper. After comparing the old and the new fitness values , we update the
and we increment the counter. Then, we repeat the
until reaching
. If
then stop the
&& return the
. The fitness values for each iteration are presented in the tables below:
Sr. No | iter = 1 | iter = 2 | iter = 3 | iter = 4 | iter = 5 |
---|---|---|---|---|---|
1. | 166.9550 | 165.6341 | 142.5585 | 134.8152 | 130.3828 |
2. | 1.9531 |
148.8582 | 146.0272 | 133.5261 | 131.2371 |
3. | 631.9194 | 221.6412 | 132.7805 | 130.4732 | 127.6477 |
4. | 1.1196 |
141.9027 | 146.6758 | 134.3392 | 132.6764 |
Sr. No | iter = 6 | iter = 7 | iter = 8 | iter = 9 |
---|---|---|---|---|
1. | 127.5254 | 128.8573 | 127.8801 | 124.4451 |
2. | 128.0198 | 126.1510 | 125.0144 | 124.4797 |
3. | 125.9585 | 124.4605 | 124.3374 | 124.3374 |
4. | 138.1068 | 126.8684 | 125.3856 | 124.5798 |
Same as any optimizer algorithm, the GOA algorithm has its benefits and drawbacks. Let’s then take a look at the benefits and drawbacks of the GOA algorithm:
Advantages | Disadvantages |
---|---|
|
|
In this article, we discussed the grasshopper optimization algorithm using a numerical example to unlock its mechanism. This algorithm searches for an optimal global solution to a problem using the swarm population in nature. We introduced this algorithm because it’s highly effective in solving global unconstrained and constrained optimization issues.