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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.optim.nonlinear.vector.leastsquares;
23
24 import org.hipparchus.analysis.MultivariateMatrixFunction;
25 import org.hipparchus.analysis.MultivariateVectorFunction;
26 import org.hipparchus.linear.ArrayRealVector;
27 import org.hipparchus.linear.RealMatrix;
28 import org.hipparchus.linear.RealVector;
29 import org.hipparchus.optim.ConvergenceChecker;
30 import org.hipparchus.optim.PointVectorValuePair;
31 import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
32
33 /**
34 * A mutable builder for {@link LeastSquaresProblem}s.
35 *
36 * @see LeastSquaresFactory
37 */
38 public class LeastSquaresBuilder {
39
40 /** max evaluations */
41 private int maxEvaluations;
42 /** max iterations */
43 private int maxIterations;
44 /** convergence checker */
45 private ConvergenceChecker<Evaluation> checker;
46 /** model function */
47 private MultivariateJacobianFunction model;
48 /** observed values */
49 private RealVector target;
50 /** initial guess */
51 private RealVector start;
52 /** weight matrix */
53 private RealMatrix weight;
54 /**
55 * Lazy evaluation.
56 *
57 */
58 private boolean lazyEvaluation;
59 /** Validator.
60 *
61 */
62 private ParameterValidator paramValidator;
63
64 /** Empty constructor.
65 * <p>
66 * This constructor is not strictly necessary, but it prevents spurious
67 * javadoc warnings with JDK 18 and later.
68 * </p>
69 * @since 3.0
70 */
71 public LeastSquaresBuilder() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
72 // nothing to do
73 }
74
75 /**
76 * Construct a {@link LeastSquaresProblem} from the data in this builder.
77 *
78 * @return a new {@link LeastSquaresProblem}.
79 */
80 public LeastSquaresProblem build() {
81 return LeastSquaresFactory.create(model,
82 target,
83 start,
84 weight,
85 checker,
86 maxEvaluations,
87 maxIterations,
88 lazyEvaluation,
89 paramValidator);
90 }
91
92 /**
93 * Configure the max evaluations.
94 *
95 * @param newMaxEvaluations the maximum number of evaluations permitted.
96 * @return this
97 */
98 public LeastSquaresBuilder maxEvaluations(final int newMaxEvaluations) {
99 this.maxEvaluations = newMaxEvaluations;
100 return this;
101 }
102
103 /**
104 * Configure the max iterations.
105 *
106 * @param newMaxIterations the maximum number of iterations permitted.
107 * @return this
108 */
109 public LeastSquaresBuilder maxIterations(final int newMaxIterations) {
110 this.maxIterations = newMaxIterations;
111 return this;
112 }
113
114 /**
115 * Configure the convergence checker.
116 *
117 * @param newChecker the convergence checker.
118 * @return this
119 */
120 public LeastSquaresBuilder checker(final ConvergenceChecker<Evaluation> newChecker) {
121 this.checker = newChecker;
122 return this;
123 }
124
125 /**
126 * Configure the convergence checker.
127 * <p>
128 * This function is an overloaded version of {@link #checker(ConvergenceChecker)}.
129 * </p>
130 *
131 * @param newChecker the convergence checker.
132 * @return this
133 */
134 public LeastSquaresBuilder checkerPair(final ConvergenceChecker<PointVectorValuePair> newChecker) {
135 return this.checker(LeastSquaresFactory.evaluationChecker(newChecker));
136 }
137
138 /**
139 * Configure the model function.
140 *
141 * @param value the model function value
142 * @param jacobian the Jacobian of {@code value}
143 * @return this
144 */
145 public LeastSquaresBuilder model(final MultivariateVectorFunction value,
146 final MultivariateMatrixFunction jacobian) {
147 return model(LeastSquaresFactory.model(value, jacobian));
148 }
149
150 /**
151 * Configure the model function.
152 *
153 * @param newModel the model function value and Jacobian
154 * @return this
155 */
156 public LeastSquaresBuilder model(final MultivariateJacobianFunction newModel) {
157 this.model = newModel;
158 return this;
159 }
160
161 /**
162 * Configure the observed data.
163 *
164 * @param newTarget the observed data.
165 * @return this
166 */
167 public LeastSquaresBuilder target(final RealVector newTarget) {
168 this.target = newTarget;
169 return this;
170 }
171
172 /**
173 * Configure the observed data.
174 *
175 * @param newTarget the observed data.
176 * @return this
177 */
178 public LeastSquaresBuilder target(final double[] newTarget) {
179 return target(new ArrayRealVector(newTarget, false));
180 }
181
182 /**
183 * Configure the initial guess.
184 *
185 * @param newStart the initial guess.
186 * @return this
187 */
188 public LeastSquaresBuilder start(final RealVector newStart) {
189 this.start = newStart;
190 return this;
191 }
192
193 /**
194 * Configure the initial guess.
195 *
196 * @param newStart the initial guess.
197 * @return this
198 */
199 public LeastSquaresBuilder start(final double[] newStart) {
200 return start(new ArrayRealVector(newStart, false));
201 }
202
203 /**
204 * Configure the weight matrix.
205 *
206 * @param newWeight the weight matrix
207 * @return this
208 */
209 public LeastSquaresBuilder weight(final RealMatrix newWeight) {
210 this.weight = newWeight;
211 return this;
212 }
213
214 /**
215 * Configure whether evaluation will be lazy or not.
216 *
217 * @param newValue Whether to perform lazy evaluation.
218 * @return this object.
219 *
220 */
221 public LeastSquaresBuilder lazyEvaluation(final boolean newValue) {
222 lazyEvaluation = newValue;
223 return this;
224 }
225
226 /**
227 * Configure the validator of the model parameters.
228 *
229 * @param newValidator Parameter validator.
230 * @return this object.
231 *
232 */
233 public LeastSquaresBuilder parameterValidator(final ParameterValidator newValidator) {
234 paramValidator = newValidator;
235 return this;
236 }
237 }