-
-
Save havenwood/3145912 to your computer and use it in GitHub Desktop.
refactored genetic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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