Skip to content

Commit

Permalink
Added ExampleRunner trait to execute examples #15
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuthichai committed Aug 5, 2016
1 parent 203669c commit 0e24861
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ trait BackendFunctions {
reducer: ((P, L), (P, L)) => (P, L))
(implicit c: ClassTag[P], p: ClassTag[L]): (P, L)
}

trait BackendType
case object SPARK_BACKEND extends BackendType
case object PAR_BACKEND extends BackendType
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class RandomSearch[P, L]
override def optimize(objective: Objective[P, L],
reducer: Reducer[(P, L)])
(implicit c: ClassTag[P], p: ClassTag[L]): RandomSearchResult[P, L] = {
val space = new RandomSpace[P](paramSpace)
val space = new RandomSpace[P](paramSpace, seed)
val startTime = DateTime.now()
val firstPoint = space.sample
val firstLoss = objective(firstPoint)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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
*/
class AckleyObjective extends Objective[Point, Double] {
def apply(p: Point): Double = {
val x = p.get[Double]("x")
val y = p.get[Double]("y")

-20 * exp(-0.2 * sqrt(0.5 * (pow(x, 2) + pow(y, 2)))) - exp(0.5 * (cos(2 * Pi * x) + cos(2 * Pi * y))) + E + 20
}
}

trait AckleyExample {
val objective = new AckleyObjective
val stop = StopStrategy.stopAfterMaxDuration(Duration.standardSeconds(5))
val numBatchTrials = 1000000

def apply(): OptimizerResult[Point, Double]

def main(args: Array[String]) {
val result = apply()
println(result)
}
}

trait AckleyRandomSearch extends AckleyExample with ExampleRunner {
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 {
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
object AckleyParRandomSearch extends AckleyRandomSearch with ParExampleRunner
object AckleySparkGridSearch extends AckleyGridSearch with SparkExampleRunner
object AckleySparkRandomSearch extends AckleyRandomSearch with SparkExampleRunner
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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
*/
class BraninObjective extends Objective[Point, Double] {
val a = 1
val b = 5.1 / (4 * pow(Pi, 2))
val c = 5 / Pi
val r = 6
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")

a * pow(x2 - b*pow(x1, 2) + c*x1 - r, 2) + s*(1-t)*cos(x1) + s
}
}

trait BraninExample {
val objective = new BraninObjective
val stop = StopStrategy.stopAfterMaxDuration(Duration.standardSeconds(5))
val numBatchTrials = 1000000

def apply(): OptimizerResult[Point, Double]

def main(args: Array[String]) {
val result = apply()
println(result)
}
}

trait BraninRandomSearch extends BraninExample 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 {
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
object BraninParRandomSearch extends BraninRandomSearch with ParExampleRunner
object BraninSparkGridSearch extends BraninGridSearch with SparkExampleRunner
object BraninSparkRandomSearch extends BraninRandomSearch with SparkExampleRunner
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.eharmony.spotz.examples

import com.eharmony.spotz.Preamble.Point
import com.eharmony.spotz.objective.Objective
import com.eharmony.spotz.optimizer.grid.{GridSearchResult, ParGridSearch, SparkGridSearch}
import com.eharmony.spotz.optimizer.random.{ParRandomSearch, RandomSearchResult, SparkRandomSearch}
import com.eharmony.spotz.optimizer.{RandomSampler, StopStrategy}
import org.apache.spark.{SparkConf, SparkContext}

/**
* @author vsuthichai
*/
trait ExampleRunner {
def randomSearch(objective: Objective[Point, Double], params: Map[String, RandomSampler[_]], stop: StopStrategy, numBatchTrials: Int): RandomSearchResult[Point, Double]
def gridSearch(objective: Objective[Point, Double], params: Map[String, Iterable[AnyVal]], numBatchTrials: Int): GridSearchResult[Point, Double]
}

trait ParExampleRunner extends ExampleRunner {
override def randomSearch(objective: Objective[Point, Double], params: Map[String, RandomSampler[_]], stop: StopStrategy, numBatchTrials: Int) = {
val optimizer = new ParRandomSearch[Point, Double](params, stop, numBatchTrials)
optimizer.minimize(objective)
}

override def gridSearch(objective: Objective[Point, Double], params: Map[String, Iterable[AnyVal]], numBatchTrials: Int): GridSearchResult[Point, Double] = {
val optimizer = new ParGridSearch[Point, Double](params, numBatchTrials)
optimizer.minimize(objective)
}
}

trait SparkExampleRunner extends ExampleRunner {
override def randomSearch(objective: Objective[Point, Double], params: Map[String, RandomSampler[_]], stop: StopStrategy, numBatchTrials: Int) = {
val sc = new SparkContext(new SparkConf().setMaster("local[*]").setAppName("Spark Random Search"))
val optimizer = new SparkRandomSearch[Point, Double](sc, params, stop, numBatchTrials)
val result = optimizer.minimize(objective)
sc.stop()
result
}

override def gridSearch(objective: Objective[Point, Double], params: Map[String, Iterable[AnyVal]], numBatchTrials: Int) = {
val sc = new SparkContext(new SparkConf().setMaster("local[*]").setAppName("Spark Grid Search"))
val optimizer = new SparkGridSearch[Point, Double](sc, params, numBatchTrials)
val result = optimizer.minimize(objective)
sc.stop()
result
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0e24861

Please sign in to comment.