LeastSquaresBuilder.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.nonlinear.vector.leastsquares;

  22. import org.hipparchus.analysis.MultivariateMatrixFunction;
  23. import org.hipparchus.analysis.MultivariateVectorFunction;
  24. import org.hipparchus.linear.ArrayRealVector;
  25. import org.hipparchus.linear.RealMatrix;
  26. import org.hipparchus.linear.RealVector;
  27. import org.hipparchus.optim.ConvergenceChecker;
  28. import org.hipparchus.optim.PointVectorValuePair;
  29. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;

  30. /**
  31.  * A mutable builder for {@link LeastSquaresProblem}s.
  32.  *
  33.  * @see LeastSquaresFactory
  34.  */
  35. public class LeastSquaresBuilder {

  36.     /** max evaluations */
  37.     private int maxEvaluations;
  38.     /** max iterations */
  39.     private int maxIterations;
  40.     /** convergence checker */
  41.     private ConvergenceChecker<Evaluation> checker;
  42.     /** model function */
  43.     private MultivariateJacobianFunction model;
  44.     /** observed values */
  45.     private RealVector target;
  46.     /** initial guess */
  47.     private RealVector start;
  48.     /** weight matrix */
  49.     private RealMatrix weight;
  50.     /**
  51.      * Lazy evaluation.
  52.      *
  53.      */
  54.     private boolean lazyEvaluation;
  55.     /** Validator.
  56.      *
  57.      */
  58.     private ParameterValidator paramValidator;

  59.     /** Empty constructor.
  60.      * <p>
  61.      * This constructor is not strictly necessary, but it prevents spurious
  62.      * javadoc warnings with JDK 18 and later.
  63.      * </p>
  64.      * @since 3.0
  65.      */
  66.     public LeastSquaresBuilder() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  67.         // nothing to do
  68.     }

  69.     /**
  70.      * Construct a {@link LeastSquaresProblem} from the data in this builder.
  71.      *
  72.      * @return a new {@link LeastSquaresProblem}.
  73.      */
  74.     public LeastSquaresProblem build() {
  75.         return LeastSquaresFactory.create(model,
  76.                                           target,
  77.                                           start,
  78.                                           weight,
  79.                                           checker,
  80.                                           maxEvaluations,
  81.                                           maxIterations,
  82.                                           lazyEvaluation,
  83.                                           paramValidator);
  84.     }

  85.     /**
  86.      * Configure the max evaluations.
  87.      *
  88.      * @param newMaxEvaluations the maximum number of evaluations permitted.
  89.      * @return this
  90.      */
  91.     public LeastSquaresBuilder maxEvaluations(final int newMaxEvaluations) {
  92.         this.maxEvaluations = newMaxEvaluations;
  93.         return this;
  94.     }

  95.     /**
  96.      * Configure the max iterations.
  97.      *
  98.      * @param newMaxIterations the maximum number of iterations permitted.
  99.      * @return this
  100.      */
  101.     public LeastSquaresBuilder maxIterations(final int newMaxIterations) {
  102.         this.maxIterations = newMaxIterations;
  103.         return this;
  104.     }

  105.     /**
  106.      * Configure the convergence checker.
  107.      *
  108.      * @param newChecker the convergence checker.
  109.      * @return this
  110.      */
  111.     public LeastSquaresBuilder checker(final ConvergenceChecker<Evaluation> newChecker) {
  112.         this.checker = newChecker;
  113.         return this;
  114.     }

  115.     /**
  116.      * Configure the convergence checker.
  117.      * <p>
  118.      * This function is an overloaded version of {@link #checker(ConvergenceChecker)}.
  119.      * </p>
  120.      *
  121.      * @param newChecker the convergence checker.
  122.      * @return this
  123.      */
  124.     public LeastSquaresBuilder checkerPair(final ConvergenceChecker<PointVectorValuePair> newChecker) {
  125.         return this.checker(LeastSquaresFactory.evaluationChecker(newChecker));
  126.     }

  127.     /**
  128.      * Configure the model function.
  129.      *
  130.      * @param value the model function value
  131.      * @param jacobian the Jacobian of {@code value}
  132.      * @return this
  133.      */
  134.     public LeastSquaresBuilder model(final MultivariateVectorFunction value,
  135.                                      final MultivariateMatrixFunction jacobian) {
  136.         return model(LeastSquaresFactory.model(value, jacobian));
  137.     }

  138.     /**
  139.      * Configure the model function.
  140.      *
  141.      * @param newModel the model function value and Jacobian
  142.      * @return this
  143.      */
  144.     public LeastSquaresBuilder model(final MultivariateJacobianFunction newModel) {
  145.         this.model = newModel;
  146.         return this;
  147.     }

  148.     /**
  149.      * Configure the observed data.
  150.      *
  151.      * @param newTarget the observed data.
  152.      * @return this
  153.      */
  154.     public LeastSquaresBuilder target(final RealVector newTarget) {
  155.         this.target = newTarget;
  156.         return this;
  157.     }

  158.     /**
  159.      * Configure the observed data.
  160.      *
  161.      * @param newTarget the observed data.
  162.      * @return this
  163.      */
  164.     public LeastSquaresBuilder target(final double[] newTarget) {
  165.         return target(new ArrayRealVector(newTarget, false));
  166.     }

  167.     /**
  168.      * Configure the initial guess.
  169.      *
  170.      * @param newStart the initial guess.
  171.      * @return this
  172.      */
  173.     public LeastSquaresBuilder start(final RealVector newStart) {
  174.         this.start = newStart;
  175.         return this;
  176.     }

  177.     /**
  178.      * Configure the initial guess.
  179.      *
  180.      * @param newStart the initial guess.
  181.      * @return this
  182.      */
  183.     public LeastSquaresBuilder start(final double[] newStart) {
  184.         return start(new ArrayRealVector(newStart, false));
  185.     }

  186.     /**
  187.      * Configure the weight matrix.
  188.      *
  189.      * @param newWeight the weight matrix
  190.      * @return this
  191.      */
  192.     public LeastSquaresBuilder weight(final RealMatrix newWeight) {
  193.         this.weight = newWeight;
  194.         return this;
  195.     }

  196.     /**
  197.      * Configure whether evaluation will be lazy or not.
  198.      *
  199.      * @param newValue Whether to perform lazy evaluation.
  200.      * @return this object.
  201.      *
  202.      */
  203.     public LeastSquaresBuilder lazyEvaluation(final boolean newValue) {
  204.         lazyEvaluation = newValue;
  205.         return this;
  206.     }

  207.     /**
  208.      * Configure the validator of the model parameters.
  209.      *
  210.      * @param newValidator Parameter validator.
  211.      * @return this object.
  212.      *
  213.      */
  214.     public LeastSquaresBuilder parameterValidator(final ParameterValidator newValidator) {
  215.         paramValidator = newValidator;
  216.         return this;
  217.     }
  218. }