public
Created

LiddleLizzard - a digital organism

  • Download Gist
liddle_lizzard.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class LiddleLizzard
 
CHROMOSOME_LENGTH = 500
 
include Comparable
 
attr_accessor :chromosome
 
def initialize
@chromosome = BitString.new(CHROMOSOME_LENGTH).bit_string
end
 
def <=>(other)
self.fitness <=> other.fitness
end
 
# calculates the "fitness" of this chromosome
def fitness
score = 0
chromosome.scan(/1*/).each do |str|
score = str.length if str.length > score
end
score
end
 
# mutates this chromosome
def mutate
chromosome[rand(500)-1] = '1'
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.