Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

working on other fitness functions

  • Loading branch information...
commit d90f4c8504ca1ed7ffb62be4e75b18174c912cfe 1 parent c2479b4
authored
2  ruby/gahelloworld.rb
@@ -20,7 +20,7 @@
20 20
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 21
 # THE SOFTWARE.
22 22
 
23  
-require 'lib/gahelloworld'
  23
+require './lib/gahelloworld'
24 24
 if ARGV.empty? || ARGV.include?("-h") ||  ARGV.include?("--help")
25 25
   puts "Genetic Algorithm Hello World. Ruby version by David Heitzman, 2012"
26 26
   puts "usage: ruby gahelloworld" 
21  ruby/lib/gahelloworld.rb
@@ -24,6 +24,8 @@ module GAHelloWorld
24 24
   RAND_SEED=srand
25 25
   TARGET_GENE='Hello World!'
26 26
   ALLOWED_LETTERS = (32..122).to_a.map{|i| i.chr}
  27
+  LETTERS=("A".ord .. "Z".ord).to_a.map{|i| i.chr} + ("a".ord .. "z".ord).to_a.map{|i| i.chr} 
  28
+  NUMBERS=("0".ord .. "9".ord).to_a.map{|i| i.chr} 
27 29
 
28 30
   class Chromosome
29 31
     attr_reader :gene_ary, :target_ary, :gene
@@ -55,9 +57,24 @@ def fitness
55 57
       @fitness ||= 
56 58
         begin
57 59
           #normal -- matches the target string
  60
+          # diff=0
  61
+          # gene_ary.size.times do |i| diff += (gene_ary[i].to_i - target_ary[i].to_i).abs  end
  62
+          # diff
  63
+
  64
+          # wants strings that match letter/number W9F3c2 etc 
58 65
           diff=0
59  
-          gene_ary.size.times do |i| diff += (gene_ary[i].to_i - target_ary[i].to_i).abs  end
60  
-          diff
  66
+          @gene.each_char do |c|
  67
+            should_be = (should_be.nil? || should_be == :letter) ? :number : :letter #vacillates
  68
+            it_is =
  69
+            if LETTERS.include?(c)
  70
+              :letter
  71
+            elsif NUMBERS.include?(c)
  72
+              :number
  73
+            else
  74
+              :other
  75
+            end
  76
+            diff += 1 if should_be != it_is
  77
+          end 
61 78
         end
62 79
     end 
63 80
 

0 notes on commit d90f4c8

Please sign in to comment.
Something went wrong with that request. Please try again.