Salome HOME
2ae938dfd4d60f8651554d2b3f5ee218cfd5058d
[modules/adao.git] / src / daComposant / daAlgorithms / DerivativeFreeOptimization.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2016 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import logging
24 from daCore import BasicObjects
25 import numpy, scipy.optimize
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "DERIVATIVEFREEOPTIMIZATION")
31         self.defineRequiredParameter(
32             name     = "Minimizer",
33             default  = "POWELL",
34             typecast = str,
35             message  = "Minimiseur utilisé",
36             listval  = ["POWELL", "SIMPLEX", "COBYLA"],
37             )
38         self.defineRequiredParameter(
39             name     = "MaximumNumberOfSteps",
40             default  = 15000,
41             typecast = int,
42             message  = "Nombre maximal de pas d'optimisation",
43             minval   = -1,
44             )
45         self.defineRequiredParameter(
46             name     = "MaximumNumberOfFunctionEvaluations",
47             default  = 15000,
48             typecast = int,
49             message  = "Nombre maximal de d'évaluations de la fonction",
50             minval   = -1,
51             )
52         self.defineRequiredParameter(
53             name     = "StateVariationTolerance",
54             default  = 1.e-4,
55             typecast = float,
56             message  = "Variation relative maximale de l'état lors de l'arrêt",
57             )
58         self.defineRequiredParameter(
59             name     = "CostDecrementTolerance",
60             default  = 1.e-7,
61             typecast = float,
62             message  = "Diminution relative minimale du cout lors de l'arrêt",
63             )
64         self.defineRequiredParameter(
65             name     = "QualityCriterion",
66             default  = "AugmentedWeightedLeastSquares",
67             typecast = str,
68             message  = "Critère de qualité utilisé",
69             listval  = ["AugmentedWeightedLeastSquares","AWLS","DA",
70                         "WeightedLeastSquares","WLS",
71                         "LeastSquares","LS","L2",
72                         "AbsoluteValue","L1",
73                         "MaximumError","ME"],
74             )
75         self.defineRequiredParameter(
76             name     = "StoreInternalVariables",
77             default  = False,
78             typecast = bool,
79             message  = "Stockage des variables internes ou intermédiaires du calcul",
80             )
81         self.defineRequiredParameter(
82             name     = "StoreSupplementaryCalculations",
83             default  = [],
84             typecast = tuple,
85             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
86             listval  = ["CurrentState", "CostFunctionJ", "CostFunctionJb", "CostFunctionJo", "CostFunctionJAtCurrentOptimum", "CurrentOptimum", "IndexOfOptimum", "InnovationAtCurrentState", "BMA", "OMA", "OMB", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentOptimum", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
87             )
88         self.defineRequiredParameter( # Pas de type
89             name     = "Bounds",
90             message  = "Liste des valeurs de bornes",
91             )
92
93     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
94         self._pre_run()
95         if logging.getLogger().level < logging.WARNING:
96             self.__disp = 1
97         else:
98             self.__disp = 0
99         #
100         # Paramètres de pilotage
101         # ----------------------
102         self.setParameters(Parameters)
103         #
104         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):
105             Bounds = self._parameters["Bounds"]
106             logging.debug("%s Prise en compte des bornes effectuee"%(self._name,))
107         else:
108             Bounds = None
109         #
110         # Opérateurs
111         # ----------
112         Hm = HO["Direct"].appliedTo
113         #
114         # Précalcul des inversions de B et R
115         # ----------------------------------
116         BI = B.getI()
117         RI = R.getI()
118         #
119         # Définition de la fonction-coût
120         # ------------------------------
121         def CostFunction(x, QualityMeasure="AugmentedWeightedLeastSquares"):
122             _X  = numpy.asmatrix(numpy.ravel( x )).T
123             self.StoredVariables["CurrentState"].store( _X )
124             _HX = Hm( _X )
125             _HX = numpy.asmatrix(numpy.ravel( _HX )).T
126             _Innovation = Y - _HX
127             if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"] or \
128                "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
129                 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
130             if "InnovationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
131                 self.StoredVariables["InnovationAtCurrentState"].store( _Innovation )
132             #
133             if QualityMeasure in ["AugmentedWeightedLeastSquares","AWLS","DA"]:
134                 if BI is None or RI is None:
135                     raise ValueError("Background and Observation error covariance matrix has to be properly defined!")
136                 Jb  = 0.5 * (_X - Xb).T * BI * (_X - Xb)
137                 Jo  = 0.5 * (_Innovation).T * RI * (_Innovation)
138             elif QualityMeasure in ["WeightedLeastSquares","WLS"]:
139                 if RI is None:
140                     raise ValueError("Observation error covariance matrix has to be properly defined!")
141                 Jb  = 0.
142                 Jo  = 0.5 * (_Innovation).T * RI * (_Innovation)
143             elif QualityMeasure in ["LeastSquares","LS","L2"]:
144                 Jb  = 0.
145                 Jo  = 0.5 * (_Innovation).T * (_Innovation)
146             elif QualityMeasure in ["AbsoluteValue","L1"]:
147                 Jb  = 0.
148                 Jo  = numpy.sum( numpy.abs(_Innovation) )
149             elif QualityMeasure in ["MaximumError","ME"]:
150                 Jb  = 0.
151                 Jo  = numpy.max( numpy.abs(_Innovation) )
152             #
153             J   = float( Jb ) + float( Jo )
154             #
155             self.StoredVariables["CostFunctionJb"].store( Jb )
156             self.StoredVariables["CostFunctionJo"].store( Jo )
157             self.StoredVariables["CostFunctionJ" ].store( J )
158             if "IndexOfOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
159                "CurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
160                "CostFunctionJAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"] or \
161                "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
162                 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
163             if "IndexOfOptimum" in self._parameters["StoreSupplementaryCalculations"]:
164                 self.StoredVariables["IndexOfOptimum"].store( IndexMin )
165             if "CurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
166                 self.StoredVariables["CurrentOptimum"].store( self.StoredVariables["CurrentState"][IndexMin] )
167             if "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
168                 self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin] )
169             if "CostFunctionJAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
170                 self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJb"][IndexMin] )
171                 self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( self.StoredVariables["CostFunctionJo"][IndexMin] )
172                 self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( self.StoredVariables["CostFunctionJ" ][IndexMin] )
173             return J
174         #
175         # Point de démarrage de l'optimisation : Xini = Xb
176         # ------------------------------------
177         Xini = numpy.ravel(Xb)
178         #
179         # Minimisation de la fonctionnelle
180         # --------------------------------
181         nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
182         #
183         if self._parameters["Minimizer"] == "POWELL":
184             Minimum, J_optimal, direc, niter, nfeval, rc = scipy.optimize.fmin_powell(
185                 func        = CostFunction,
186                 x0          = Xini,
187                 args        = (self._parameters["QualityCriterion"],),
188                 maxiter     = self._parameters["MaximumNumberOfSteps"]-1,
189                 maxfun      = self._parameters["MaximumNumberOfFunctionEvaluations"],
190                 xtol        = self._parameters["StateVariationTolerance"],
191                 ftol        = self._parameters["CostDecrementTolerance"],
192                 full_output = True,
193                 disp        = self.__disp,
194                 )
195         elif self._parameters["Minimizer"] == "SIMPLEX":
196             Minimum, J_optimal, niter, nfeval, rc = scipy.optimize.fmin(
197                 func        = CostFunction,
198                 x0          = Xini,
199                 args        = (self._parameters["QualityCriterion"],),
200                 maxiter     = self._parameters["MaximumNumberOfSteps"]-1,
201                 maxfun      = self._parameters["MaximumNumberOfFunctionEvaluations"],
202                 xtol        = self._parameters["StateVariationTolerance"],
203                 ftol        = self._parameters["CostDecrementTolerance"],
204                 full_output = True,
205                 disp        = self.__disp,
206                 )
207         elif self._parameters["Minimizer"] == "COBYLA":
208             def make_constraints(bounds):
209                 constraints = []
210                 for (i,(a,b)) in enumerate(bounds):
211                     lower = lambda x: x[i] - a
212                     upper = lambda x: b - x[i]
213                     constraints = constraints + [lower] + [upper]
214                 return constraints
215             if Bounds is None:
216                 raise ValueError("Bounds have to be given for all axes as a list of lower/upper pairs!")
217             Minimum = scipy.optimize.fmin_cobyla(
218                 func        = CostFunction,
219                 x0          = Xini,
220                 cons        = make_constraints( Bounds ),
221                 args        = (self._parameters["QualityCriterion"],),
222                 consargs    = (), # To avoid extra-args
223                 maxfun      = self._parameters["MaximumNumberOfFunctionEvaluations"],
224                 rhobeg      = 1.0,
225                 rhoend      = self._parameters["StateVariationTolerance"],
226                 catol       = 2.*self._parameters["StateVariationTolerance"],
227                 disp        = self.__disp,
228                 )
229         else:
230             raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
231         #
232         IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
233         MinJ     = self.StoredVariables["CostFunctionJ"][IndexMin]
234         Minimum  = self.StoredVariables["CurrentState"][IndexMin]
235         #
236         # Obtention de l'analyse
237         # ----------------------
238         Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
239         #
240         self.StoredVariables["Analysis"].store( Xa.A1 )
241         #
242         if "OMA"                           in self._parameters["StoreSupplementaryCalculations"] or \
243            "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
244             if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
245                 HXa = self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
246             elif "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
247                 HXa = self.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
248             else:
249                 HXa = Hm(Xa)
250         #
251         if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
252             self.StoredVariables["Innovation"].store( numpy.ravel(d) )
253         if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
254             self.StoredVariables["OMB"].store( numpy.ravel(d) )
255         if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
256             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
257         if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
258             self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
259         if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
260             self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(Hm(Xb)) )
261         if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
262             self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
263         #
264         self._post_run()
265         return 0
266
267 # ==============================================================================
268 if __name__ == "__main__":
269     print '\n AUTODIAGNOSTIC \n'