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 | * http://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 | package agents.anac.y2019.harddealer.math3.fitting.leastsquares;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.analysis.MultivariateMatrixFunction;
|
---|
20 | import agents.anac.y2019.harddealer.math3.analysis.MultivariateVectorFunction;
|
---|
21 | import agents.anac.y2019.harddealer.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
|
---|
22 | import agents.anac.y2019.harddealer.math3.linear.ArrayRealVector;
|
---|
23 | import agents.anac.y2019.harddealer.math3.linear.RealMatrix;
|
---|
24 | import agents.anac.y2019.harddealer.math3.linear.RealVector;
|
---|
25 | import agents.anac.y2019.harddealer.math3.optim.ConvergenceChecker;
|
---|
26 | import agents.anac.y2019.harddealer.math3.optim.PointVectorValuePair;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * A mutable builder for {@link LeastSquaresProblem}s.
|
---|
30 | *
|
---|
31 | * @see LeastSquaresFactory
|
---|
32 | * @since 3.3
|
---|
33 | */
|
---|
34 | public class LeastSquaresBuilder {
|
---|
35 |
|
---|
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 | * @since 3.4
|
---|
54 | */
|
---|
55 | private boolean lazyEvaluation;
|
---|
56 | /** Validator.
|
---|
57 | *
|
---|
58 | * @since 3.4
|
---|
59 | */
|
---|
60 | private ParameterValidator paramValidator;
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Construct a {@link LeastSquaresProblem} from the data in this builder.
|
---|
65 | *
|
---|
66 | * @return a new {@link LeastSquaresProblem}.
|
---|
67 | */
|
---|
68 | public LeastSquaresProblem build() {
|
---|
69 | return LeastSquaresFactory.create(model,
|
---|
70 | target,
|
---|
71 | start,
|
---|
72 | weight,
|
---|
73 | checker,
|
---|
74 | maxEvaluations,
|
---|
75 | maxIterations,
|
---|
76 | lazyEvaluation,
|
---|
77 | paramValidator);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Configure the max evaluations.
|
---|
82 | *
|
---|
83 | * @param newMaxEvaluations the maximum number of evaluations permitted.
|
---|
84 | * @return this
|
---|
85 | */
|
---|
86 | public LeastSquaresBuilder maxEvaluations(final int newMaxEvaluations) {
|
---|
87 | this.maxEvaluations = newMaxEvaluations;
|
---|
88 | return this;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Configure the max iterations.
|
---|
93 | *
|
---|
94 | * @param newMaxIterations the maximum number of iterations permitted.
|
---|
95 | * @return this
|
---|
96 | */
|
---|
97 | public LeastSquaresBuilder maxIterations(final int newMaxIterations) {
|
---|
98 | this.maxIterations = newMaxIterations;
|
---|
99 | return this;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Configure the convergence checker.
|
---|
104 | *
|
---|
105 | * @param newChecker the convergence checker.
|
---|
106 | * @return this
|
---|
107 | */
|
---|
108 | public LeastSquaresBuilder checker(final ConvergenceChecker<Evaluation> newChecker) {
|
---|
109 | this.checker = newChecker;
|
---|
110 | return this;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Configure the convergence checker.
|
---|
115 | * <p/>
|
---|
116 | * This function is an overloaded version of {@link #checker(ConvergenceChecker)}.
|
---|
117 | *
|
---|
118 | * @param newChecker the convergence checker.
|
---|
119 | * @return this
|
---|
120 | */
|
---|
121 | public LeastSquaresBuilder checkerPair(final ConvergenceChecker<PointVectorValuePair> newChecker) {
|
---|
122 | return this.checker(LeastSquaresFactory.evaluationChecker(newChecker));
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Configure the model function.
|
---|
127 | *
|
---|
128 | * @param value the model function value
|
---|
129 | * @param jacobian the Jacobian of {@code value}
|
---|
130 | * @return this
|
---|
131 | */
|
---|
132 | public LeastSquaresBuilder model(final MultivariateVectorFunction value,
|
---|
133 | final MultivariateMatrixFunction jacobian) {
|
---|
134 | return model(LeastSquaresFactory.model(value, jacobian));
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Configure the model function.
|
---|
139 | *
|
---|
140 | * @param newModel the model function value and Jacobian
|
---|
141 | * @return this
|
---|
142 | */
|
---|
143 | public LeastSquaresBuilder model(final MultivariateJacobianFunction newModel) {
|
---|
144 | this.model = newModel;
|
---|
145 | return this;
|
---|
146 | }
|
---|
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 | /**
|
---|
160 | * Configure the observed data.
|
---|
161 | *
|
---|
162 | * @param newTarget the observed data.
|
---|
163 | * @return this
|
---|
164 | */
|
---|
165 | public LeastSquaresBuilder target(final double[] newTarget) {
|
---|
166 | return target(new ArrayRealVector(newTarget, false));
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Configure the initial guess.
|
---|
171 | *
|
---|
172 | * @param newStart the initial guess.
|
---|
173 | * @return this
|
---|
174 | */
|
---|
175 | public LeastSquaresBuilder start(final RealVector newStart) {
|
---|
176 | this.start = newStart;
|
---|
177 | return this;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Configure the initial guess.
|
---|
182 | *
|
---|
183 | * @param newStart the initial guess.
|
---|
184 | * @return this
|
---|
185 | */
|
---|
186 | public LeastSquaresBuilder start(final double[] newStart) {
|
---|
187 | return start(new ArrayRealVector(newStart, false));
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Configure the weight matrix.
|
---|
192 | *
|
---|
193 | * @param newWeight the weight matrix
|
---|
194 | * @return this
|
---|
195 | */
|
---|
196 | public LeastSquaresBuilder weight(final RealMatrix newWeight) {
|
---|
197 | this.weight = newWeight;
|
---|
198 | return this;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Configure whether evaluation will be lazy or not.
|
---|
203 | *
|
---|
204 | * @param newValue Whether to perform lazy evaluation.
|
---|
205 | * @return this object.
|
---|
206 | *
|
---|
207 | * @since 3.4
|
---|
208 | */
|
---|
209 | public LeastSquaresBuilder lazyEvaluation(final boolean newValue) {
|
---|
210 | lazyEvaluation = newValue;
|
---|
211 | return this;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Configure the validator of the model parameters.
|
---|
216 | *
|
---|
217 | * @param newValidator Parameter validator.
|
---|
218 | * @return this object.
|
---|
219 | *
|
---|
220 | * @since 3.4
|
---|
221 | */
|
---|
222 | public LeastSquaresBuilder parameterValidator(final ParameterValidator newValidator) {
|
---|
223 | paramValidator = newValidator;
|
---|
224 | return this;
|
---|
225 | }
|
---|
226 | }
|
---|