class GA

Attributes

avg_log[R]
best_log[R]

Public Class Methods

new(&block) click to toggle source
# File lib/rbga.rb, line 13
def initialize(&block)
    instance_eval &block
    @population = Population.new @population_size, @chromosome_type, @chromosome_length, @mutation_method, @mutation_probability
    @best_log = []
    @avg_log = []
end

Public Instance Methods

avg() click to toggle source
# File lib/rbga.rb, line 112
def avg
    avg_fit = 0
    @population.each do |ch|
        avg_fit += ch.fitness
    end
    return avg_fit/@population.size
end
best() click to toggle source
# File lib/rbga.rb, line 99
def best
    best_idx = 0
    best_fit = nil
    @population.each_index do |idx|
        ch = @population[idx]
        ch.fitness = @fitness.call ch
        best_fit = ch.fitness if best_fit==nil
        best_idx = idx if best_fit < ch.fitness
        best_fit = ch.fitness if best_fit < ch.fitness
    end
    return @population[best_idx]
end
chromosome_length(len) click to toggle source
# File lib/rbga.rb, line 24
def chromosome_length(len)
    @chromosome_length = len
end
chromosome_type(type) click to toggle source
# File lib/rbga.rb, line 20
def chromosome_type(type)
    @chromosome_type = type
end
crossover(chs1, chs2) click to toggle source
# File lib/rbga.rb, line 86
def crossover(chs1, chs2)
    send(@crossover_method, chs1, chs2)
end
crossover_method(crossover) click to toggle source
# File lib/rbga.rb, line 32
def crossover_method(crossover)
    @crossover_method = crossover
end
evolve(generations) click to toggle source
# File lib/rbga.rb, line 52
def evolve(generations)
    generations.times do |i|
        offspring = Population.new @population_size, @chromosome_type, @chromosome_length, @mutation_method, @mutation_probability
        offspring.clear
        offspring << @population
        offspring.flatten!(1)
        @population.size.times do |i|
            chs1 = @population[select]
            chs2 = @population[select]
            while chs1==chs2
                chs2 = @population[select]
            end
            new_chs = crossover(chs1,chs2)
            while offspring.include?(new_chs[0]) or offspring.include?(new_chs[1])
                chs1 = @population[select]
                chs2 = @population[select]
                while chs1==chs2
                    chs2 = @population[select]
                end
                new_chs = crossover(chs1,chs2)
            end
            offspring << new_chs[0] << new_chs[1]
        end
        survive offspring
        @best_log << best.fitness
        @avg_log << avg
    end

end
mutation_method(mutation) click to toggle source
# File lib/rbga.rb, line 36
def mutation_method(mutation)
    @mutation_method = mutation
end
mutation_probability(prob) click to toggle source
# File lib/rbga.rb, line 40
def mutation_probability(prob)
    @mutation_probability = prob
end
population_size(size) click to toggle source
# File lib/rbga.rb, line 28
def population_size(size)
    @population_size = size
end
select() click to toggle source
# File lib/rbga.rb, line 82
def select
    send(@selection_method)
end
selection_method(selection) click to toggle source
# File lib/rbga.rb, line 44
def selection_method(selection)
    @selection_method = selection
end
set_fitness_eval(&block) click to toggle source
# File lib/rbga.rb, line 90
def set_fitness_eval(&block)
    @fitness = block
end
survive(population) click to toggle source
# File lib/rbga.rb, line 94
def survive(population)
    population.each { |e| e.fitness = @fitness.call e }
    send(@survive_method, population)
end
survive_method(survive) click to toggle source
# File lib/rbga.rb, line 48
def survive_method(survive)
    @survive_method = survive
end