Le 27 avr. 2013 à 19:25, Gilles a écrit :

> On Sat, 27 Apr 2013 09:50:49 +0200, eric henon wrote:
>> 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
>> [...]
>> 
>> It is about a contraint ...
> 
> Indeed.
> 
>> [...]
>> 
>> Do you any idea about what goes wrong ?
> 
> Yes. The "SimpleBounds" (constraint) is not a parameter of 
> "LevenbergMarquardtOptimizer";
> hence an exception is thrown. Just try to remove this parameter.

That works !
Very hard to guess that this parameter (Bound) is not required according to the 
documentation.

Thank you very much Gilles.
I give below the code that works well.
Have a nice week.
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 optimNL1 {

        public static void main (String[] args) {



final double[] dataPointsX  = {0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0,2.2, 
2.4,2.6, 2.8, 3.0, 3.2, 3.4, 3.6, 3.8, 4.0, 4.2, 4.4, 4.6, 4.8};
final double[] observations = {-19.8,-9.0, -5.9, -4.1, -3.0, -2.2, -1.6, -1.2, 
-0.9, -0.6, -0.4,-0.2, -0.06, 0.08, 0.22, 0.33, 0.44, 0.53, 0.61, 0.69, 0.76, 
0.83, 0.89, 0.95
};

// weight Matrix
final double[] weights = new double[dataPointsX.length];
for (int i = 0; i < weights.length; i++) {weights[i] = 1.0; }

//guess
double[] startPoint = {0.1,   2.0,   -10.}; // expectedSolution : a = 1, b=0.5, 
c=-4;  (y = a + b ln x +c/x)

// build an optimizer
final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();


// build the function embedding dataX
myFunction1 f = new myFunction1(dataPointsX);
//build the associated jacobian function embedding the same dataX
myJacobianFunction1 jF = new myJacobianFunction1(dataPointsX);

// solve the problem associated with function myFunction1 and its Jacobian 
myJacobianFunction1
// number of parameters is known through length of array startPoint
// an initial guess is provided for the parameters
final PointVectorValuePair optimum = optimizer.optimize( new MaxEval(100),
                                                             new 
InitialGuess(startPoint),                                                       
   
                                                             new 
Target(observations),
                                                             new 
Weight(weights),
                                                             new 
ModelFunction(f),
                                                             new 
ModelFunctionJacobian(jF));


// get the parameters
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 of constructor
        
}//end of class

********** FUNCTION ********
import org.apache.commons.math3.analysis.MultivariateVectorFunction;
import java.util.*;

public class myFunction1 implements MultivariateVectorFunction {

private ArrayList<Double> x = new ArrayList<Double>();

public myFunction1(double[] dataPoints) {
        
        for (int i=0; i<dataPoints.length; i++) {x.add(dataPoints[i]);} 
}



// function value will be called by LevenbergMarquardtOptimizer
public double[] value (double[] param) {
        
double[] values = new double[x.size()];

for (int i = 0; i < x.size(); ++i) {
   final double t = x.get(i);
   values[i] = param[0] + param[1]* Math.log(t) + param[2]/t; 
}
return values;

}// end of value function
}// end of class
***********************************

****** JACOBIAN FUNCTION *******
import java.util.ArrayList;

import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
import org.apache.commons.math3.analysis.MultivariateVectorFunction;


public class myJacobianFunction1 implements MultivariateMatrixFunction {

private ArrayList<Double> x = new ArrayList<Double>();

public myJacobianFunction1(double[] dataPoints) {
        
        for (int i=0; i<dataPoints.length; i++) {x.add(dataPoints[i]);} 
}

//function value will be called by LevenbergMarquardtOptimizer
public double[][] value (double[] param) {
        

double[][] jacobian = new double[x.size()][3];

for (int i = 0; i < x.size(); ++i) {
   final double t = x.get(i);
   
   jacobian[i][0] = 1; 
   jacobian[i][1] = Math.log(t);
   jacobian[i][2] = 1/t;
   
   
}
return jacobian;

}// end of value function
}// end of class
*****************************************
        

 



> 
> Regards,
> Gilles
> 
>> [...]
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to