Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

deleted old files

  • Loading branch information...
commit 4c4c3769cc525abe8c067ddb2aab720736a29906 1 parent 8637b50
@martinstannard authored
Showing with 0 additions and 173 deletions.
  1. +0 −79 lib/genetic.rb
  2. +0 −94 test/test_genetic.rb
View
79 lib/genetic.rb
@@ -1,79 +0,0 @@
-$:.unshift(File.dirname(__FILE__)) unless
-$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-
-module Genetic
- VERSION = '0.0.1'
-
- class Chromosome
-
- attr_reader :genes
- attr_accessor :fitness
-
- def initialize(options = {})
- @genes = options[:genes] || create(options[:length] || 20)
- @fitness = 0
- end
-
- def create(length = 20)
- @genes = (0...length).collect { (rand > 0.5 ? '1' : '0')}.join
- end
-
- def replace(string)
- @genes = string
- end
-
- def cleave(position)
- [@genes[0...position], @genes[position..-1]]
- end
-
- def crossover(other, juncture = rand(@genes.length))
- genes_cleave = cleave juncture
- other_cleave = other.cleave juncture
- replace(genes_cleave[0] + other_cleave[1])
- other.replace(other_cleave[0] + genes_cleave[1])
- end
-
- def mutate(hit = rand(@genes.length))
- @genes[hit] = (@genes[hit] == ?0 ? ?1 : ?0)
- end
-
- end
-
- class Population
-
- attr_reader :pop
-
- def initialize(count = 20, length = 20)
- @pop = Array.new(count) { Chromosome.new({:length => length}) }
- end
-
- def size
- @pop.size
- end
-
- def each
- @pop.each { |p| yield p }
- end
-
- def calculate_fitness
- @pop.each do |p|
- yield p
- end
- end
-
- def sum_fitness
- @pop.inject(0) { |sum, p| sum += p.fitness }
- end
-
- def sort
- @pop.sort! { |a, b| b.fitness <=> a.fitness }
- end
-
- def select(sel = nil)
- selector = sel || rand * sum_fitness
- puts @pop.detect { |c| c.fitness <= selector }
- end
-
- end
-
-end
View
94 test/test_genetic.rb
@@ -1,94 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestChromosome < Test::Unit::TestCase
-
- def setup
- @chromosome = Genetic::Chromosome.new({:genes => '1' * 20})
- @other = Genetic::Chromosome.new({:genes => '0' * 20})
- end
-
- def test_create
- chrom = Genetic::Chromosome.new
- assert chrom.genes.size == 20
- end
-
- def test_cleave_size
- assert @chromosome.cleave(10).size == 2
- end
-
- def test_cleave_lengths
- assert @chromosome.cleave(10)[0].size == 10
- assert @chromosome.cleave(10)[1].size == 10
- end
-
- def test_replace
- assert @chromosome.replace('0' * 20) == '0' * 20
- end
-
- def test_middle_crossover
- @chromosome.crossover(@other, 10)
- assert @chromosome.genes == '1' * 10 + '0' * 10
- assert @other.genes == '0' * 10 + '1' * 10
- end
-
- def test_start_crossover
- @chromosome.crossover(@other, 0)
- assert @chromosome.genes == '0' * 20
- assert @other.genes == '1' * 20
- end
-
- def test_end_crossover
- @chromosome.crossover(@other, @chromosome.genes.length)
- assert @chromosome.genes == '1' * 20
- assert @other.genes == '0' * 20
- end
-
- def test_five_crossover
- @chromosome.crossover(@other, 5)
- assert @chromosome.genes == '1' * 5 + '0' * 15
- assert @other.genes == '0' * 5 + '1' * 15
- end
-
- def test_mutate
- @chromosome.mutate(0)
- assert @chromosome.genes.count('0') == 1
- assert @chromosome.genes == '0' + '1' * 19
- @chromosome.mutate(1)
- assert @chromosome.genes.count('0') == 2
- assert @chromosome.genes == '00' + '1' * 18
- end
-
-end
-
-class TestPopulation < Test::Unit::TestCase
-
- def setup
- @population = Genetic::Population.new(20)
- @population.each {|p| p.fitness = 1 }
- end
-
- def test_population_size
- assert @population.pop.size == 20
- end
-
- def test_sum_fitness
- @population.each {|p| p.fitness = 1 }
- @population.sum_fitness == 20
- @population.each {|p| p.fitness = 5 }
- @population.sum_fitness == 100
- end
-
- def test_sort
- @population.each {|p| p.fitness = rand(100) }
- @population.sort
- puts @population.pop.to_yaml
- (0..@population.size-2).each do |index|
- assert @population.pop[index].fitness <= @population.pop[index+1].fitness
- end
- end
-
- def test_select
- assert @population.select(0.0) == @population.pop[0]
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.