-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ExampleRunner trait to execute examples #15
- Loading branch information
1 parent
203669c
commit 0e24861
Showing
10 changed files
with
185 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
examples/src/main/scala/com/eharmony/spotz/examples/AckleyExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
74 changes: 74 additions & 0 deletions
74
examples/src/main/scala/com/eharmony/spotz/examples/BraninExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
46 changes: 46 additions & 0 deletions
46
examples/src/main/scala/com/eharmony/spotz/examples/SparkExampleRunner.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
73 changes: 0 additions & 73 deletions
73
examples/src/main/scala/com/eharmony/spotz/examples/ackley/AckleyExample.scala
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
examples/src/main/scala/com/eharmony/spotz/examples/ackley/AckleyObjective.scala
This file was deleted.
Oops, something went wrong.
73 changes: 0 additions & 73 deletions
73
examples/src/main/scala/com/eharmony/spotz/examples/branin/BraninExample.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.