Salome HOME
Improving elementary storage facilities
[modules/adao.git] / src / daComposant / daAlgorithms / 3DVAR.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2014 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, PlatformInfo
25 m = PlatformInfo.SystemUsage()
26 import numpy, scipy.optimize
27
28 # ==============================================================================
29 class ElementaryAlgorithm(BasicObjects.Algorithm):
30     def __init__(self):
31         BasicObjects.Algorithm.__init__(self, "3DVAR")
32         self.defineRequiredParameter(
33             name     = "Minimizer",
34             default  = "LBFGSB",
35             typecast = str,
36             message  = "Minimiseur utilisé",
37             listval  = ["LBFGSB","TNC", "CG", "NCG", "BFGS"],
38             )
39         self.defineRequiredParameter(
40             name     = "MaximumNumberOfSteps",
41             default  = 15000,
42             typecast = int,
43             message  = "Nombre maximal de pas d'optimisation",
44             minval   = -1,
45             )
46         self.defineRequiredParameter(
47             name     = "CostDecrementTolerance",
48             default  = 1.e-7,
49             typecast = float,
50             message  = "Diminution relative minimale du cout lors de l'arrêt",
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             )
65         self.defineRequiredParameter(
66             name     = "StoreInternalVariables",
67             default  = False,
68             typecast = bool,
69             message  = "Stockage des variables internes ou intermédiaires du calcul",
70             )
71         self.defineRequiredParameter(
72             name     = "StoreSupplementaryCalculations",
73             default  = [],
74             typecast = tuple,
75             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
76             listval  = ["APosterioriCovariance", "BMA", "OMA", "OMB", "Innovation", "SigmaObs2", "MahalanobisConsistency", "SimulationQuantiles"]
77             )
78         self.defineRequiredParameter(
79             name     = "Quantiles",
80             default  = [],
81             typecast = tuple,
82             message  = "Liste des valeurs de quantiles",
83             )
84         self.defineRequiredParameter(
85             name     = "SetSeed",
86             typecast = numpy.random.seed,
87             message  = "Graine fixée pour le générateur aléatoire",
88             )
89         self.defineRequiredParameter(
90             name     = "NumberOfSamplesForQuantiles",
91             default  = 100,
92             typecast = int,
93             message  = "Nombre d'échantillons simulés pour le calcul des quantiles",
94             minval   = 1,
95             )
96         self.defineRequiredParameter(
97             name     = "SimulationForQuantiles",
98             default  = "Linear",
99             typecast = str,
100             message  = "Type de simulation pour l'estimation des quantiles",
101             listval  = ["Linear", "NonLinear"]
102             )
103
104     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
105         if logging.getLogger().level < logging.WARNING:
106             self.__iprint, self.__disp = 1, 1
107             self.__message = scipy.optimize.tnc.MSG_ALL
108         else:
109             self.__iprint, self.__disp = -1, 0
110             self.__message = scipy.optimize.tnc.MSG_NONE
111         #
112         logging.debug("%s Lancement"%self._name)
113         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
114         #
115         # Paramètres de pilotage
116         # ----------------------
117         self.setParameters(Parameters)
118         #
119         if self._parameters.has_key("Bounds") and (type(self._parameters["Bounds"]) is type([]) or type(self._parameters["Bounds"]) is type(())) and (len(self._parameters["Bounds"]) > 0):
120             Bounds = self._parameters["Bounds"]
121             logging.debug("%s Prise en compte des bornes effectuee"%(self._name,))
122         else:
123             Bounds = None
124         #
125         # Correction pour pallier a un bug de TNC sur le retour du Minimum
126         if self._parameters.has_key("Minimizer") == "TNC":
127             self.setParameterValue("StoreInternalVariables",True)
128         #
129         # Opérateur d'observation
130         # -----------------------
131         Hm = HO["Direct"].appliedTo
132         Ha = HO["Adjoint"].appliedInXTo
133         #
134         # Utilisation éventuelle d'un vecteur H(Xb) précalculé
135         # ----------------------------------------------------
136         if HO["AppliedToX"] is not None and HO["AppliedToX"].has_key("HXb"):
137             HXb = HO["AppliedToX"]["HXb"]
138         else:
139             HXb = Hm( Xb )
140         HXb = numpy.asmatrix(numpy.ravel( HXb )).T
141         #
142         # Calcul de l'innovation
143         # ----------------------
144         if Y.size != HXb.size:
145             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))
146         if max(Y.shape) != max(HXb.shape):
147             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))
148         d  = Y - HXb
149         #
150         # Précalcul des inversions de B et R
151         # ----------------------------------
152         BI = B.getI()
153         RI = R.getI()
154         #
155         # Définition de la fonction-coût
156         # ------------------------------
157         def CostFunction(x):
158             _X  = numpy.asmatrix(numpy.ravel( x )).T
159             _HX = Hm( _X )
160             _HX = numpy.asmatrix(numpy.ravel( _HX )).T
161             Jb  = 0.5 * (_X - Xb).T * BI * (_X - Xb)
162             Jo  = 0.5 * (Y - _HX).T * RI * (Y - _HX)
163             J   = float( Jb ) + float( Jo )
164             if self._parameters["StoreInternalVariables"]:
165                 self.StoredVariables["CurrentState"].store( _X )
166             self.StoredVariables["CostFunctionJb"].store( Jb )
167             self.StoredVariables["CostFunctionJo"].store( Jo )
168             self.StoredVariables["CostFunctionJ" ].store( J )
169             return J
170         #
171         def GradientOfCostFunction(x):
172             _X      = numpy.asmatrix(numpy.ravel( x )).T
173             _HX     = Hm( _X )
174             _HX     = numpy.asmatrix(numpy.ravel( _HX )).T
175             GradJb  = BI * (_X - Xb)
176             GradJo  = - Ha( (_X, RI * (Y - _HX)) )
177             GradJ   = numpy.asmatrix( numpy.ravel( GradJb ) + numpy.ravel( GradJo ) ).T
178             return GradJ.A1
179         #
180         # Point de démarrage de l'optimisation : Xini = Xb
181         # ------------------------------------
182         if type(Xb) is type(numpy.matrix([])):
183             Xini = Xb.A1.tolist()
184         else:
185             Xini = list(Xb)
186         #
187         # Minimisation de la fonctionnelle
188         # --------------------------------
189         nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
190         #
191         if self._parameters["Minimizer"] == "LBFGSB":
192             Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
193                 func        = CostFunction,
194                 x0          = Xini,
195                 fprime      = GradientOfCostFunction,
196                 args        = (),
197                 bounds      = Bounds,
198                 maxfun      = self._parameters["MaximumNumberOfSteps"]-1,
199                 factr       = self._parameters["CostDecrementTolerance"]*1.e14,
200                 pgtol       = self._parameters["ProjectedGradientTolerance"],
201                 iprint      = self.__iprint,
202                 )
203             nfeval = Informations['funcalls']
204             rc     = Informations['warnflag']
205         elif self._parameters["Minimizer"] == "TNC":
206             Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
207                 func        = CostFunction,
208                 x0          = Xini,
209                 fprime      = GradientOfCostFunction,
210                 args        = (),
211                 bounds      = Bounds,
212                 maxfun      = self._parameters["MaximumNumberOfSteps"],
213                 pgtol       = self._parameters["ProjectedGradientTolerance"],
214                 ftol        = self._parameters["CostDecrementTolerance"],
215                 messages    = self.__message,
216                 )
217         elif self._parameters["Minimizer"] == "CG":
218             Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
219                 f           = CostFunction,
220                 x0          = Xini,
221                 fprime      = GradientOfCostFunction,
222                 args        = (),
223                 maxiter     = self._parameters["MaximumNumberOfSteps"],
224                 gtol        = self._parameters["GradientNormTolerance"],
225                 disp        = self.__disp,
226                 full_output = True,
227                 )
228         elif self._parameters["Minimizer"] == "NCG":
229             Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
230                 f           = CostFunction,
231                 x0          = Xini,
232                 fprime      = GradientOfCostFunction,
233                 args        = (),
234                 maxiter     = self._parameters["MaximumNumberOfSteps"],
235                 avextol     = self._parameters["CostDecrementTolerance"],
236                 disp        = self.__disp,
237                 full_output = True,
238                 )
239         elif self._parameters["Minimizer"] == "BFGS":
240             Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
241                 f           = CostFunction,
242                 x0          = Xini,
243                 fprime      = GradientOfCostFunction,
244                 args        = (),
245                 maxiter     = self._parameters["MaximumNumberOfSteps"],
246                 gtol        = self._parameters["GradientNormTolerance"],
247                 disp        = self.__disp,
248                 full_output = True,
249                 )
250         else:
251             raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
252         #
253         IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
254         MinJ     = self.StoredVariables["CostFunctionJ"][IndexMin]
255         #
256         # Correction pour pallier a un bug de TNC sur le retour du Minimum
257         # ----------------------------------------------------------------
258         if self._parameters["StoreInternalVariables"]:
259             Minimum = self.StoredVariables["CurrentState"][IndexMin]
260         #
261         # Obtention de l'analyse
262         # ----------------------
263         Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
264         #
265         self.StoredVariables["Analysis"].store( Xa.A1 )
266         #
267         if  "OMA"                   in self._parameters["StoreSupplementaryCalculations"] or \
268             "SigmaObs2"             in self._parameters["StoreSupplementaryCalculations"] or \
269             "SimulationQuantiles"   in self._parameters["StoreSupplementaryCalculations"]:
270             HXa = Hm(Xa)
271         #
272         # Calcul de la covariance d'analyse
273         # ---------------------------------
274         if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"] or \
275            "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
276             HtM = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
277             HtM = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
278             HaM = HO["Adjoint"].asMatrix(ValueForMethodForm = Xa)
279             HaM = HaM.reshape(Xa.size,Y.size) # ADAO & check shape
280             HessienneI = []
281             nb = Xa.size
282             for i in range(nb):
283                 _ee    = numpy.matrix(numpy.zeros(nb)).T
284                 _ee[i] = 1.
285                 _HtEE  = numpy.dot(HtM,_ee)
286                 _HtEE  = numpy.asmatrix(numpy.ravel( _HtEE )).T
287                 HessienneI.append( numpy.ravel( BI*_ee + HaM * (RI * _HtEE) ) )
288             HessienneI = numpy.matrix( HessienneI )
289             A = HessienneI.I
290             if min(A.shape) != max(A.shape):
291                 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)))
292             if (numpy.diag(A) < 0).any():
293                 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,))
294             if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
295                 try:
296                     L = numpy.linalg.cholesky( A )
297                 except:
298                     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,))
299             self.StoredVariables["APosterioriCovariance"].store( A )
300         #
301         # Calculs et/ou stockages supplémentaires
302         # ---------------------------------------
303         if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
304             self.StoredVariables["Innovation"].store( numpy.ravel(d) )
305         if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
306             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
307         if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
308             self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
309         if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
310             self.StoredVariables["OMB"].store( numpy.ravel(d) )
311         if "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"]:
312             TraceR = R.trace(Y.size)
313             self.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(Y)).T-numpy.asmatrix(numpy.ravel(HXa)).T)) ) / TraceR )
314         if "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
315             self.StoredVariables["MahalanobisConsistency"].store( float( 2.*MinJ/d.size ) )
316         if "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
317             Qtls = self._parameters["Quantiles"]
318             nech = self._parameters["NumberOfSamplesForQuantiles"]
319             HXa  = numpy.matrix(numpy.ravel( HXa )).T
320             YfQ  = None
321             for i in range(nech):
322                 if self._parameters["SimulationForQuantiles"] == "Linear":
323                     dXr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A) - Xa.A1).T
324                     dYr = numpy.matrix(numpy.ravel( HtM * dXr )).T
325                     Yr = HXa + dYr
326                 elif self._parameters["SimulationForQuantiles"] == "NonLinear":
327                     Xr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A)).T
328                     Yr = numpy.matrix(numpy.ravel( Hm( Xr ) )).T
329                 if YfQ is None:
330                     YfQ = Yr
331                 else:
332                     YfQ = numpy.hstack((YfQ,Yr))
333             YfQ.sort(axis=-1)
334             YQ = None
335             for quantile in Qtls:
336                 if not (0. <= quantile <= 1.): continue
337                 indice = int(nech * quantile - 1./nech)
338                 if YQ is None: YQ = YfQ[:,indice]
339                 else:          YQ = numpy.hstack((YQ,YfQ[:,indice]))
340             self.StoredVariables["SimulationQuantiles"].store( YQ )
341         #
342         logging.debug("%s Nombre d'évaluation(s) de l'opérateur d'observation direct/tangent/adjoint : %i/%i/%i"%(self._name, HO["Direct"].nbcalls()[0],HO["Tangent"].nbcalls()[0],HO["Adjoint"].nbcalls()[0]))
343         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
344         logging.debug("%s Terminé"%self._name)
345         #
346         return 0
347
348 # ==============================================================================
349 if __name__ == "__main__":
350     print '\n AUTODIAGNOSTIC \n'