class Algorithmically::Swarm::ParticleSwarm

Public Class Methods

new(problem_size, max_gens, search_space, vel_space, pop_size, max_vel, c1, c2) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 6
def initialize(problem_size, max_gens, search_space, vel_space, pop_size, max_vel, c1, c2)
  @search_space, @vel_space = search_space, vel_space
  @search_space = Array.new(problem_size) {|i| [-5, 5]}
  @vel_space = Array.new(problem_size) {|i| [-1, 1]}
  best = search(max_gens, @search_space, @vel_space, pop_size, max_vel, c1, c2)
  puts "done! Solution: f=#{best[:cost]}, s=#{best[:position].inspect}"
end

Public Instance Methods

create_particle(search_space, vel_space) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 24
def create_particle(search_space, vel_space)
  particle = {}
  particle[:position] = random_vector(search_space)
  particle[:cost] = objective_function(particle[:position])
  particle[:b_position] = Array.new(particle[:position])
  particle[:b_cost] = particle[:cost]
  particle[:velocity] = random_vector(vel_space)
  particle
end
get_global_best(population, current_best=nil) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 34
def get_global_best(population, current_best=nil)
  population.sort! { |x, y| x[:cost] <=> y[:cost] }
  best = population.first
  if current_best.nil? or best[:cost] <= current_best[:cost]
    current_best = {}
    current_best[:position] = Array.new(best[:position])
    current_best[:cost] = best[:cost]
  end
  current_best
end
objective_function(vector) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 14
def objective_function(vector)
  vector.inject(0.0) { |sum, x| sum + (x ** 2.0) }
end
random_vector(minmax) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 18
def random_vector(minmax)
  Array.new(minmax.size) do |i|
    minmax[i][0] + ((minmax[i][1] - minmax[i][0]) * rand())
  end
end
update_best_position(particle) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 68
def update_best_position(particle)
  return if particle[:cost] > particle[:b_cost]
  particle[:b_cost] = particle[:cost]
  particle[:b_position] = Array.new(particle[:position])
end
update_position(part, bounds) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 55
def update_position(part, bounds)
  part[:position].each_with_index do |v, i|
    part[:position][i] = v + part[:velocity][i]
    if part[:position][i] > bounds[i][1]
      part[:position][i]=bounds[i][1]-(part[:position][i]-bounds[i][1]).abs
      part[:velocity][i] *= -1.0
    elsif part[:position][i] < bounds[i][0]
      part[:position][i]=bounds[i][0]+(part[:position][i]-bounds[i][0]).abs
      part[:velocity][i] *= -1.0
    end
  end
end
update_velocity(particle, gbest, max_v, c1, c2) click to toggle source
# File lib/Algorithmically/Swarm/particle_swarm.rb, line 45
def update_velocity(particle, gbest, max_v, c1, c2)
  particle[:velocity].each_with_index do |v, i|
    v1 = c1 * rand() * (particle[:b_position][i] - particle[:position][i])
    v2 = c2 * rand() * (gbest[:position][i] - particle[:position][i])
    particle[:velocity][i] = v + v1 + v2
    particle[:velocity][i] = max_v if particle[:velocity][i] > max_v
    particle[:velocity][i] = -max_v if particle[:velocity][i] < -max_v
  end
end