1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2019 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",
80 "SimulatedObservationAtBackground",
81 "SimulatedObservationAtCurrentState",
82 "SimulatedObservationAtOptimum",
85 self.defineRequiredParameter( # Pas de type
87 message = "Liste des valeurs de bornes",
89 self.requireInputArguments(
90 mandatory= ("Xb", "Y", "HO" ),
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(Parameters, Xb, Y, R, B, Q)
96 Hm = HO["Direct"].appliedTo
98 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
99 # ----------------------------------------------------
100 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
101 HXb = Hm( Xb, HO["AppliedInX"]["HXb"])
104 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
106 # Calcul de l'innovation
107 # ----------------------
108 if Y.size != HXb.size:
109 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))
110 if max(Y.shape) != max(HXb.shape):
111 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))
114 # Définition de la fonction-coût
115 # ------------------------------
117 _X = numpy.asmatrix(numpy.ravel( x )).T
118 if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
119 self.StoredVariables["CurrentState"].store( _X )
121 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
122 if self._toStore("SimulatedObservationAtCurrentState"):
123 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
127 self.StoredVariables["CostFunctionJb"].store( Jb )
128 self.StoredVariables["CostFunctionJo"].store( Jo )
129 self.StoredVariables["CostFunctionJ" ].store( J )
132 def GradientOfCostFunction(x):
133 _X = numpy.asmatrix(numpy.ravel( x )).T
134 Hg = HO["Tangent"].asMatrix( _X )
137 # Point de démarrage de l'optimisation : Xini = Xb
138 # ------------------------------------
139 if isinstance(Xb, type(numpy.matrix([]))):
140 Xini = Xb.A1.tolist()
144 # Minimisation de la fonctionnelle
145 # --------------------------------
146 if self._parameters["Minimizer"] == "MMQR":
148 Minimum, J_optimal, Informations = mmqr.mmqr(
151 fprime = GradientOfCostFunction,
152 bounds = self._parameters["Bounds"],
153 quantile = self._parameters["Quantile"],
154 maxfun = self._parameters["MaximumNumberOfSteps"],
155 toler = self._parameters["CostDecrementTolerance"],
158 nfeval = Informations[2]
161 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
163 # Obtention de l'analyse
164 # ----------------------
165 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
167 self.StoredVariables["Analysis"].store( Xa.A1 )
169 if self._toStore("OMA") or \
170 self._toStore("SimulatedObservationAtOptimum"):
173 # Calculs et/ou stockages supplémentaires
174 # ---------------------------------------
175 if self._toStore("Innovation"):
176 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
177 if self._toStore("BMA"):
178 self.StoredVariables["BMA"].store( numpy.ravel(Xb - Xa) )
179 if self._toStore("OMA"):
180 self.StoredVariables["OMA"].store( numpy.ravel(Y - HXa) )
181 if self._toStore("OMB"):
182 self.StoredVariables["OMB"].store( numpy.ravel(d) )
183 if self._toStore("SimulatedObservationAtBackground"):
184 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
185 if self._toStore("SimulatedObservationAtOptimum"):
186 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
191 # ==============================================================================
192 if __name__ == "__main__":
193 print('\n AUTODIAGNOSTIC \n')