From b65156518aa4eea50d8dec1320b5eca3b48ca239 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Thu, 11 Feb 2021 21:02:03 +0100 Subject: [PATCH] Improvement and documentation of state initialization --- doc/en/ref_algorithm_3DVAR.rst | 2 ++ doc/en/ref_algorithm_4DVAR.rst | 2 ++ doc/en/snippets/InitializationPoint.rst | 8 ++++++++ doc/fr/ref_algorithm_3DVAR.rst | 2 ++ doc/fr/ref_algorithm_4DVAR.rst | 2 ++ doc/fr/snippets/InitializationPoint.rst | 9 +++++++++ src/daComposant/daAlgorithms/3DVAR.py | 17 +++++++++++++++-- src/daComposant/daAlgorithms/4DVAR.py | 18 ++++++++++++++---- src/daComposant/daCore/Aidsm.py | 6 ++++-- 9 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 doc/en/snippets/InitializationPoint.rst create mode 100644 doc/fr/snippets/InitializationPoint.rst diff --git a/doc/en/ref_algorithm_3DVAR.rst b/doc/en/ref_algorithm_3DVAR.rst index 1c85c0b..ba85aca 100644 --- a/doc/en/ref_algorithm_3DVAR.rst +++ b/doc/en/ref_algorithm_3DVAR.rst @@ -60,6 +60,8 @@ which is usually designed as the "*3D-VAR*" function (see for example .. include:: snippets/GradientNormTolerance.rst +.. include:: snippets/InitializationPoint.rst + .. include:: snippets/MaximumNumberOfSteps.rst .. include:: snippets/Minimizer_xDVAR.rst diff --git a/doc/en/ref_algorithm_4DVAR.rst b/doc/en/ref_algorithm_4DVAR.rst index 1ae07cf..f5a0f68 100644 --- a/doc/en/ref_algorithm_4DVAR.rst +++ b/doc/en/ref_algorithm_4DVAR.rst @@ -75,6 +75,8 @@ filters, specially the :ref:`section_ref_algorithm_ExtendedKalmanFilter` or the .. include:: snippets/GradientNormTolerance.rst +.. include:: snippets/InitializationPoint.rst + .. include:: snippets/MaximumNumberOfSteps.rst .. include:: snippets/Minimizer_xDVAR.rst diff --git a/doc/en/snippets/InitializationPoint.rst b/doc/en/snippets/InitializationPoint.rst new file mode 100644 index 0000000..5a949e7 --- /dev/null +++ b/doc/en/snippets/InitializationPoint.rst @@ -0,0 +1,8 @@ +.. index:: single: InitializationPoint + +InitializationPoint + *Vector*. The variable specifies one vector to be used as the initial state + around which an iterative algorithm starts. By default, this initial state is + not required and is equal to the background :math:`\mathbf{x}^b`. If + provided, it replaces the background only for initialization. Its value must + allow to build a vector of the same size as the background. diff --git a/doc/fr/ref_algorithm_3DVAR.rst b/doc/fr/ref_algorithm_3DVAR.rst index b385f8c..4bb750e 100644 --- a/doc/fr/ref_algorithm_3DVAR.rst +++ b/doc/fr/ref_algorithm_3DVAR.rst @@ -61,6 +61,8 @@ qui est usuellement désignée comme la fonctionnelle "*3D-VAR*" (voir par exemp .. include:: snippets/GradientNormTolerance.rst +.. include:: snippets/InitializationPoint.rst + .. include:: snippets/MaximumNumberOfSteps.rst .. include:: snippets/Minimizer_xDVAR.rst diff --git a/doc/fr/ref_algorithm_4DVAR.rst b/doc/fr/ref_algorithm_4DVAR.rst index 9b25c4c..052e532 100644 --- a/doc/fr/ref_algorithm_4DVAR.rst +++ b/doc/fr/ref_algorithm_4DVAR.rst @@ -76,6 +76,8 @@ l':ref:`section_ref_algorithm_UnscentedKalmanFilter`. .. include:: snippets/GradientNormTolerance.rst +.. include:: snippets/InitializationPoint.rst + .. include:: snippets/MaximumNumberOfSteps.rst .. include:: snippets/Minimizer_xDVAR.rst diff --git a/doc/fr/snippets/InitializationPoint.rst b/doc/fr/snippets/InitializationPoint.rst new file mode 100644 index 0000000..12236bc --- /dev/null +++ b/doc/fr/snippets/InitializationPoint.rst @@ -0,0 +1,9 @@ +.. index:: single: InitializationPoint + +InitializationPoint + *Vecteur*. La variable désigne un vecteur à utiliser comme l'état initial + autour duquel démarre un algorithme itératif. Par défaut, cet état initial + n'a pas besoin d'être fourni et il est égal à l'ébauche :math:`\mathbf{x}^b`. + Dans le cas où il est fourni, il ne remplace l'ébauche que pour + l'initialisation. Sa valeur doit permettre de construire un vecteur de taille + identique à l'ébauche. diff --git a/src/daComposant/daAlgorithms/3DVAR.py b/src/daComposant/daAlgorithms/3DVAR.py index 7848a37..3b9c397 100644 --- a/src/daComposant/daAlgorithms/3DVAR.py +++ b/src/daComposant/daAlgorithms/3DVAR.py @@ -138,6 +138,11 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): 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", "B" ), ) @@ -236,9 +241,17 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): GradJ = numpy.asmatrix( numpy.ravel( GradJb ) + numpy.ravel( GradJo ) ).T return GradJ.A1 # - # Point de démarrage de l'optimisation : Xini = Xb + # Point de démarrage de l'optimisation # ------------------------------------ - Xini = numpy.ravel(Xb) + if self._parameters["InitializationPoint"] is not None: + __ipt = numpy.ravel(self._parameters["InitializationPoint"]) + if __ipt.size != numpy.ravel(Xb).size: + raise ValueError("Incompatible size %i of forced initial point to replace the Xb of size %i" \ + %(__ipt.size,numpy.ravel(Xb).size)) + else: + Xini = __ipt + else: + Xini = numpy.ravel(Xb) # # Minimisation de la fonctionnelle # -------------------------------- diff --git a/src/daComposant/daAlgorithms/4DVAR.py b/src/daComposant/daAlgorithms/4DVAR.py index ef64a6e..bf7bd8e 100644 --- a/src/daComposant/daAlgorithms/4DVAR.py +++ b/src/daComposant/daAlgorithms/4DVAR.py @@ -107,6 +107,11 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): 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", "EM", "R", "B" ), optional = ("U", "CM"), @@ -260,12 +265,17 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): GradJ = numpy.ravel( GradJb ) - numpy.ravel( GradJo ) return GradJ # - # Point de démarrage de l'optimisation : Xini = Xb + # Point de démarrage de l'optimisation # ------------------------------------ - if isinstance(Xb, type(numpy.matrix([]))): - Xini = Xb.A1.tolist() + if self._parameters["InitializationPoint"] is not None: + __ipt = numpy.ravel(self._parameters["InitializationPoint"]) + if __ipt.size != numpy.ravel(Xb).size: + raise ValueError("Incompatible size %i of forced initial point to replace the Xb of size %i" \ + %(__ipt.size,numpy.ravel(Xb).size)) + else: + Xini = __ipt else: - Xini = list(Xb) + Xini = numpy.ravel(Xb) # # Minimisation de la fonctionnelle # -------------------------------- diff --git a/src/daComposant/daCore/Aidsm.py b/src/daComposant/daCore/Aidsm.py index 6281c84..a981db1 100644 --- a/src/daComposant/daCore/Aidsm.py +++ b/src/daComposant/daCore/Aidsm.py @@ -507,12 +507,14 @@ class Aidsm(object): Parameters = None, Script = None): "Mise a jour d'un concept de calcul" - if "AlgorithmParameters" not in self.__adaoObject or self.__adaoObject["AlgorithmParameters"] is None: + Concept = "AlgorithmParameters" + if Concept not in self.__adaoObject or self.__adaoObject[Concept] is None: raise ValueError("\n\nNo algorithm registred, set one before updating parameters or executing\n") - self.__adaoObject["AlgorithmParameters"].updateParameters( + self.__adaoObject[Concept].updateParameters( asDict = Parameters, asScript = self.__with_directory(Script), ) + # RaJ du register return 0 def setRegulationParameters(self, -- 2.39.2