From 638487104745d6366bb26a744fd1acc858770f03 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Thu, 31 Mar 2022 20:27:35 +0200 Subject: [PATCH] Code improvements for warning on iteration control --- src/daComposant/daAlgorithms/3DVAR.py | 1 + src/daComposant/daAlgorithms/4DVAR.py | 1 + .../daAlgorithms/DerivativeFreeOptimization.py | 1 + .../daAlgorithms/DifferentialEvolution.py | 1 + .../daAlgorithms/NonLinearLeastSquares.py | 1 + .../daAlgorithms/ParticleSwarmOptimization.py | 1 + .../daAlgorithms/QuantileRegression.py | 1 + src/daComposant/daAlgorithms/TabuSearch.py | 1 + src/daComposant/daCore/BasicObjects.py | 15 ++++++++++++++- 9 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/daComposant/daAlgorithms/3DVAR.py b/src/daComposant/daAlgorithms/3DVAR.py index 996c7ec..355426b 100644 --- a/src/daComposant/daAlgorithms/3DVAR.py +++ b/src/daComposant/daAlgorithms/3DVAR.py @@ -71,6 +71,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = -1, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "CostDecrementTolerance", diff --git a/src/daComposant/daAlgorithms/4DVAR.py b/src/daComposant/daAlgorithms/4DVAR.py index 0a45328..a18d94d 100644 --- a/src/daComposant/daAlgorithms/4DVAR.py +++ b/src/daComposant/daAlgorithms/4DVAR.py @@ -67,6 +67,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = -1, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "CostDecrementTolerance", diff --git a/src/daComposant/daAlgorithms/DerivativeFreeOptimization.py b/src/daComposant/daAlgorithms/DerivativeFreeOptimization.py index 7ad0c03..9a561c2 100644 --- a/src/daComposant/daAlgorithms/DerivativeFreeOptimization.py +++ b/src/daComposant/daAlgorithms/DerivativeFreeOptimization.py @@ -47,6 +47,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = -1, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "MaximumNumberOfFunctionEvaluations", diff --git a/src/daComposant/daAlgorithms/DifferentialEvolution.py b/src/daComposant/daAlgorithms/DifferentialEvolution.py index df66f0d..1a4ed93 100644 --- a/src/daComposant/daAlgorithms/DifferentialEvolution.py +++ b/src/daComposant/daAlgorithms/DifferentialEvolution.py @@ -55,6 +55,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de générations", minval = 0, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "MaximumNumberOfFunctionEvaluations", diff --git a/src/daComposant/daAlgorithms/NonLinearLeastSquares.py b/src/daComposant/daAlgorithms/NonLinearLeastSquares.py index ffdbb07..e99b0ac 100644 --- a/src/daComposant/daAlgorithms/NonLinearLeastSquares.py +++ b/src/daComposant/daAlgorithms/NonLinearLeastSquares.py @@ -67,6 +67,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = -1, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "CostDecrementTolerance", diff --git a/src/daComposant/daAlgorithms/ParticleSwarmOptimization.py b/src/daComposant/daAlgorithms/ParticleSwarmOptimization.py index 808de35..1a0402d 100644 --- a/src/daComposant/daAlgorithms/ParticleSwarmOptimization.py +++ b/src/daComposant/daAlgorithms/ParticleSwarmOptimization.py @@ -33,6 +33,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = 0, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "MaximumNumberOfFunctionEvaluations", diff --git a/src/daComposant/daAlgorithms/QuantileRegression.py b/src/daComposant/daAlgorithms/QuantileRegression.py index 6f67538..3c13072 100644 --- a/src/daComposant/daAlgorithms/QuantileRegression.py +++ b/src/daComposant/daAlgorithms/QuantileRegression.py @@ -49,6 +49,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = 1, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "CostDecrementTolerance", diff --git a/src/daComposant/daAlgorithms/TabuSearch.py b/src/daComposant/daAlgorithms/TabuSearch.py index 09b4bd8..ef6303b 100644 --- a/src/daComposant/daAlgorithms/TabuSearch.py +++ b/src/daComposant/daAlgorithms/TabuSearch.py @@ -33,6 +33,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): typecast = int, message = "Nombre maximal de pas d'optimisation", minval = 1, + oldname = "MaximumNumberOfSteps", ) self.defineRequiredParameter( name = "SetSeed", diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index 4b435c6..a219937 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -32,6 +32,7 @@ import logging import copy import time import numpy +import warnings from functools import partial from daCore import Persistence, PlatformInfo, Interfaces from daCore import Templates @@ -662,6 +663,7 @@ class Algorithm(object): self.__variable_names_not_public = {"nextStep":False} # Duplication dans AlgorithmAndParameters self.__canonical_parameter_name = {} # Correspondance "lower"->"correct" self.__canonical_stored_name = {} # Correspondance "lower"->"correct" + self.__replace_by_the_new_name = {} # Nouveau nom à partir d'un nom ancien # self.StoredVariables = {} self.StoredVariables["APosterioriCorrelations"] = Persistence.OneMatrix(name = "APosterioriCorrelations") @@ -905,7 +907,7 @@ class Algorithm(object): """ raise NotImplementedError("Mathematical assimilation calculation has not been implemented!") - def defineRequiredParameter(self, name = None, default = None, typecast = None, message = None, minval = None, maxval = None, listval = None, listadv = None): + def defineRequiredParameter(self, name = None, default = None, typecast = None, message = None, minval = None, maxval = None, listval = None, listadv = None, oldname = None): """ Permet de définir dans l'algorithme des paramètres requis et leurs caractéristiques par défaut. @@ -921,8 +923,12 @@ class Algorithm(object): "listval" : listval, "listadv" : listadv, "message" : message, + "oldname" : oldname, } self.__canonical_parameter_name[name.lower()] = name + if oldname is not None: + self.__canonical_parameter_name[oldname.lower()] = name # Conversion + self.__replace_by_the_new_name[oldname.lower()] = name logging.debug("%s %s (valeur par défaut = %s)", self._name, message, self.setParameterValue(name)) def getRequiredParameters(self, noDetails=True): @@ -1008,6 +1014,13 @@ class Algorithm(object): __inverse_fromDico_keys[self.__canonical_parameter_name[k.lower()]] = k #~ __inverse_fromDico_keys = dict([(self.__canonical_parameter_name[k.lower()],k) for k in fromDico.keys()]) __canonic_fromDico_keys = __inverse_fromDico_keys.keys() + # + for k in __inverse_fromDico_keys.values(): + if k.lower() in self.__replace_by_the_new_name: + __newk = self.__replace_by_the_new_name[k.lower()] + __msg = "the parameter '%s' used in '%s' algorithm case is deprecated and has to be replaced by '%s'. Please update your code."%(k,self._name,__newk) + warnings.warn(__msg, FutureWarning, stacklevel=50) + # for k in self.__required_parameters.keys(): if k in __canonic_fromDico_keys: self._parameters[k] = self.setParameterValue(k,fromDico[__inverse_fromDico_keys[k]]) -- 2.39.2