# Chapter 2: Personal Best and Velocity Update #

## 2.1 Defining Personal Best #

In Particle Swarm Optimization (PSO), the concept of personal best, or pBest, plays a pivotal role in guiding particles towards optimal solutions. Let’s dive into what personal best is and why it’s so important in the context of PSO.

### 2.1.1 Concept of Personal Best #

At its core, personal best represents the best position a particle has visited based on the objective function value. You can think of it as a particle’s personal memory of success. Each particle in the swarm maintains its own personal best, which serves as a reference point for its individual improvement throughout the optimization process.

Imagine a particle exploring a vast search space, seeking the optimal solution to a complex problem. As it moves through various positions, the particle evaluates the quality of each position using the objective function. Whenever the particle discovers a position that is better than its current personal best, it updates its personal best to reflect this new, superior solution.

Updating the personal best is a simple yet crucial process. At each iteration, the particle compares its current position with its personal best. If the current position yields a better objective function value, the personal best is updated accordingly. This way, the particle keeps track of its own best-known solution, ensuring that it never loses sight of its most promising findings.

### 2.1.2 Importance in PSO #

The personal best serves as a guiding light for each particle, attracting it towards its own best-known positions. By maintaining a record of its individual success, a particle is encouraged to explore the areas surrounding its personal best, potentially discovering even better solutions in the process.

In the grand scheme of PSO, personal best plays a crucial role in balancing exploration and exploitation. While the global best (which we’ll discuss in the next chapter) promotes exploration by guiding particles towards the best solution found by the entire swarm, personal best encourages exploitation of good solutions. This balance is essential for preventing premature convergence to suboptimal solutions and maintaining diversity within the swarm.

Moreover, personal best is a key factor in updating a particle’s velocity. In the velocity update equation, the cognitive component is directly influenced by the particle’s personal best. This component contributes to the direction and magnitude of the particle’s movement, steering it towards its own best-known position.

By having each particle maintain its own personal best, PSO ensures that the swarm remains diverse and adaptable. Even if some particles converge towards suboptimal solutions, others may still explore different regions of the search space, guided by their personal bests. This diversity is crucial for the swarm’s ability to escape local optima and find globally optimal solutions.

## 2.2 Velocity Update Formula #

In the heart of Particle Swarm Optimization (PSO) lies the velocity update formula, a crucial equation that guides particles towards optimal solutions. This formula determines how each particle adjusts its velocity in response to its personal best and the global best found by the swarm. Let’s dive into the basic velocity update equation and explore its components in detail.

### 2.2.1 Basic Velocity Update Equation #

The velocity update equation in PSO consists of three main components: the inertia term, the cognitive component, and the social component. Mathematically, the velocity update equation for a particle can be represented as:

```
v_i(t+1) = w * v_i(t) + c1 * r1 * (pBest_i - x_i(t)) + c2 * r2 * (gBest - x_i(t))
```

where:

`v_i(t+1)`

is the new velocity of particle i`v_i(t)`

is the current velocity of particle i`w`

is the inertia weight`c1`

and`c2`

are the cognitive and social coefficients, respectively`r1`

and`r2`

are random numbers between 0 and 1`pBest_i`

is the personal best position of particle i`gBest`

is the global best position found by the swarm`xi(t)`

is the current position of particle i

The inertia term (`w * v_i(t)`

) helps maintain the particle’s current direction, while the cognitive and social components guide the particle towards its personal best and the global best, respectively. By balancing these components, the velocity update equation enables particles to explore the search space effectively and converge towards optimal solutions.

### 2.2.2 Cognitive Component in Depth #

The cognitive component (`c1 * r1 * (pBesti - xi(t))`

) plays a vital role in the velocity update equation. It represents the particle’s tendency to move towards its own best-known position, the personal best (`pBesti`

). The cognitive coefficient (`c1`

) determines the weight given to the particle’s personal experience.

When the cognitive component is large, particles are strongly attracted towards their personal bests, encouraging exploitation of promising regions. Conversely, a smaller cognitive component allows particles to explore more freely, potentially discovering new optimal solutions.

Balancing the cognitive component with the other terms in the velocity update equation is crucial. The cognitive coefficient (`c1`

) can be adjusted to control the influence of personal best on the particle’s movement. Higher values of `c1`

emphasize exploitation, while lower values promote exploration.

The cognitive component helps maintain diversity within the swarm by allowing each particle to explore independently based on its own experience. This diversity is essential for preventing premature convergence and enabling the swarm to escape local optima.

## 2.3 Influence of Personal Experience #

### 2.3.1 Psychological Analogy: Learning from Personal Best #

The concept of personal best in Particle Swarm Optimization (PSO) bears a striking resemblance to the human psychological process of learning from past successes. Just as particles in PSO remember and are influenced by their best positions, humans tend to learn from their most successful experiences. This parallel highlights the importance of reinforcement learning, where behavior that leads to positive outcomes is strengthened and encouraged.

In PSO, particles are inherently drawn towards their personal best positions, reinforcing the exploration of promising regions in the search space. This behavior mirrors how humans often gravitate towards strategies and actions that have yielded favorable results in the past. By exploiting personal knowledge while balancing exploration, both particles and humans can navigate complex problem spaces effectively.

### 2.3.2 Impact on Search Behavior #

#### 2.3.2.1 Benefits of Strong Personal Experience #

When particles have discovered good personal bests, they can quickly converge towards these promising regions, accelerating the optimization process. This strong attraction to personal bests allows particles to thoroughly exploit and refine solutions in high-potential areas, increasing the chances of finding optimal solutions efficiently.

```
# Pseudocode illustrating the influence of personal best on particle movement
if self.current_fitness < self.personal_best_fitness:
self.personal_best_position = self.current_position.copy()
self.personal_best_fitness = self.current_fitness
```

#### 2.3.2.2 Limitations of Strong Personal Experience #

However, an excessive focus on personal bests can also pose limitations. If particles are too strongly influenced by their personal experiences, they may converge prematurely to suboptimal solutions, getting stuck in local optima. This strong bias towards personal bests can reduce exploration and diversity within the swarm, potentially causing the particles to miss global optima located in unexplored regions of the search space.

#### 2.3.2.3 Balancing Personal Experience and Exploration #

To mitigate these limitations, it is crucial to maintain a balance between the influence of personal experience and exploration. Ensuring particles have a healthy mix of personal best influence and exploratory behavior is essential for avoiding premature convergence and stagnation. The cognitive coefficient (`c1`

) plays a vital role in controlling the strength of personal best influence. By adjusting this coefficient, you can tune the balance between exploitation and exploration.

Moreover, various strategies can be employed to promote exploration while still leveraging personal best knowledge. Techniques such as time-varying coefficients, where the cognitive coefficient decreases over time, can encourage exploration in the early stages and exploitation later on. Multi-swarm approaches, where multiple subswarms independently explore different regions, can also help maintain diversity and prevent stagnation.

By understanding the psychological analogy of learning from personal best and carefully balancing its influence with exploration, you can harness the power of personal experience in PSO while avoiding its potential pitfalls, ultimately leading to more effective and efficient optimization.

## Exercises #

This exercise builds upon the basic PSO model introduced in Chapter 1, focusing on enhancing the script to incorporate personal best tracking and velocity updates based on personal best. By the end of this exercise, you will have a more advanced PSO implementation that demonstrates the influence of personal best on particle movement.

### Exercise 1: Enhancing the Basic Model #

Modify your initial Python script from Chapter 1 to include a `Particle`

class that keeps track of each particle’s current position, velocity, current fitness, and personal best tracking. Use the following suggestions to guide your implementation:

Create a new attributes, including the new

`personal_best_position`

and`personal_best_fitness`

, for each particle, initialized to the particle’s starting position.After evaluating each particle’s fitness in the main PSO loop, compare the current fitness with the fitness of the particle’s

`personal_best`

. If the current fitness is better, update the`personal_best`

to the current position.

### Exercise 2: Coding the Velocity Update #

Incorporate the velocity update formula into your PSO script, focusing on calculating and updating velocities based on personal best. Use the following suggestions to guide your implementation:

Define the cognitive coefficient (

`c1`

) and the inertia weight (`w`

) as constants or parameters in your script.Inside the main PSO loop, after updating the personal best, calculate the new velocity for each particle using the velocity update formula that includes the personal best information:

```
# Pseudocode for velocity update
cognitive_component = c1 * random.random() * (particle.personal_best - particle.current_position)
new_velocity = w * particle.velocity + cognitive_component
```

- Update the particle’s position based on the new velocity:

```
# Pseudocode for position update
particle.current_position += new_velocity
```

- Ensure that the particle’s position remains within the defined search space boundaries. If a particle exceeds the boundaries, you can either clip its position to the nearest boundary or implement a boundary handling technique like reflection or wrapping. Also clip the particle velocity to a reasonable bound, such as [-1, 1].

### Exercise 3: Visualization and Observation #

Enhance your visualization code to represent the changes in particle movement due to the personal best-driven velocity update. Consider the following:

Use different colors or markers to distinguish between the particles’ current positions and their personal best positions.

Optionally, draw lines or arrows connecting each particle’s current position to its personal best position to visualize the influence of personal best on the particle’s movement.

Run the PSO algorithm with the updated personal best and velocity update components, and observe the particles’ movement in the visualization. Pay attention to any emergent patterns or behaviors:

- Do the particles tend to converge towards their personal best positions over time?
- Are there any instances of particles overshooting or oscillating around their personal best positions?
- How does the convergence pattern compare to the basic PSO model without personal best influence?

Experiment with different values for the cognitive coefficient (

`c1`

) and inertia weight (`w`

) to see how they affect the particles’ behavior and convergence. Discuss your findings and any interesting observations.

By completing this exercise, you will gain practical experience in implementing personal best tracking and velocity updates in PSO. You’ll witness firsthand how personal best influences particle movement and contributes to the optimization process. The visualization will provide valuable insights into the convergence patterns and the impact of personal experience on the swarm’s behavior.

## Answers #

## Show

### Exercise 1: Enhancing the Basic Model #

To enhance the basic PSO model by tracking the personal best for each particle, we can modify the Python script as follows:

**Define the Particle Class**: Start by defining a`Particle`

class to include the current position, velocity, current fitness, and personal best attributes. We can also add a method to update the personal best.

```
import numpy as np
class Particle:
def __init__(self, position):
self.current_position = np.array(position)
self.velocity = np.random.uniform(0, 1, size=np.shape(position))
self.current_fitness = float('inf')
self.personal_best_position = np.array(position)
self.personal_best_fitness = float('inf')
def update_personal_best(self):
if self.current_fitness < self.personal_best_fitness:
self.personal_best_position = self.current_position.copy()
self.personal_best_fitness = self.current_fitness
```

**PSO Algorithm Modifications**: In the main PSO loop, after calculating the fitness for each particle, update the personal best.

```
def pso(num_particles, num_iterations, search_space):
low, high = (search_space[0,0],search_space[1,0]), (search_space[0,1],search_space[1,1])
particles = [Particle(np.random.uniform(low, high, 2)) for _ in range(num_particles)]
for _ in range(num_iterations):
for particle in particles:
particle.current_fitness = evaluate_fitness(particle.current_position)
particle.update_personal_best()
```

Here, `evaluate_fitness`

is a function you would define based on your specific optimization problem.

### Exercise 2: Coding the Velocity Update #

To incorporate the velocity update based on personal best, follow these steps:

**Constants Definition**: Define the cognitive coefficient (`c1`

) and inertia weight (`w`

) at the start of your PSO function.

```
c1 = 2.05
w = 0.7
```

**Velocity and Position Update**: Inside the main PSO loop, update the velocity and position for each particle using the new formula.

```
import random
for _ in range(num_iterations):
for particle in particles:
cognitive_component = c1 * random.random() * (particle.personal_best_position - particle.current_position)
particle.velocity = w * particle.velocity + cognitive_component
particle.current_position += particle.velocity
```

**Boundary Handling**: Use`np.clip`

to ensure that particles do not exceed the defined boundaries of the search space.

```
particle.current_position = np.clip(particle.current_position, search_space[0], search_space[1])
particle.current_position = np.clip(particle.velocity, -1, 1)
```

### Exercise 3: Visualization and Observation #

For visualization, you can extend the plotting functions to show both the current and personal best positions of the particles:

```
import matplotlib.pyplot as plt
def plot_particles(particles):
fig, ax = plt.subplots()
for particle in particles:
ax.scatter(*particle.current_position, color='blue', marker='o', label='Current Position' if particle == particles[0] else "")
ax.scatter(*particle.personal_best_position, color='red', marker='x', label='Personal Best' if particle == particles[0] else "")
ax.legend()
ax.set_title('Particle Positions and Personal Bests')
plt.show()
```

Tying this together, the complete PSO algorithm with local best tracking is listed below:

```
import numpy as np
import random
import matplotlib.pyplot as plt
class Particle:
def __init__(self, position):
self.current_position = np.array(position)
self.velocity = np.random.uniform(-1, 1, size=np.shape(position))
self.current_fitness = float('inf')
self.personal_best_position = np.array(position)
self.personal_best_fitness = float('inf')
def update_personal_best(self):
if self.current_fitness < self.personal_best_fitness:
self.personal_best_position = self.current_position.copy()
self.personal_best_fitness = self.current_fitness
def rosenbrock(x, y):
return (1 - x)**2 + 100 * (y - x**2)**2
def pso(num_particles, num_iterations, search_space):
low, high = (search_space[0,0],search_space[1,0]), (search_space[0,1],search_space[1,1])
particles = [Particle(np.random.uniform(low, high, 2)) for _ in range(num_particles)]
c1 = 2.05
w = 0.7
for i in range(num_iterations):
for particle in particles:
particle.current_fitness = rosenbrock(*particle.current_position)
particle.update_personal_best()
cognitive_component = c1 * random.random() * (particle.personal_best_position - particle.current_position)
particle.velocity = w * particle.velocity + cognitive_component
particle.current_position += particle.velocity
particle.current_position = np.clip(particle.current_position, search_space[0], search_space[1])
particle.current_position = np.clip(particle.velocity, -1, 1)
if i % 10 == 0: # Plot every 10 iterations
plot_particles(particles, i)
def plot_particles(particles, iteration):
fig, ax = plt.subplots()
for particle in particles:
ax.scatter(*particle.current_position, color='blue', marker='o')
ax.scatter(*particle.personal_best_position, color='red', marker='x')
ax.set_xlim([-2, 2])
ax.set_ylim([-1, 3])
ax.set_title(f'Iteration {iteration}')
plt.show()
# PSO parameters
num_particles = 50
num_iterations = 100
search_space = np.array([[-2, 2], [-1, 3]]) # Define the search space boundaries for x and y
# Execute PSO
pso(num_particles, num_iterations, search_space)
```

## Summary #

Chapter 2 explored the concept of personal best and its pivotal role in guiding particles towards optimal solutions in Particle Swarm Optimization (PSO). The chapter defined personal best as the best position a particle has visited based on the objective function value, serving as a particle’s individual memory of success. The importance of personal best in balancing exploration and exploitation was highlighted, along with its influence on the velocity update equation. The chapter also provided an in-depth explanation of the velocity update formula, breaking down its components, including the inertia term, cognitive component, and social component. The psychological analogy of learning from personal best was explored, drawing parallels to human learning from past successes. The chapter discussed the impact of personal experience on search behavior, the benefits and limitations of strong personal experience, and strategies for balancing personal experience and exploration. Practical exercises were provided to enhance the basic PSO model with personal best tracking and velocity updates, enabling readers to observe the influence of personal best on particle movement through visualization.

### Key Takeaways #

- Personal best represents a particle’s individual memory of success, guiding it towards promising regions in the search space.
- The velocity update equation, consisting of the inertia term, cognitive component, and social component, determines how particles adjust their velocities based on personal and global best positions.
- Balancing the influence of personal experience with exploration is crucial to avoid premature convergence and maintain diversity in the swarm.

### Exercise Encouragement #

Roll up your sleeves and dive into the world of personal best in PSO! The exercises in this chapter will give you hands-on experience in implementing personal best tracking and velocity updates. You’ll witness firsthand how personal best influences particle movement and contributes to the optimization process. Don’t be afraid to experiment with different parameters and observe the emergent patterns in the visualization. Your efforts will deepen your understanding of PSO and equip you with valuable skills for tackling real-world optimization problems. Embrace the challenge, and let your curiosity guide you towards mastering the power of personal best in PSO!

### Glossary: #

**Personal Best (pBest)**: The best position encountered by an individual particle so far, based on the objective function value.**Velocity Update Equation**: The mathematical formula that determines how a particle’s velocity is updated based on its current velocity, personal best, and global best.**Cognitive Component**: The term in the velocity update equation that represents the influence of a particle’s personal best on its movement.**Social Component**: The term in the velocity update equation that represents the influence of the global best on a particle’s movement.**Inertia Term**: The component in the velocity update equation that helps maintain a particle’s current direction.**Cognitive Coefficient (c1)**: A parameter that determines the weight given to a particle’s personal experience in the velocity update equation.**Exploitation**: The process of thoroughly exploring and refining solutions in high-potential areas discovered by particles.**Exploration**: The process of searching for new optimal solutions in unexplored regions of the search space.**Premature Convergence**: The phenomenon where particles converge too quickly to suboptimal solutions, getting stuck in local optima.**Boundary Handling**: Techniques used to ensure that particles remain within the defined search space boundaries during the optimization process.

### Next Chapter: #

In Chapter 3, we’ll explore the concept of global best and its impact on the collective behavior of the swarm, discussing how social interaction influences particle movement and strategies for balancing exploration and exploitation.