Apache Community Optimization Issues

I am trying to solve a limited nonlinear optimization problem of size 267 with the java optimization library provided by Apache Commons.

After 3 days of decryption, this is what I have:

public class optimize2 { public static void main(String []args){ double[] point = {1.,2.}; double[] cost = {3., 2.}; MultivariateFunction function = new MultivariateFunction() { public double value(double[] point) { double x = point[0]; double y = point[1]; return x * y; } }; MultivariateOptimizer optimize = new BOBYQAOptimizer(5); optimize.optimize( new MaxEval(200), GoalType.MAXIMIZE, new InitialGuess(point), new ObjectiveFunction(function), new LinearConstraint(cost, Relationship.EQ, 30)); } 

}

For some reason, optimize.optimize () throws a null pointer error. Maybe I'm just dumb, but I can't figure out how to make this work.

Here is the error:

An exception is thrown in the main thread java.lang.NullPointerException on org.apache.commons.math3.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer.setup (BOBYQAOptimizer.java:2401) on org.apache.commons.math3.optim.nonlinearar .scalar.noderiv.BOBYQAOptimizer.doOptimize (BOBYQAOptimizer.java:236) at org.apache.commons.math3.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer.doOptimize (at BOBYQAOptimizer.java:49) at org.apache.comm .optim.BaseOptimizer.optimize (BaseOptimizer.java:143) at org.apache.commons.math3.optim.BaseMultivariateOptimizer.optimize (BaseMultivariateOptimizer.java:66) at org.apache.commons.math3.optim.nonlinear.ivalar.ptalim.Omari .optimize (multivariate Optimizer.java:64) in Test.Code.optimize2.main (optimize2.java:39)

+4
source share
1 answer

Looking directly at the BOBYQA code , it actually seems that the problem is that you did not explicitly specify any bounds on the variables. Line 2401 ( setup method) is read as follows:

 boundDifference[i] = upperBound[i] - lowerBound[i]; 

In the doOptimze method doOptimze before calling setup boundaries are set using the following methods:

 final double[] lowerBound = getLowerBound(); final double[] upperBound = getUpperBound(); 

These methods are defined in BaseMultivariateOptimizer as follows:

 public double[] getLowerBound() { return lowerBound == null ? null : lowerBound.clone(); } 

(and similarly for getUpperBound() ). But lowerBound and upperBound in BaseMultivariateOptimizer are set only if the optimization data in the optimize call contains boundary information. If no borders are specified in the optimize call, you should get a NullPointerException .

Looking at the BOBYQA test code , it seems that this should be enough if you add the following argument to the optimize call:

 SimpleBounds.unbounded(point.length) 

Having said that, I also don’t think that you can completely solve your problem using any of the nonlinear optimizers in Apache Commons Math, since, as far as I can tell, none of these optimizers can handle linear or nonlinear constraints. I recommend that you instead take a look at, for example, the Michael Powell COBYLA2 algorithm. I ported the original FORTRAN code of this algorithm to Java, and you can find the code here and.

+4
source

Source: https://habr.com/ru/post/1484734/


All Articles