**STEP – 6:** *gBest = 1.0512 Repeat this until stopping Criteria met* (**Table 11**)

#### **5.3 PSO algorithm demo with one more example**

Find the minimum of the function using PSO algorithm.

$$f(\mathbf{x}) = -\mathbf{x}^2 + \mathbf{5x} + \mathbf{20}$$

Using 9 particles with initial positions.

*x*<sup>1</sup> ¼ �9*:*6, *x*<sup>2</sup> ¼ �6, *x*<sup>3</sup> ¼ �2*:*6, *x*<sup>4</sup> ¼ �1*:*1, *x*<sup>5</sup> ¼ 0*:*6, *x*<sup>6</sup> ¼ 2*:*3, *x*<sup>7</sup> ¼ 2*:*8, *x*<sup>8</sup> ¼ 8*:*3, *x*<sup>9</sup> ¼ �10,

#### **Step 1: Evaluation of objective function as**

$$f\_1^0 = -120.16, f\_2^0 = -46, f\_3^0 = 0.24, f\_4^0 = 13.29, f\_5^0 = 22.64, f\_6^0 = 26.21, f\_7^0 = 26.16,$$

$$f\_8^0 = -7.39, f\_9^0 = -30$$

Let C1 = C2 = 1 and set initial velocities of the particles to zero.

$$v\_1^0 = \mathbf{0}, v\_1^0 = v\_2^0, v\_3^0, v\_4^0, v\_5^0, v\_6^0, v\_7^0, v\_8^0, v\_9^0 = \mathbf{0}$$

**Step 2:** Set the iteration number as t = 0 + 1 and go to step 3. **Step 3:** Find the Pbest for every particle.

$$P\_{best,\ i}^{t+1} = \begin{cases} P\_{best,\ i}^t \dot{\sharp} f\_i^{t+1} > P\_{best,\ i}^t \\\\ \varkappa\_i^{t+1} \dot{\sharp} f\_i^{t+1} \le P\_{best,\ i}^t \end{cases}$$

So,

*Particle Swarm Optimization DOI: http://dx.doi.org/10.5772/intechopen.107156*

$$P\_{\text{best, }1}^1 = -9.6, P\_{\text{best, }2}^1 = -6, P\_{\text{best, }3}^1 = -2.6, P\_{\text{best, }4}^1 = -1.1, P\_{\text{best, }5}^1 = 0.6,$$

$$P\_{\text{best, }6}^1 = 2.3, P\_{\text{best, }7}^1 = 2.8, P\_{\text{best, }8}^1 = 8.3, P\_{\text{best, }9}^1 = 10$$

**Step 4:** Gbest = max(Pbest) so Gbest = 2.3.

**Step 5:** Updating the velocities of the particle by considering the value of random numbers r1 =0.213, r2 = 0.876, C1 = C2 = 1, w = 1.

$$v\_{i}^{t+1} = v\_{i}^{t} + \mathbf{C1r}\_{1}^{t} \left[ P\_{\text{best},i}^{t} - \boldsymbol{\pi}\_{i}^{t} \right] + \mathbf{C2r}\_{2}^{t} \left[ G\_{\text{best}}^{t} - \boldsymbol{\pi}\_{i}^{t} \right]; \quad i = 1, 2, 3, \dots, 9.$$

$$v\_{1}^{1} = \mathbf{0} + \mathbf{0.213}(-9.6 + 9.6) + \mathbf{0.876}(2.3 + 9.6) = \mathbf{10.4244}$$

*v*1 <sup>¼</sup> <sup>7</sup>*:*2708, *<sup>v</sup>*<sup>1</sup> <sup>¼</sup> <sup>4</sup>*:*2924, *<sup>v</sup>*<sup>1</sup> <sup>¼</sup> <sup>1</sup>*:*4892, *<sup>v</sup>*<sup>1</sup> <sup>6</sup> <sup>¼</sup> 0, *<sup>v</sup>*<sup>1</sup> ¼ �0*:*4380, *<sup>v</sup>*<sup>1</sup> <sup>¼</sup> <sup>5</sup>*:*256, *<sup>v</sup>*<sup>1</sup> ¼ �6*:*7452

**Step 6:** Update the values of position as well.

$$\mathbf{x}\_{i}^{t+1} = \mathbf{x}\_{i}^{t} + v\_{i}^{t+1}$$

$$\begin{aligned} \mathbf{x}\_{1}^{1} = \mathbf{0.8244}, \mathbf{x}\_{2}^{1} = \mathbf{1.2708}, \mathbf{x}\_{3}^{1} = \mathbf{1.6924}, \mathbf{x}\_{4}^{1} = \mathbf{1.8784}, \mathbf{x}\_{5}^{1} = \mathbf{2.0892}, \mathbf{x}\_{6}^{1} = \mathbf{2.3},\\ \mathbf{x}\_{7}^{1} = \mathbf{2.362}, \mathbf{x}\_{8}^{1} = \mathbf{3.044}, \mathbf{x}\_{9}^{1} = \mathbf{3.2548} \end{aligned} \end{aligned} \text{<8.0892, } \mathbf{x}\_{6}^{1} = \mathbf{2.3},$$

**Step 7:** Finding objective function values of

$$f\_1^1 = 23.4424, f\_2^1 = 24.739, f\_3^1 = 25.5978, f\_4^1 = 25.8636, f\_5^1 = 26.0812, f\_6^1 = 26.21,$$

$$f\_7^1 = 26.231, f\_8^1 = 25.9541, f\_9^1 = 25.6803$$

**Step 8:** Stopping Criteria.

If the terminal rule is satisfied, go to step 2. Otherwise stop the iteration and note the result.

#### **PSO Algorithm Demo with nature inspired hybrid algorithm:**

The optimal integration of Distributed Generation (DG) would be investigated by determining the reduced power loss, enhanced voltage at every bus, reduced total operating cost and enhanced voltage stability of the test network.

In this section, the candidate busses for DG placement and the capacity of the DGs are estimated with the help of Ant-Lion Optimization Algorithm (ALOA) methodology.

Later the elitism phase of the ALOA methodology for updating the DGs location will be done by introducing Particle Swam Optimization Algorithm (PSOA) in ALOA.

The ALOA imitates the ant-lion's hunting mechanism. In ant-lion's life cycle there are two main stages, namely, Larva stage or phase and adult stage. The antlion using its larva stage to find the food and the adult stage for reproduction. The inspiration for this ALOA is the larva stage. The antlion prepare one pit by digging in the sand in the shape of cone by moving in a circular way and throwing the sand out of the pit with its heavy jaw. After preparation of trap the antlion will wait for food. The level of hungry of an antlion and the size of the moon decides the trap size. If any pray come toward the surface of the trap, that will fall into the trap very easily. Then the antlion will catch the pray, when it is identified by the antlion.

The matrices *Mant-lion* and *Mant* stores the random places of the antlions and ants respectively,

*Swarm Intelligence - Recent Advances and Current Applications*

$$M\_{\text{ant-limit}} = \begin{pmatrix} AL\_{1,1} & AL\_{1,2} & \dots & AL\_{1,n} \\ AL\_{2,1} & AL\_{2,2} & \dots & AL\_{2,d} \\ \vdots & \vdots & \vdots & \vdots \\ AL\_{n,1} & AL\_{n,2} & \dots & AL\_{n,d} \end{pmatrix} \tag{19}$$
 
$$M\_{\text{ant}} = \begin{pmatrix} A\_{1,1} & A\_{1,2} & \dots & A\_{1,n} \\ A\_{2,1} & A\_{2,2} & \dots & A\_{2,d} \\ \vdots & \vdots & \vdots & \vdots \\ A\_{n,1} & A\_{n,2} & \dots & A\_{n,d} \end{pmatrix}$$

Antlion's and ant's fitness will be stored in *Mfit-AL* and *Mfit-A* matrices,

$$\begin{aligned} \mathbf{M}\_{fit-AL} &= \begin{pmatrix} f([AL\_{1,1}, AL\_{1,2}, \dots, AL\_{1,n}]) \\ f([AL\_{2,1}, AL\_{2,2}, \dots, AL\_{2,d}]) \\ \vdots \\ f([AL\_{n,1}, AL\_{n,2}, \dots, \dots, AL\_{n,d}]) \end{pmatrix} \\ \mathbf{M}\_{fit-A} &= \begin{pmatrix} f([A\_{1,1}, A\_{1,2}, \dots, \dots, A\_{1,n}]) \\ f([A\_{2,1}, A\_{2,2}, \dots, \dots, A\_{2,d}]) \\ \vdots \\ f([A\_{n,1}, A\_{n,2}, \dots, \dots, A\_{n,d}]) \end{pmatrix} \end{aligned} \tag{20}$$

#### *5.3.1 Ant's random movement*

When ants are moving randomly in search space, then the antlions are ready to find them. All the ants randomly move in search space to find their food is given as,

$$\text{Input position} = [0, \text{cumulative sum}(\text{2}^\* \text{ random number} - 1), \dots$$

$$\text{till max} \, i \, num \, \text{ iteration}] \tag{21}$$

$$random\ number = \begin{cases} \text{1 } \text{if}\,\,rand > 0.5\\ \text{0 } \text{if}\,\,rand \le 0.5 \end{cases} \tag{22}$$

The random movement of ants is restricted within search space. Hence, the ant's position will be updated as,

$$\text{New ant position} \quad = \frac{(\text{old } ant \, position - a\_m) \left(d\_m - c\_m^t\right)}{b\_m - a\_m} + c\_l \tag{23}$$

Where, *am* and *bm* represents minimum and maximum of ant's random walk, *c t m* and *dt <sup>m</sup>* represents minimum and maximum of *mth* variable at *t th* iteration.

#### *5.3.2 Trapping of ants*

The equation for the ant's trapping into ant-lion's pit is as,

$$\begin{aligned} \boldsymbol{c}\_{m}^{t} &= A\boldsymbol{n}t - \textit{l}\boldsymbol{n}\boldsymbol{n}\_{n}^{t} + \boldsymbol{c}^{t} \\ \boldsymbol{d}\_{m}^{t} &= A\boldsymbol{n}t - \textit{l}\boldsymbol{n}\boldsymbol{n}\_{n}^{t} + \boldsymbol{d}^{t} \end{aligned} \tag{24}$$

#### *Construction of trap*

By using roulette wheel method, the best ant-lion is chosen. *Ants sliding toward ant lion*

Ants sliding into the pit are represented by,

$$\begin{aligned} \boldsymbol{c^{t}} &= \frac{\boldsymbol{c^{t}}}{10^{\frac{\text{wt}}{\text{r}}}} \\ \boldsymbol{d^{t}} &= \frac{\boldsymbol{d^{t}}}{10^{\frac{\text{wt}}{\text{r}}}} \\ \boldsymbol{w} &= \begin{cases} 2 & \dot{\boldsymbol{f}}\,\text{ $t>0.1S$ } \\ 3 & \dot{\boldsymbol{f}}\,\text{ $t>0.5S$ } \\ 4 & \dot{\boldsymbol{f}}\,\text{ $t>0.75S$ } \\ 5 & \dot{\boldsymbol{f}}\,\text{ $t>0.9S$ } \\ 6 & \dot{\boldsymbol{f}}\,\text{ $t>0.9S$ } \end{cases} \end{aligned} \tag{26}$$

#### *5.3.3 Catching the pray and reconstruction process of pit*

If the ant's objective function is better than that of the antlions, then the ant-lion moved to a new position to find the opportunity to catch the pray in a better way. The new ant-lion's position is given as,

$$\begin{aligned} \text{Ant}-\text{lion new position} &= \text{Ant position} \text{ for ant objective function} \\ &\text{ant}-\text{lion objective function} \end{aligned} \tag{27}$$

#### *5.3.4 Elitism*

In each stage the best solution is stored called Elitism. Each ant is assumed to be connected to an antlion using roulette wheel and is represented by,

$$\text{eleite\\_ant} = \frac{\text{random\\_walk\\_around\\_the\\_selected\\_ant} - \text{tion}}{2} \tag{28}$$

#### *5.3.5 The particle swarm optimization algorithm (PSOA)*

This optimization algorithm is stochastic population-based method to solve optimization issues. PSO is a nature inspired meta-heuristic algorithm, works with a particles swarm. In a search space every particle is considered as a solution with two characteristics namely, its own velocity and position. The velocity represents the direction and the distance for optimize the position in next iteration. The position defines the present value of the solution. For the entire particle its own present best named as *pbest* and the overall global solution named as *Gbest* estimated.

#### *5.3.6 Mathematical formulation for PSOA*

PSO is a stochastic population-based metaheuristic to solve continuous optimization problems. The main idea of the metaheuristic came from the observation of behavior of natural organisms to find food. PSO works with a swarm of particles. Each particle is a solution to a problem in the decision space and has two characteristics: its own position and velocity. The position represents the current values in the solution; the velocity defines the direction and the distance to optimize the position at next iteration. For each particle *i* its own past best position *p* best *i* and the entire swarm's best overall position G are remembered. In basic PSO the velocity and position of each particle are updated in the following equations,

$$\rho\_i v(k+1) = \omega v\_i(k) + \rho\_1 c\_1 \left(p\_i^{\text{heat}} - X\_i(k)\right) + \rho\_2 c\_2 \left(\text{GX}\_i(k)\right) \tag{29}$$

$$X\_i(k+1) = X\_i(k) + \upsilon\_i(k+1) \tag{30}$$

Where, *i* is a particle index, *k* is an iteration number, *vi*ð Þ*k* is velocity, *Xi*ð Þ*k* is the position of particle *i* at iteration *k*, *p*best *<sup>i</sup>* is the best position found by particle *i* (personal best), *G* is the best position found by the swarm (global best, best of personal bests), *w* is an inertia coefficient, *ρ*1, *ρ*<sup>2</sup> ð Þ are random numbers in [0,1] interval, *c*1, *c*<sup>2</sup> are positive constants representing the factors of particle's attraction toward its own best position or toward the swarm's best position (**Figure 7**).
