1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2010 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
22 Calcul de la fonction coût
24 __author__ = "Sophie RICCI - Octobre 2008"
27 from daCore import BasicObjects, Persistence
30 # ==============================================================================
31 class ElementaryDiagnostic(BasicObjects.Diagnostic,Persistence.OneScalar):
32 def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
33 BasicObjects.Diagnostic.__init__(self, name)
34 Persistence.OneScalar.__init__( self, name, unit, basetype = float)
36 def _formula(self, X, HX, Xb, Y, R, B):
38 Calcul de la fonction cout
40 # Jb = 1./2. * (X - Xb).T * B.I * (X - Xb)
41 Jb = 1./2. * numpy.dot((X - Xb) ,numpy.asarray(numpy.dot(B.I,(X - Xb)).A1))
42 logging.info( "Partial cost function : Jb = %s"%Jb )
44 # Jo = 1./2. * (Y - HX).T * R.I * (Y - HX)
45 Jo = 1./2. * numpy.dot((Y - HX) ,numpy.asarray(numpy.dot(R.I,(Y - HX)).A1))
46 logging.info( "Partial cost function : Jo = %s"%Jo )
49 logging.info( "Total cost function : J = Jo + Jb = %s"%J )
52 def calculate(self, x = None, Hx = None, xb = None, yo = None, R = None, B = None , step = None):
54 Teste les arguments, active la formule de calcul et stocke le résultat
56 if (x is None) or (xb is None) or (yo is None) :
57 raise ValueError("Vectors x, xb and yo must be given to compute J")
58 # if (type(x) is not float) and (type(x) is not numpy.float64) :
59 # if (x.size < 1) or (xb.size < 1) or (yo.size < 1):
60 # raise ValueError("Vectors x, xb and yo must not be empty")
61 if hasattr(numpy.matrix(x),'A1') :
62 X = numpy.matrix(x).A1
63 if hasattr(numpy.matrix(xb),'A1') :
64 Xb = numpy.matrix(xb).A1
65 if hasattr(numpy.matrix(yo),'A1') :
66 Y = numpy.matrix(yo).A1
70 raise ValueError("The given vector must be given")
72 # raise ValueError("The given vector must not be empty")
74 if (B is None ) or (R is None ):
75 raise ValueError("The matrices B and R must be given")
76 # if (B.size < 1) or (R.size < 1) :
77 # raise ValueError("The matrices B and R must not be empty")
79 value = self._formula(X, HX, Xb, Y, R, B)
81 self.store( value = value, step = step )
83 #===============================================================================
84 if __name__ == "__main__":
87 D = ElementaryDiagnostic("Ma fonction cout")
89 # Vecteur de type array
90 # ---------------------
91 x = numpy.array([1., 2.])
92 xb = numpy.array([2., 2.])
93 yo = numpy.array([5., 6.])
94 H = numpy.matrix(numpy.identity(2))
98 B = numpy.matrix(numpy.identity(2))
99 R = numpy.matrix(numpy.identity(2))
101 D.calculate( x = x, Hx = Hx, xb = xb, yo = yo, R = R, B = B)
102 print "Le vecteur x choisi est...:", x
103 print "L ebauche xb choisie est...:", xb
104 print "Le vecteur d observation est...:", yo
107 print "La fonction cout J vaut ...: %.2e"%D.valueserie(0)
108 print "La fonction cout J vaut ...: ",D.valueserie(0)
110 if (abs(D.valueserie(0) - 16.5) > 1.e-6) :
111 raise ValueError("The computation of the cost function is NOT correct")
113 print "The computation of the cost function is OK"
122 H = numpy.matrix(numpy.identity(1))
128 D.calculate( x = x, Hx = Hx, xb = xb, yo = yo, R = R, B = B)
129 print "Le vecteur x choisi est...:", x
130 print "L ebauche xb choisie est...:", xb
131 print "Le vecteur d observation est...:", yo
134 print "La fonction cout J vaut ...: %.2e"%D.valueserie(1)
135 if (abs(D.valueserie(1) - 8.5) > 1.e-6) :
136 raise ValueError("The computation of the cost function is NOT correct")
138 print "The computation of the cost function is OK"