Skip to content

Instantly share code, notes, and snippets.

@havenwood
Forked from Poincare/gist:3055730
Created July 19, 2012 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/3145912 to your computer and use it in GitHub Desktop.
Save havenwood/3145912 to your computer and use it in GitHub Desktop.
refactored genetic
class Chromosome
attr_accessor :bit_string, :fitness
def initialize length, bit_string = nil
@length = length
if bit_string.nil?
generate_random length
else
@bit_string = bit_string
end
end
def generate_random length
length.times do
@bit_string ||= ''
@bit_string << rand(2).to_s
end
end
def self.get_random_chromosomes length, number
number.times do
@res ||= []
@res << Chromosome.new(length)
end
set_fitnesses res
res
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment