tkrhgch
tkrhgch

Reputation: 343

BBOptim equivalent or alternative in Scala

I'm planning on converting some R code to Scala and came to a function called BBOptim. It seems to be a wrapper over SPG but having little knowledge about mathematics, I don't know what the equivalent code would be in Scala.

For example, is it possible to convert the code below to Scala? Or can there be an alternative for this? I'm suspecting the SpectralProjectedGradient or NonLinearMinimizer in the Breeze library could be used.

# Use a preset seed so test values are reproducable. 
require("setRNG")
old.seed <- setRNG(list(kind="Mersenne-Twister", normal.kind="Inversion",
    seed=1234))

rosbkext <- function(x){
# Extended Rosenbrock function
n <- length(x)
j <- 2 * (1:(n/2))
jm1 <- j - 1
sum(100 * (x[j] - x[jm1]^2)^2 + (1 - x[jm1])^2)
}

p0 <- rnorm(500)
BBoptim(par=p0, fn=rosbkext)

Thanks in advance.

Edited

I'm restricted on only using the JVM so calling R from Scala is no possible.

Upvotes: 1

Views: 143

Answers (2)

Rex Kerr
Rex Kerr

Reputation: 167901

Your safest bet if you're limited to the JVM, though not the most idiomatic Scala, is to pick a suitable optimization routine from Apache Commons Math. The exact one you're using isn't there, but the general solvers work pretty well on most classes of problems I've thrown at them. You might have to try several different classes of solution to get a decent box contstraint--make a sharp penalty near where your walls are, then restrict the search space to those surfaces if you're near a wall.

Otherwise, it's fairly easy to call R from Scala using rScala. That way you don't have to change the algorithm at all. (But you do have to have R installed.)

Upvotes: 0

Hack-R
Hack-R

Reputation: 23200

As you mentioned, this R library is a wrapper for spg, which is the Spectral Projected Gradient algorithm.

The SPG implementation by the TANGO project for nonlinear gradient optimization is written in Fortran 77 with interfaces to a number of languages, including Java. All Java libraries work with Scala and thus this should be a suitable solution. This implementation is the focus of several academic articles on Spectral Projected Gradients.

You might also check out the Java API for the Open Optimization Library.

Upvotes: 1

Related Questions