commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mister Mak <mmaks...@gmail.com>
Subject Re: [math] Struggling with opimization
Date Thu, 23 May 2013 21:17:16 GMT
Thanks to Thomas and Gilles for their tips.  The univariate solution
proposed works (for the record:
     UnivariateFunction f = new UnivariateFunction() {public double
value(double x) {return -(x - 5.0) * (x - 5.0) + 2.25; }};
        BrentOptimizer optimizer = new BrentOptimizer(1e-6, 1e-12);
        UnivariatePointValuePair solution = optimizer.optimize(new
UnivariateObjectiveFunction(f), new MaxEval(100), GoalType.MAXIMIZE, new
SearchInterval(-10, 10));
        System.out.println("Min is: " + solution.getValue() + "\tobtained
at: " + solution.getPoint());
).

I am now trying to maximize a function of two variables, and can't get the
following code running:

        MultivariateFunction g =
            new MultivariateFunction() {
                public double value(double[] x) {
                    return -(x[0] - 5.0) * (x[1] - 5.0) + 2.25;
                }
            };
        SimplexOptimizer optimizerMult = new SimplexOptimizer(1e-6, 1e-12);
        PointValuePair solutionMult = optimizerMult.optimize(new
ObjectiveFunction(g), GoalType.MAXIMIZE, new MultiDirectionalSimplex(100));

The last line causes the following NullPointerException:

Exception in thread "main" java.lang.NullPointerException
    at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.AbstractSimplex.build(AbstractSimplex.java:217)
    at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer.doOptimize(SimplexOptimizer.java:151)
    at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer.doOptimize(SimplexOptimizer.java:89)
    at
org.apache.commons.math3.optim.BaseOptimizer.optimize(BaseOptimizer.java:143)
    at
org.apache.commons.math3.optim.BaseMultivariateOptimizer.optimize(BaseMultivariateOptimizer.java:66)
    at
org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer.optimize(MultivariateOptimizer.java:64)
    at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer.optimize(SimplexOptimizer.java:122)
    at ca.queensu.montecarlo.Test_MC.<init>(Unknown Source)
    at ca.queensu.montecarlo.Test_MC.main(Unknown Source)

Any suggestions?

Thanks again,

Philippe

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message