Skip to content

Instantly share code, notes, and snippets.

@mungruby
Created October 5, 2012 02:49
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 mungruby/3837791 to your computer and use it in GitHub Desktop.
Save mungruby/3837791 to your computer and use it in GitHub Desktop.
LiddleLizzard - a digital organism
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment