Salome HOME
Improvement of algorithms arguments validation and tests
[modules/adao.git] / src / daComposant / daAlgorithms / 4DVAR.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, "4DVAR")
31         self.defineRequiredParameter(
32             name     = "ConstrainedBy",
33             default  = "EstimateProjection",
34             typecast = str,
35             message  = "Prise en compte des contraintes",
36             listval  = ["EstimateProjection"],
37             )
38         self.defineRequiredParameter(
39             name     = "EstimationOf",
40             default  = "State",
41             typecast = str,
42             message  = "Estimation d'etat ou de parametres",
43             listval  = ["State", "Parameters"],
44             )
45         self.defineRequiredParameter(
46             name     = "Minimizer",
47             default  = "LBFGSB",
48             typecast = str,
49             message  = "Minimiseur utilisé",
50             listval  = ["LBFGSB","TNC", "CG", "NCG", "BFGS"],
51             )
52         self.defineRequiredParameter(
53             name     = "MaximumNumberOfSteps",
54             default  = 15000,
55             typecast = int,
56             message  = "Nombre maximal de pas d'optimisation",
57             minval   = -1,
58             )
59         self.defineRequiredParameter(
60             name     = "CostDecrementTolerance",
61             default  = 1.e-7,
62             typecast = float,
63             message  = "Diminution relative minimale du coût lors de l'arrêt",
64             minval   = 0.,
65             )
66         self.defineRequiredParameter(
67             name     = "ProjectedGradientTolerance",
68             default  = -1,
69             typecast = float,
70             message  = "Maximum des composantes du gradient projeté lors de l'arrêt",
71             minval   = -1,
72             )
73         self.defineRequiredParameter(
74             name     = "GradientNormTolerance",
75             default  = 1.e-05,
76             typecast = float,
77             message  = "Maximum des composantes du gradient lors de l'arrêt",
78             minval   = 0.,
79             )
80         self.defineRequiredParameter(
81             name     = "StoreInternalVariables",
82             default  = False,
83             typecast = bool,
84             message  = "Stockage des variables internes ou intermédiaires du calcul",
85             )
86         self.defineRequiredParameter(
87             name     = "StoreSupplementaryCalculations",
88             default  = [],
89             typecast = tuple,
90             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
91             listval  = [
92                 "Analysis",
93                 "BMA",
94                 "CostFunctionJ",
95                 "CostFunctionJAtCurrentOptimum",
96                 "CostFunctionJb",
97                 "CostFunctionJbAtCurrentOptimum",
98                 "CostFunctionJo",
99                 "CostFunctionJoAtCurrentOptimum",
100                 "CurrentOptimum",
101                 "CurrentState",
102                 "IndexOfOptimum",
103                 ]
104             )
105         self.defineRequiredParameter( # Pas de type
106             name     = "Bounds",
107             message  = "Liste des valeurs de bornes",
108             )
109         self.requireInputArguments(
110             mandatory= ("Xb", "Y", "HO", "EM", "R", "B" ),
111             optional = ("U", "CM"),
112             )
113         self.setAttributes(tags=(
114             "DataAssimilation",
115             "NonLinear",
116             "Variational",
117             "Dynamic",
118             ))
119
120     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
121         self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
122         #
123         # Correction pour pallier a un bug de TNC sur le retour du Minimum
124         if "Minimizer" in self._parameters and self._parameters["Minimizer"] == "TNC":
125             self.setParameterValue("StoreInternalVariables",True)
126         #
127         # Opérateurs
128         # ----------
129         Hm = HO["Direct"].appliedControledFormTo
130         #
131         Mm = EM["Direct"].appliedControledFormTo
132         #
133         if CM is not None and "Tangent" in CM and U is not None:
134             Cm = CM["Tangent"].asMatrix(Xb)
135         else:
136             Cm = None
137         #
138         def Un(_step):
139             if U is not None:
140                 if hasattr(U,"store") and 1<=_step<len(U) :
141                     _Un = numpy.asmatrix(numpy.ravel( U[_step] )).T
142                 elif hasattr(U,"store") and len(U)==1:
143                     _Un = numpy.asmatrix(numpy.ravel( U[0] )).T
144                 else:
145                     _Un = numpy.asmatrix(numpy.ravel( U )).T
146             else:
147                 _Un = None
148             return _Un
149         def CmUn(_xn,_un):
150             if Cm is not None and _un is not None: # Attention : si Cm est aussi dans M, doublon !
151                 _Cm   = Cm.reshape(_xn.size,_un.size) # ADAO & check shape
152                 _CmUn = _Cm * _un
153             else:
154                 _CmUn = 0.
155             return _CmUn
156         #
157         # Remarque : les observations sont exploitées à partir du pas de temps
158         # numéro 1, et sont utilisées dans Yo comme rangées selon ces indices.
159         # Donc le pas 0 n'est pas utilisé puisque la première étape commence
160         # avec l'observation du pas 1.
161         #
162         # Nombre de pas identique au nombre de pas d'observations
163         # -------------------------------------------------------
164         if hasattr(Y,"stepnumber"):
165             duration = Y.stepnumber()
166         else:
167             duration = 2
168         #
169         # Précalcul des inversions de B et R
170         # ----------------------------------
171         BI = B.getI()
172         RI = R.getI()
173         #
174         # Définition de la fonction-coût
175         # ------------------------------
176         self.DirectCalculation = [None,] # Le pas 0 n'est pas observé
177         self.DirectInnovation  = [None,] # Le pas 0 n'est pas observé
178         def CostFunction(x):
179             _X  = numpy.asmatrix(numpy.ravel( x )).T
180             if self._parameters["StoreInternalVariables"] or \
181                 self._toStore("CurrentState") or \
182                 self._toStore("CurrentOptimum"):
183                 self.StoredVariables["CurrentState"].store( _X )
184             Jb  = 0.5 * (_X - Xb).T * BI * (_X - Xb)
185             self.DirectCalculation = [None,]
186             self.DirectInnovation  = [None,]
187             Jo  = 0.
188             _Xn = _X
189             for step in range(0,duration-1):
190                 self.DirectCalculation.append( _Xn )
191                 if hasattr(Y,"store"):
192                     _Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
193                 else:
194                     _Ynpu = numpy.asmatrix(numpy.ravel( Y )).T
195                 _Un = Un(step)
196                 #
197                 # Etape d'évolution
198                 if self._parameters["EstimationOf"] == "State":
199                     _Xn = Mm( (_Xn, _Un) ) + CmUn(_Xn, _Un)
200                 elif self._parameters["EstimationOf"] == "Parameters":
201                     pass
202                 #
203                 if self._parameters["Bounds"] is not None and self._parameters["ConstrainedBy"] == "EstimateProjection":
204                     _Xn = numpy.max(numpy.hstack((_Xn,numpy.asmatrix(self._parameters["Bounds"])[:,0])),axis=1)
205                     _Xn = numpy.min(numpy.hstack((_Xn,numpy.asmatrix(self._parameters["Bounds"])[:,1])),axis=1)
206                 #
207                 # Etape de différence aux observations
208                 if self._parameters["EstimationOf"] == "State":
209                     _YmHMX = _Ynpu - numpy.asmatrix(numpy.ravel( Hm( (_Xn, None) ) )).T
210                 elif self._parameters["EstimationOf"] == "Parameters":
211                     _YmHMX = _Ynpu - numpy.asmatrix(numpy.ravel( Hm( (_Xn, _Un) ) )).T - CmUn(_Xn, _Un)
212                 self.DirectInnovation.append( _YmHMX )
213                 # Ajout dans la fonctionnelle d'observation
214                 Jo = Jo + _YmHMX.T * RI * _YmHMX
215             Jo  = 0.5 * Jo
216             J   = float( Jb ) + float( Jo )
217             self.StoredVariables["CostFunctionJb"].store( Jb )
218             self.StoredVariables["CostFunctionJo"].store( Jo )
219             self.StoredVariables["CostFunctionJ" ].store( J )
220             if self._toStore("IndexOfOptimum") or \
221                 self._toStore("CurrentOptimum") or \
222                 self._toStore("CostFunctionJAtCurrentOptimum") or \
223                 self._toStore("CostFunctionJbAtCurrentOptimum") or \
224                 self._toStore("CostFunctionJoAtCurrentOptimum"):
225                 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
226             if self._toStore("IndexOfOptimum"):
227                 self.StoredVariables["IndexOfOptimum"].store( IndexMin )
228             if self._toStore("CurrentOptimum"):
229                 self.StoredVariables["CurrentOptimum"].store( self.StoredVariables["CurrentState"][IndexMin] )
230             if self._toStore("CostFunctionJAtCurrentOptimum"):
231                 self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
232             if self._toStore("CostFunctionJbAtCurrentOptimum"):
233                 self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
234             if self._toStore("CostFunctionJoAtCurrentOptimum"):
235                 self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
236             return J
237         #
238         def GradientOfCostFunction(x):
239             _X      = numpy.asmatrix(numpy.ravel( x )).T
240             GradJb  = BI * (_X - Xb)
241             GradJo  = 0.
242             for step in range(duration-1,0,-1):
243                 # Etape de récupération du dernier stockage de l'évolution
244                 _Xn = self.DirectCalculation.pop()
245                 # Etape de récupération du dernier stockage de l'innovation
246                 _YmHMX = self.DirectInnovation.pop()
247                 # Calcul des adjoints
248                 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
249                 Ha = Ha.reshape(_Xn.size,_YmHMX.size) # ADAO & check shape
250                 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
251                 Ma = Ma.reshape(_Xn.size,_Xn.size) # ADAO & check shape
252                 # Calcul du gradient par etat adjoint
253                 GradJo = GradJo + Ha * RI * _YmHMX # Equivaut pour Ha lineaire à : Ha( (_Xn, RI * _YmHMX) )
254                 GradJo = Ma * GradJo               # Equivaut pour Ma lineaire à : Ma( (_Xn, GradJo) )
255             GradJ   = numpy.asmatrix( numpy.ravel( GradJb ) - numpy.ravel( GradJo ) ).T
256             return GradJ.A1
257         #
258         # Point de démarrage de l'optimisation : Xini = Xb
259         # ------------------------------------
260         if isinstance(Xb, type(numpy.matrix([]))):
261             Xini = Xb.A1.tolist()
262         else:
263             Xini = list(Xb)
264         #
265         # Minimisation de la fonctionnelle
266         # --------------------------------
267         nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
268         #
269         if self._parameters["Minimizer"] == "LBFGSB":
270             # Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
271             if "0.19" <= scipy.version.version <= "1.1.0":
272                 import lbfgsbhlt as optimiseur
273             else:
274                 import scipy.optimize as optimiseur
275             Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
276                 func        = CostFunction,
277                 x0          = Xini,
278                 fprime      = GradientOfCostFunction,
279                 args        = (),
280                 bounds      = self._parameters["Bounds"],
281                 maxfun      = self._parameters["MaximumNumberOfSteps"]-1,
282                 factr       = self._parameters["CostDecrementTolerance"]*1.e14,
283                 pgtol       = self._parameters["ProjectedGradientTolerance"],
284                 iprint      = self._parameters["optiprint"],
285                 )
286             nfeval = Informations['funcalls']
287             rc     = Informations['warnflag']
288         elif self._parameters["Minimizer"] == "TNC":
289             Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
290                 func        = CostFunction,
291                 x0          = Xini,
292                 fprime      = GradientOfCostFunction,
293                 args        = (),
294                 bounds      = self._parameters["Bounds"],
295                 maxfun      = self._parameters["MaximumNumberOfSteps"],
296                 pgtol       = self._parameters["ProjectedGradientTolerance"],
297                 ftol        = self._parameters["CostDecrementTolerance"],
298                 messages    = self._parameters["optmessages"],
299                 )
300         elif self._parameters["Minimizer"] == "CG":
301             Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
302                 f           = CostFunction,
303                 x0          = Xini,
304                 fprime      = GradientOfCostFunction,
305                 args        = (),
306                 maxiter     = self._parameters["MaximumNumberOfSteps"],
307                 gtol        = self._parameters["GradientNormTolerance"],
308                 disp        = self._parameters["optdisp"],
309                 full_output = True,
310                 )
311         elif self._parameters["Minimizer"] == "NCG":
312             Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
313                 f           = CostFunction,
314                 x0          = Xini,
315                 fprime      = GradientOfCostFunction,
316                 args        = (),
317                 maxiter     = self._parameters["MaximumNumberOfSteps"],
318                 avextol     = self._parameters["CostDecrementTolerance"],
319                 disp        = self._parameters["optdisp"],
320                 full_output = True,
321                 )
322         elif self._parameters["Minimizer"] == "BFGS":
323             Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
324                 f           = CostFunction,
325                 x0          = Xini,
326                 fprime      = GradientOfCostFunction,
327                 args        = (),
328                 maxiter     = self._parameters["MaximumNumberOfSteps"],
329                 gtol        = self._parameters["GradientNormTolerance"],
330                 disp        = self._parameters["optdisp"],
331                 full_output = True,
332                 )
333         else:
334             raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
335         #
336         IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
337         MinJ     = self.StoredVariables["CostFunctionJ"][IndexMin]
338         #
339         # Correction pour pallier a un bug de TNC sur le retour du Minimum
340         # ----------------------------------------------------------------
341         if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
342             Minimum = self.StoredVariables["CurrentState"][IndexMin]
343         #
344         # Obtention de l'analyse
345         # ----------------------
346         Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
347         #
348         self.StoredVariables["Analysis"].store( Xa.A1 )
349         #
350         # Calculs et/ou stockages supplémentaires
351         # ---------------------------------------
352         if self._toStore("BMA"):
353             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
354         #
355         self._post_run(HO)
356         return 0
357
358 # ==============================================================================
359 if __name__ == "__main__":
360     print('\n AUTODIAGNOSTIC\n')