![]() |
Previous article Grey wolf optimization- Introduction talked about inspiration of grey wolf optimization, and its mathematical modelling and algorithm. In this article we will implement grey wolf optimization (GWO) for two fitness functions – Rastrigin function and Sphere function. The aim of Grey wolf optimization algorithm is to find minimize of fitness function. Fitness Functions:1) Rastrigin function: Rastrigin function is a non-convex function used as a performance test problem for optimization algorithms. Function equation:
[Tex]f(x_1 \cdots x_n) = 10n + \sum_{i=1}^n (x_i^2 -10cos(2\pi x_i)) [/Tex]
[Tex]\text{minimum at }f(0, \cdots, 0) = 0 [/Tex]
![]() Figure 1 Rastrigin function of 2 variables
Rastrigin Function is one of the most challenging functions for an optimization problem. Having a lot of cosine oscillations on the plane introduces a myriad of local minimums in which particles can get stuck.
2) Sphere function: Sphere function is used as a performance test problem for optimization algorithms.
Function equation:
[Tex]f(x_1 \cdots x_n) = \sum_{i=1}^n x_i^2 [/Tex]
[Tex]\text{minimum at }f(0, \cdots, 0) = 0 [/Tex]
![]() Figure2: Sphere function of two variables Choice of hyper-parameters
Pseudocode:Step1: Randomly initialize Grey wolf population of N particles Xi ( i=1, 2, …, n) Step2: Calculate the fitness value of each individuals sort grey wolf population based on fitness values alpha_wolf = wolf with least fitness value beta_wolf = wolf with second least fitness value gamma_wolf = wolf with third least fitness value Step 3: For Iter in range(max_iter): # loop max_iter times calculate the value of a a = 2*(1 - Iter/max_iter) For i in range(N): # for each wolf a. Compute the value of A1, A2, A3 and C1, C2, C3 A1 = a*(2*r1 -1), A2 = a*(2*r2 -1), A3 = a*(2*r3 -1) C1 = 2*r1, C2 = 2*r2, C3 = 2*r3 b. Computer X1, X2, X3 X1 = alpha_wolf.position - A1*abs(C1*alpha_wolf_position - ith_wolf.position) X2 = beta_wolf.position - A2*abs(C2*beta_wolf_position - ith_wolf.position) X3 = gamma_wolf.position - A3*abs(C3*gamma_wolf_position - ith_wolf.position) c. Compute new solution and it's fitness Xnew = (X1 + X2 + X3) / 3 fnew = fitness( Xnew) d. Update the ith_wolf greedily if( fnew < ith_wolf.fitness) ith_wolf.position = Xnew ith_wolf.fitness = fnew End-for # compute new alpha, beta and gamma sort grey wolf population based on fitness values alpha_wolf = wolf with least fitness value beta_wolf = wolf with second least fitness value gamma_wolf = wolf with third least fitness value End -for Step 4: Return best wolf in the population Implementation:
Python3
Output:Begin grey wolf optimization on rastrigin function Goal is to minimize Rastrigin's function in 3 variables Function has known min = 0.0 at (0, 0, 0) Setting num_particles = 50 Setting max_iter = 100 Starting GWO algorithm Iter = 10 best fitness = 2.996 Iter = 20 best fitness = 2.749 Iter = 30 best fitness = 0.470 Iter = 40 best fitness = 0.185 Iter = 50 best fitness = 0.005 Iter = 60 best fitness = 0.001 Iter = 70 best fitness = 0.001 Iter = 80 best fitness = 0.001 Iter = 90 best fitness = 0.000 GWO completed Best solution found: ['0.000706', '-0.000746', '-0.000526'] fitness of best solution = 0.000264 End GWO for rastrigin Begin grey wolf optimization on sphere function Goal is to minimize sphere function in 3 variables Function has known min = 0.0 at (0, 0, 0) Setting num_particles = 50 Setting max_iter = 100 Starting GWO algorithm Iter = 10 best fitness = 0.001 Iter = 20 best fitness = 0.001 Iter = 30 best fitness = 0.000 Iter = 40 best fitness = 0.000 Iter = 50 best fitness = 0.000 Iter = 60 best fitness = 0.000 Iter = 70 best fitness = 0.000 Iter = 80 best fitness = 0.000 Iter = 90 best fitness = 0.000 GWO completed Best solution found: ['-0.000064', '0.000879', '-0.000934'] fitness of best solution = 0.000002 End GWO for sphere References:
|
Reffered: https://www.geeksforgeeks.org
Machine Learning |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |