如何在Java Apache Math中使用SimplexSolver或SimplexOptimizer? [英] How to use SimplexSolver or SimplexOptimizer in java apache math?

查看:130
本文介绍了如何在Java Apache Math中使用SimplexSolver或SimplexOptimizer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用apache commons数学库3.5+版本来解决优化问题.基本上,我正在尝试将(gamma)分布拟合到某些数据点.我似乎找不到任何有关如何使用新的(版本3.5)优化工具(例如SimplexSolver,SimplexOptimizer或OptimizationData)来解决琐碎的优化问题的简单示例.

I'm trying to use the apache commons math library version 3.5+ to solve an optimization problem. Basically, I'm trying to fit a (gamma) distribution to some data points. I can't seem to find any simple examples of how to use the new (version 3.5) optimization tools, such as SimplexSolver, SimplexOptimizer, or OptimizationData, to solve a trivial optimization problem.

之前曾在这里问过类似的问题,但所有答案似乎都是针对较旧版本的apache数学-在3.5方面进行了重组,并且找不到我能找到的示例代码.

Similar questions have been asked here before, but all the answers seem to be for older version of apache math - in 3.5 things were restructured and none of the example code I could find works.

有人有可行的示例如何使用新的优化器或求解器吗?我对SimplexOptimizer最为感兴趣,但是在这一点上,任何事情都将是有用的.

Does anyone have a working example how to use the new optimizers or solvers? I'm most interested in SimplexOptimizer, but at this point anything would be useful.

推荐答案

实际上,优化器可能难以使用:许多参数,其中不同类型的优化器需要不同的组合,并且它们全部隐藏在他们收到的通用OptimizationData数组.除非您开始将代码与它们所引用的论文相匹配,否则您将很难从中获得任何结果.

Indeed, the optimizers may be hard to use: Lots of parameters, of which different combinations are required for the different types of optimizers, and they are all hidden in the generic OptimizationData array that they receive. Unless you start matching the code with the papers that they refer to, you can hardly get any results out of them whatsoever.

我还想偶尔尝试使用一些求解器/优化器,对我来说,可靠,有效的示例"的主要来源是这些类的单元测试,通常情况很复杂,涉及很多案例.例如,关于SimplexOptimizer,您可能想看看

I also wanted to use some of thes solvers/optimizers a try occasionally, the main source of reliable, working ""examples"" for me turned out to be the unit tests of these classes, which usually are quite elaborate and cover many cases. For example, regarding the SimplexOptimizer, you may want to have a look at the org/apache/commons/math4/optim/nonlinear/scalar/noderiv/ test cases, containing the test classes SimplexOptimizerMultiDirectionalTest.java and SimplexOptimizerNelderMeadTest.java.

(对不起,也许这不是您期望或希望的,但是...当我试图弄清楚这些优化器实际需要哪个OptimizationData时,我发现这些测试非常有用.)

(Sorry, maybe this is not what you expected or hoped for, but ... I found these tests tremendously helpful when I tried to figure out which OptimizationData these optimizers actually need...)

编辑

仅供参考,是一个完整的示例,该示例摘自以下基本单元测试之一:

Just for reference, a complete example, extracted from one of the basic unit tests:

import java.util.Arrays;

import org.apache.commons.math3.analysis.MultivariateFunction;
import org.apache.commons.math3.optim.InitialGuess;
import org.apache.commons.math3.optim.MaxEval;
import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex;
import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer;
import org.apache.commons.math3.util.FastMath;

public class SimplexOptimizerExample
{
    public static void main(String[] args)
    {
        SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
        final FourExtrema fourExtrema = new FourExtrema();

        final PointValuePair optimum =
            optimizer.optimize(
                new MaxEval(100), 
                new ObjectiveFunction(fourExtrema), 
                GoalType.MINIMIZE, 
                new InitialGuess(new double[]{ -3, 0 }), 
                new NelderMeadSimplex(new double[]{ 0.2, 0.2 }));

        System.out.println(Arrays.toString(optimum.getPoint()) + " : "
            + optimum.getSecond());
    }

    private static class FourExtrema implements MultivariateFunction
    {
        // The following function has 4 local extrema.
        final double xM = -3.841947088256863675365;
        final double yM = -1.391745200270734924416;
        final double xP = 0.2286682237349059125691;
        final double yP = -yM;
        final double valueXmYm = 0.2373295333134216789769; // Local maximum.
        final double valueXmYp = -valueXmYm; // Local minimum.
        final double valueXpYm = -0.7290400707055187115322; // Global minimum.
        final double valueXpYp = -valueXpYm; // Global maximum.

        public double value(double[] variables)
        {
            final double x = variables[0];
            final double y = variables[1];
            return (x == 0 || y == 0) ? 0 : FastMath.atan(x)
                * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y)
                / (x * y);
        }
    }
}

这篇关于如何在Java Apache Math中使用SimplexSolver或SimplexOptimizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆