我目前正在尝试使用apache库在java中实现无衍生多变量优化。但是,我无法向optimize()方法提供所需的“OptimizationData”。以下是我到目前为止运行优化的内容。
public static double[] Optimize(double[][] contractDataMatrix, double[] modelData,String modelType,String weightType){
ObjectiveFunction objective = new
ObjectiveFunction(contractDataMatrix,modelType,weightType);
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer optimizer =new
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer(.01,.001);
org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction obj = new
org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction(objective);
org.apache.commons.math3.optim.PointValuePair finalData=optimizer.optimize(
new org.apache.commons.math3.optim.MaxEval(200),
obj,
GoalType.MINIMIZE,
new InitialGuess(modelData)
);
return finalData.getPoint();
}
首先,我创建一个ObjectiveFunction,它是我作为apache ObjectiveFunction类的包装器创建的一个类,它扩展了它。我之所以这样做,是因为目标函数本身就是几个参数的函数,这些参数对我的问题并不重要。然后我构建一个SimplexOptimizer,并尝试按照我在网上找到的一个例子调用optimize()。尽管文档没有明确说明需要什么作为输入,但我相信我提供了所有必要的参数,我可以在下面的编辑中讨论一个可能的例外。无论如何,我收到以下错误:
Exception in thread "main" org.apache.commons.math3.exception.NullArgumentException: null is not allowed
at org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer.checkParameters(SimplexOptimizer.java:214)
at org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer.doOptimize(SimplexOptimizer.java:128)
at org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer.doOptimize(SimplexOptimizer.java:89)
at org.apache.commons.math3.optim.BaseOptimizer.optimize(BaseOptimizer.java:154)
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 compfin3.CompFin3.Optimize(CompFin3.java:181)
at compfin3.CompFin3.main(CompFin3.java:118)
Java Result: 1
有没有人知道这个错误所指的“null”值,以及我如何修复它?
最佳,
保
编辑: 我相信我已经找到了问题的根源。看来,作为我传递给优化器的优化数据的一部分,我忘了定义我正在使用的特定AbstractSimplex。不幸的是,文档中给出的构造函数都没有实际工作。当我尝试
org.apache.commons.math3.optim.PointValuePair finalData=optimizer.optimize(
new org.apache.commons.math3.optim.MaxEval(200),
obj,
GoalType.MINIMIZE,
new InitialGuess(modelData),
new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.AbstractSimplex(2)
);
我收到编译错误
AbstractSimplex is abstract, cannot be instantiated
即使这是该类的文档构造函数之一。
答案 0 :(得分:1)
以下代码解决了我的问题:
public static double[] Optimize(double[][] contractDataMatrix,double[] minData, double[] maxData,double[] modelData,String modelType,String weightType){
ObjectiveFunction objective = new ObjectiveFunction(contractDataMatrix,modelType,weightType);
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer optimizer =new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer(.01,.001);
org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction obj = new org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction(objective);
int n = modelData.length;
org.apache.commons.math3.optim.PointValuePair finalData=optimizer.optimize(
new org.apache.commons.math3.optim.MaxEval(200),
obj,
GoalType.MINIMIZE,
new InitialGuess(modelData),
new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex(n)
);
return finalData.getPoint();
}