commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From luca.marche...@univr.it
Subject Re: [math] Problem using the LevenbergMarquardtOptimizer
Date Fri, 01 Feb 2013 18:34:26 GMT
package testmptheory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
import org.apache.commons.math3.analysis.MultivariateVectorFunction;
import org.apache.commons.math3.optim.InitialGuess;
import org.apache.commons.math3.optim.MaxEval;
import org.apache.commons.math3.optim.PointVectorValuePair;
import org.apache.commons.math3.optim.SimpleBounds;
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;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author luca
 */
public class QuadraticProblemNew implements Serializable {

    private static final long serialVersionUID = 7072187082052755854L;

    private static List<Double> x;
    private static List<Double> y;

    public static void addPoint(double xp, double yp) {
        x.add(xp);
        y.add(yp);
    }

    public static double[] calculateTarget() {
        double[] target = new double[y.size()];
        for (int i = 0; i < y.size(); i++) {
                target[i] = y.get(i).doubleValue();
        }
        return target;
    }

    public static void main(String[] a) {

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

        // data-set
        addPoint(1, 34.234064369);
        addPoint(2, 68.2681162306);
        addPoint(3, 118.6158990846);
        addPoint(4, 184.1381972386);
        addPoint(5, 266.5998779163);
        addPoint(6, 364.1477352516);
        addPoint(7, 478.0192260919);
        addPoint(8, 608.1409492707);
        addPoint(9, 754.5988686671);
        addPoint(10, 916.1288180859);

        LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();

        MaxEval mEv = new MaxEval(100);

        final double[] iSolution = {1, 1, 1};
        InitialGuess initialSolution = new InitialGuess(iSolution);

        double[] lb = { 0, 0, 0};
        double[] ub = { 2, 2, 2};
        SimpleBounds bounds = new SimpleBounds(lb,ub);
        SimpleBounds unbounds = SimpleBounds.unbounded(iSolution.length);
        
        Target trg = new Target(calculateTarget());
        
        double[] w = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
        Weight weights = new Weight(w);
        
        ModelFunction mFun = new ModelFunction(new MultivariateVectorFunction(){
            private static final long serialVersionUID = -8673650298627399464L;
            @Override
            public double[] value(double[] point) throws IllegalArgumentException {
                double[] values = new double[x.size()];
                for (int i = 0; i < values.length; ++i) {
                    values[i] = (point[0] * x.get(i) + point[1]) * x.get(i) + point[2];
                }
                return values;
            }
        });

        ModelFunctionJacobian mFunJ = new ModelFunctionJacobian(new MultivariateMatrixFunction(){
            private static final long serialVersionUID = -8673650298627399464L;
            @Override
            public double[][] value(double[] point) throws IllegalArgumentException {
                double[][] jacobian = new double[x.size()][3];
                for (int i = 0; i < jacobian.length; ++i) {
                    jacobian[i][0] = x.get(i) * x.get(i);
                    jacobian[i][1] = x.get(i);
                    jacobian[i][2] = 1.0;
                }
                return jacobian;
            }
        });
        
        PointVectorValuePair optimum = optimizer.optimize(mEv,initialSolution,unbounds,trg,weights,mFun,mFunJ);
        double[] optimalValues = optimum.getPoint();
        System.out.println("No-bounds estimation:");
        System.out.println("A: " + optimalValues[0]);
        System.out.println("B: " + optimalValues[1]);
        System.out.println("C: " + optimalValues[2]);

        optimum = optimizer.optimize(mEv,initialSolution,bounds,trg,weights,mFun,mFunJ);
        optimalValues = optimum.getPoint();
        System.out.println("\nBounded estimation:");
        System.out.println("A: " + optimalValues[0]);
        System.out.println("B: " + optimalValues[1]);
        System.out.println("C: " + optimalValues[2]);
    }
}
Mime
View raw message