1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2018 EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 from daCore import BasicObjects
25 import numpy, scipy.optimize
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
30 BasicObjects.Algorithm.__init__(self, "3DVAR")
31 self.defineRequiredParameter(
35 message = "Minimiseur utilisé",
36 listval = ["LBFGSB","TNC", "CG", "NCG", "BFGS"],
38 self.defineRequiredParameter(
39 name = "MaximumNumberOfSteps",
42 message = "Nombre maximal de pas d'optimisation",
45 self.defineRequiredParameter(
46 name = "CostDecrementTolerance",
49 message = "Diminution relative minimale du cout lors de l'arrêt",
51 self.defineRequiredParameter(
52 name = "ProjectedGradientTolerance",
55 message = "Maximum des composantes du gradient projeté lors de l'arrêt",
58 self.defineRequiredParameter(
59 name = "GradientNormTolerance",
62 message = "Maximum des composantes du gradient lors de l'arrêt",
64 self.defineRequiredParameter(
65 name = "StoreInternalVariables",
68 message = "Stockage des variables internes ou intermédiaires du calcul",
70 self.defineRequiredParameter(
71 name = "StoreSupplementaryCalculations",
74 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
76 "APosterioriCorrelations",
77 "APosterioriCovariance",
78 "APosterioriStandardDeviations",
79 "APosterioriVariances",
90 "InnovationAtCurrentState",
91 "CostFunctionJAtCurrentOptimum",
92 "CostFunctionJbAtCurrentOptimum",
93 "CostFunctionJoAtCurrentOptimum",
95 "MahalanobisConsistency",
96 "SimulationQuantiles",
97 "SimulatedObservationAtBackground",
98 "SimulatedObservationAtCurrentState",
99 "SimulatedObservationAtOptimum",
100 "SimulatedObservationAtCurrentOptimum",
103 self.defineRequiredParameter(
107 message = "Liste des valeurs de quantiles",
111 self.defineRequiredParameter(
113 typecast = numpy.random.seed,
114 message = "Graine fixée pour le générateur aléatoire",
116 self.defineRequiredParameter(
117 name = "NumberOfSamplesForQuantiles",
120 message = "Nombre d'échantillons simulés pour le calcul des quantiles",
123 self.defineRequiredParameter(
124 name = "SimulationForQuantiles",
127 message = "Type de simulation pour l'estimation des quantiles",
128 listval = ["Linear", "NonLinear"]
130 self.defineRequiredParameter( # Pas de type
132 message = "Liste des valeurs de bornes",
134 self.requireInputArguments(
135 mandatory= ("Xb", "Y", "HO", "R", "B" ),
138 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
139 self._pre_run(Parameters, Xb, Y, R, B, Q)
141 # Correction pour pallier a un bug de TNC sur le retour du Minimum
142 if "Minimizer" in self._parameters and self._parameters["Minimizer"] == "TNC":
143 self.setParameterValue("StoreInternalVariables",True)
147 Hm = HO["Direct"].appliedTo
148 Ha = HO["Adjoint"].appliedInXTo
150 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
151 # ----------------------------------------------------
152 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
153 HXb = Hm( Xb, HO["AppliedInX"]["HXb"])
156 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
157 if Y.size != HXb.size:
158 raise ValueError("The size %i of observations Y and %i of observed calculation H(X) are different, they have to be identical."%(Y.size,HXb.size))
159 if max(Y.shape) != max(HXb.shape):
160 raise ValueError("The shapes %s of observations Y and %s of observed calculation H(X) are different, they have to be identical."%(Y.shape,HXb.shape))
162 # Précalcul des inversions de B et R
163 # ----------------------------------
167 # Définition de la fonction-coût
168 # ------------------------------
170 _X = numpy.asmatrix(numpy.ravel( x )).T
171 if self._parameters["StoreInternalVariables"] or \
172 "CurrentState" in self._parameters["StoreSupplementaryCalculations"] or \
173 "CurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
174 self.StoredVariables["CurrentState"].store( _X )
176 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
177 _Innovation = Y - _HX
178 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"] or \
179 "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
180 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
181 if "InnovationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
182 self.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
184 Jb = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
185 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
188 self.StoredVariables["CostFunctionJb"].store( Jb )
189 self.StoredVariables["CostFunctionJo"].store( Jo )
190 self.StoredVariables["CostFunctionJ" ].store( J )
191 if "IndexOfOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
192 "CurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
193 "CostFunctionJAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
194 "CostFunctionJbAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
195 "CostFunctionJoAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
196 "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
197 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
198 if "IndexOfOptimum" in self._parameters["StoreSupplementaryCalculations"]:
199 self.StoredVariables["IndexOfOptimum"].store( IndexMin )
200 if "CurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
201 self.StoredVariables["CurrentOptimum"].store( self.StoredVariables["CurrentState"][IndexMin] )
202 if "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
203 self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
204 if "CostFunctionJAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
205 self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
206 if "CostFunctionJbAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
207 self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
208 if "CostFunctionJoAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
209 self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
212 def GradientOfCostFunction(x):
213 _X = numpy.asmatrix(numpy.ravel( x )).T
215 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
216 GradJb = BI * (_X - Xb)
217 GradJo = - Ha( (_X, RI * (Y - _HX)) )
218 GradJ = numpy.asmatrix( numpy.ravel( GradJb ) + numpy.ravel( GradJo ) ).T
221 # Point de démarrage de l'optimisation : Xini = Xb
222 # ------------------------------------
223 Xini = numpy.ravel(Xb)
225 # Minimisation de la fonctionnelle
226 # --------------------------------
227 nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
229 if self._parameters["Minimizer"] == "LBFGSB":
230 Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
233 fprime = GradientOfCostFunction,
235 bounds = self._parameters["Bounds"],
236 maxfun = self._parameters["MaximumNumberOfSteps"]-1,
237 factr = self._parameters["CostDecrementTolerance"]*1.e14,
238 pgtol = self._parameters["ProjectedGradientTolerance"],
239 iprint = self._parameters["optiprint"],
241 nfeval = Informations['funcalls']
242 rc = Informations['warnflag']
243 elif self._parameters["Minimizer"] == "TNC":
244 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
247 fprime = GradientOfCostFunction,
249 bounds = self._parameters["Bounds"],
250 maxfun = self._parameters["MaximumNumberOfSteps"],
251 pgtol = self._parameters["ProjectedGradientTolerance"],
252 ftol = self._parameters["CostDecrementTolerance"],
253 messages = self._parameters["optmessages"],
255 elif self._parameters["Minimizer"] == "CG":
256 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
259 fprime = GradientOfCostFunction,
261 maxiter = self._parameters["MaximumNumberOfSteps"],
262 gtol = self._parameters["GradientNormTolerance"],
263 disp = self._parameters["optdisp"],
266 elif self._parameters["Minimizer"] == "NCG":
267 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
270 fprime = GradientOfCostFunction,
272 maxiter = self._parameters["MaximumNumberOfSteps"],
273 avextol = self._parameters["CostDecrementTolerance"],
274 disp = self._parameters["optdisp"],
277 elif self._parameters["Minimizer"] == "BFGS":
278 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
281 fprime = GradientOfCostFunction,
283 maxiter = self._parameters["MaximumNumberOfSteps"],
284 gtol = self._parameters["GradientNormTolerance"],
285 disp = self._parameters["optdisp"],
289 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
291 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
292 MinJ = self.StoredVariables["CostFunctionJ"][IndexMin]
294 # Correction pour pallier a un bug de TNC sur le retour du Minimum
295 # ----------------------------------------------------------------
296 if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
297 Minimum = self.StoredVariables["CurrentState"][IndexMin]
299 # Obtention de l'analyse
300 # ----------------------
301 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
303 self.StoredVariables["Analysis"].store( Xa.A1 )
305 if "OMA" in self._parameters["StoreSupplementaryCalculations"] or \
306 "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"] or \
307 "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"] or \
308 "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
309 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
310 HXa = self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
311 elif "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
312 HXa = self.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
316 # Calcul de la covariance d'analyse
317 # ---------------------------------
318 if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"] or \
319 "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
320 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
321 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
322 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
323 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
327 _ee = numpy.matrix(numpy.zeros(nb)).T
329 _HtEE = numpy.dot(HtM,_ee)
330 _HtEE = numpy.asmatrix(numpy.ravel( _HtEE )).T
331 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
332 HessienneI = numpy.matrix( HessienneI )
334 if min(A.shape) != max(A.shape):
335 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(self._name,str(A.shape)))
336 if (numpy.diag(A) < 0).any():
337 raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(self._name,))
338 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
340 L = numpy.linalg.cholesky( A )
342 raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(self._name,))
343 self.StoredVariables["APosterioriCovariance"].store( A )
345 # Calculs et/ou stockages supplémentaires
346 # ---------------------------------------
347 if "Innovation" in self._parameters["StoreSupplementaryCalculations"] or \
348 "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"] or \
349 "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"] or \
350 "OMB" in self._parameters["StoreSupplementaryCalculations"]:
352 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
353 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
354 if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
355 self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
356 if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
357 self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
358 if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
359 self.StoredVariables["OMB"].store( numpy.ravel(d) )
360 if "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"]:
361 TraceR = R.trace(Y.size)
362 self.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
363 if "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
364 self.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
365 if "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
366 nech = self._parameters["NumberOfSamplesForQuantiles"]
367 HXa = numpy.matrix(numpy.ravel( HXa )).T
369 for i in range(nech):
370 if self._parameters["SimulationForQuantiles"] == "Linear":
371 dXr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A) - Xa.A1).T
372 dYr = numpy.matrix(numpy.ravel( HtM * dXr )).T
374 elif self._parameters["SimulationForQuantiles"] == "NonLinear":
375 Xr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A)).T
376 Yr = numpy.matrix(numpy.ravel( Hm( Xr ) )).T
380 YfQ = numpy.hstack((YfQ,Yr))
383 for quantile in self._parameters["Quantiles"]:
384 if not (0. <= float(quantile) <= 1.): continue
385 indice = int(nech * float(quantile) - 1./nech)
386 if YQ is None: YQ = YfQ[:,indice]
387 else: YQ = numpy.hstack((YQ,YfQ[:,indice]))
388 self.StoredVariables["SimulationQuantiles"].store( YQ )
389 if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
390 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
391 if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
392 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
397 # ==============================================================================
398 if __name__ == "__main__":
399 print('\n AUTODIAGNOSTIC \n')