The velocity of each particle can be initialized to 0.

The personal best for each particle can be initialized to that particle.

The global best can be initialized to the particle with the best fitness, as given by the objective functions

`Q(p)`

(i.e. `Q(position)`

). Of course, both the

```
velocity' = inertia * velocity + c_1 * r_1 *
(personal_best_position - position) + c_2 * r_2 *
(global_best_position - position)
```

where

`c_1`

is the cognition parameter, `c_2`

is the social parameter, and `r_1`

and `r_2`

are random numbers in [0, 1]. The positions and velocities are actually vectors, so you would implement this formula for both the

The velocity needs to be scaled in order to keep it within range. Otherwise, the positions will become too large and often go out of bounds, and your program will behave wildly. The following is how to scale the velocity:

` if `

`velocity_x`^{2} + velocity_y^{2}

> maximum_velocity^{2}

velocity = (maximum_velocity/sqrt(velocity_x^{2}
+ velocity_y^{2})) * velocity

Again, do this for both the

` position' = position + velocity' `

` if(Q(position) > Q(personal_best_position)) `

personal_best_position = position

` if(Q(position) > Q(global_best_position)) `

global_best_position = position

` error_x += (position_x[k] - global_best_position_x)`^{2}

error_y += (position_y[k] - global_best_position_y)^{2}

for each particle

Then

` error_x = sqrt((1/(2*num_particles))*error_x) `

error_y = sqrt((1/(2*num_particles))*error_y)

This is the measure that should be included as part of your stopping condition.

You can plot this error over the number of iterations to get a qualitative view of the performance.

As mentioned in the Project 5 handout, you can also compute the percentage of particles that have converged to within a small distance of the global maximum. You can also plot this over the number of iterations.

Number of epochs until convergence is another measure of performance.

A purely qualitative assessment is to obtain a scatter plot of the particles after the updating iterations. Below are two examples of what you might see for Problem 2 (two maxima). In the first graph, most of the points have converged to the global maximum, although they are covered up by the global maximum point on the graph.

The more performance measures and graphs you analyze, the higher grade you can earn.

The velocity formula is modified as follows:

```
velocity' = inertia * velocity + c_1 * r_1 *
(personal_best_position - position) + c_2 * r_2 *
(global_best_position - position) + c_3 * r_3 *
(local_best_position - position)
```

The best neighbor is the particle whose

`Q(position)`

is
the largest. You can try k-nearest neighbors for extra credit. For example, 4-nearest neighbors are given by the 2 elements before and the 2 elements after the given particle.

For the Euclidean distance method (extra credit), the nearest neighbors of a particle are those that are within a given radius as measured by Euclidean distance. The radius is another parameter that can vary over a range of values.

You can also try other topologies for extra credit, such as star or wheel.