Salome HOME
Update and documentation of InputValuesTest
[modules/adao.git] / src / daComposant / daAlgorithms / 3DVAR.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2020 EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import logging
24 from daCore import BasicObjects
25 import numpy, scipy.optimize, scipy.version
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "3DVAR")
31         self.defineRequiredParameter(
32             name     = "Minimizer",
33             default  = "LBFGSB",
34             typecast = str,
35             message  = "Minimiseur utilisé",
36             listval  = ["LBFGSB","TNC", "CG", "NCG", "BFGS"],
37             )
38         self.defineRequiredParameter(
39             name     = "MaximumNumberOfSteps",
40             default  = 15000,
41             typecast = int,
42             message  = "Nombre maximal de pas d'optimisation",
43             minval   = -1,
44             )
45         self.defineRequiredParameter(
46             name     = "CostDecrementTolerance",
47             default  = 1.e-7,
48             typecast = float,
49             message  = "Diminution relative minimale du coût lors de l'arrêt",
50             minval   = 0.,
51             )
52         self.defineRequiredParameter(
53             name     = "ProjectedGradientTolerance",
54             default  = -1,
55             typecast = float,
56             message  = "Maximum des composantes du gradient projeté lors de l'arrêt",
57             minval   = -1,
58             )
59         self.defineRequiredParameter(
60             name     = "GradientNormTolerance",
61             default  = 1.e-05,
62             typecast = float,
63             message  = "Maximum des composantes du gradient lors de l'arrêt",
64             minval   = 0.,
65             )
66         self.defineRequiredParameter(
67             name     = "StoreInternalVariables",
68             default  = False,
69             typecast = bool,
70             message  = "Stockage des variables internes ou intermédiaires du calcul",
71             )
72         self.defineRequiredParameter(
73             name     = "StoreSupplementaryCalculations",
74             default  = [],
75             typecast = tuple,
76             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
77             listval  = [
78                 "Analysis",
79                 "APosterioriCorrelations",
80                 "APosterioriCovariance",
81                 "APosterioriStandardDeviations",
82                 "APosterioriVariances",
83                 "BMA",
84                 "CostFunctionJ",
85                 "CostFunctionJAtCurrentOptimum",
86                 "CostFunctionJb",
87                 "CostFunctionJbAtCurrentOptimum",
88                 "CostFunctionJo",
89                 "CostFunctionJoAtCurrentOptimum",
90                 "CurrentOptimum",
91                 "CurrentState",
92                 "IndexOfOptimum",
93                 "Innovation",
94                 "InnovationAtCurrentState",
95                 "JacobianMatrixAtBackground",
96                 "JacobianMatrixAtOptimum",
97                 "KalmanGainAtOptimum",
98                 "MahalanobisConsistency",
99                 "OMA",
100                 "OMB",
101                 "SigmaObs2",
102                 "SimulatedObservationAtBackground",
103                 "SimulatedObservationAtCurrentOptimum",
104                 "SimulatedObservationAtCurrentState",
105                 "SimulatedObservationAtOptimum",
106                 "SimulationQuantiles",
107                 ]
108             )
109         self.defineRequiredParameter(
110             name     = "Quantiles",
111             default  = [],
112             typecast = tuple,
113             message  = "Liste des valeurs de quantiles",
114             minval   = 0.,
115             maxval   = 1.,
116             )
117         self.defineRequiredParameter(
118             name     = "SetSeed",
119             typecast = numpy.random.seed,
120             message  = "Graine fixée pour le générateur aléatoire",
121             )
122         self.defineRequiredParameter(
123             name     = "NumberOfSamplesForQuantiles",
124             default  = 100,
125             typecast = int,
126             message  = "Nombre d'échantillons simulés pour le calcul des quantiles",
127             minval   = 1,
128             )
129         self.defineRequiredParameter(
130             name     = "SimulationForQuantiles",
131             default  = "Linear",
132             typecast = str,
133             message  = "Type de simulation pour l'estimation des quantiles",
134             listval  = ["Linear", "NonLinear"]
135             )
136         self.defineRequiredParameter( # Pas de type
137             name     = "Bounds",
138             message  = "Liste des valeurs de bornes",
139             )
140         self.requireInputArguments(
141             mandatory= ("Xb", "Y", "HO", "R", "B" ),
142             )
143
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)
146         #
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)
150         #
151         # Opérateurs
152         # ----------
153         Hm = HO["Direct"].appliedTo
154         Ha = HO["Adjoint"].appliedInXTo
155         #
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"] )
160         else:
161             HXb = Hm( Xb )
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))
167         #
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 )
172         #
173         # Précalcul des inversions de B et R
174         # ----------------------------------
175         BI = B.getI()
176         RI = R.getI()
177         #
178         # Définition de la fonction-coût
179         # ------------------------------
180         def CostFunction(x):
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 )
186             _HX = Hm( _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 )
194             #
195             Jb  = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
196             Jo  = float( 0.5 * _Innovation.T * RI * _Innovation )
197             J   = Jb + Jo
198             #
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] )
221             return J
222         #
223         def GradientOfCostFunction(x):
224             _X      = numpy.asmatrix(numpy.ravel( x )).T
225             _HX     = Hm( _X )
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
230             return GradJ.A1
231         #
232         # Point de démarrage de l'optimisation : Xini = Xb
233         # ------------------------------------
234         Xini = numpy.ravel(Xb)
235         #
236         # Minimisation de la fonctionnelle
237         # --------------------------------
238         nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
239         #
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
244             else:
245                 import scipy.optimize as optimiseur
246             Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
247                 func        = CostFunction,
248                 x0          = Xini,
249                 fprime      = GradientOfCostFunction,
250                 args        = (),
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"],
256                 )
257             nfeval = Informations['funcalls']
258             rc     = Informations['warnflag']
259         elif self._parameters["Minimizer"] == "TNC":
260             Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
261                 func        = CostFunction,
262                 x0          = Xini,
263                 fprime      = GradientOfCostFunction,
264                 args        = (),
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"],
270                 )
271         elif self._parameters["Minimizer"] == "CG":
272             Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
273                 f           = CostFunction,
274                 x0          = Xini,
275                 fprime      = GradientOfCostFunction,
276                 args        = (),
277                 maxiter     = self._parameters["MaximumNumberOfSteps"],
278                 gtol        = self._parameters["GradientNormTolerance"],
279                 disp        = self._parameters["optdisp"],
280                 full_output = True,
281                 )
282         elif self._parameters["Minimizer"] == "NCG":
283             Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
284                 f           = CostFunction,
285                 x0          = Xini,
286                 fprime      = GradientOfCostFunction,
287                 args        = (),
288                 maxiter     = self._parameters["MaximumNumberOfSteps"],
289                 avextol     = self._parameters["CostDecrementTolerance"],
290                 disp        = self._parameters["optdisp"],
291                 full_output = True,
292                 )
293         elif self._parameters["Minimizer"] == "BFGS":
294             Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
295                 f           = CostFunction,
296                 x0          = Xini,
297                 fprime      = GradientOfCostFunction,
298                 args        = (),
299                 maxiter     = self._parameters["MaximumNumberOfSteps"],
300                 gtol        = self._parameters["GradientNormTolerance"],
301                 disp        = self._parameters["optdisp"],
302                 full_output = True,
303                 )
304         else:
305             raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
306         #
307         IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
308         MinJ     = self.StoredVariables["CostFunctionJ"][IndexMin]
309         #
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]
314         #
315         # Obtention de l'analyse
316         # ----------------------
317         Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
318         #
319         self.StoredVariables["Analysis"].store( Xa.A1 )
320         #
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]
329             else:
330                 HXa = Hm( Xa )
331         #
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"):
347             HessienneI = []
348             nb = Xa.size
349             for i in range(nb):
350                 _ee    = numpy.matrix(numpy.zeros(nb)).T
351                 _ee[i] = 1.
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 )
356             A = HessienneI.I
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
362                 try:
363                     L = numpy.linalg.cholesky( A )
364                 except:
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 )
374         #
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"):
381             d  = Y - HXb
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
398             YfQ  = None
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
403                     Yr = HXa + dYr
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
407                 if YfQ is None:
408                     YfQ = Yr
409                 else:
410                     YfQ = numpy.hstack((YfQ,Yr))
411             YfQ.sort(axis=-1)
412             YQ = None
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) )
423         #
424         self._post_run(HO)
425         return 0
426
427 # ==============================================================================
428 if __name__ == "__main__":
429     print('\n AUTODIAGNOSTIC\n')