Skip to content

Commit

Permalink
Refactored examples #15
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuthichai committed Aug 5, 2016
1 parent 0e24861 commit 3f04deb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.eharmony.spotz.examples

import com.eharmony.spotz.Preamble.Point
import com.eharmony.spotz.objective.Objective
import com.eharmony.spotz.optimizer.grid.GridSearchResult
import com.eharmony.spotz.optimizer.random.RandomSearchResult
import com.eharmony.spotz.optimizer.{OptimizerResult, StopStrategy, UniformDouble}
import org.joda.time.Duration

Expand Down Expand Up @@ -35,22 +33,18 @@ trait AckleyExample {
}
}

trait AckleyRandomSearch extends AckleyExample with ExampleRunner {
trait AckleyRandomSearch extends AckleyExample with RandomSearchRunner {
val hyperParameters = Map(
("x", UniformDouble(-5, 5)),
("y", UniformDouble(-5, 5))
)

def apply(): RandomSearchResult[Point, Double] = randomSearch(objective, hyperParameters, stop, numBatchTrials)
}

trait AckleyGridSearch extends AckleyExample with ExampleRunner {
trait AckleyGridSearch extends AckleyExample with GridSearchRunner {
val hyperParameters = Map(
("x", Range.Double(-5, 5, 0.01)),
("y", Range.Double(-5, 5, 0.01))
)

def apply(): GridSearchResult[Point, Double] = gridSearch(objective, hyperParameters, numBatchTrials)
}

object AckleyParGridSearch extends AckleyGridSearch with ParExampleRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package com.eharmony.spotz.examples

import com.eharmony.spotz.Preamble.Point
import com.eharmony.spotz.objective.Objective
import com.eharmony.spotz.optimizer.grid.GridSearchResult
import com.eharmony.spotz.optimizer.random.RandomSearchResult
import com.eharmony.spotz.optimizer.{OptimizerResult, StopStrategy, UniformDouble}
import org.joda.time.Duration

import scala.math._


/**
* @author vsuthichai
* Input Domain:
* This function is usually evaluated on the square x1 ∈ [-5, 10], x2 ∈ [0, 15].
*
* f(x) = a(x2 - b(x1)^2 + cx1 - r)^2 + s(1 - t)cos(x1) + s
*
* Global Minimum:
* f(x*) = 0.397887 at x* = (-Pi, 12.275), (Pi, 2.275), (9.42478, 2.475)*
*/
class BraninObjective extends Objective[Point, Double] {
val a = 1
Expand All @@ -21,16 +25,7 @@ class BraninObjective extends Objective[Point, Double] {
val s = 10
val t = 1 / (8 * Pi)

/**
* Input Domain:
* This function is usually evaluated on the square x1 ∈ [-5, 10], x2 ∈ [0, 15].
*
* Global Minimum:
* f(x*) = 0.397887 at x* = (-Pi, 12.275), (Pi, 2.275), (9.42478, 2.475)
*
* @param point
* @return a Double which is the result of evaluating the Branin function
*/

override def apply(point: Point): Double = {
val x1 = point.get[Double]("x1")
val x2 = point.get[Double]("x2")
Expand All @@ -52,20 +47,18 @@ trait BraninExample {
}
}

trait BraninRandomSearch extends BraninExample with ExampleRunner {
trait BraninRandomSearch extends BraninExample with RandomSearchRunner with ExampleRunner {
val hyperParameters = Map(
("x1", UniformDouble(-5, 10)),
("x2", UniformDouble(0, 15))
)
def apply(): RandomSearchResult[Point, Double] = randomSearch(objective, hyperParameters, stop, numBatchTrials)
}

trait BraninGridSearch extends BraninExample with ExampleRunner {
trait BraninGridSearch extends BraninExample with GridSearchRunner with ExampleRunner {
val hyperParameters = Map(
("x1", Range.Double(-5, 10, 0.01)),
("x2", Range.Double(0, 15, 0.01))
)
def apply(): GridSearchResult[Point, Double] = gridSearch(objective, hyperParameters, numBatchTrials)
}

object BraninParGridSearch extends BraninGridSearch with ParExampleRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,20 @@ trait SparkExampleRunner extends ExampleRunner {
result
}
}

trait GridSearchRunner extends ExampleRunner {
val hyperParameters: Map[String, Iterable[AnyVal]]
val objective: Objective[Point, Double]
val numBatchTrials: Int

def apply(): GridSearchResult[Point, Double] = gridSearch(objective, hyperParameters, numBatchTrials)
}

trait RandomSearchRunner extends ExampleRunner {
val hyperParameters: Map[String, RandomSampler[_]]
val objective: Objective[Point, Double]
val stop: StopStrategy
val numBatchTrials: Int

def apply(): RandomSearchResult[Point, Double] = randomSearch(objective, hyperParameters, stop, numBatchTrials)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.eharmony.spotz.examples

import org.junit.Assert._
import org.junit.{Ignore, Test}

/**
* @author vsuthichai
*/
class AckleyTest {
@Ignore
def testAckleyParRandomSearch() {
val result = AckleyParRandomSearch()
val point = result.bestPoint

assertEquals(result.bestLoss, 0.0, 0.002)
assertEquals(point.get[Double]("x"), 0.0, 0.001)
assertEquals(point.get[Double]("y"), 0.0, 0.001)
}

@Test
def testAckleyParGridSearch() {
val result = AckleyParGridSearch()
val point = result.bestPoint

assertEquals(result.bestLoss, 0.0, 0.002)
assertEquals(point.get[Double]("x"), 0.0, 0.001)
assertEquals(point.get[Double]("y"), 0.0, 0.001)
}

@Ignore
def testAckleySparkRandomSearch() {
val result = AckleySparkRandomSearch()
val point = result.bestPoint

assertEquals(result.bestLoss, 0.0, 0.002)
assertEquals(point.get[Double]("x"), 0.0, 0.001)
assertEquals(point.get[Double]("y"), 0.0, 0.001)
}

@Test
def testAckleySparkGridSearch() {
val result = AckleySparkGridSearch()
val point = result.bestPoint

assertEquals(result.bestLoss, 0.0, 0.002)
assertEquals(point.get[Double]("x"), 0.0, 0.001)
assertEquals(point.get[Double]("y"), 0.0, 0.001)
}
}

0 comments on commit 3f04deb

Please sign in to comment.