<jupyter_start><jupyter_text>Particle swarm optimizationThe method uses a group of particles which move in the search space. Each partcle is represented by a vector x with length n (depending on the given problem), indicating the position in the n-dimensional space and it has a direction v to be used to update the current position.The velocity (direction) vector v counts the following rules:- each particle wants to maintain its current direction- each particle is attracted by the best position (value) it reached so far- each particle is attracted by the best particle in the swarm (in the population)Thus, at each interation of the algorithm, each particle updates its position and velocity from the previous iteration t as follows:- v[t+1] = w1 \* v[t] + w2 \* rand() \* (p-x) + w3 \* rand() \* (g-x)- x[t+1] = x[t] + v[t+1]Parameters:- w1 - inertion parameter - it helps the particle hold its balance in between exploration and exploatation- w2 - cognitive parameter - the tendency to duplicate actions from the past that proved to be successfull- w3 - social parameter - the tendency to follow the success of the other particles in the swarm ---  Libraries- Numpy for linear algebra operations<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Rastrigin - DEMO Function- n - number of input parameters- minx, maxx - the domain of the function<jupyter_code>minx, maxx = -5.12, 5.12
n = 3
def rastrigin(x):
    return 10 * len(x) + sum([xi * xi - 10 * np.cos(2 * 3.14 * xi) for xi in x])<jupyter_output><empty_output><jupyter_text>---  Particle classHolds all the information needed for the particle to evolve and its past best information- value - vector of dimension n- velocity - the velocity vector for the particle- fitness - score value of the demo rastrigin function- best_value - stores the best position in the history of the particle- best_fitness - stores the best score value in the history of the particle<jupyter_code>class Particle:
    def __init__(self):
        self.value = (maxx - minx) * np.random.rand(n) + minx
        self.velocity = np.random.rand(n)
        self.fitness = rastrigin(self.value)
        
        self.best_value = np.copy(self.value)
        self.best_fitness = self.fitness<jupyter_output><empty_output><jupyter_text>---  Actual algorithmThe algorithm runs for 500 iterations with a swarm size of 50. At each iteration, update the velocity, the position value, the fitness score and the particles best value and fitness for each particle in the swarm. Also, store and update the best particle information (position value and fitness).Three hyper-parameters are used: w1, w2, w3. These should be updated depending on the problem at hand. For the Rastrigin DEMO function, this setup gives good results. A superviser genetic algorithm can be used to optimize the hyper-parameters.<jupyter_code># hyper-parameters
w1 = 0.7
w2 = 1.4
w3 = 1.4

swarm_size = 50
swarm = [ Particle() for i in range(swarm_size) ]

best_swarm_fitness = 1000
for i in range(swarm_size):
    if swarm[i].fitness < best_swarm_fitness:
        best_swarm_fitness = swarm[i].fitness
        best_swarm_value = swarm[i].value
        
for iteration in range(500):
    for i in range(swarm_size):
        swarm[i].velocity = w1 * swarm[i].velocity + w2 * np.random.rand(n) * (swarm[i].best_value - swarm[i].value) + w3 * np.random.rand(n) * (best_swarm_value - swarm[i].value)
        swarm[i].value = swarm[i].value + swarm[i].velocity
        swarm[i].fitness = rastrigin(swarm[i].value)
        if swarm[i].fitness < swarm[i].best_fitness:
            swarm[i].best_value = swarm[i].value
            swarm[i].best_fitness = swarm[i].fitness
        if swarm[i].fitness < best_swarm_fitness:
            best_swarm_fitness = swarm[i].fitness
            best_swarm_value = swarm[i].value

print("Solution: ", best_swarm_value)
print("Fitness: ", best_swarm_fitness)<jupyter_output>Solution:  [  1.35520013e-09   1.65970072e-09  -1.65246978e-09]
Fitness:  0.0
{'fraction_non_alphanumeric': 0.09070464767616192, 'fraction_numerical': 0.021989005497251374, 'mean_word_length': 3.782556750298686, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '105861', 'license': 'MIT', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1210, 'n_words': 454, 'path': 'particle_swarm_optimization.ipynb', 'repo_name': 'ashcrok/particle-swarm-optimization', 'size': '4002'}