1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2022 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
22 "Verification de la disponibilite de l'ensemble des algorithmes"
27 from adao import adaoBuilder
29 # ==============================================================================
30 class Test_Adao(unittest.TestCase):
32 """Verification de la disponibilite de l'ensemble des algorithmes\n(Utilisation d'un operateur matriciel)"""
33 print(self.test1.__doc__.strip()+"\n")
35 for algo in ("3DVAR", "Blue", "ExtendedBlue", "LinearLeastSquares", "NonLinearLeastSquares", "DerivativeFreeOptimization"):
37 msg = "Algorithme en test : %s"%algo
38 print(msg+"\n"+"-"*len(msg))
40 adaopy = adaoBuilder.New()
41 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "Bounds":[[-1,10.],[-1,10.],[-1,10.]]})
42 adaopy.setBackground (Vector = [0,1,2])
43 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
44 adaopy.setObservation (Vector = [0.5,1.5,2.5])
45 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
46 adaopy.setObservationOperator(Matrix = "1 0 0;0 2 0;0 0 3")
47 adaopy.setObserver("Analysis",Template="ValuePrinter")
49 Xa[algo] = adaopy.get("Analysis")[-1]
52 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "UnscentedKalmanFilter", "EnsembleKalmanFilter", "4DVAR"):
54 msg = "Algorithme en test : %s"%algo
55 print(msg+"\n"+"-"*len(msg))
57 adaopy = adaoBuilder.New()
58 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "SetSeed":1000})
59 adaopy.setBackground (Vector = [0,1,2])
60 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
61 adaopy.setObservation (Vector = [0.5,1.5,2.5])
62 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
63 adaopy.setObservationOperator(Matrix = "1 0 0;0 2 0;0 0 3")
64 adaopy.setEvolutionError (ScalarSparseMatrix = 1.)
65 adaopy.setEvolutionModel (Matrix = "1 0 0;0 1 0;0 0 1")
66 adaopy.setObserver("Analysis",Template="ValuePrinter")
68 Xa[algo] = adaopy.get("Analysis")[-1]
71 for algo in ("ParticleSwarmOptimization", "QuantileRegression", ):
73 msg = "Algorithme en test : %s"%algo
74 print(msg+"\n"+"-"*len(msg))
76 adaopy = adaoBuilder.New()
77 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"BoxBounds":3*[[-1,3]], "SetSeed":1000})
78 adaopy.setBackground (Vector = [0,1,2])
79 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
80 adaopy.setObservation (Vector = [0.5,1.5,2.5])
81 adaopy.setObservationError (DiagonalSparseMatrix = "1 2 3")
82 adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
83 adaopy.setObserver("Analysis",Template="ValuePrinter")
85 Xa[algo] = adaopy.get("Analysis")[-1]
88 for algo in ("EnsembleBlue", ):
90 msg = "Algorithme en test : %s"%algo
91 print(msg+"\n"+"-"*len(msg))
93 adaopy = adaoBuilder.New()
94 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"SetSeed":1000, })
95 adaopy.setBackground (VectorSerie = 100*[[0,1,2]])
96 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
97 adaopy.setObservation (Vector = [0.5,1.5,2.5])
98 adaopy.setObservationError (DiagonalSparseMatrix = "1 2 3")
99 adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
100 adaopy.setObserver("Analysis",Template="ValuePrinter")
102 Xa[algo] = adaopy.get("Analysis")[-1]
106 msg = "Tests des ecarts attendus :"
107 print(msg+"\n"+"="*len(msg))
108 verify_similarity_of_algo_results(("3DVAR", "Blue", "ExtendedBlue", "4DVAR", "DerivativeFreeOptimization"), Xa, 5.e-5)
109 verify_similarity_of_algo_results(("LinearLeastSquares", "NonLinearLeastSquares"), Xa, 5.e-7)
110 verify_similarity_of_algo_results(("KalmanFilter", "ExtendedKalmanFilter", "UnscentedKalmanFilter"), Xa, 1.e-14)
111 verify_similarity_of_algo_results(("KalmanFilter", "EnsembleKalmanFilter"), Xa, 2.e-1)
112 print(" Les resultats obtenus sont corrects.")
118 """Verification de la disponibilite de l'ensemble des algorithmes\n(Utilisation d'un operateur fonctionnel)"""
119 print(self.test2.__doc__)
121 M = numpy.diag([1.,2.,3.])
122 def H(x): return M @ numpy.ravel( x )
123 for algo in ("3DVAR", "Blue", "ExtendedBlue", "NonLinearLeastSquares", "DerivativeFreeOptimization"):
125 msg = "Algorithme en test : %s"%algo
126 print(msg+"\n"+"-"*len(msg))
128 adaopy = adaoBuilder.New()
129 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "Bounds":[[-1,10.],[-1,10.],[-1,10.]]})
130 adaopy.setBackground (Vector = [0,1,2])
131 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
132 adaopy.setObservation (Vector = [0.5,1.5,2.5])
133 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
134 adaopy.setObservationOperator(OneFunction = H)
135 adaopy.setObserver("Analysis",Template="ValuePrinter")
137 Xa[algo] = adaopy.get("Analysis")[-1]
140 M = numpy.diag([1.,2.,3.])
141 def H(x): return M @ numpy.ravel( x )
142 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "EnsembleKalmanFilter", "UnscentedKalmanFilter", "4DVAR"):
144 msg = "Algorithme en test : %s"%algo
145 print(msg+"\n"+"-"*len(msg))
147 adaopy = adaoBuilder.New()
148 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "SetSeed":1000})
149 adaopy.setBackground (Vector = [0,1,2])
150 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
151 adaopy.setObservation (Vector = [0.5,1.5,2.5])
152 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
153 adaopy.setObservationOperator(OneFunction = H)
154 adaopy.setEvolutionError (ScalarSparseMatrix = 1.)
155 adaopy.setEvolutionModel (Matrix = "1 0 0;0 1 0;0 0 1")
156 adaopy.setObserver("Analysis",Template="ValuePrinter")
158 Xa[algo] = adaopy.get("Analysis")[-1]
161 M = numpy.identity(3)
162 def H(x): return M @ numpy.ravel( x )
163 for algo in ("ParticleSwarmOptimization", "QuantileRegression", ):
165 msg = "Algorithme en test : %s"%algo
166 print(msg+"\n"+"-"*len(msg))
168 adaopy = adaoBuilder.New()
169 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"BoxBounds":3*[[-1,3]], "SetSeed":1000})
170 adaopy.setBackground (Vector = [0,1,2])
171 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
172 adaopy.setObservation (Vector = [0.5,1.5,2.5])
173 adaopy.setObservationError (DiagonalSparseMatrix = "1 2 3")
174 adaopy.setObservationOperator(OneFunction = H)
175 adaopy.setObserver("Analysis",Template="ValuePrinter")
177 Xa[algo] = adaopy.get("Analysis")[-1]
181 msg = "Tests des ecarts attendus :"
182 print(msg+"\n"+"="*len(msg))
183 verify_similarity_of_algo_results(("3DVAR", "Blue", "ExtendedBlue", "4DVAR", "DerivativeFreeOptimization"), Xa, 5.e-5)
184 verify_similarity_of_algo_results(("KalmanFilter", "ExtendedKalmanFilter", "UnscentedKalmanFilter"), Xa, 2.e-14)
185 verify_similarity_of_algo_results(("KalmanFilter", "EnsembleKalmanFilter"), Xa, 2e-1)
186 print(" Les resultats obtenus sont corrects.")
191 def almost_equal_vectors(v1, v2, precision = 1.e-15, msg = ""):
192 """Comparaison de deux vecteurs"""
193 print(" Difference maximale %s: %.2e"%(msg, max(abs(v2 - v1))))
194 return max(abs(v2 - v1)) < precision
196 def verify_similarity_of_algo_results(serie = [], Xa = {}, precision = 1.e-15):
197 print(" Comparaisons :")
200 if algo1 is algo2: break
201 assert almost_equal_vectors( Xa[algo1], Xa[algo2], precision, "entre %s et %s "%(algo1, algo2) )
202 print(" Algorithmes dont les resultats sont similaires a %.0e : %s\n"%(precision, serie,))
205 #===============================================================================
206 if __name__ == "__main__":
207 print("\nAUTODIAGNOSTIC\n==============")
208 sys.stderr = sys.stdout
209 unittest.main(verbosity=2)