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
22 "Verification de la disponibilite de l'ensemble des algorithmes"
24 # ==============================================================================
26 from adao import adaoBuilder
28 """Verification de la disponibilite de l'ensemble des algorithmes\n(Utilisation d'un operateur matriciel)"""
31 for algo in ("3DVAR", "Blue", "ExtendedBlue", "LinearLeastSquares", "NonLinearLeastSquares", "DerivativeFreeOptimization"):
33 msg = "Algorithme en test : %s"%algo
34 print(msg+"\n"+"-"*len(msg))
36 adaopy = adaoBuilder.New()
37 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "Bounds":[[-1,10.],[-1,10.],[-1,10.]]})
38 adaopy.setBackground (Vector = [0,1,2])
39 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
40 adaopy.setObservation (Vector = [0.5,1.5,2.5])
41 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
42 adaopy.setObservationOperator(Matrix = "1 0 0;0 2 0;0 0 3")
43 adaopy.setObserver("Analysis",Template="ValuePrinter")
45 Xa[algo] = adaopy.get("Analysis")[-1]
48 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "UnscentedKalmanFilter", "EnsembleKalmanFilter", "4DVAR"):
50 msg = "Algorithme en test : %s"%algo
51 print(msg+"\n"+"-"*len(msg))
53 adaopy = adaoBuilder.New()
54 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "SetSeed":1000})
55 adaopy.setBackground (Vector = [0,1,2])
56 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
57 adaopy.setObservation (Vector = [0.5,1.5,2.5])
58 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
59 adaopy.setObservationOperator(Matrix = "1 0 0;0 2 0;0 0 3")
60 adaopy.setEvolutionError (ScalarSparseMatrix = 1.)
61 adaopy.setEvolutionModel (Matrix = "1 0 0;0 1 0;0 0 1")
62 adaopy.setObserver("Analysis",Template="ValuePrinter")
64 Xa[algo] = adaopy.get("Analysis")[-1]
67 for algo in ("ParticleSwarmOptimization", "QuantileRegression", ):
69 msg = "Algorithme en test : %s"%algo
70 print(msg+"\n"+"-"*len(msg))
72 adaopy = adaoBuilder.New()
73 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"BoxBounds":3*[[-1,3]], "SetSeed":1000})
74 adaopy.setBackground (Vector = [0,1,2])
75 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
76 adaopy.setObservation (Vector = [0.5,1.5,2.5])
77 adaopy.setObservationError (DiagonalSparseMatrix = "1 2 3")
78 adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
79 adaopy.setObserver("Analysis",Template="ValuePrinter")
81 Xa[algo] = adaopy.get("Analysis")[-1]
84 for algo in ("EnsembleBlue", ):
86 msg = "Algorithme en test : %s"%algo
87 print(msg+"\n"+"-"*len(msg))
89 adaopy = adaoBuilder.New()
90 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"SetSeed":1000, })
91 adaopy.setBackground (VectorSerie = 100*[[0,1,2]])
92 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
93 adaopy.setObservation (Vector = [0.5,1.5,2.5])
94 adaopy.setObservationError (DiagonalSparseMatrix = "1 2 3")
95 adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
96 adaopy.setObserver("Analysis",Template="ValuePrinter")
98 Xa[algo] = adaopy.get("Analysis")[-1]
102 msg = "Tests des ecarts attendus :"
103 print(msg+"\n"+"="*len(msg))
104 verify_similarity_of_algo_results(("3DVAR", "Blue", "ExtendedBlue", "4DVAR", "DerivativeFreeOptimization"), Xa, 5.e-5)
105 verify_similarity_of_algo_results(("LinearLeastSquares", "NonLinearLeastSquares"), Xa, 5.e-7)
106 verify_similarity_of_algo_results(("KalmanFilter", "ExtendedKalmanFilter", "UnscentedKalmanFilter"), Xa, 1.e-14)
107 verify_similarity_of_algo_results(("KalmanFilter", "EnsembleKalmanFilter"), Xa, 5.e-2)
108 print(" Les resultats obtenus sont corrects.")
114 """Verification de la disponibilite de l'ensemble des algorithmes\n(Utilisation d'un operateur fonctionnel)"""
117 M = numpy.matrix("1 0 0;0 2 0;0 0 3")
118 def H(x): return M * numpy.asmatrix(numpy.ravel( x )).T
119 for algo in ("3DVAR", "Blue", "ExtendedBlue", "NonLinearLeastSquares", "DerivativeFreeOptimization"):
121 msg = "Algorithme en test : %s"%algo
122 print(msg+"\n"+"-"*len(msg))
124 adaopy = adaoBuilder.New()
125 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "Bounds":[[-1,10.],[-1,10.],[-1,10.]]})
126 adaopy.setBackground (Vector = [0,1,2])
127 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
128 adaopy.setObservation (Vector = [0.5,1.5,2.5])
129 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
130 adaopy.setObservationOperator(OneFunction = H)
131 adaopy.setObserver("Analysis",Template="ValuePrinter")
133 Xa[algo] = adaopy.get("Analysis")[-1]
136 M = numpy.matrix("1 0 0;0 2 0;0 0 3")
137 def H(x): return M * numpy.asmatrix(numpy.ravel( x )).T
138 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "EnsembleKalmanFilter", "UnscentedKalmanFilter", "4DVAR"):
140 msg = "Algorithme en test : %s"%algo
141 print(msg+"\n"+"-"*len(msg))
143 adaopy = adaoBuilder.New()
144 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "SetSeed":1000})
145 adaopy.setBackground (Vector = [0,1,2])
146 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
147 adaopy.setObservation (Vector = [0.5,1.5,2.5])
148 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
149 adaopy.setObservationOperator(OneFunction = H)
150 adaopy.setEvolutionError (ScalarSparseMatrix = 1.)
151 adaopy.setEvolutionModel (Matrix = "1 0 0;0 1 0;0 0 1")
152 adaopy.setObserver("Analysis",Template="ValuePrinter")
154 Xa[algo] = adaopy.get("Analysis")[-1]
157 M = numpy.matrix("1 0 0;0 1 0;0 0 1")
158 def H(x): return M * numpy.asmatrix(numpy.ravel( x )).T
159 for algo in ("ParticleSwarmOptimization", "QuantileRegression", ):
161 msg = "Algorithme en test : %s"%algo
162 print(msg+"\n"+"-"*len(msg))
164 adaopy = adaoBuilder.New()
165 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"BoxBounds":3*[[-1,3]], "SetSeed":1000})
166 adaopy.setBackground (Vector = [0,1,2])
167 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
168 adaopy.setObservation (Vector = [0.5,1.5,2.5])
169 adaopy.setObservationError (DiagonalSparseMatrix = "1 2 3")
170 adaopy.setObservationOperator(OneFunction = H)
171 adaopy.setObserver("Analysis",Template="ValuePrinter")
173 Xa[algo] = adaopy.get("Analysis")[-1]
177 msg = "Tests des ecarts attendus :"
178 print(msg+"\n"+"="*len(msg))
179 verify_similarity_of_algo_results(("3DVAR", "Blue", "ExtendedBlue", "4DVAR", "DerivativeFreeOptimization"), Xa, 5.e-5)
180 verify_similarity_of_algo_results(("KalmanFilter", "ExtendedKalmanFilter", "UnscentedKalmanFilter"), Xa, 1.e14)
181 verify_similarity_of_algo_results(("KalmanFilter", "EnsembleKalmanFilter"), Xa, 5.e-2)
182 print(" Les resultats obtenus sont corrects.")
187 def almost_equal_vectors(v1, v2, precision = 1.e-15, msg = ""):
188 """Comparaison de deux vecteurs"""
189 print(" Difference maximale %s: %.2e"%(msg, max(abs(v2 - v1))))
190 return max(abs(v2 - v1)) < precision
192 def verify_similarity_of_algo_results(serie = [], Xa = {}, precision = 1.e-15):
193 print(" Comparaisons :")
196 if algo1 is algo2: break
197 assert almost_equal_vectors( Xa[algo1], Xa[algo2], precision, "entre %s et %s "%(algo1, algo2) )
198 print(" Algorithmes dont les resultats sont similaires a %.0e : %s\n"%(precision, serie,))
201 #===============================================================================
202 if __name__ == "__main__":
203 print('\nAUTODIAGNOSTIC\n')