1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2019 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, scipy.version
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 coût lors de l'arrêt",
52 self.defineRequiredParameter(
53 name = "ProjectedGradientTolerance",
56 message = "Maximum des composantes du gradient projeté lors de l'arrêt",
59 self.defineRequiredParameter(
60 name = "GradientNormTolerance",
63 message = "Maximum des composantes du gradient lors de l'arrêt",
66 self.defineRequiredParameter(
67 name = "StoreInternalVariables",
70 message = "Stockage des variables internes ou intermédiaires du calcul",
72 self.defineRequiredParameter(
73 name = "StoreSupplementaryCalculations",
76 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
79 "APosterioriCorrelations",
80 "APosterioriCovariance",
81 "APosterioriStandardDeviations",
82 "APosterioriVariances",
85 "CostFunctionJAtCurrentOptimum",
87 "CostFunctionJbAtCurrentOptimum",
89 "CostFunctionJoAtCurrentOptimum",
94 "InnovationAtCurrentState",
95 "JacobianMatrixAtBackground",
96 "JacobianMatrixAtOptimum",
97 "KalmanGainAtOptimum",
98 "MahalanobisConsistency",
102 "SimulatedObservationAtBackground",
103 "SimulatedObservationAtCurrentOptimum",
104 "SimulatedObservationAtCurrentState",
105 "SimulatedObservationAtOptimum",
106 "SimulationQuantiles",
109 self.defineRequiredParameter(
113 message = "Liste des valeurs de quantiles",
117 self.defineRequiredParameter(
119 typecast = numpy.random.seed,
120 message = "Graine fixée pour le générateur aléatoire",
122 self.defineRequiredParameter(
123 name = "NumberOfSamplesForQuantiles",
126 message = "Nombre d'échantillons simulés pour le calcul des quantiles",
129 self.defineRequiredParameter(
130 name = "SimulationForQuantiles",
133 message = "Type de simulation pour l'estimation des quantiles",
134 listval = ["Linear", "NonLinear"]
136 self.defineRequiredParameter( # Pas de type
138 message = "Liste des valeurs de bornes",
140 self.requireInputArguments(
141 mandatory= ("Xb", "Y", "HO", "R", "B" ),
144 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
145 self._pre_run(Parameters, Xb, Y, R, B, Q)
147 # Correction pour pallier a un bug de TNC sur le retour du Minimum
148 if "Minimizer" in self._parameters and self._parameters["Minimizer"] == "TNC":
149 self.setParameterValue("StoreInternalVariables",True)
153 Hm = HO["Direct"].appliedTo
154 Ha = HO["Adjoint"].appliedInXTo
156 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
157 # ----------------------------------------------------
158 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
159 HXb = Hm( Xb, HO["AppliedInX"]["HXb"] )
162 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
163 if Y.size != HXb.size:
164 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))
165 if max(Y.shape) != max(HXb.shape):
166 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))
168 if self._toStore("JacobianMatrixAtBackground"):
169 HtMb = HO["Tangent"].asMatrix(ValueForMethodForm = Xb)
170 HtMb = HtMb.reshape(Y.size,Xb.size) # ADAO & check shape
171 self.StoredVariables["JacobianMatrixAtBackground"].store( HtMb )
173 # Précalcul des inversions de B et R
174 # ----------------------------------
178 # Définition de la fonction-coût
179 # ------------------------------
181 _X = numpy.asmatrix(numpy.ravel( x )).T
182 if self._parameters["StoreInternalVariables"] or \
183 self._toStore("CurrentState") or \
184 self._toStore("CurrentOptimum"):
185 self.StoredVariables["CurrentState"].store( _X )
187 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
188 _Innovation = Y - _HX
189 if self._toStore("SimulatedObservationAtCurrentState") or \
190 self._toStore("SimulatedObservationAtCurrentOptimum"):
191 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
192 if self._toStore("InnovationAtCurrentState"):
193 self.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
195 Jb = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
196 Jo = float( 0.5 * _Innovation.T * RI * _Innovation )
199 self.StoredVariables["CostFunctionJb"].store( Jb )
200 self.StoredVariables["CostFunctionJo"].store( Jo )
201 self.StoredVariables["CostFunctionJ" ].store( J )
202 if self._toStore("IndexOfOptimum") or \
203 self._toStore("CurrentOptimum") or \
204 self._toStore("CostFunctionJAtCurrentOptimum") or \
205 self._toStore("CostFunctionJbAtCurrentOptimum") or \
206 self._toStore("CostFunctionJoAtCurrentOptimum") or \
207 self._toStore("SimulatedObservationAtCurrentOptimum"):
208 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
209 if self._toStore("IndexOfOptimum"):
210 self.StoredVariables["IndexOfOptimum"].store( IndexMin )
211 if self._toStore("CurrentOptimum"):
212 self.StoredVariables["CurrentOptimum"].store( self.StoredVariables["CurrentState"][IndexMin] )
213 if self._toStore("SimulatedObservationAtCurrentOptimum"):
214 self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
215 if self._toStore("CostFunctionJbAtCurrentOptimum"):
216 self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
217 if self._toStore("CostFunctionJoAtCurrentOptimum"):
218 self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
219 if self._toStore("CostFunctionJAtCurrentOptimum"):
220 self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
223 def GradientOfCostFunction(x):
224 _X = numpy.asmatrix(numpy.ravel( x )).T
226 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
227 GradJb = BI * (_X - Xb)
228 GradJo = - Ha( (_X, RI * (Y - _HX)) )
229 GradJ = numpy.asmatrix( numpy.ravel( GradJb ) + numpy.ravel( GradJo ) ).T
232 # Point de démarrage de l'optimisation : Xini = Xb
233 # ------------------------------------
234 Xini = numpy.ravel(Xb)
236 # Minimisation de la fonctionnelle
237 # --------------------------------
238 nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
240 if self._parameters["Minimizer"] == "LBFGSB":
241 # Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
242 if "0.19" <= scipy.version.version <= "1.1.0":
243 import lbfgsbhlt as optimiseur
245 import scipy.optimize as optimiseur
246 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
249 fprime = GradientOfCostFunction,
251 bounds = self._parameters["Bounds"],
252 maxfun = self._parameters["MaximumNumberOfSteps"]-1,
253 factr = self._parameters["CostDecrementTolerance"]*1.e14,
254 pgtol = self._parameters["ProjectedGradientTolerance"],
255 iprint = self._parameters["optiprint"],
257 nfeval = Informations['funcalls']
258 rc = Informations['warnflag']
259 elif self._parameters["Minimizer"] == "TNC":
260 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
263 fprime = GradientOfCostFunction,
265 bounds = self._parameters["Bounds"],
266 maxfun = self._parameters["MaximumNumberOfSteps"],
267 pgtol = self._parameters["ProjectedGradientTolerance"],
268 ftol = self._parameters["CostDecrementTolerance"],
269 messages = self._parameters["optmessages"],
271 elif self._parameters["Minimizer"] == "CG":
272 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
275 fprime = GradientOfCostFunction,
277 maxiter = self._parameters["MaximumNumberOfSteps"],
278 gtol = self._parameters["GradientNormTolerance"],
279 disp = self._parameters["optdisp"],
282 elif self._parameters["Minimizer"] == "NCG":
283 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
286 fprime = GradientOfCostFunction,
288 maxiter = self._parameters["MaximumNumberOfSteps"],
289 avextol = self._parameters["CostDecrementTolerance"],
290 disp = self._parameters["optdisp"],
293 elif self._parameters["Minimizer"] == "BFGS":
294 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
297 fprime = GradientOfCostFunction,
299 maxiter = self._parameters["MaximumNumberOfSteps"],
300 gtol = self._parameters["GradientNormTolerance"],
301 disp = self._parameters["optdisp"],
305 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
307 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
308 MinJ = self.StoredVariables["CostFunctionJ"][IndexMin]
310 # Correction pour pallier a un bug de TNC sur le retour du Minimum
311 # ----------------------------------------------------------------
312 if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
313 Minimum = self.StoredVariables["CurrentState"][IndexMin]
315 # Obtention de l'analyse
316 # ----------------------
317 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
319 self.StoredVariables["Analysis"].store( Xa.A1 )
321 if self._toStore("OMA") or \
322 self._toStore("SigmaObs2") or \
323 self._toStore("SimulationQuantiles") or \
324 self._toStore("SimulatedObservationAtOptimum"):
325 if self._toStore("SimulatedObservationAtCurrentState"):
326 HXa = self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
327 elif self._toStore("SimulatedObservationAtCurrentOptimum"):
328 HXa = self.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
332 # Calcul de la covariance d'analyse
333 # ---------------------------------
334 if self._toStore("APosterioriCovariance") or \
335 self._toStore("SimulationQuantiles") or \
336 self._toStore("JacobianMatrixAtOptimum") or \
337 self._toStore("KalmanGainAtOptimum"):
338 HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
339 HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
340 if self._toStore("APosterioriCovariance") or \
341 self._toStore("SimulationQuantiles") or \
342 self._toStore("KalmanGainAtOptimum"):
343 HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
344 HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
345 if self._toStore("APosterioriCovariance") or \
346 self._toStore("SimulationQuantiles"):
350 _ee = numpy.matrix(numpy.zeros(nb)).T
352 _HtEE = numpy.dot(HtM,_ee)
353 _HtEE = numpy.asmatrix(numpy.ravel( _HtEE )).T
354 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
355 HessienneI = numpy.matrix( HessienneI )
357 if min(A.shape) != max(A.shape):
358 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)))
359 if (numpy.diag(A) < 0).any():
360 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,))
361 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
363 L = numpy.linalg.cholesky( A )
365 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,))
366 if self._toStore("APosterioriCovariance"):
367 self.StoredVariables["APosterioriCovariance"].store( A )
368 if self._toStore("JacobianMatrixAtOptimum"):
369 self.StoredVariables["JacobianMatrixAtOptimum"].store( HtM )
370 if self._toStore("KalmanGainAtOptimum"):
371 if (Y.size <= Xb.size): KG = B * HaM * (R + numpy.dot(HtM, B * HaM)).I
372 elif (Y.size > Xb.size): KG = (BI + numpy.dot(HaM, RI * HtM)).I * HaM * RI
373 self.StoredVariables["KalmanGainAtOptimum"].store( KG )
375 # Calculs et/ou stockages supplémentaires
376 # ---------------------------------------
377 if self._toStore("Innovation") or \
378 self._toStore("SigmaObs2") or \
379 self._toStore("MahalanobisConsistency") or \
380 self._toStore("OMB"):
382 if self._toStore("Innovation"):
383 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
384 if self._toStore("BMA"):
385 self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
386 if self._toStore("OMA"):
387 self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
388 if self._toStore("OMB"):
389 self.StoredVariables["OMB"].store( numpy.ravel(d) )
390 if self._toStore("SigmaObs2"):
391 TraceR = R.trace(Y.size)
392 self.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
393 if self._toStore("MahalanobisConsistency"):
394 self.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
395 if self._toStore("SimulationQuantiles"):
396 nech = self._parameters["NumberOfSamplesForQuantiles"]
397 HXa = numpy.matrix(numpy.ravel( HXa )).T
399 for i in range(nech):
400 if self._parameters["SimulationForQuantiles"] == "Linear":
401 dXr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A) - Xa.A1).T
402 dYr = numpy.matrix(numpy.ravel( HtM * dXr )).T
404 elif self._parameters["SimulationForQuantiles"] == "NonLinear":
405 Xr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A)).T
406 Yr = numpy.matrix(numpy.ravel( Hm( Xr ) )).T
410 YfQ = numpy.hstack((YfQ,Yr))
413 for quantile in self._parameters["Quantiles"]:
414 if not (0. <= float(quantile) <= 1.): continue
415 indice = int(nech * float(quantile) - 1./nech)
416 if YQ is None: YQ = YfQ[:,indice]
417 else: YQ = numpy.hstack((YQ,YfQ[:,indice]))
418 self.StoredVariables["SimulationQuantiles"].store( YQ )
419 if self._toStore("SimulatedObservationAtBackground"):
420 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
421 if self._toStore("SimulatedObservationAtOptimum"):
422 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
427 # ==============================================================================
428 if __name__ == "__main__":
429 print('\n AUTODIAGNOSTIC\n')