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