Minimum finding for univariate nonlinear function in Java

倖福魔咒の 提交于 2019-12-19 09:28:14

问题


I'm looking for a simple way to accomplish in Java what MATLAB's fminsearch() does. I don't need to be as general as fminsearch, in my case I only want to find the minimum (function and parameter values at minimum) of a single-variable nonlinear function. I don't know the analytical expression of the function, but I can evaluate it easily.

Do you know of a library that performs this, or of an easy algorithm I could re-implement?

Note: I saw that apache's common-math seems to have something like this (UnivariateOptimizer), but most of the methods seem to be deprecated and I couldn't find a good explanation how to use it. Any tips related to that are welcome as well.

Thanks!


回答1:


Apache Commons Math is generally a good place to start for numerical computations in Java. Usage is best learnt by example, looking at the API documentation and the unit test source code for the various classes and methods.

The optimization classes that are referenced in the user guide are, as you have noted, deprecated. They can still be called, but eventually they will of course be phased out from the library. For reasons unknown to me, ongoing optimization development is now taking place in the optim rather than the optimization sub-package.

For univariate function (local optimum) minimization, Apache Commons Math provides an implementation of the Brent method. Usage is outlined in the unit tests of the BrentOptimizer, from which I have copied this excerpt:

@Test
public void testSinMin() {
    UnivariateFunction f = new Sin();
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14);

    Assert.assertEquals(3 * Math.PI / 2, 
        optimizer.optimize(new MaxEval(200),
                           new UnivariateObjectiveFunction(f),
                           GoalType.MINIMIZE,
                           new SearchInterval(4, 5)).getPoint(), 1e-8);

    Assert.assertTrue(optimizer.getEvaluations() <= 50);
    Assert.assertEquals(200, optimizer.getMaxEvaluations());
    ...
}


来源:https://stackoverflow.com/questions/15999892/minimum-finding-for-univariate-nonlinear-function-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!