Example usage for org.apache.commons.math3.fitting.leastsquares LeastSquaresBuilder parameterValidator

List of usage examples for org.apache.commons.math3.fitting.leastsquares LeastSquaresBuilder parameterValidator

Introduction

In this page you can find the example usage for org.apache.commons.math3.fitting.leastsquares LeastSquaresBuilder parameterValidator.

Prototype

public LeastSquaresBuilder parameterValidator(final ParameterValidator newValidator) 

Source Link

Document

Configure the validator of the model parameters.

Usage

From source file:de.bund.bfr.math.LeastSquaresOptimization.java

private LeastSquaresBuilder createLeastSquaresBuilder(List<Double> startValues, int maxIterations) {
    LeastSquaresBuilder builder = new LeastSquaresBuilder()
            .model(optimizerFunction, optimizerFunction.createJacobian()).maxEvaluations(Integer.MAX_VALUE)
            .maxIterations(maxIterations).target(Doubles.toArray(targetValues))
            .start(Doubles.toArray(startValues));

    if (!minValues.isEmpty() || !maxValues.isEmpty()) {
        builder.parameterValidator(params -> {

            for (int i = 0; i < parameters.size(); i++) {
                double value = params.getEntry(i);
                Double min = minValues.get(parameters.get(i));
                Double max = maxValues.get(parameters.get(i));

                if (min != null && value < min) {
                    value = min;/* w ww  .jav a2  s.  c om*/
                }

                if (max != null && value > max) {
                    value = max;
                }

                params.setEntry(i, value);
            }

            return params;
        });
    }

    return builder;
}