Salome HOME
Minor print update
[modules/adao.git] / src / daComposant / daAlgorithms / UnscentedKalmanFilter.py
index 50e22d95c465e029f8e2411b64ab218a9b4c5d4f..43c873069d4a1c1c165af2932bee03a23a11f993 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2008-2018 EDF R&D
+# Copyright (C) 2008-2019 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
@@ -83,16 +83,17 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             typecast = tuple,
             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
             listval  = [
+                "Analysis",
                 "APosterioriCorrelations",
                 "APosterioriCovariance",
                 "APosterioriStandardDeviations",
                 "APosterioriVariances",
                 "BMA",
-                "CurrentState",
                 "CostFunctionJ",
                 "CostFunctionJb",
                 "CostFunctionJo",
-                "Innovation",
+                "CurrentState",
+                "InnovationAtCurrentState",
                 ]
             )
         self.defineRequiredParameter( # Pas de type
@@ -135,10 +136,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         #
         # Opérateurs
         # ----------
-        H = HO["Direct"].appliedControledFormTo
+        Hm = HO["Direct"].appliedControledFormTo
         #
         if self._parameters["EstimationOf"] == "State":
-            M = EM["Direct"].appliedControledFormTo
+            Mm = EM["Direct"].appliedControledFormTo
         #
         if CM is not None and "Tangent" in CM and U is not None:
             Cm = CM["Tangent"].asMatrix(Xb)
@@ -164,16 +165,14 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # Initialisation
         # --------------
         Xn = Xb
-        if hasattr(B,"asfullmatrix"):
-            Pn = B.asfullmatrix(Xn.size)
-        else:
-            Pn = B
+        if hasattr(B,"asfullmatrix"): Pn = B.asfullmatrix(Xn.size)
+        else:                         Pn = B
         #
         self.StoredVariables["Analysis"].store( Xn.A1 )
         if self._toStore("APosterioriCovariance"):
             self.StoredVariables["APosterioriCovariance"].store( Pn )
             covarianceXa = Pn
-        Xa               = Xn
+        Xa = XaMin       = Xb
         previousJMinimum = numpy.finfo(float).max
         #
         for step in range(duration-1):
@@ -204,7 +203,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             XEtnnp = []
             for point in range(nbSpts):
                 if self._parameters["EstimationOf"] == "State":
-                    XEtnnpi = numpy.asmatrix(numpy.ravel( M( (Xnp[:,point], Un) ) )).T
+                    XEtnnpi = numpy.asmatrix(numpy.ravel( Mm( (Xnp[:,point], Un) ) )).T
                     if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
                         Cm = Cm.reshape(Xn.size,Un.size) # ADAO & check shape
                         XEtnnpi = XEtnnpi + Cm * Un
@@ -243,9 +242,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             Ynnp = []
             for point in range(nbSpts):
                 if self._parameters["EstimationOf"] == "State":
-                    Ynnpi = numpy.asmatrix(numpy.ravel( H( (Xnnp[:,point], None) ) )).T
+                    Ynnpi = numpy.asmatrix(numpy.ravel( Hm( (Xnnp[:,point], None) ) )).T
                 elif self._parameters["EstimationOf"] == "Parameters":
-                    Ynnpi = numpy.asmatrix(numpy.ravel( H( (Xnnp[:,point], Un) ) )).T
+                    Ynnpi = numpy.asmatrix(numpy.ravel( Hm( (Xnnp[:,point], Un) ) )).T
                 Ynnp.append( Ynnpi )
             Ynnp = numpy.hstack( Ynnp )
             #
@@ -269,12 +268,15 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             if self._parameters["Bounds"] is not None and self._parameters["ConstrainedBy"] == "EstimateProjection":
                 Xn = numpy.max(numpy.hstack((Xn,numpy.asmatrix(self._parameters["Bounds"])[:,0])),axis=1)
                 Xn = numpy.min(numpy.hstack((Xn,numpy.asmatrix(self._parameters["Bounds"])[:,1])),axis=1)
+            Xa = Xn # Pointeurs
             #
-            self.StoredVariables["Analysis"].store( Xn.A1 )
+            # ---> avec analysis
+            self.StoredVariables["Analysis"].store( Xa )
             if self._toStore("APosterioriCovariance"):
                 self.StoredVariables["APosterioriCovariance"].store( Pn )
-            if self._toStore("Innovation"):
-                self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
+            # ---> avec current state
+            if self._toStore("InnovationAtCurrentState"):
+                self.StoredVariables["InnovationAtCurrentState"].store( d )
             if self._parameters["StoreInternalVariables"] \
                 or self._toStore("CurrentState"):
                 self.StoredVariables["CurrentState"].store( Xn )
@@ -282,34 +284,31 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 or self._toStore("CostFunctionJ") \
                 or self._toStore("CostFunctionJb") \
                 or self._toStore("CostFunctionJo"):
-                Jb  = 0.5 * (Xn - Xb).T * BI * (Xn - Xb)
-                Jo  = 0.5 * d.T * RI * d
-                J   = float( Jb ) + float( Jo )
+                Jb  = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
+                Jo  = float( 0.5 * d.T * RI * d )
+                J   = Jb + Jo
                 self.StoredVariables["CostFunctionJb"].store( Jb )
                 self.StoredVariables["CostFunctionJo"].store( Jo )
                 self.StoredVariables["CostFunctionJ" ].store( J )
-                if J < previousJMinimum:
-                    previousJMinimum  = J
-                    Xa                = Xn
-                    if self._toStore("APosterioriCovariance"):
-                        covarianceXa  = Pn
-            else:
-                Xa = Xn
-            #
+            if self._parameters["EstimationOf"] == "Parameters" \
+                and J < previousJMinimum:
+                previousJMinimum    = J
+                XaMin               = Xa
+                if self._toStore("APosterioriCovariance"):
+                    covarianceXaMin = Pn
         #
-        # Stockage supplementaire de l'optimum en estimation de parametres
-        # ----------------------------------------------------------------
+        # Stockage final supplémentaire de l'optimum en estimation de paramètres
+        # ----------------------------------------------------------------------
         if self._parameters["EstimationOf"] == "Parameters":
-            self.StoredVariables["Analysis"].store( Xa.A1 )
+            self.StoredVariables["Analysis"].store( XaMin )
             if self._toStore("APosterioriCovariance"):
-                self.StoredVariables["APosterioriCovariance"].store( covarianceXa )
-        #
-        if self._toStore("BMA"):
-            self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
+                self.StoredVariables["APosterioriCovariance"].store( covarianceXaMin )
+            if self._toStore("BMA"):
+                self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(XaMin) )
         #
         self._post_run(HO)
         return 0
 
 # ==============================================================================
 if __name__ == "__main__":
-    print('\n AUTODIAGNOSTIC \n')
+    print('\n AUTODIAGNOSTIC\n')