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
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
30 BasicObjects.Algorithm.__init__(self, "QUANTILEREGRESSION")
31 self.defineRequiredParameter(
35 message = "Quantile pour la regression de quantile",
39 self.defineRequiredParameter(
43 message = "Minimiseur utilisé",
46 self.defineRequiredParameter(
47 name = "MaximumNumberOfSteps",
50 message = "Nombre maximal de pas d'optimisation",
53 self.defineRequiredParameter(
54 name = "CostDecrementTolerance",
57 message = "Maximum de variation de la fonction d'estimation lors de l'arrêt",
59 self.defineRequiredParameter(
60 name = "StoreInternalVariables",
63 message = "Stockage des variables internes ou intermédiaires du calcul",
65 self.defineRequiredParameter(
66 name = "StoreSupplementaryCalculations",
69 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
70 listval = ["BMA", "OMA", "OMB", "CurrentState", "CostFunctionJ", "CostFunctionJb", "CostFunctionJo", "Innovation", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
72 self.defineRequiredParameter( # Pas de type
74 message = "Liste des valeurs de bornes",
77 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
78 self._pre_run(Parameters)
80 Hm = HO["Direct"].appliedTo
82 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
83 # ----------------------------------------------------
84 if HO["AppliedToX"] is not None and HO["AppliedToX"].has_key("HXb"):
85 HXb = Hm( Xb, HO["AppliedToX"]["HXb"])
88 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
90 # Calcul de l'innovation
91 # ----------------------
92 if Y.size != HXb.size:
93 raise ValueError("The size %i of observations Y and %i of observed calculation H(X) are different, they have to be identical."%(Y.size,HXb.size))
94 if max(Y.shape) != max(HXb.shape):
95 raise ValueError("The shapes %s of observations Y and %s of observed calculation H(X) are different, they have to be identical."%(Y.shape,HXb.shape))
98 # Définition de la fonction-coût
99 # ------------------------------
101 _X = numpy.asmatrix(numpy.ravel( x )).T
102 if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
103 self.StoredVariables["CurrentState"].store( _X )
105 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
106 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
107 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
111 self.StoredVariables["CostFunctionJb"].store( Jb )
112 self.StoredVariables["CostFunctionJo"].store( Jo )
113 self.StoredVariables["CostFunctionJ" ].store( J )
116 def GradientOfCostFunction(x):
117 _X = numpy.asmatrix(numpy.ravel( x )).T
118 Hg = HO["Tangent"].asMatrix( _X )
121 # Point de démarrage de l'optimisation : Xini = Xb
122 # ------------------------------------
123 if type(Xb) is type(numpy.matrix([])):
124 Xini = Xb.A1.tolist()
128 # Minimisation de la fonctionnelle
129 # --------------------------------
130 if self._parameters["Minimizer"] == "MMQR":
132 Minimum, J_optimal, Informations = mmqr.mmqr(
135 fprime = GradientOfCostFunction,
136 bounds = self._parameters["Bounds"],
137 quantile = self._parameters["Quantile"],
138 maxfun = self._parameters["MaximumNumberOfSteps"],
139 toler = self._parameters["CostDecrementTolerance"],
142 nfeval = Informations[2]
145 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
147 # Obtention de l'analyse
148 # ----------------------
149 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
151 self.StoredVariables["Analysis"].store( Xa.A1 )
153 if "OMA" in self._parameters["StoreSupplementaryCalculations"] or \
154 "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
157 # Calculs et/ou stockages supplémentaires
158 # ---------------------------------------
159 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
160 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
161 if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
162 self.StoredVariables["BMA"].store( numpy.ravel(Xb - Xa) )
163 if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
164 self.StoredVariables["OMA"].store( numpy.ravel(Y - HXa) )
165 if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
166 self.StoredVariables["OMB"].store( numpy.ravel(d) )
167 if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
168 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
169 if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
170 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
175 # ==============================================================================
176 if __name__ == "__main__":
177 print '\n AUTODIAGNOSTIC \n'