Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 2042c84ccb7e961d402e5cab8d24da952dd4fd21 @martinstannard committed Dec 17, 2008
Showing with 262 additions and 0 deletions.
  1. +4 −0 History.txt
  2. +11 −0 Manifest.txt
  3. +7 −0 PostInstall.txt
  4. +48 −0 README.rdoc
  5. +28 −0 Rakefile
  6. BIN lib/.genetic.rb.swp
  7. +50 −0 lib/genetic.rb
  8. +10 −0 script/console
  9. +14 −0 script/destroy
  10. +14 −0 script/generate
  11. +73 −0 test/test_genetic.rb
  12. +3 −0 test/test_helper.rb
@@ -0,0 +1,4 @@
+== 0.0.1 2008-12-16
+
+* 1 major enhancement:
+ * Initial release
@@ -0,0 +1,11 @@
+History.txt
+Manifest.txt
+PostInstall.txt
+README.rdoc
+Rakefile
+lib/genetic.rb
+script/console
+script/destroy
+script/generate
+test/test_genetic.rb
+test/test_helper.rb
@@ -0,0 +1,7 @@
+
+For more information on genetic, see http://genetic.rubyforge.org
+
+NOTE: Change this information in PostInstall.txt
+You can also delete it if you don't want it.
+
+
@@ -0,0 +1,48 @@
+= genetic
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 FIXME full name
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
+%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
+require File.dirname(__FILE__) + '/lib/genetic'
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.new('genetic', Genetic::VERSION) do |p|
+ p.developer('Martin Stannard', 'mstannard@gmail.com')
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
+ p.rubyforge_name = p.name # TODO this is default value
+ # p.extra_deps = [
+ # ['activesupport','>= 2.0.2'],
+ # ]
+ p.extra_dev_deps = [
+ ['newgem', ">= #{::Newgem::VERSION}"]
+ ]
+
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
+ p.rsync_args = '-av --delete --ignore-errors'
+end
+
+require 'newgem/tasks' # load /tasks/*.rake
+Dir['tasks/**/*.rake'].each { |t| load t }
+
+# TODO - want other tests/tasks run by default? Add them to the list
+# task :default => [:spec, :features]
Binary file not shown.
@@ -0,0 +1,50 @@
+$:.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
+
+ def initialize(options = {})
+ @genes = options[:genes] || create(options[:length])
+ 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
+
+ end
+
+end
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs = " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs << " -r #{File.dirname(__FILE__) + '/../lib/genetic.rb'}"
+puts "Loading genetic gem"
+exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,73 @@
+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
+ asset 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)
+ end
+
+ def test_population_size
+ assert @population.pop.size == 20
+ end
+
+end
@@ -0,0 +1,3 @@
+require 'stringio'
+require 'test/unit'
+require File.dirname(__FILE__) + '/../lib/genetic'

0 comments on commit 2042c84

Please sign in to comment.