From dd102fd8f24a2f8e6d67db47dc9b4681fc94ec8b Mon Sep 17 00:00:00 2001 From: Lazar Tsarev Date: Wed, 10 Jun 2015 17:37:58 +0300 Subject: [PATCH] fixes selecting a random char for the mutation, because until now the letter 'z' was not a possible mutation --- C#/GAHelloWorld/Chromosome.cs | 2 +- clojure/src/net/auxesia/chromosome.clj | 4 ++-- java/src/main/java/net/auxesia/Chromosome.java | 2 +- python/gahelloworld.py | 4 ++-- ruby/lib/gahelloworld.rb | 2 +- scala/src/main/scala/net/auxesia/Chromosome.scala | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/C#/GAHelloWorld/Chromosome.cs b/C#/GAHelloWorld/Chromosome.cs index fb39d83..481f496 100644 --- a/C#/GAHelloWorld/Chromosome.cs +++ b/C#/GAHelloWorld/Chromosome.cs @@ -83,7 +83,7 @@ public Chromosome Mutate() char[] mutatedGene = this.Gene.ToCharArray(); int randomIndex = rand.Next(0, this.Gene.Length); - int mutateChange = rand.Next(32, 122); + int mutateChange = rand.Next(32, 123); mutatedGene[randomIndex] = (char)mutateChange; diff --git a/clojure/src/net/auxesia/chromosome.clj b/clojure/src/net/auxesia/chromosome.clj index 864f7b0..3733ba5 100644 --- a/clojure/src/net/auxesia/chromosome.clj +++ b/clojure/src/net/auxesia/chromosome.clj @@ -50,7 +50,7 @@ [c] (let [old-gene (:gene c) idx (rand-int (count old-gene)) - new-gene (assoc old-gene idx (char (mod (+ (int (get old-gene idx)) (+ 32 (rand-int 90))) 122)))] + new-gene (assoc old-gene idx (char (mod (+ (int (get old-gene idx)) (+ 32 (rand-int 90))) 123)))] (Chromosome. new-gene (fitness new-gene)))) (defn generate @@ -69,4 +69,4 @@ (let [pivot (rand-int (count *target-gene*)) child1 (into (subvec (:gene c1) 0 pivot) (subvec (:gene c2) pivot)) child2 (into (subvec (:gene c2) 0 pivot) (subvec (:gene c1) pivot))] - (vector (generate child1) (generate child2)))) \ No newline at end of file + (vector (generate child1) (generate child2)))) diff --git a/java/src/main/java/net/auxesia/Chromosome.java b/java/src/main/java/net/auxesia/Chromosome.java index 8f07880..6cd96db 100644 --- a/java/src/main/java/net/auxesia/Chromosome.java +++ b/java/src/main/java/net/auxesia/Chromosome.java @@ -113,7 +113,7 @@ public Chromosome mutate() { char[] arr = gene.toCharArray(); int idx = rand.nextInt(arr.length); int delta = (rand.nextInt() % 90) + 32; - arr[idx] = (char) ((arr[idx] + delta) % 122); + arr[idx] = (char) ((arr[idx] + delta) % 123); return new Chromosome(String.valueOf(arr)); } diff --git a/python/gahelloworld.py b/python/gahelloworld.py index aa8f251..46e2ae1 100644 --- a/python/gahelloworld.py +++ b/python/gahelloworld.py @@ -71,7 +71,7 @@ def mutate(self): gene = list(self.gene) delta = randint(32, 121) idx = randint(0, len(gene) - 1) - gene[idx] = chr((ord(gene[idx]) + delta) % 122) + gene[idx] = chr((ord(gene[idx]) + delta) % 123) return Chromosome(''.join(gene)) @@ -179,4 +179,4 @@ def evolve(self): if pop.population[0].fitness == 0: break else:pop.evolve() else: - print("Maximum generations reached without success.") \ No newline at end of file + print("Maximum generations reached without success.") diff --git a/ruby/lib/gahelloworld.rb b/ruby/lib/gahelloworld.rb index f4fb488..d3158f3 100644 --- a/ruby/lib/gahelloworld.rb +++ b/ruby/lib/gahelloworld.rb @@ -23,7 +23,7 @@ module GAHelloWorld RAND_SEED=srand TARGET_GENE='Hello World!' - ALLOWED_LETTERS = (32..122).to_a.map{|i| i.chr} + ALLOWED_LETTERS = (32..123).to_a.map{|i| i.chr} class Chromosome attr_reader :gene_ary, :target_ary, :gene diff --git a/scala/src/main/scala/net/auxesia/Chromosome.scala b/scala/src/main/scala/net/auxesia/Chromosome.scala index 9819414..f6e55a3 100644 --- a/scala/src/main/scala/net/auxesia/Chromosome.scala +++ b/scala/src/main/scala/net/auxesia/Chromosome.scala @@ -104,7 +104,7 @@ object Chromosome { */ def mutate(ch: Chromosome) = { var arr = ch.gene.toArray - arr(Random.nextInt(ch.gene.length)) = (Random.nextInt(90) + 32).toChar + arr(Random.nextInt(ch.gene.length)) = (Random.nextInt(91) + 32).toChar Chromosome(arr.mkString) } -} \ No newline at end of file +}