mail@pastecode.io avatar
7 months ago
1.6 kB
  Population size (N)
  Maximum number of iterations (max_iter)
  Inertia weight (w)
  Cognitive influence factor (c1)
  Social influence factor (c2)

  Initialize the population of particles randomly within the search space
  Initialize particle velocities randomly
  Initialize personal best (Pbest) positions for each particle
  Initialize global best (Gbest) position

Main Loop:
  Repeat for each iteration until max_iter is reached:
    For eachparticle i in the population:
      Calculate the fitness value of particle i's current position

      Update Pbest for particle i:
        If the fitness of particle i's current position is better than its previous Pbest fitness:
          Update particle i's Pbest position

      Update Gbest:
        If the fitness of particle i's current position is better than the fitness of Gbest:
          Update Gbest position to particle i's current position

      Update particle velocity and position:
        For each dimension d in the particle's position and velocity:
          Generate random values r1 and r2

          Update particle velocity:
            New velocity = (w * old velocity) + (c1 * r1 * (Pbest - current position)) + (c2 * r2 * (Gbest - current position))

          Update particle position:
            New position = current position + new velocity

          Ensure particle position is within the boundaries of the search space

End of Main Loop

  Return the Gbest position as the optimal solution
Leave a Comment