Salome HOME
Documentation update with features and review corrections
[modules/adao.git] / src / daComposant / daAlgorithms / NonLinearLeastSquares.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2024 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 numpy
24 from daCore import BasicObjects, NumericObjects
25 from daAlgorithms.Atoms import ecwnlls
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "NONLINEARLEASTSQUARES")
31         self.defineRequiredParameter(
32             name     = "Variant",
33             default  = "NonLinearLeastSquares",
34             typecast = str,
35             message  = "Variant ou formulation de la méthode",
36             listval  = [
37                 "NonLinearLeastSquares",
38             ],
39             listadv  = [
40                 "OneCorrection",
41             ],
42         )
43         self.defineRequiredParameter(
44             name     = "Minimizer",
45             default  = "LBFGSB",
46             typecast = str,
47             message  = "Minimiseur utilisé",
48             listval  = [
49                 "LBFGSB",
50                 "TNC",
51                 "CG",
52                 "BFGS",
53                 "LM",
54             ],
55             listadv  = [
56                 "NCG",
57             ],
58         )
59         self.defineRequiredParameter(
60             name     = "EstimationOf",
61             default  = "Parameters",
62             typecast = str,
63             message  = "Estimation d'état ou de paramètres",
64             listval  = ["State", "Parameters"],
65         )
66         self.defineRequiredParameter(
67             name     = "MaximumNumberOfIterations",
68             default  = 15000,
69             typecast = int,
70             message  = "Nombre maximal de pas d'optimisation",
71             minval   = -1,
72             oldname  = "MaximumNumberOfSteps",
73         )
74         self.defineRequiredParameter(
75             name     = "CostDecrementTolerance",
76             default  = 1.e-7,
77             typecast = float,
78             message  = "Diminution relative minimale du coût lors de l'arrêt",
79             minval   = 0.,
80         )
81         self.defineRequiredParameter(
82             name     = "ProjectedGradientTolerance",
83             default  = -1,
84             typecast = float,
85             message  = "Maximum des composantes du gradient projeté lors de l'arrêt",
86             minval   = -1,
87         )
88         self.defineRequiredParameter(
89             name     = "GradientNormTolerance",
90             default  = 1.e-05,
91             typecast = float,
92             message  = "Maximum des composantes du gradient lors de l'arrêt",
93             minval   = 0.,
94         )
95         self.defineRequiredParameter(
96             name     = "StoreInternalVariables",
97             default  = False,
98             typecast = bool,
99             message  = "Stockage des variables internes ou intermédiaires du calcul",
100         )
101         self.defineRequiredParameter(
102             name     = "StoreSupplementaryCalculations",
103             default  = [],
104             typecast = tuple,
105             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
106             listval  = [
107                 "Analysis",
108                 "BMA",
109                 "CostFunctionJ",
110                 "CostFunctionJAtCurrentOptimum",
111                 "CostFunctionJb",
112                 "CostFunctionJbAtCurrentOptimum",
113                 "CostFunctionJo",
114                 "CostFunctionJoAtCurrentOptimum",
115                 "CurrentIterationNumber",
116                 "CurrentOptimum",
117                 "CurrentState",
118                 "CurrentStepNumber",
119                 "ForecastState",
120                 "IndexOfOptimum",
121                 "Innovation",
122                 "InnovationAtCurrentAnalysis",
123                 "InnovationAtCurrentState",
124                 "OMA",
125                 "OMB",
126                 "SimulatedObservationAtBackground",
127                 "SimulatedObservationAtCurrentOptimum",
128                 "SimulatedObservationAtCurrentState",
129                 "SimulatedObservationAtOptimum",
130             ]
131         )
132         self.defineRequiredParameter(  # Pas de type
133             name     = "Bounds",
134             message  = "Liste des paires de bornes",
135         )
136         self.defineRequiredParameter(
137             name     = "InitializationPoint",
138             typecast = numpy.ravel,
139             message  = "État initial imposé (par défaut, c'est l'ébauche si None)",
140         )
141         self.requireInputArguments(
142             mandatory= ("Xb", "Y", "HO", "R"),
143             optional = ("U", "EM", "CM", "Q"),
144         )
145         self.setAttributes(
146             tags=(
147                 "Optimization",
148                 "NonLinear",
149                 "Variational",
150             ),
151             features=(
152                 "LocalOptimization",
153                 "DerivativeNeeded",
154                 "ParallelDerivativesOnly",
155             ),
156         )
157
158     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
159         self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
160         #
161         # --------------------------
162         if self._parameters["Variant"] == "NonLinearLeastSquares":
163             NumericObjects.multiXOsteps(self, Xb, Y, U, HO, EM, CM, R, B, Q, ecwnlls.ecwnlls)
164         #
165         # --------------------------
166         elif self._parameters["Variant"] == "OneCorrection":
167             ecwnlls.ecwnlls(self, Xb, Y, U, HO, CM, R, B)
168         #
169         # --------------------------
170         else:
171             raise ValueError("Error in Variant name: %s"%self._parameters["Variant"])
172         #
173         self._post_run(HO, EM)
174         return 0
175
176 # ==============================================================================
177 if __name__ == "__main__":
178     print("\n AUTODIAGNOSTIC\n")