1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2017 EDF R&D
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 from daCore import BasicObjects, PlatformInfo
25 import numpy, scipy.optimize
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
30 BasicObjects.Algorithm.__init__(self, "DERIVATIVEFREEOPTIMIZATION")
31 self.defineRequiredParameter(
35 message = "Minimiseur utilisé",
36 listval = ["BOBYQA", "COBYLA", "NEWUOA", "POWELL", "SIMPLEX", "SUBPLEX"],
38 self.defineRequiredParameter(
39 name = "MaximumNumberOfSteps",
42 message = "Nombre maximal de pas d'optimisation",
45 self.defineRequiredParameter(
46 name = "MaximumNumberOfFunctionEvaluations",
49 message = "Nombre maximal de d'évaluations de la fonction",
52 self.defineRequiredParameter(
53 name = "StateVariationTolerance",
56 message = "Variation relative maximale de l'état lors de l'arrêt",
58 self.defineRequiredParameter(
59 name = "CostDecrementTolerance",
62 message = "Diminution relative minimale du cout lors de l'arrêt",
64 self.defineRequiredParameter(
65 name = "QualityCriterion",
66 default = "AugmentedWeightedLeastSquares",
68 message = "Critère de qualité utilisé",
69 listval = ["AugmentedWeightedLeastSquares","AWLS","DA",
70 "WeightedLeastSquares","WLS",
71 "LeastSquares","LS","L2",
75 self.defineRequiredParameter(
76 name = "StoreInternalVariables",
79 message = "Stockage des variables internes ou intermédiaires du calcul",
81 self.defineRequiredParameter(
82 name = "StoreSupplementaryCalculations",
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"]
88 self.defineRequiredParameter( # Pas de type
90 message = "Liste des valeurs de bornes",
93 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
95 if logging.getLogger().level < logging.WARNING:
100 # Paramètres de pilotage
101 # ----------------------
102 self.setParameters(Parameters)
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,))
112 Hm = HO["Direct"].appliedTo
114 # Précalcul des inversions de B et R
115 # ----------------------------------
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 )
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 )
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"]:
140 raise ValueError("Observation error covariance matrix has to be properly defined!")
142 Jo = 0.5 * (_Innovation).T * RI * (_Innovation)
143 elif QualityMeasure in ["LeastSquares","LS","L2"]:
145 Jo = 0.5 * (_Innovation).T * (_Innovation)
146 elif QualityMeasure in ["AbsoluteValue","L1"]:
148 Jo = numpy.sum( numpy.abs(_Innovation) )
149 elif QualityMeasure in ["MaximumError","ME"]:
151 Jo = numpy.max( numpy.abs(_Innovation) )
153 J = float( Jb ) + float( Jo )
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] )
175 # Point de démarrage de l'optimisation : Xini = Xb
176 # ------------------------------------
177 Xini = numpy.ravel(Xb)
179 # Minimisation de la fonctionnelle
180 # --------------------------------
181 nbPreviousSteps = self.StoredVariables["CostFunctionJ"].stepnumber()
183 if self._parameters["Minimizer"] == "POWELL":
184 Minimum, J_optimal, direc, niter, nfeval, rc = scipy.optimize.fmin_powell(
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"],
195 elif self._parameters["Minimizer"] == "COBYLA" and not PlatformInfo.has_nlopt:
196 def make_constraints(bounds):
198 for (i,(a,b)) in enumerate(bounds):
199 lower = lambda x: x[i] - a
200 upper = lambda x: b - x[i]
201 constraints = constraints + [lower] + [upper]
204 raise ValueError("Bounds have to be given for all axes as a list of lower/upper pairs!")
205 Minimum = scipy.optimize.fmin_cobyla(
208 cons = make_constraints( Bounds ),
209 args = (self._parameters["QualityCriterion"],),
210 consargs = (), # To avoid extra-args
211 maxfun = self._parameters["MaximumNumberOfFunctionEvaluations"],
213 rhoend = self._parameters["StateVariationTolerance"],
214 catol = 2.*self._parameters["StateVariationTolerance"],
217 elif self._parameters["Minimizer"] == "COBYLA" and PlatformInfo.has_nlopt:
219 opt = nlopt.opt(nlopt.LN_COBYLA, Xini.size)
221 # DFO, so no gradient
222 return CostFunction(_Xx, self._parameters["QualityCriterion"])
223 opt.set_min_objective(_f)
224 if Bounds is not None:
225 lub = numpy.array(Bounds).reshape((Xini.size,2))
229 print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
230 print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
231 opt.set_upper_bounds(ub)
232 opt.set_lower_bounds(lb)
233 opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
234 opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
235 opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
236 Minimum = opt.optimize( Xini )
238 print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
239 print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
240 print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
241 elif self._parameters["Minimizer"] == "SIMPLEX" and not PlatformInfo.has_nlopt:
242 Minimum, J_optimal, niter, nfeval, rc = scipy.optimize.fmin(
245 args = (self._parameters["QualityCriterion"],),
246 maxiter = self._parameters["MaximumNumberOfSteps"]-1,
247 maxfun = self._parameters["MaximumNumberOfFunctionEvaluations"],
248 xtol = self._parameters["StateVariationTolerance"],
249 ftol = self._parameters["CostDecrementTolerance"],
253 elif self._parameters["Minimizer"] == "SIMPLEX" and PlatformInfo.has_nlopt:
255 opt = nlopt.opt(nlopt.LN_NELDERMEAD, Xini.size)
257 # DFO, so no gradient
258 return CostFunction(_Xx, self._parameters["QualityCriterion"])
259 opt.set_min_objective(_f)
260 if Bounds is not None:
261 lub = numpy.array(Bounds).reshape((Xini.size,2))
265 print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
266 print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
267 opt.set_upper_bounds(ub)
268 opt.set_lower_bounds(lb)
269 opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
270 opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
271 opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
272 Minimum = opt.optimize( Xini )
274 print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
275 print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
276 print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
277 elif self._parameters["Minimizer"] == "BOBYQA" and PlatformInfo.has_nlopt:
279 opt = nlopt.opt(nlopt.LN_BOBYQA, Xini.size)
281 # DFO, so no gradient
282 return CostFunction(_Xx, self._parameters["QualityCriterion"])
283 opt.set_min_objective(_f)
284 if Bounds is not None:
285 lub = numpy.array(Bounds).reshape((Xini.size,2))
289 print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
290 print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
291 opt.set_upper_bounds(ub)
292 opt.set_lower_bounds(lb)
293 opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
294 opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
295 opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
296 Minimum = opt.optimize( Xini )
298 print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
299 print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
300 print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
301 elif self._parameters["Minimizer"] == "NEWUOA" and PlatformInfo.has_nlopt:
303 opt = nlopt.opt(nlopt.LN_NEWUOA, Xini.size)
305 # DFO, so no gradient
306 return CostFunction(_Xx, self._parameters["QualityCriterion"])
307 opt.set_min_objective(_f)
308 if Bounds is not None:
309 lub = numpy.array(Bounds).reshape((Xini.size,2))
313 print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
314 print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
315 opt.set_upper_bounds(ub)
316 opt.set_lower_bounds(lb)
317 opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
318 opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
319 opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
320 Minimum = opt.optimize( Xini )
322 print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
323 print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
324 print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
325 elif self._parameters["Minimizer"] == "SUBPLEX" and PlatformInfo.has_nlopt:
327 opt = nlopt.opt(nlopt.LN_SBPLX, Xini.size)
329 # DFO, so no gradient
330 return CostFunction(_Xx, self._parameters["QualityCriterion"])
331 opt.set_min_objective(_f)
332 if Bounds is not None:
333 lub = numpy.array(Bounds).reshape((Xini.size,2))
337 print "%s: upper bounds %s"%(opt.get_algorithm_name(),ub)
338 print "%s: lower bounds %s"%(opt.get_algorithm_name(),lb)
339 opt.set_upper_bounds(ub)
340 opt.set_lower_bounds(lb)
341 opt.set_ftol_rel(self._parameters["CostDecrementTolerance"])
342 opt.set_xtol_rel(2.*self._parameters["StateVariationTolerance"])
343 opt.set_maxeval(self._parameters["MaximumNumberOfFunctionEvaluations"])
344 Minimum = opt.optimize( Xini )
346 print "%s: optimal state: %s"%(opt.get_algorithm_name(),Minimum)
347 print "%s: minimum of J: %s"%(opt.get_algorithm_name(),opt.last_optimum_value())
348 print "%s: return code: %i"%(opt.get_algorithm_name(),opt.last_optimize_result())
350 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
352 IndexMin = numpy.argmin( self.StoredVariables["CostFunctionJ"][nbPreviousSteps:] ) + nbPreviousSteps
353 MinJ = self.StoredVariables["CostFunctionJ"][IndexMin]
354 Minimum = self.StoredVariables["CurrentState"][IndexMin]
356 # Obtention de l'analyse
357 # ----------------------
358 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
360 self.StoredVariables["Analysis"].store( Xa.A1 )
362 if "OMA" in self._parameters["StoreSupplementaryCalculations"] or \
363 "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
364 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
365 HXa = self.StoredVariables["SimulatedObservationAtCurrentState"][IndexMin]
366 elif "SimulatedObservationAtCurrentOptimum" in self._parameters["StoreSupplementaryCalculations"]:
367 HXa = self.StoredVariables["SimulatedObservationAtCurrentOptimum"][-1]
371 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
372 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
373 if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
374 self.StoredVariables["OMB"].store( numpy.ravel(d) )
375 if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
376 self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
377 if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
378 self.StoredVariables["OMA"].store( numpy.ravel(Y) - numpy.ravel(HXa) )
379 if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
380 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(Hm(Xb)) )
381 if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
382 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
387 # ==============================================================================
388 if __name__ == "__main__":
389 print '\n AUTODIAGNOSTIC \n'