Permalink
Browse files

Formatting updates and revisiting the code to be more idiomatic scala.

  • Loading branch information...
1 parent f793547 commit 636c1cfeb79ab639508e9e2352d85ecd9a3df19e @jsvazic jsvazic committed Jul 30, 2012
View
@@ -1,6 +1,7 @@
.classpath
.project
.settings
+.cache
.scala_dependencies
.metadata/
bin/
@@ -12,4 +13,4 @@ clojure/pom.xml
clojure/*jar
clojure/lib
clojure/classes
-python/__pycache__/
+python/__pycache__/
@@ -56,28 +56,26 @@ object Chromosome {
def apply(gene: String) = {
def calcFitness(gene: Array[Char]): Int = {
gene.zip(TARGET_GENE).map(
- i => abs(i._1.intValue - i._2.intValue)
- ).foldLeft(0)(_+_)
+ i => abs(i._1.intValue - i._2.intValue)).foldLeft(0)(_ + _)
}
new Chromosome(gene, calcFitness(gene.toCharArray))
}
/**
- * Helper method used to generate a random gene for use in constructing a
+ * Helper method used to generate a random gene for use in constructing a
* new [[net.auxesia.Chromosome]] instance.
- *
- * @return A valid gene for use when constructing a new
+ *
+ * @return A valid gene for use when constructing a new
* [[net.auxesia.Chromosome]] instance.
*/
- def generateRandom: Chromosome = {
+ def generateRandom = {
Chromosome(new Array[Char](TARGET_GENE.length).map(
- i => (Random.nextInt(90) + 32).toChar
- ).mkString)
+ i => (Random.nextInt(90) + 32).toChar).mkString)
}
-
+
/**
- * Method to mate one [[net.auxesia.Chromosome]]'s gene with another,
+ * Method to mate one [[net.auxesia.Chromosome]]'s gene with another,
* resulting in two distinct offspring genes that can be used to create
* new instances of [[net.auxesia.Chromosome]]'s as required.
*
@@ -87,25 +85,24 @@ object Chromosome {
* @return A 2-element vector of resuling [[net.auxesia.Chromosome]]
* genes created through the mating algorithm.
*/
- def mate(first: Chromosome, second: Chromosome): Vector[Chromosome] = {
+ def mate(first: Chromosome, second: Chromosome) = {
val pivot = Random.nextInt(first.gene.length)
Vector[Chromosome](
- Chromosome(first.gene.substring(0, pivot) + second.gene.substring(pivot)),
- Chromosome(second.gene.substring(0, pivot) + first.gene.substring(pivot))
- )
+ Chromosome(first.gene.substring(0, pivot) + second.gene.substring(pivot)),
+ Chromosome(second.gene.substring(0, pivot) + first.gene.substring(pivot)))
}
-
+
/**
* Method used to mutate the gene of the given chromosome, returning a
* new [[net.auxesia.Chromsome]] instance. Only one character in the
* gene is mutated, the rest of the gene remains the same.
*
* @param ch The [[net.auxesia.Chromosome]] to mutate.
*
- * @return A new [[net.auxesia.Chromosome]], with its gene having a one
+ * @return A new [[net.auxesia.Chromosome]], with its gene having a one
* character delta compared to the given [[net.auxesia.Chromosome]]'s gene.
*/
- def mutate(ch: Chromosome): Chromosome = {
+ def mutate(ch: Chromosome) = {
var arr = ch.gene.toArray
arr(Random.nextInt(ch.gene.length)) = (Random.nextInt(90) + 32).toChar
Chromosome(arr.mkString)
@@ -25,54 +25,48 @@ package net.auxesia
/**
* Object driver for the genetic algorithm "Hello, world!" simulation.
- *
+ *
* @author John Svazic
*/
-object GAHelloWorld {
+object GAHelloWorld extends App {
+
+ val startTime = System.currentTimeMillis
- /**
- * Main driver function for the simulation.
- *
- * @param args Command-line arguments (ignored).
- */
- def main(args: Array[String]): Unit = {
- val startTime = System.currentTimeMillis
- // The size of the simulation population
- val populationSize = 2048
+ // The size of the simulation population
+ val populationSize = 2048
- // The maximum number of generations for the simulation.
- val maxGenerations = 16384
+ // The maximum number of generations for the simulation.
+ val maxGenerations = 16384
- // The probability of crossover for any member of the population,
- // where 0.0 <= crossoverRatio <= 1.0
- val crossoverRatio = 0.8f
+ // The probability of crossover for any member of the population,
+ // where 0.0 <= crossoverRatio <= 1.0
+ val crossoverRatio = 0.8f
- // The portion of the population that will be retained without change
- // between evolutions, where 0.0 <= elitismRatio < 1.0
- val elitismRatio = 0.1f
+ // The portion of the population that will be retained without change
+ // between evolutions, where 0.0 <= elitismRatio < 1.0
+ val elitismRatio = 0.1f
- // The probability of mutation for any member of the population,
- // where 0.0 <= mutationRatio <= 1.0
- val mutationRatio = 0.03f
+ // The probability of mutation for any member of the population,
+ // where 0.0 <= mutationRatio <= 1.0
+ val mutationRatio = 0.03f
- // Create the initial population
- val pop = Population(populationSize, crossoverRatio, elitismRatio, mutationRatio)
+ // Create the initial population
+ val pop = Population(populationSize, crossoverRatio, elitismRatio, mutationRatio)
- // Start evolving the population, stopping when the maximum number of
- // generations is reached, or when we find a solution.
+ // Start evolving the population, stopping when the maximum number of
+ // generations is reached, or when we find a solution.
- var generation = 1;
- while (generation <= maxGenerations && pop.population(0).fitness != 0) {
- println("Generation " + generation + ": " + pop.population(0).gene)
- pop.evolve
- generation += 1
- }
-
- // We're done, so shutdown the population (which uses Akka)
- pop.shutdown
- val endTime = System.currentTimeMillis
-
+ var generation = 1
+ while (generation <= maxGenerations && pop.population(0).fitness != 0) {
println("Generation " + generation + ": " + pop.population(0).gene)
- println("Total execution time: " + (endTime - startTime) + "ms")
+ pop.evolve
+ generation += 1
}
+
+ // We're done, so shutdown the population (which uses Akka)
+ pop.shutdown
+ val endTime = System.currentTimeMillis
+
+ println("Generation " + generation + ": " + pop.population(0).gene)
+ println("Total execution time: " + (endTime - startTime) + "ms")
}
Oops, something went wrong.

0 comments on commit 636c1cf

Please sign in to comment.