Thanks Luc for your help.
I modified my code as you recommend (it is given below), but it doesn't work
either.
The error message (at running) is :
Exception in thread "main"
org.apache.commons.math3.exception.MathUnsupportedOperationException: contrainte
at
org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer.checkParameters(LevenbergMarquardtOptimizer.java:954)
at
org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer.doOptimize(LevenbergMarquardtOptimizer.java:287)
at
org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer.doOptimize(LevenbergMarquardtOptimizer.java:113)
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.vector.MultivariateVectorOptimizer.optimize(MultivariateVectorOptimizer.java:87)
at
org.apache.commons.math3.optim.nonlinear.vector.JacobianMultivariateVectorOptimizer.optimize(JacobianMultivariateVectorOptimizer.java:83)
at
org.apache.commons.math3.optim.nonlinear.vector.jacobian.AbstractLeastSquaresOptimizer.optimize(AbstractLeastSquaresOptimizer.java:197)
at optimNL.main(optimNL.java:33)
It is about a contraint ...
My code is below.
- class optimNL:
* a new LevenbergMarquardtOptimizer is now built using optim library rather
than optimisation
(org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer)
* the optimize method of this class is used with 7 parameters as specified in
the doc
Methods inherited from class
org.apache.commons.math3.optim.nonlinear.vector.jacobian.AbstractLeastSquaresOptimizer
* specified parameters in the doc are
MaxEval
InitialGuess
SimpleBounds
Target
Weight
ModelFunction
ModelFunctionJacobian
* I built a model function and its Jacobian, according to the
MultivariateVectorFunction and MultivariateMatrixFunction interfaces,
respectively.
I currently test on a very simple case : 2x + 1.
But that doesn't work either on the Bevington pb with the same error message.
Do you any idea about what goes wrong ?
Thank you in advance for any help.
Eric
*********** main ************
import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction;
import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian;
import org.apache.commons.math3.optim.nonlinear.vector.Target;
import org.apache.commons.math3.optim.nonlinear.vector.Weight;
import
org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer;
import org.apache.commons.math3.optim.*;
public class optimNL {
public static void main (String[] args) {
final double[][] dataPoints = {
// column 1 = x
{1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.},
// column 2 = observations
{3.1,4.9,6.8,9.0,11.3,13.1,14.5,17.1,19.1,20.5,23.2,25.2},
};
// weight Matrix
final double[] weights = new double[12];
for (int i = 0; i < weights.length; i++) {weights[i] = 1 / dataPoints[1][i];}
//guess
double[] startPoint = {1, 0.0}; // expectedSolution : a = 2, b=1; (2x + 1)
double[] lowerBound = {-10., -10.};
double[] upperBound = {10.0, 10.0};
double[] observations = dataPoints[1];
final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
final PointVectorValuePair optimum = optimizer.optimize( new MaxEval(100),
new
InitialGuess(startPoint),
new
SimpleBounds(lowerBound, upperBound),
new
Target(observations),
new
Weight(weights),
new ModelFunction(
new myFunction()),
new
ModelFunctionJacobian(new myJacobianFunction()));
final double[] solution = optimum.getPoint();
for (int i = 0; i < startPoint.length; i++) {
System.out.println("Parameter " + i + ", solution = " + solution[i]);
}// end of for
}// end
}//end of class
*********** myFunction ************
import org.apache.commons.math3.analysis.MultivariateVectorFunction;
public class myFunction implements MultivariateVectorFunction {
private double[] x = {1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.};
public myFunction() {}
public double[] value (double[] param) {
double[] values = new double[x.length];
System.out.println("function has been called");
for (int i = 0; i < x.length; ++i) {
final double t = x[i];
values[i] = param[0] * t + param[1];
}
return values;
}// end of value function
}// end of class myFunction
*********** myJacobianFunction ************
import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
public class myJacobianFunction implements MultivariateMatrixFunction {
private double[] x = {1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.};
public myJacobianFunction() {}
public double[][] value (double[] param) {
double[][] jacobian = new double[x.length][2];
System.out.println("Jacobian has been called");
for (int i = 0; i < x.length; ++i) {
final double t = x[i];
jacobian[i][0] = t;
jacobian[i][1] = 1;
}
return jacobian;
}// end of value function
}// end of class myJacobianFunction
-------------------------------------------------------------
Le 17 avr. 2013 à 10:24, Luc Maisonobe a écrit :
> Le 17/04/2013 10:08, Eric HENON a écrit :
>> Hello,
>>
>> I'm trying to do a non linear
>> regression using the LevenbergMarquardtOptimizer.
>>
>> I have copied the proposed Demo code at:
>> http://commons.apache.org/proper/commons-math/userguide/optimization.html
>>
>> and I get the following error at the java
>> compilation time:
>>
>> "The method optimize(OptimizationData..) in the type
>> AbstractLeastSquaresOptimizer is not
>> applicable for the arguments (int, QuadraticProblem, double[], double[],
>> double[])"
>>
>> It seems that the optimize method employed in the Demo code is not
>> the appropriate one.
>
> You are right. This code correspond to the former API in the
> optimization package, which is still available but deprecated now. The
> new API is in the optim package and it is the one you use, which expect
> OptimizationData parameters.
>
> You can try to wrap the arguments in the various implementations of the
> OptimizationData interface according to their meaning.
>
> So you should end up with something like;
>
> lm.optimize(new MaxEval(maxEval),
> new ModelFunction(function),
> new ModelFunctionJacobian(jacobian),
> new Target(target),
> new Weight(weight),
> new InitialGuess(initial));
>
> hope this helps.
>
> Luc
>
>>
>> I tried to change the imports, but that does not work.
>>
>> Any help will be appreciated.
>> Thanks.
>> Eric
>>
>> ****
>> ...
>> LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
>> ...
>> PointVectorValuePair optimum = optimizer.optimize(100, problem,
>> problem.calculateTarget(), weights, initialSolution);
>> ****
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>