Salome HOME
Minor internal modifications and bounds corrections
[modules/adao.git] / src / daComposant / daAlgorithms / NonLinearLeastSquares.py
index c894d51363c5d000f8c18a8c08c348a86083b59d..aad66fc737b92a4dac264863d8522024f8a2e278 100644 (file)
@@ -1,28 +1,28 @@
-#-*-coding:iso-8859-1-*-
+# -*- coding: utf-8 -*-
 #
-#  Copyright (C) 2008-2015 EDF R&D
+# Copyright (C) 2008-2021 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
-#  License as published by the Free Software Foundation; either
-#  version 2.1 of the License.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
 #
-#  This library is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  Lesser General Public License for more details.
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
 #
-#  You should have received a copy of the GNU Lesser General Public
-#  License along with this library; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
-#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
-#  Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
+# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
 
 import logging
 from daCore import BasicObjects
-import numpy, scipy.optimize
+import numpy, scipy.optimize, scipy.version
 
 # ==============================================================================
 class ElementaryAlgorithm(BasicObjects.Algorithm):
@@ -32,7 +32,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             name     = "Minimizer",
             default  = "LBFGSB",
             typecast = str,
-            message  = "Minimiseur utilisé",
+            message  = "Minimiseur utilisé",
             listval  = ["LBFGSB","TNC", "CG", "NCG", "BFGS", "LM"],
             )
         self.defineRequiredParameter(
@@ -46,101 +46,145 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             name     = "CostDecrementTolerance",
             default  = 1.e-7,
             typecast = float,
-            message  = "Diminution relative minimale du cout lors de l'arrêt",
+            message  = "Diminution relative minimale du coût lors de l'arrêt",
+            minval   = 0.,
             )
         self.defineRequiredParameter(
             name     = "ProjectedGradientTolerance",
             default  = -1,
             typecast = float,
-            message  = "Maximum des composantes du gradient projeté lors de l'arrêt",
+            message  = "Maximum des composantes du gradient projeté lors de l'arrêt",
             minval   = -1,
             )
         self.defineRequiredParameter(
             name     = "GradientNormTolerance",
             default  = 1.e-05,
             typecast = float,
-            message  = "Maximum des composantes du gradient lors de l'arrêt",
+            message  = "Maximum des composantes du gradient lors de l'arrêt",
+            minval   = 0.,
             )
         self.defineRequiredParameter(
             name     = "StoreInternalVariables",
             default  = False,
             typecast = bool,
-            message  = "Stockage des variables internes ou intermédiaires du calcul",
+            message  = "Stockage des variables internes ou intermédiaires du calcul",
             )
         self.defineRequiredParameter(
             name     = "StoreSupplementaryCalculations",
             default  = [],
             typecast = tuple,
-            message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
-            listval  = ["BMA", "OMA", "OMB", "CurrentState", "CostFunctionJ", "Innovation", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
+            message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
+            listval  = [
+                "Analysis",
+                "BMA",
+                "CostFunctionJ",
+                "CostFunctionJAtCurrentOptimum",
+                "CostFunctionJb",
+                "CostFunctionJbAtCurrentOptimum",
+                "CostFunctionJo",
+                "CostFunctionJoAtCurrentOptimum",
+                "CurrentIterationNumber",
+                "CurrentOptimum",
+                "CurrentState",
+                "IndexOfOptimum",
+                "Innovation",
+                "InnovationAtCurrentState",
+                "OMA",
+                "OMB",
+                "SimulatedObservationAtBackground",
+                "SimulatedObservationAtCurrentOptimum",
+                "SimulatedObservationAtCurrentState",
+                "SimulatedObservationAtOptimum",
+                ]
             )
+        self.defineRequiredParameter( # Pas de type
+            name     = "Bounds",
+            message  = "Liste des valeurs de bornes",
+            )
+        self.defineRequiredParameter(
+            name     = "InitializationPoint",
+            typecast = numpy.ravel,
+            message  = "État initial imposé (par défaut, c'est l'ébauche si None)",
+            )
+        self.requireInputArguments(
+            mandatory= ("Xb", "Y", "HO", "R"),
+            )
+        self.setAttributes(tags=(
+            "Optimization",
+            "NonLinear",
+            "Variational",
+            ))
 
     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
-        self._pre_run()
-        if logging.getLogger().level < logging.WARNING:
-            self.__iprint, self.__disp = 1, 1
-            self.__message = scipy.optimize.tnc.MSG_ALL
-        else:
-            self.__iprint, self.__disp = -1, 0
-            self.__message = scipy.optimize.tnc.MSG_NONE
-        #
-        # Paramètres de pilotage
-        # ----------------------
-        self.setParameters(Parameters)
-        #
-        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):
-            Bounds = self._parameters["Bounds"]
-            logging.debug("%s Prise en compte des bornes effectuee"%(self._name,))
-        else:
-            Bounds = None
-        #
-        # Correction pour pallier a un bug de TNC sur le retour du Minimum
-        if self._parameters.has_key("Minimizer") == "TNC":
-            self.setParameterValue("StoreInternalVariables",True)
+        self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
         #
-        # Opérateurs
+        # Opérateurs
         # ----------
         Hm = HO["Direct"].appliedTo
         Ha = HO["Adjoint"].appliedInXTo
         #
-        # Utilisation éventuelle d'un vecteur H(Xb) précalculé
+        # Utilisation éventuelle d'un vecteur H(Xb) précalculé
         # ----------------------------------------------------
-        if HO["AppliedToX"] is not None and HO["AppliedToX"].has_key("HXb"):
-            HXb = HO["AppliedToX"]["HXb"]
+        if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
+            HXb = Hm( Xb, HO["AppliedInX"]["HXb"] )
         else:
             HXb = Hm( Xb )
         HXb = numpy.asmatrix(numpy.ravel( HXb )).T
-        #
-        # Calcul de l'innovation
-        # ----------------------
         if Y.size != HXb.size:
             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))
         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
         #
-        # Précalcul des inversions de B et R
+        # Précalcul des inversions de B et R
         # ----------------------------------
         RI = R.getI()
         if self._parameters["Minimizer"] == "LM":
             RdemiI = R.choleskyI()
         #
-        # Définition de la fonction-coût
+        # Définition de la fonction-coût
         # ------------------------------
         def CostFunction(x):
             _X  = numpy.asmatrix(numpy.ravel( x )).T
+            if self._parameters["StoreInternalVariables"] or \
+                self._toStore("CurrentState") or \
+                self._toStore("CurrentOptimum"):
+                self.StoredVariables["CurrentState"].store( _X )
             _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"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
-                self.StoredVariables["CurrentState"].store( _X )
-            if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+            _Innovation = Y - _HX
+            if self._toStore("SimulatedObservationAtCurrentState") or \
+                self._toStore("SimulatedObservationAtCurrentOptimum"):
                 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
+            if self._toStore("InnovationAtCurrentState"):
+                self.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
+            #
+            Jb  = 0.
+            Jo  = float( 0.5 * _Innovation.T * RI * _Innovation )
+            J   = Jb + Jo
+            #
+            self.StoredVariables["CurrentIterationNumber"].store( len(self.StoredVariables["CostFunctionJ"]) )
             self.StoredVariables["CostFunctionJb"].store( Jb )
             self.StoredVariables["CostFunctionJo"].store( Jo )
             self.StoredVariables["CostFunctionJ" ].store( J )
+            if self._toStore("IndexOfOptimum") or \
+                self._toStore("CurrentOptimum") or \
+                self._toStore("CostFunctionJAtCurrentOptimum") or \
+                self._toStore("CostFunctionJbAtCurrentOptimum") or \
+                self._toStore("CostFunctionJoAtCurrentOptimum") or \
+                self._toStore("SimulatedObservationAtCurrentOptimum"):
+                IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
+            if self._toStore("IndexOfOptimum"):
+                self.StoredVariables["IndexOfOptimum"].store( IndexMin )
+            if self._toStore("CurrentOptimum"):
+                self.StoredVariables["CurrentOptimum"].store( self.StoredVariables["CurrentState"][IndexMin] )
+            if self._toStore("SimulatedObservationAtCurrentOptimum"):
+                self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
+            if self._toStore("CostFunctionJbAtCurrentOptimum"):
+                self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
+            if self._toStore("CostFunctionJoAtCurrentOptimum"):
+                self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
+            if self._toStore("CostFunctionJAtCurrentOptimum"):
+                self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
             return J
         #
         def GradientOfCostFunction(x):
@@ -156,16 +200,18 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             _X  = numpy.asmatrix(numpy.ravel( x )).T
             _HX = Hm( _X )
             _HX = numpy.asmatrix(numpy.ravel( _HX )).T
+            _Innovation = Y - _HX
             Jb  = 0.
-            Jo  = 0.5 * (Y - _HX).T * RI * (Y - _HX)
-            J   = float( Jb ) + float( Jo )
-            if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+            Jo  = float( 0.5 * _Innovation.T * RI * _Innovation )
+            J   = Jb + Jo
+            if self._parameters["StoreInternalVariables"] or \
+                self._toStore("CurrentState"):
                 self.StoredVariables["CurrentState"].store( _X )
             self.StoredVariables["CostFunctionJb"].store( Jb )
             self.StoredVariables["CostFunctionJo"].store( Jo )
             self.StoredVariables["CostFunctionJ" ].store( J )
             #
-            return numpy.ravel( RdemiI*(Y - _HX) )
+            return numpy.ravel( RdemiI*_Innovation )
         #
         def GradientOfCostFunctionLM(x):
             _X      = numpy.asmatrix(numpy.ravel( x )).T
@@ -176,28 +222,30 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             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
+        # Point de démarrage de l'optimisation : Xini = Xb
         # ------------------------------------
-        if type(Xb) is type(numpy.matrix([])):
-            Xini = Xb.A1.tolist()
-        else:
-            Xini = list(Xb)
+        Xini = self._parameters["InitializationPoint"]
         #
         # Minimisation de la fonctionnelle
         # --------------------------------
         nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
         #
         if self._parameters["Minimizer"] == "LBFGSB":
-            Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
+            # Minimum, J_optimal, Informations = scipy.optimize.fmin_l_bfgs_b(
+            if "0.19" <= scipy.version.version <= "1.1.0":
+                import lbfgsbhlt as optimiseur
+            else:
+                import scipy.optimize as optimiseur
+            Minimum, J_optimal, Informations = optimiseur.fmin_l_bfgs_b(
                 func        = CostFunction,
                 x0          = Xini,
                 fprime      = GradientOfCostFunction,
                 args        = (),
-                bounds      = Bounds,
+                bounds      = self._parameters["Bounds"],
                 maxfun      = self._parameters["MaximumNumberOfSteps"]-1,
                 factr       = self._parameters["CostDecrementTolerance"]*1.e14,
                 pgtol       = self._parameters["ProjectedGradientTolerance"],
-                iprint      = self.__iprint,
+                iprint      = self._parameters["optiprint"],
                 )
             nfeval = Informations['funcalls']
             rc     = Informations['warnflag']
@@ -207,11 +255,11 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 x0          = Xini,
                 fprime      = GradientOfCostFunction,
                 args        = (),
-                bounds      = Bounds,
+                bounds      = self._parameters["Bounds"],
                 maxfun      = self._parameters["MaximumNumberOfSteps"],
                 pgtol       = self._parameters["ProjectedGradientTolerance"],
                 ftol        = self._parameters["CostDecrementTolerance"],
-                messages    = self.__message,
+                messages    = self._parameters["optmessages"],
                 )
         elif self._parameters["Minimizer"] == "CG":
             Minimum, fopt, nfeval, grad_calls, rc = scipy.optimize.fmin_cg(
@@ -221,7 +269,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 args        = (),
                 maxiter     = self._parameters["MaximumNumberOfSteps"],
                 gtol        = self._parameters["GradientNormTolerance"],
-                disp        = self.__disp,
+                disp        = self._parameters["optdisp"],
                 full_output = True,
                 )
         elif self._parameters["Minimizer"] == "NCG":
@@ -232,7 +280,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 args        = (),
                 maxiter     = self._parameters["MaximumNumberOfSteps"],
                 avextol     = self._parameters["CostDecrementTolerance"],
-                disp        = self.__disp,
+                disp        = self._parameters["optdisp"],
                 full_output = True,
                 )
         elif self._parameters["Minimizer"] == "BFGS":
@@ -243,7 +291,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 args        = (),
                 maxiter     = self._parameters["MaximumNumberOfSteps"],
                 gtol        = self._parameters["GradientNormTolerance"],
-                disp        = self.__disp,
+                disp        = self._parameters["optdisp"],
                 full_output = True,
                 )
         elif self._parameters["Minimizer"] == "LM":
@@ -266,7 +314,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         #
         # Correction pour pallier a un bug de TNC sur le retour du Minimum
         # ----------------------------------------------------------------
-        if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
             Minimum = self.StoredVariables["CurrentState"][IndexMin]
         #
         # Obtention de l'analyse
@@ -275,22 +323,32 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         #
         self.StoredVariables["Analysis"].store( Xa.A1 )
         #
-        if "OMA"                           in self._parameters["StoreSupplementaryCalculations"] or \
-           "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
-            HXa = Hm(Xa)
+        if self._toStore("OMA") or \
+            self._toStore("SimulatedObservationAtOptimum"):
+            if self._toStore("SimulatedObservationAtCurrentState"):
+                HXa = self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
+            elif self._toStore("SimulatedObservationAtCurrentOptimum"):
+                HXa = self.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
+            else:
+                HXa = Hm( Xa )
         #
         #
-        # Calculs et/ou stockages supplémentaires
+        # Calculs et/ou stockages supplémentaires
         # ---------------------------------------
-        if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("Innovation") or \
+            self._toStore("OMB"):
+            d  = Y - HXb
+        if self._toStore("Innovation"):
             self.StoredVariables["Innovation"].store( numpy.ravel(d) )
-        if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("BMA"):
             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
-        if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("OMA"):
             self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
-        if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("OMB"):
             self.StoredVariables["OMB"].store( numpy.ravel(d) )
-        if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("SimulatedObservationAtBackground"):
+            self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
+        if self._toStore("SimulatedObservationAtOptimum"):
             self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
         #
         self._post_run(HO)
@@ -298,4 +356,4 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
 
 # ==============================================================================
 if __name__ == "__main__":
-    print '\n AUTODIAGNOSTIC \n'
+    print('\n AUTODIAGNOSTIC\n')