Skip to content

Instantly share code, notes, and snippets.

@NagyGa1
Created January 16, 2019 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NagyGa1/b164756a963bb3efd07906d90ddbb3b2 to your computer and use it in GitHub Desktop.
Save NagyGa1/b164756a963bb3efd07906d90ddbb3b2 to your computer and use it in GitHub Desktop.
BOBYQA basic example
val point = doubleArrayOf(1.0, 2.0)
val function = object : MultivariateFunction {
private var eval = 0
// noise
private val random = Random()
override fun value(point: DoubleArray?): Double {
val x = point!![0]
val y = point[1]
println(String.format("#%d f(%.4f, %.4f) = %.4f", ++eval, x, y, (abs(6.0 - x) + Math.abs(5.0 - y))))
return (abs(6.0 - x) + Math.abs(5.0 - y)) + (random.nextDouble() * 0.1 - 0.05);
}
}
val optimizer = BOBYQAOptimizer(5, 5.0, 0.1)
val ret = optimizer.optimize(
MaxEval(200),
GoalType.MINIMIZE,
InitialGuess(point),
ObjectiveFunction(function),
// new LinearConstraint(cost, Relationship.EQ, 30) as OptimizationData,
//SimpleBounds.unbounded(point.length)
SimpleBounds(doubleArrayOf(0.0, 0.0), doubleArrayOf(4.0, 10.0))
)
@NagyGa1
Copy link
Author

NagyGa1 commented Jan 16, 2019

Output:

#1 f(1.3333, 2.0000) =  7.6667
#2 f(2.6667, 2.0000) =  6.3333
#3 f(1.3333, 3.3333) =  6.3333
#4 f(0.0000, 2.0000) =  9.0000
#5 f(1.3333, 0.6667) =  9.0000
#6 f(2.2935, 4.2584) =  4.4480
#7 f(4.0000, 6.3076) =  3.3076
#8 f(2.4396, 8.4700) =  7.0305
#9 f(4.0000, 5.1900) =  2.1900
#10 f(4.0000, 5.4924) =  2.4924
#11 f(3.6997, 5.1538) =  2.4540
#12 f(4.0000, 4.8876) =  2.1124
#13 f(4.0000, 4.7590) =  2.2410
#14 f(3.9062, 4.8530) =  2.2409
#15 f(4.0000, 4.9876) =  2.0124
#16 f(4.0000, 4.9902) =  2.0098

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment