1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2020 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, NumericObjects
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",
76 "CurrentIterationNumber",
81 "SimulatedObservationAtBackground",
82 "SimulatedObservationAtCurrentState",
83 "SimulatedObservationAtOptimum",
86 self.defineRequiredParameter( # Pas de type
88 message = "Liste des valeurs de bornes",
90 self.requireInputArguments(
91 mandatory= ("Xb", "Y", "HO" ),
93 self.setAttributes(tags=(
99 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
100 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
102 Hm = HO["Direct"].appliedTo
104 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
105 # ----------------------------------------------------
106 if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
107 HXb = Hm( Xb, HO["AppliedInX"]["HXb"])
110 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
112 # Calcul de l'innovation
113 # ----------------------
114 if Y.size != HXb.size:
115 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))
116 if max(Y.shape) != max(HXb.shape):
117 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))
120 # Définition de la fonction-coût
121 # ------------------------------
123 _X = numpy.asmatrix(numpy.ravel( x )).T
124 if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
125 self.StoredVariables["CurrentState"].store( _X )
127 _HX = numpy.asmatrix(numpy.ravel( _HX )).T
128 if self._toStore("SimulatedObservationAtCurrentState"):
129 self.StoredVariables["SimulatedObservationAtCurrentState"].store( _HX )
134 self.StoredVariables["CurrentIterationNumber"].store( len(self.StoredVariables["CostFunctionJ"]) )
135 self.StoredVariables["CostFunctionJb"].store( Jb )
136 self.StoredVariables["CostFunctionJo"].store( Jo )
137 self.StoredVariables["CostFunctionJ" ].store( J )
140 def GradientOfCostFunction(x):
141 _X = numpy.asmatrix(numpy.ravel( x )).T
142 Hg = HO["Tangent"].asMatrix( _X )
145 # Point de démarrage de l'optimisation : Xini = Xb
146 # ------------------------------------
147 if isinstance(Xb, type(numpy.matrix([]))):
148 Xini = Xb.A1.tolist()
152 # Minimisation de la fonctionnelle
153 # --------------------------------
154 if self._parameters["Minimizer"] == "MMQR":
155 Minimum, J_optimal, Informations = NumericObjects.mmqr(
158 fprime = GradientOfCostFunction,
159 bounds = self._parameters["Bounds"],
160 quantile = self._parameters["Quantile"],
161 maxfun = self._parameters["MaximumNumberOfSteps"],
162 toler = self._parameters["CostDecrementTolerance"],
165 nfeval = Informations[2]
168 raise ValueError("Error in Minimizer name: %s"%self._parameters["Minimizer"])
170 # Obtention de l'analyse
171 # ----------------------
172 Xa = numpy.asmatrix(numpy.ravel( Minimum )).T
174 self.StoredVariables["Analysis"].store( Xa.A1 )
176 if self._toStore("OMA") or \
177 self._toStore("SimulatedObservationAtOptimum"):
180 # Calculs et/ou stockages supplémentaires
181 # ---------------------------------------
182 if self._toStore("Innovation"):
183 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
184 if self._toStore("BMA"):
185 self.StoredVariables["BMA"].store( numpy.ravel(Xb - Xa) )
186 if self._toStore("OMA"):
187 self.StoredVariables["OMA"].store( numpy.ravel(Y - HXa) )
188 if self._toStore("OMB"):
189 self.StoredVariables["OMB"].store( numpy.ravel(d) )
190 if self._toStore("SimulatedObservationAtBackground"):
191 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
192 if self._toStore("SimulatedObservationAtOptimum"):
193 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
198 # ==============================================================================
199 if __name__ == "__main__":
200 print('\n AUTODIAGNOSTIC\n')