Thursday, 27 February 2014

GAs speed x10

For the astute observer, you will notice that in my previous post, there is a slight performance problem if the evaluation function is expensive. This line of code:
val sortedChromos = population.sortBy(w => evaluate(decode(w), target))
Actually, it should not be even coded like this, the evaluation should happen once, and then we should sort. That's one thing. The other thing, is that we could simply parallelise this... First time I have actually found a good usage for .par ;-) With this in mind, here is a new implementation of the above:
def parSort(population: Population): Population = {
  val parPopulation = population.par
  val evaledParPopulation = => (e, evaluate(decode(e), target))).toList
  val sortedParPopulation = evaledParPopulation.sortBy(e => e._2).map(e => e._1)

val sortedChromos = parSort(population)
The third problem with a maximum of 2,000 iterations with the first sort/eval takes 51 seconds, with the par/sort/eval, 5 seconds :-)

No comments:

Blog Archive