1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2021 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, "4DVAR")
31 self.defineRequiredParameter(
32 name = "ConstrainedBy",
33 default = "EstimateProjection",
35 message = "Prise en compte des contraintes",
36 listval = ["EstimateProjection"],
38 self.defineRequiredParameter(
39 name = "EstimationOf",
42 message = "Estimation d'etat ou de parametres",
43 listval = ["State", "Parameters"],
45 self.defineRequiredParameter(
49 message = "Minimiseur utilisé",
50 listval = ["LBFGSB","TNC", "CG", "NCG", "BFGS"],
52 self.defineRequiredParameter(
53 name = "MaximumNumberOfSteps",
56 message = "Nombre maximal de pas d'optimisation",
59 self.defineRequiredParameter(
60 name = "CostDecrementTolerance",
63 message = "Diminution relative minimale du coût lors de l'arrêt",
66 self.defineRequiredParameter(
67 name = "ProjectedGradientTolerance",
70 message = "Maximum des composantes du gradient projeté lors de l'arrêt",
73 self.defineRequiredParameter(
74 name = "GradientNormTolerance",
77 message = "Maximum des composantes du gradient lors de l'arrêt",
80 self.defineRequiredParameter(
81 name = "StoreInternalVariables",
84 message = "Stockage des variables internes ou intermédiaires du calcul",
86 self.defineRequiredParameter(
87 name = "StoreSupplementaryCalculations",
90 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
95 "CostFunctionJAtCurrentOptimum",
97 "CostFunctionJbAtCurrentOptimum",
99 "CostFunctionJoAtCurrentOptimum",
100 "CurrentIterationNumber",
106 self.defineRequiredParameter( # Pas de type
108 message = "Liste des valeurs de bornes",
110 self.requireInputArguments(
111 mandatory= ("Xb", "Y", "HO", "EM", "R", "B" ),
112 optional = ("U", "CM"),
114 self.setAttributes(tags=(
121 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
122 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
124 # Correction pour pallier a un bug de TNC sur le retour du Minimum
125 if "Minimizer" in self._parameters and self._parameters["Minimizer"] == "TNC":
126 self.setParameterValue("StoreInternalVariables",True)
130 Hm = HO["Direct"].appliedControledFormTo
132 Mm = EM["Direct"].appliedControledFormTo
134 if CM is not None and "Tangent" in CM and U is not None:
135 Cm = CM["Tangent"].asMatrix(Xb)
141 if hasattr(U,"store") and 1<=_step<len(U) :
142 _Un = numpy.asmatrix(numpy.ravel( U[_step] )).T
143 elif hasattr(U,"store") and len(U)==1:
144 _Un = numpy.asmatrix(numpy.ravel( U[0] )).T
146 _Un = numpy.asmatrix(numpy.ravel( U )).T
151 if Cm is not None and _un is not None: # Attention : si Cm est aussi dans M, doublon !
152 _Cm = Cm.reshape(_xn.size,_un.size) # ADAO & check shape
158 # Remarque : les observations sont exploitées à partir du pas de temps
159 # numéro 1, et sont utilisées dans Yo comme rangées selon ces indices.
160 # Donc le pas 0 n'est pas utilisé puisque la première étape commence
161 # avec l'observation du pas 1.
163 # Nombre de pas identique au nombre de pas d'observations
164 # -------------------------------------------------------
165 if hasattr(Y,"stepnumber"):
166 duration = Y.stepnumber()
170 # Précalcul des inversions de B et R
171 # ----------------------------------
175 # Définition de la fonction-coût
176 # ------------------------------
177 self.DirectCalculation = [None,] # Le pas 0 n'est pas observé
178 self.DirectInnovation = [None,] # Le pas 0 n'est pas observé
180 _X = numpy.asmatrix(numpy.ravel( x )).T
181 if self._parameters["StoreInternalVariables"] or \
182 self._toStore("CurrentState") or \
183 self._toStore("CurrentOptimum"):
184 self.StoredVariables["CurrentState"].store( _X )
185 Jb = float( 0.5 * (_X - Xb).T * BI * (_X - Xb) )
186 self.DirectCalculation = [None,]
187 self.DirectInnovation = [None,]
190 for step in range(0,duration-1):
191 if hasattr(Y,"store"):
192 _Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
194 _Ynpu = numpy.asmatrix(numpy.ravel( Y )).T
198 if self._parameters["EstimationOf"] == "State":
199 _Xn = Mm( (_Xn, _Un) ) + CmUn(_Xn, _Un)
200 elif self._parameters["EstimationOf"] == "Parameters":
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)
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)
214 self.DirectCalculation.append( _Xn )
215 self.DirectInnovation.append( _YmHMX )
217 # Ajout dans la fonctionnelle d'observation
218 Jo = Jo + 0.5 * float( _YmHMX.T * RI * _YmHMX )
221 self.StoredVariables["CurrentIterationNumber"].store( len(self.StoredVariables["CostFunctionJ"]) )
222 self.StoredVariables["CostFunctionJb"].store( Jb )
223 self.StoredVariables["CostFunctionJo"].store( Jo )
224 self.StoredVariables["CostFunctionJ" ].store( J )
225 if self._toStore("IndexOfOptimum") or \
226 self._toStore("CurrentOptimum") or \
227 self._toStore("CostFunctionJAtCurrentOptimum") or \
228 self._toStore("CostFunctionJbAtCurrentOptimum") or \
229 self._toStore("CostFunctionJoAtCurrentOptimum"):
230 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
231 if self._toStore("IndexOfOptimum"):
232 self.StoredVariables["IndexOfOptimum"].store( IndexMin )
233 if self._toStore("CurrentOptimum"):
234 self.StoredVariables["CurrentOptimum"].store( self.StoredVariables["CurrentState"][IndexMin] )
235 if self._toStore("CostFunctionJAtCurrentOptimum"):
236 self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
237 if self._toStore("CostFunctionJbAtCurrentOptimum"):
238 self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
239 if self._toStore("CostFunctionJoAtCurrentOptimum"):
240 self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
243 def GradientOfCostFunction(x):
244 _X = numpy.asmatrix(numpy.ravel( x )).T
245 GradJb = BI * (_X - Xb)
247 for step in range(duration-1,0,-1):
248 # Etape de récupération du dernier stockage de l'évolution
249 _Xn = self.DirectCalculation.pop()
250 # Etape de récupération du dernier stockage de l'innovation
251 _YmHMX = self.DirectInnovation.pop()
252 # Calcul des adjoints
253 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
254 Ha = Ha.reshape(_Xn.size,_YmHMX.size) # ADAO & check shape
255 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = _Xn)
256 Ma = Ma.reshape(_Xn.size,_Xn.size) # ADAO & check shape
257 # Calcul du gradient par etat adjoint
258 GradJo = GradJo + Ha * RI * _YmHMX # Equivaut pour Ha lineaire à : Ha( (_Xn, RI * _YmHMX) )
259 GradJo = Ma * GradJo # Equivaut pour Ma lineaire à : Ma( (_Xn, GradJo) )
260 GradJ = numpy.ravel( GradJb ) - numpy.ravel( GradJo )
263 # Point de démarrage de l'optimisation : Xini = Xb
264 # ------------------------------------
265 if isinstance(Xb, type(numpy.matrix([]))):
266 Xini = Xb.A1.tolist()
270 # Minimisation de la fonctionnelle
271 # --------------------------------
272 nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
274 if self._parameters["Minimizer"] == "LBFGSB":
275 # Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
276 if "0.19" <= scipy.version.version <= "1.1.0":
277 import lbfgsbhlt as optimiseur
279 import scipy.optimize as optimiseur
280 Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
283 fprime = GradientOfCostFunction,
285 bounds = self._parameters["Bounds"],
286 maxfun = self._parameters["MaximumNumberOfSteps"]-1,
287 factr = self._parameters["CostDecrementTolerance"]*1.e14,
288 pgtol = self._parameters["ProjectedGradientTolerance"],
289 iprint = self._parameters["optiprint"],
291 nfeval = Informations['funcalls']
292 rc = Informations['warnflag']
293 elif self._parameters["Minimizer"] == "TNC":
294 Minimum, nfeval, rc = scipy.optimize.fmin_tnc(
297 fprime = GradientOfCostFunction,
299 bounds = self._parameters["Bounds"],
300 maxfun = self._parameters["MaximumNumberOfSteps"],
301 pgtol = self._parameters["ProjectedGradientTolerance"],
302 ftol = self._parameters["CostDecrementTolerance"],
303 messages = self._parameters["optmessages"],
305 elif self._parameters["Minimizer"] == "CG":
306 Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
309 fprime = GradientOfCostFunction,
311 maxiter = self._parameters["MaximumNumberOfSteps"],
312 gtol = self._parameters["GradientNormTolerance"],
313 disp = self._parameters["optdisp"],
316 elif self._parameters["Minimizer"] == "NCG":
317 Minimum, fopt, nfeval, grad_calls, hcalls, rc = scipy.optimize.fmin_ncg(
320 fprime = GradientOfCostFunction,
322 maxiter = self._parameters["MaximumNumberOfSteps"],
323 avextol = self._parameters["CostDecrementTolerance"],
324 disp = self._parameters["optdisp"],
327 elif self._parameters["Minimizer"] == "BFGS":
328 Minimum, fopt, gopt, Hopt, nfeval, grad_calls, rc = scipy.optimize.fmin_bfgs(
331 fprime = GradientOfCostFunction,
333 maxiter = self._parameters["MaximumNumberOfSteps"],
334 gtol = self._parameters["GradientNormTolerance"],
335 disp = self._parameters["optdisp"],
339 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
341 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
342 MinJ = self.StoredVariables["CostFunctionJ"][IndexMin]
344 # Correction pour pallier a un bug de TNC sur le retour du Minimum
345 # ----------------------------------------------------------------
346 if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
347 Minimum = self.StoredVariables["CurrentState"][IndexMin]
349 # Obtention de l'analyse
350 # ----------------------
351 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
353 self.StoredVariables["Analysis"].store( Xa.A1 )
355 # Calculs et/ou stockages supplémentaires
356 # ---------------------------------------
357 if self._toStore("BMA"):
358 self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
363 # ==============================================================================
364 if __name__ == "__main__":
365 print('\n AUTODIAGNOSTIC\n')