Created
October 5, 2012 02:49
-
-
Save mungruby/3837791 to your computer and use it in GitHub Desktop.
LiddleLizzard - a digital organism
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 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