#-*-coding:iso-8859-1-*-
#
-# Copyright (C) 2008-2012 EDF R&D
+# Copyright (C) 2008-2013 EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
default = "LBFGSB",
typecast = str,
message = "Minimiseur utilisé",
- listval = ["LBFGSB","TNC", "CG", "NCG", "BFGS"],
+ listval = ["LBFGSB","TNC", "CG", "NCG", "BFGS", "LM"],
)
self.defineRequiredParameter(
name = "MaximumNumberOfSteps",
listval = ["BMA", "OMA", "OMB", "Innovation"]
)
- def run(self, Xb=None, Y=None, H=None, M=None, R=None, B=None, Q=None, Parameters=None):
- """
- Calcul de l'estimateur moindres carrés pondérés non linéaires
- (assimilation variationnelle sans ébauche)
- """
+ def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
logging.debug("%s Lancement"%self._name)
logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
#
#
# Opérateur d'observation
# -----------------------
- Hm = H["Direct"].appliedTo
- Ha = H["Adjoint"].appliedInXTo
+ Hm = HO["Direct"].appliedTo
+ Ha = HO["Adjoint"].appliedInXTo
#
# Utilisation éventuelle d'un vecteur H(Xb) précalculé
# ----------------------------------------------------
- if H["AppliedToX"] is not None and H["AppliedToX"].has_key("HXb"):
- logging.debug("%s Utilisation de HXb"%self._name)
- HXb = H["AppliedToX"]["HXb"]
+ if HO["AppliedToX"] is not None and HO["AppliedToX"].has_key("HXb"):
+ HXb = HO["AppliedToX"]["HXb"]
else:
- logging.debug("%s Calcul de Hm(Xb)"%self._name)
HXb = Hm( Xb )
- HXb = numpy.asmatrix(HXb).flatten().T
+ HXb = numpy.asmatrix(numpy.ravel( HXb )).T
#
# Calcul de l'innovation
# ----------------------
if max(Y.shape) != max(HXb.shape):
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))
d = Y - HXb
- logging.debug("%s Innovation d = %s"%(self._name, d))
#
# Précalcul des inversions de B et R
# ----------------------------------
- # if B is not None:
- # BI = B.I
- # elif self._parameters["B_scalar"] is not None:
- # BI = 1.0 / self._parameters["B_scalar"]
- # else:
- # raise ValueError("Background error covariance matrix has to be properly defined!")
- #
- if R is not None:
- RI = R.I
- elif self._parameters["R_scalar"] is not None:
- RI = 1.0 / self._parameters["R_scalar"]
- else:
- raise ValueError("Observation error covariance matrix has to be properly defined!")
+ RI = R.getI()
+ if self._parameters["Minimizer"] == "LM":
+ RdemiI = R.choleskyI()
#
# Définition de la fonction-coût
# ------------------------------
def CostFunction(x):
- _X = numpy.asmatrix(x).flatten().T
- logging.debug("%s CostFunction X = %s"%(self._name, numpy.asmatrix( _X ).flatten()))
+ _X = numpy.asmatrix(numpy.ravel( x )).T
_HX = Hm( _X )
- _HX = numpy.asmatrix(_HX).flatten().T
+ _HX = numpy.asmatrix(numpy.ravel( _HX )).T
Jb = 0.
Jo = 0.5 * (Y - _HX).T * RI * (Y - _HX)
J = float( Jb ) + float( Jo )
- logging.debug("%s CostFunction Jb = %s"%(self._name, Jb))
- logging.debug("%s CostFunction Jo = %s"%(self._name, Jo))
- logging.debug("%s CostFunction J = %s"%(self._name, J))
if self._parameters["StoreInternalVariables"]:
self.StoredVariables["CurrentState"].store( _X.A1 )
self.StoredVariables["CostFunctionJb"].store( Jb )
self.StoredVariables["CostFunctionJo"].store( Jo )
self.StoredVariables["CostFunctionJ" ].store( J )
- return float( J )
+ return J
#
def GradientOfCostFunction(x):
- _X = numpy.asmatrix(x).flatten().T
- logging.debug("%s GradientOfCostFunction X = %s"%(self._name, numpy.asmatrix( _X ).flatten()))
+ _X = numpy.asmatrix(numpy.ravel( x )).T
_HX = Hm( _X )
- _HX = numpy.asmatrix(_HX).flatten().T
+ _HX = numpy.asmatrix(numpy.ravel( _HX )).T
GradJb = 0.
GradJo = - Ha( (_X, RI * (Y - _HX)) )
- GradJ = numpy.asmatrix( GradJb ).flatten().T + numpy.asmatrix( GradJo ).flatten().T
- logging.debug("%s GradientOfCostFunction GradJb = %s"%(self._name, numpy.asmatrix( GradJb ).flatten()))
- logging.debug("%s GradientOfCostFunction GradJo = %s"%(self._name, numpy.asmatrix( GradJo ).flatten()))
- logging.debug("%s GradientOfCostFunction GradJ = %s"%(self._name, numpy.asmatrix( GradJ ).flatten()))
+ GradJ = numpy.asmatrix( numpy.ravel( GradJb ) + numpy.ravel( GradJo ) ).T
return GradJ.A1
#
+ def CostFunctionLM(x):
+ _X = numpy.asmatrix(numpy.ravel( x )).T
+ _HX = Hm( _X )
+ _HX = numpy.asmatrix(numpy.ravel( _HX )).T
+ Jb = 0.
+ Jo = 0.5 * (Y - _HX).T * RI * (Y - _HX)
+ J = float( Jb ) + float( Jo )
+ if self._parameters["StoreInternalVariables"]:
+ self.StoredVariables["CurrentState"].store( _X.A1 )
+ self.StoredVariables["CostFunctionJb"].store( Jb )
+ self.StoredVariables["CostFunctionJo"].store( Jo )
+ self.StoredVariables["CostFunctionJ" ].store( J )
+ #
+ return numpy.ravel( RdemiI*(Y - _HX) )
+ #
+ def GradientOfCostFunctionLM(x):
+ _X = numpy.asmatrix(numpy.ravel( x )).T
+ _HX = Hm( _X )
+ _HX = numpy.asmatrix(numpy.ravel( _HX )).T
+ GradJb = 0.
+ GradJo = - Ha( (_X, RI * (Y - _HX)) )
+ GradJ = numpy.asmatrix( numpy.ravel( GradJb ) + numpy.ravel( GradJo ) ).T
+ return - RdemiI*HO["Tangent"].asMatrix( _X )
+ #
# Point de démarrage de l'optimisation : Xini = Xb
# ------------------------------------
if type(Xb) is type(numpy.matrix([])):
Xini = Xb.A1.tolist()
else:
Xini = list(Xb)
- logging.debug("%s Point de démarrage Xini = %s"%(self._name, Xini))
#
# Minimisation de la fonctionnelle
# --------------------------------
+ nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
+ #
if self._parameters["Minimizer"] == "LBFGSB":
Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
func = CostFunction,
disp = disp,
full_output = True,
)
+ elif self._parameters["Minimizer"] == "LM":
+ Minimum, cov_x, infodict, mesg, rc = scipy.optimize.leastsq(
+ func = CostFunctionLM,
+ x0 = Xini,
+ Dfun = GradientOfCostFunctionLM,
+ args = (),
+ ftol = self._parameters["CostDecrementTolerance"],
+ maxfev = self._parameters["MaximumNumberOfSteps"],
+ gtol = self._parameters["GradientNormTolerance"],
+ full_output = True,
+ )
+ nfeval = infodict['nfev']
else:
raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
#
- StepMin = numpy.argmin( self.StoredVariables["CostFunctionJ"].valueserie() )
- MinJ = self.StoredVariables["CostFunctionJ"].valueserie(step = StepMin)
+ IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
+ MinJ = self.StoredVariables["CostFunctionJ"][IndexMin]
#
# Correction pour pallier a un bug de TNC sur le retour du Minimum
# ----------------------------------------------------------------
if self._parameters["StoreInternalVariables"]:
- Minimum = self.StoredVariables["CurrentState"].valueserie(step = StepMin)
- #
- logging.debug("%s %s Step of min cost = %s"%(self._name, self._parameters["Minimizer"], StepMin))
- logging.debug("%s %s Minimum cost = %s"%(self._name, self._parameters["Minimizer"], MinJ))
- logging.debug("%s %s Minimum state = %s"%(self._name, self._parameters["Minimizer"], Minimum))
- logging.debug("%s %s Nb of F = %s"%(self._name, self._parameters["Minimizer"], nfeval))
- logging.debug("%s %s RetCode = %s"%(self._name, self._parameters["Minimizer"], rc))
+ Minimum = self.StoredVariables["CurrentState"][IndexMin]
#
# Obtention de l'analyse
# ----------------------
- Xa = numpy.asmatrix(Minimum).flatten().T
- logging.debug("%s Analyse Xa = %s"%(self._name, Xa))
+ Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
#
self.StoredVariables["Analysis"].store( Xa.A1 )
#
# Calculs et/ou stockages supplémentaires
# ---------------------------------------
if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
- self.StoredVariables["Innovation"].store( numpy.asmatrix(d).flatten().A1 )
+ self.StoredVariables["Innovation"].store( numpy.ravel(d) )
if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
- self.StoredVariables["BMA"].store( numpy.asmatrix(Xb - Xa).flatten().A1 )
+ self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
- self.StoredVariables["OMA"].store( numpy.asmatrix(Y - Hm(Xa)).flatten().A1 )
+ self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(Hm(Xa)) )
if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
- self.StoredVariables["OMB"].store( numpy.asmatrix(d).flatten().A1 )
+ self.StoredVariables["OMB"].store( numpy.ravel(d) )
#
logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
logging.debug("%s Terminé"%self._name)