Example usage for org.apache.commons.math.optimization.direct NelderMead setConvergenceChecker

List of usage examples for org.apache.commons.math.optimization.direct NelderMead setConvergenceChecker

Introduction

In this page you can find the example usage for org.apache.commons.math.optimization.direct NelderMead setConvergenceChecker.

Prototype

public void setConvergenceChecker(RealConvergenceChecker convergenceChecker) 

Source Link

Usage

From source file:edu.valelab.GaussianFit.ZCalibrator.java

/**
 * Use the fitfunction to estimate the z position given width in x and y
 * /*from  w w  w  .  j  a  va  2s . c  o m*/
 * minimize the distance D in sqrt wx and sqrt wy space
 * D = sqrt (  square (sqrt wx - sqrt wx, calib) + sqr(sqrt wy - sqrt w, calib) )
 * 
 * 
 */

public double getZ(double wx, double wy) {
    if (!hasFitFunctions())
        return 0.0;

    NelderMead nmx = new NelderMead();
    SimpleScalarValueChecker convergedChecker_ = new SimpleScalarValueChecker(1e-6, -1);

    MultiVariateZFunction mz = new MultiVariateZFunction(fitFunctionWx_, fitFunctionWy_, wx, wy);

    double[] params0_ = new double[1]; // initial estimates:
    params0_[0] = 15; // TODO: Need the middle z value of the stack here!!!

    nmx.setStartConfiguration(params0_);
    nmx.setConvergenceChecker(convergedChecker_);
    nmx.setMaxIterations(maxIterations_);

    double[] paramsOut = { 0.0 };

    try {
        RealPointValuePair result = nmx.optimize(mz, GoalType.MINIMIZE, params0_);
        paramsOut = result.getPoint();
    } catch (java.lang.OutOfMemoryError e) {
        throw (e);
    } catch (Exception e) {
        ij.IJ.log(" " + e.toString());
    }

    return paramsOut[0];
}

From source file:edu.valelab.gaussianfit.fitting.ZCalibrator.java

/**
 * Use the fitfunction to estimate the z position given width in x and y
 * /*from w w  w. ja  v a 2s. c o  m*/
 * minimize the distance D in sqrt wx and sqrt wy space
 * D = sqrt (  square (sqrt wx - sqrt wx, calib) + sqr(sqrt wy - sqrt w, calib) )
 * 
 * 
 * @param wx - width in x
 * @param wy - width in y
 * @return - calculated z position
 */

public double getZ(double wx, double wy) {
    if (!hasFitFunctions())
        return 0.0;

    NelderMead nmx = new NelderMead();
    SimpleScalarValueChecker convergedChecker_ = new SimpleScalarValueChecker(1e-6, -1);

    MultiVariateZFunction mz = new MultiVariateZFunction(fitFunctionWx_, fitFunctionWy_, wx, wy);

    double[] params0_ = new double[1]; // initial estimates:
    params0_[0] = 15; // TODO: Need the middle z value of the stack here!!!

    nmx.setStartConfiguration(params0_);
    nmx.setConvergenceChecker(convergedChecker_);
    nmx.setMaxIterations(maxIterations_);

    double[] paramsOut = { 0.0 };

    try {
        RealPointValuePair result = nmx.optimize(mz, GoalType.MINIMIZE, params0_);
        paramsOut = result.getPoint();
    } catch (java.lang.OutOfMemoryError e) {
        throw (e);
    } catch (FunctionEvaluationException e) {
        ij.IJ.log(" " + e.toString());
    } catch (OptimizationException e) {
        ij.IJ.log(" " + e.toString());
    } catch (IllegalArgumentException e) {
        ij.IJ.log(" " + e.toString());
    }

    return paramsOut[0];
}

From source file:edu.valelab.gaussianfit.fitting.ZCalibrator.java

/**
 * Creates fitFunctionWx_ and fitFunctionWy_ based on data in data_
 * /*from  w w  w . j  a  v a  2  s. co  m*/
 * 
 * @throws org.apache.commons.math.FunctionEvaluationException
 * @throws org.apache.commons.math.optimization.OptimizationException
 */
public void fitFunction() throws FunctionEvaluationException, OptimizationException {

    NelderMead nmx = new NelderMead();
    SimpleScalarValueChecker convergedChecker_ = new SimpleScalarValueChecker(1e-6, -1);

    double[][] wxData = getDataAsArray(0);
    MultiVariateZCalibrationFunction mvcx = new MultiVariateZCalibrationFunction(wxData);

    double[] params0_ = new double[5]; // initial estimates:
    params0_[0] = 37; // TODO: better estimate for c
    params0_[1] = 200; // Estimate for w0
    params0_[2] = 10; // TODO: better estimate for d
    params0_[3] = 1; // TODO: better estimate for A
    params0_[4] = 1; // TODO: better estimate for B

    nmx.setStartConfiguration(params0_);
    nmx.setConvergenceChecker(convergedChecker_);
    nmx.setMaxIterations(maxIterations_);

    double[] paramsOut;

    RealPointValuePair result = nmx.optimize(mvcx, GoalType.MINIMIZE, params0_);
    paramsOut = result.getPoint();

    // write fit result to Results Table:
    ResultsTable res = new ResultsTable();
    res.incrementCounter();
    res.addValue("c", paramsOut[0]);
    res.addValue("w0", paramsOut[1]);
    res.addValue("d", paramsOut[2]);
    res.addValue("A", paramsOut[3]);
    res.addValue("B", paramsOut[4]);

    fitFunctionWx_ = paramsOut;

    double[][] yxData = getDataAsArray(1);
    MultiVariateZCalibrationFunction yvcx = new MultiVariateZCalibrationFunction(yxData);

    nmx.setStartConfiguration(params0_);

    result = nmx.optimize(yvcx, GoalType.MINIMIZE, params0_);
    paramsOut = result.getPoint();

    res.incrementCounter();
    res.addValue("c", paramsOut[0]);
    res.addValue("w0", paramsOut[1]);
    res.addValue("d", paramsOut[2]);
    res.addValue("A", paramsOut[3]);
    res.addValue("B", paramsOut[4]);

    res.show("Fit Parameters");

    fitFunctionWy_ = paramsOut;

    plotFitFunctions();

}

From source file:edu.valelab.GaussianFit.ZCalibrator.java

/**
 * Creates fitFunctionWx_ and fitFunctionWy_ based on data in data_
 * //from ww  w .  j  a  va  2s  .  c  om
 * 
 */
public void fitFunction() throws FunctionEvaluationException, OptimizationException {

    NelderMead nmx = new NelderMead();
    SimpleScalarValueChecker convergedChecker_ = new SimpleScalarValueChecker(1e-6, -1);

    double[][] wxData = getDataAsArray(0);
    MultiVariateZCalibrationFunction mvcx = new MultiVariateZCalibrationFunction(wxData);

    double[] params0_ = new double[5]; // initial estimates:
    params0_[0] = 37; // TODO: better estimate for c
    params0_[1] = 200; // Estimate for w0
    params0_[2] = 10; // TODO: better estimate for d
    params0_[3] = 1; // TODO: better estimate for A
    params0_[4] = 1; // TODO: better estimate for B

    nmx.setStartConfiguration(params0_);
    nmx.setConvergenceChecker(convergedChecker_);
    nmx.setMaxIterations(maxIterations_);

    double[] paramsOut = { 0.0 };

    RealPointValuePair result = nmx.optimize(mvcx, GoalType.MINIMIZE, params0_);
    paramsOut = result.getPoint();

    //for (int i = 0; i < paramsOut.length; i++) {
    //  System.out.println("Result " + i + " value: " + (int) paramsOut[i]);
    //}

    // write fit result to Results Table:
    ResultsTable res = new ResultsTable();
    res.incrementCounter();
    res.addValue("c", paramsOut[0]);
    res.addValue("w0", paramsOut[1]);
    res.addValue("d", paramsOut[2]);
    res.addValue("A", paramsOut[3]);
    res.addValue("B", paramsOut[4]);

    fitFunctionWx_ = paramsOut;

    double[][] yxData = getDataAsArray(1);
    MultiVariateZCalibrationFunction yvcx = new MultiVariateZCalibrationFunction(yxData);

    nmx.setStartConfiguration(params0_);

    result = nmx.optimize(yvcx, GoalType.MINIMIZE, params0_);
    paramsOut = result.getPoint();

    System.out.println("Y:");

    //for (int i = 0; i < paramsOut.length; i++) {
    //  System.out.println("Result " + i + " value: " + (int) paramsOut[i]);
    //}

    res.incrementCounter();
    res.addValue("c", paramsOut[0]);
    res.addValue("w0", paramsOut[1]);
    res.addValue("d", paramsOut[2]);
    res.addValue("A", paramsOut[3]);
    res.addValue("B", paramsOut[4]);

    res.show("Fit Parameters");

    fitFunctionWy_ = paramsOut;

    plotFitFunctions();

}