Skip to content

Commit

Permalink
Merge branch 'akka'
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvazic committed Oct 13, 2011
2 parents ba60663 + 26191dd commit ef47c24
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 146 deletions.
9 changes: 2 additions & 7 deletions scala/README.md
Expand Up @@ -2,7 +2,7 @@

This is a simple project intended to showcase genetic algorithms with a well
known example for all new developers; namely the classic "Hello, world!"
example, written in Scala (2.8.1).
example, written in Scala (2.9.0).

## Overview

Expand All @@ -15,7 +15,7 @@ application.

## Usage

The project currently uses [SBT](http://code.google.com/p/simple-build-tool/)
The project currently uses [SBT 0.7.7](http://code.google.com/p/simple-build-tool/)
for the build. To build it locally, run the following from a shell:

> sbt update package
Expand All @@ -24,11 +24,6 @@ To execute the simulation, run:

> sbt run
or if you like to have an executable .jar file, change directories to the
target/scala_2.8.1 directory and execute:

> java -jar gahelloworld_2.8.1-1.0.jar
## Copyright and License

The MIT License
Expand Down
6 changes: 3 additions & 3 deletions scala/project/build.properties
Expand Up @@ -2,7 +2,7 @@
#Tue Apr 12 13:23:52 EDT 2011
project.organization=John Svazic
project.name=GAHelloWorld
sbt.version=0.7.5
sbt.version=0.7.7
project.version=1.0
build.scala.versions=2.8.1
project.initialize=false
build.scala.versions=2.9.0
project.initialize=false
8 changes: 5 additions & 3 deletions scala/project/build/GAHelloWorld.scala
@@ -1,5 +1,7 @@
import sbt._

class GAHelloWorld(info: ProjectInfo) extends DefaultProject(info) {
val scalatest = "org.scalatest" % "scalatest" % "1.3"
}
class GAHelloWorld(info: ProjectInfo) extends DefaultProject(info) with AkkaProject {
val scalatest = "org.scalatest" % "scalatest_2.9.0" % "1.6.1"
val akkaSTM = akkaModule("stm")
val akkaRemote = akkaModule("remote")
}
6 changes: 6 additions & 0 deletions scala/project/plugins/Plugins.scala
@@ -0,0 +1,6 @@
import sbt._

class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
val akkaRepo = "Akka Repo" at "http://akka.io/repository"
val akkaPlugin = "se.scalablesolutions.akka" % "akka-sbt-plugin" % "1.1.3"
}
3 changes: 3 additions & 0 deletions scala/project/plugins/project/build.properties
@@ -0,0 +1,3 @@
#Project properties
#Wed Aug 10 09:52:20 EDT 2011
plugin.uptodate=true
83 changes: 45 additions & 38 deletions scala/src/main/scala/net/auxesia/Chromosome.scala
Expand Up @@ -37,39 +37,7 @@ import scala.util.Random
* @param gene The gene representing the chromosome.
* @param fitness The fitness of the chromosome.
*/
class Chromosome private (val gene: String, val fitness: Int) {

/**
* 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.
*
* @return A new [[net.auxesia.Chromosome]] with one character replaced
* in the original gene.
*/
def mutate: Chromosome = {
var arr = gene.toArray
arr(Random.nextInt(gene.length)) = (Random.nextInt(90) + 32).toChar
Chromosome(arr.mkString)
}

/**
* Method to mate this [[net.auxesia.Chromosome]] with another, resulting
* in two distinct offspring.
*
* @param other The other [[net.auxesia.Chromosome]] to mate with.
*
* @return A 2-element array of resuling [[net.auxesia.Chromosome]]
* objects created through the mating algorithm.
*/
def mate(other: Chromosome): Array[Chromosome] = {
val arr = new Array[Chromosome](2)
val pivot = Random.nextInt(gene.length)
arr(0) = Chromosome(gene.substring(0, pivot) + other.gene.substring(pivot))
arr(1) = Chromosome(other.gene.substring(0, pivot) + gene.substring(pivot))
return arr
}
}
class Chromosome private (val gene: String, val fitness: Int)

/**
* A factory object for constructing new [[net.auxesia.Chromosome]] instances.
Expand All @@ -87,20 +55,59 @@ 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)(_+_)
gene.zip(TARGET_GENE).map(
i => abs(i._1.intValue - i._2.intValue)
).foldLeft(0)(_+_)
}

new Chromosome(gene, calcFitness(gene.toCharArray))
}

/**
* Helper method used to generate a random [[net.auxesia.Chromosome]]
* instance.
* Helper method used to generate a random gene for use in constructing a
* new [[net.auxesia.Chromosome]] instance.
*
* @return A [[net.auxesia.Chromosome]] instance with a random, valid gene.
* @return A valid gene for use when constructing a new
* [[net.auxesia.Chromosome]] instance.
*/
def generateRandom: Chromosome = {
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,
* resulting in two distinct offspring genes that can be used to create
* new instances of [[net.auxesia.Chromosome]]'s as required.
*
* @param first The first gene to mate.
* @param second The second gene to mate.
*
* @return A 2-element vector of resuling [[net.auxesia.Chromosome]]
* genes created through the mating algorithm.
*/
def mate(first: Chromosome, second: Chromosome): Vector[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))
)
}

/**
* 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
* character delta compared to the given [[net.auxesia.Chromosome]]'s gene.
*/
def mutate(ch: Chromosome): Chromosome = {
var arr = ch.gene.toArray
arr(Random.nextInt(ch.gene.length)) = (Random.nextInt(90) + 32).toChar
Chromosome(arr.mkString)
}
}
3 changes: 3 additions & 0 deletions scala/src/main/scala/net/auxesia/GAHelloWorld.scala
Expand Up @@ -67,6 +67,9 @@ object GAHelloWorld {
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)
Expand Down

0 comments on commit ef47c24

Please sign in to comment.