Salome HOME
Improving warning message for available DFO minimizer choice
[modules/adao.git] / src / daComposant / daAlgorithms / DerivativeFreeOptimization.py
index ccf94a9c24cadf416c900d058b350bd45efb9d5f..4734eefc8e1b7855bbab60fc49156a0adf81f051 100644 (file)
@@ -1,6 +1,6 @@
-#-*-coding:iso-8859-1-*-
+# -*- coding: utf-8 -*-
 #
-# Copyright (C) 2008-2017 EDF R&D
+# Copyright (C) 2008-2018 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
@@ -32,7 +32,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             name     = "Minimizer",
             default  = "BOBYQA",
             typecast = str,
-            message  = "Minimiseur utilisé",
+            message  = "Minimiseur utilisé",
             listval  = ["BOBYQA", "COBYLA", "NEWUOA", "POWELL", "SIMPLEX", "SUBPLEX"],
             )
         self.defineRequiredParameter(
@@ -46,26 +46,26 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             name     = "MaximumNumberOfFunctionEvaluations",
             default  = 15000,
             typecast = int,
-            message  = "Nombre maximal d'évaluations de la fonction",
+            message  = "Nombre maximal d'évaluations de la fonction",
             minval   = -1,
             )
         self.defineRequiredParameter(
             name     = "StateVariationTolerance",
             default  = 1.e-4,
             typecast = float,
-            message  = "Variation relative maximale de l'état lors de l'arrêt",
+            message  = "Variation relative maximale de l'état lors de l'arrêt",
             )
         self.defineRequiredParameter(
             name     = "CostDecrementTolerance",
             default  = 1.e-7,
             typecast = float,
-            message  = "Diminution relative minimale du cout lors de l'arrêt",
+            message  = "Diminution relative minimale du cout lors de l'arrêt",
             )
         self.defineRequiredParameter(
             name     = "QualityCriterion",
             default  = "AugmentedWeightedLeastSquares",
             typecast = str,
-            message  = "Critère de qualité utilisé",
+            message  = "Critère de qualité utilisé",
             listval  = ["AugmentedWeightedLeastSquares","AWLS","DA",
                         "WeightedLeastSquares","WLS",
                         "LeastSquares","LS","L2",
@@ -76,49 +76,58 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             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  = ["CurrentState", "CostFunctionJ", "CostFunctionJb", "CostFunctionJo", "CostFunctionJAtCurrentOptimum", "CurrentOptimum", "IndexOfOptimum", "InnovationAtCurrentState", "BMA", "OMA", "OMB", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentOptimum", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
+            message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
+            listval  = [
+                "CurrentState",
+                "CostFunctionJ",
+                "CostFunctionJb",
+                "CostFunctionJo",
+                "CostFunctionJAtCurrentOptimum",
+                "CostFunctionJbAtCurrentOptimum",
+                "CostFunctionJoAtCurrentOptimum",
+                "CurrentOptimum",
+                "IndexOfOptimum",
+                "InnovationAtCurrentState",
+                "BMA",
+                "OMA",
+                "OMB",
+                "SimulatedObservationAtBackground",
+                "SimulatedObservationAtCurrentOptimum",
+                "SimulatedObservationAtCurrentState",
+                "SimulatedObservationAtOptimum",
+                ]
             )
         self.defineRequiredParameter( # Pas de type
             name     = "Bounds",
             message  = "Liste des valeurs de bornes",
             )
+        self.requireInputArguments(
+            mandatory= ("Xb", "Y", "HO", "R", "B" ),
+            )
 
     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.__disp = 1
-        else:
-            self.__disp = 0
-        #
-        # Paramètres de pilotage
-        # ----------------------
-        self.setParameters(Parameters)
+        self._pre_run(Parameters, Xb, Y, R, B, Q)
         #
         if not PlatformInfo.has_nlopt and not self._parameters["Minimizer"] in ["COBYLA", "POWELL", "SIMPLEX"]:
+            logging.warning("%s Minimization by SIMPLEX is forced because %s is unavailable (COBYLA, POWELL are also available)"%(self._name,self._parameters["Minimizer"]))
             self._parameters["Minimizer"] = "SIMPLEX"
-        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
         #
-        # Opérateurs
+        # Opérateurs
         # ----------
         Hm = HO["Direct"].appliedTo
         #
-        # Précalcul des inversions de B et R
+        # Précalcul des inversions de B et R
         # ----------------------------------
         BI = B.getI()
         RI = R.getI()
         #
-        # Définition de la fonction-coût
+        # Définition de la fonction-coût
         # ------------------------------
         def CostFunction(x, QualityMeasure="AugmentedWeightedLeastSquares"):
             _X  = numpy.asmatrix(numpy.ravel( x )).T
@@ -160,6 +169,8 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             if "IndexOfOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
                "CurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
                "CostFunctionJAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
+               "CostFunctionJbAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
+               "CostFunctionJoAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
                "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
                 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
             if "IndexOfOptimum" in self._parameters["StoreSupplementaryCalculations"]:
@@ -169,12 +180,14 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             if "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
                 self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
             if "CostFunctionJAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
+                self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
+            if "CostFunctionJbAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
                 self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
+            if "CostFunctionJoAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
                 self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
-                self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
             return J
         #
-        # Point de démarrage de l'optimisation : Xini = Xb
+        # Point de démarrage de l'optimisation : Xini = Xb
         # ------------------------------------
         Xini = numpy.ravel(Xb)
         if len(Xini) < 2 and self._parameters["Minimizer"] == "NEWUOA":
@@ -194,7 +207,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 xtol        = self._parameters["StateVariationTolerance"],
                 ftol        = self._parameters["CostDecrementTolerance"],
                 full_output = True,
-                disp        = self.__disp,
+                disp        = self._parameters["optdisp"],
                 )
         elif self._parameters["Minimizer"] == "COBYLA" and not PlatformInfo.has_nlopt:
             def make_constraints(bounds):
@@ -204,19 +217,19 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                     upper = lambda x: b - x[i]
                     constraints = constraints + [lower] + [upper]
                 return constraints
-            if Bounds is None:
+            if self._parameters["Bounds"] is None:
                 raise ValueError("Bounds have to be given for all axes as a list of lower/upper pairs!")
             Minimum = scipy.optimize.fmin_cobyla(
                 func        = CostFunction,
                 x0          = Xini,
-                cons        = make_constraints( Bounds ),
+                cons        = make_constraints( self._parameters["Bounds"] ),
                 args        = (self._parameters["QualityCriterion"],),
                 consargs    = (), # To avoid extra-args
                 maxfun      = self._parameters["MaximumNumberOfFunctionEvaluations"],
                 rhobeg      = 1.0,
                 rhoend      = self._parameters["StateVariationTolerance"],
                 catol       = 2.*self._parameters["StateVariationTolerance"],
-                disp        = self.__disp,
+                disp        = self._parameters["optdisp"],
                 )
         elif self._parameters["Minimizer"] == "COBYLA" and PlatformInfo.has_nlopt:
             import nlopt
@@ -225,23 +238,23 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 # DFO, so no gradient
                 return CostFunction(_Xx, self._parameters["QualityCriterion"])
             opt.set_min_objective(_f)
-            if Bounds is not None:
-                lub = numpy.array(Bounds).reshape((Xini.size,2))
-                lb = lub[:,0]
-                ub = lub[:,1]
-                if self.__disp:
-                    print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
-                    print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
+            if self._parameters["Bounds"] is not None:
+                lub = numpy.array(self._parameters["Bounds"],dtype=float).reshape((Xini.size,2))
+                lb = lub[:,0] ; lb[numpy.isnan(lb)] = -float('inf')
+                ub = lub[:,1] ; ub[numpy.isnan(ub)] = +float('inf')
+                if self._parameters["optdisp"]:
+                    print("%s: upper bounds %s"%(opt.get_algorithm_name(),ub))
+                    print("%s: lower bounds %s"%(opt.get_algorithm_name(),lb))
                 opt.set_upper_bounds(ub)
                 opt.set_lower_bounds(lb)
             opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
             opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
             opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
             Minimum = opt.optimize( Xini )
-            if self.__disp:
-                print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
-                print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
-                print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
+            if self._parameters["optdisp"]:
+                print("%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum))
+                print("%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value()))
+                print("%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result()))
         elif self._parameters["Minimizer"] == "SIMPLEX" and not PlatformInfo.has_nlopt:
             Minimum, J_optimal, niter, nfeval, rc = scipy.optimize.fmin(
                 func        = CostFunction,
@@ -252,7 +265,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 xtol        = self._parameters["StateVariationTolerance"],
                 ftol        = self._parameters["CostDecrementTolerance"],
                 full_output = True,
-                disp        = self.__disp,
+                disp        = self._parameters["optdisp"],
                 )
         elif self._parameters["Minimizer"] == "SIMPLEX" and PlatformInfo.has_nlopt:
             import nlopt
@@ -261,23 +274,23 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 # DFO, so no gradient
                 return CostFunction(_Xx, self._parameters["QualityCriterion"])
             opt.set_min_objective(_f)
-            if Bounds is not None:
-                lub = numpy.array(Bounds).reshape((Xini.size,2))
-                lb = lub[:,0]
-                ub = lub[:,1]
-                if self.__disp:
-                    print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
-                    print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
+            if self._parameters["Bounds"] is not None:
+                lub = numpy.array(self._parameters["Bounds"],dtype=float).reshape((Xini.size,2))
+                lb = lub[:,0] ; lb[numpy.isnan(lb)] = -float('inf')
+                ub = lub[:,1] ; ub[numpy.isnan(ub)] = +float('inf')
+                if self._parameters["optdisp"]:
+                    print("%s: upper bounds %s"%(opt.get_algorithm_name(),ub))
+                    print("%s: lower bounds %s"%(opt.get_algorithm_name(),lb))
                 opt.set_upper_bounds(ub)
                 opt.set_lower_bounds(lb)
             opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
             opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
             opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
             Minimum = opt.optimize( Xini )
-            if self.__disp:
-                print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
-                print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
-                print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
+            if self._parameters["optdisp"]:
+                print("%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum))
+                print("%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value()))
+                print("%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result()))
         elif self._parameters["Minimizer"] == "BOBYQA" and PlatformInfo.has_nlopt:
             import nlopt
             opt = nlopt.opt(nlopt.LN_BOBYQA, Xini.size)
@@ -285,23 +298,23 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 # DFO, so no gradient
                 return CostFunction(_Xx, self._parameters["QualityCriterion"])
             opt.set_min_objective(_f)
-            if Bounds is not None:
-                lub = numpy.array(Bounds).reshape((Xini.size,2))
-                lb = lub[:,0]
-                ub = lub[:,1]
-                if self.__disp:
-                    print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
-                    print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
+            if self._parameters["Bounds"] is not None:
+                lub = numpy.array(self._parameters["Bounds"],dtype=float).reshape((Xini.size,2))
+                lb = lub[:,0] ; lb[numpy.isnan(lb)] = -float('inf')
+                ub = lub[:,1] ; ub[numpy.isnan(ub)] = +float('inf')
+                if self._parameters["optdisp"]:
+                    print("%s: upper bounds %s"%(opt.get_algorithm_name(),ub))
+                    print("%s: lower bounds %s"%(opt.get_algorithm_name(),lb))
                 opt.set_upper_bounds(ub)
                 opt.set_lower_bounds(lb)
             opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
             opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
             opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
             Minimum = opt.optimize( Xini )
-            if self.__disp:
-                print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
-                print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
-                print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
+            if self._parameters["optdisp"]:
+                print("%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum))
+                print("%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value()))
+                print("%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result()))
         elif self._parameters["Minimizer"] == "NEWUOA" and PlatformInfo.has_nlopt:
             import nlopt
             opt = nlopt.opt(nlopt.LN_NEWUOA, Xini.size)
@@ -309,23 +322,23 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 # DFO, so no gradient
                 return CostFunction(_Xx, self._parameters["QualityCriterion"])
             opt.set_min_objective(_f)
-            if Bounds is not None:
-                lub = numpy.array(Bounds).reshape((Xini.size,2))
-                lb = lub[:,0]
-                ub = lub[:,1]
-                if self.__disp:
-                    print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
-                    print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
+            if self._parameters["Bounds"] is not None:
+                lub = numpy.array(self._parameters["Bounds"],dtype=float).reshape((Xini.size,2))
+                lb = lub[:,0] ; lb[numpy.isnan(lb)] = -float('inf')
+                ub = lub[:,1] ; ub[numpy.isnan(ub)] = +float('inf')
+                if self._parameters["optdisp"]:
+                    print("%s: upper bounds %s"%(opt.get_algorithm_name(),ub))
+                    print("%s: lower bounds %s"%(opt.get_algorithm_name(),lb))
                 opt.set_upper_bounds(ub)
                 opt.set_lower_bounds(lb)
             opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
             opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
             opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
             Minimum = opt.optimize( Xini )
-            if self.__disp:
-                print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
-                print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
-                print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
+            if self._parameters["optdisp"]:
+                print("%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum))
+                print("%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value()))
+                print("%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result()))
         elif self._parameters["Minimizer"] == "SUBPLEX" and PlatformInfo.has_nlopt:
             import nlopt
             opt = nlopt.opt(nlopt.LN_SBPLX, Xini.size)
@@ -333,23 +346,23 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 # DFO, so no gradient
                 return CostFunction(_Xx, self._parameters["QualityCriterion"])
             opt.set_min_objective(_f)
-            if Bounds is not None:
-                lub = numpy.array(Bounds).reshape((Xini.size,2))
-                lb = lub[:,0]
-                ub = lub[:,1]
-                if self.__disp:
-                    print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
-                    print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
+            if self._parameters["Bounds"] is not None:
+                lub = numpy.array(self._parameters["Bounds"],dtype=float).reshape((Xini.size,2))
+                lb = lub[:,0] ; lb[numpy.isnan(lb)] = -float('inf')
+                ub = lub[:,1] ; ub[numpy.isnan(ub)] = +float('inf')
+                if self._parameters["optdisp"]:
+                    print("%s: upper bounds %s"%(opt.get_algorithm_name(),ub))
+                    print("%s: lower bounds %s"%(opt.get_algorithm_name(),lb))
                 opt.set_upper_bounds(ub)
                 opt.set_lower_bounds(lb)
             opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
             opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
             opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
             Minimum = opt.optimize( Xini )
-            if self.__disp:
-                print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
-                print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
-                print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
+            if self._parameters["optdisp"]:
+                print("%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum))
+                print("%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value()))
+                print("%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result()))
         else:
             raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
         #
@@ -390,4 +403,4 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
 
 # ==============================================================================
 if __name__ == "__main__":
-    print '\n AUTODIAGNOSTIC \n'
+    print('\n AUTODIAGNOSTIC \n')