MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
forgfact.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/elec/forgfact.cc,v 1.25 2017/01/12 14:46:22 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <limits>
35 
36 #include "forgfact.h"
37 
38 
40 : iNumErr(i)
41 {
42  ASSERT(iNumErr >= 0);
43 }
44 
45 
47 {
48  NO_OP;
49 }
50 
51 
53 : ForgettingFactor(0), dk(d)
54 {
55  NO_OP;
56 }
57 
58 
60 {
61  NO_OP;
62 }
63 
64 
65 void ConstForgettingFactor::Update(const doublereal* /* pErr */ )
66 {
67  NO_OP;
68 }
69 
70 
72  integer n2,
73  integer i,
74  doublereal r,
75  doublereal f,
76  doublereal kr,
77  doublereal kl)
78 : ForgettingFactor(i),
79 N1(n1), N2(n2), dRho(r), dFact(f), dkRef(kr), dkLim(kl),
80 dk(kr), pdErrM(NULL), pdErrS(NULL), iRef1(n1-1), iRef2(n1-n2-1),
81 dErr1M(0.), dErr1S(0.), dErr2M(0.), dErr2S(0.) {
82  ASSERT(N1 > 0);
83  ASSERT(N2 > 0);
84  ASSERT(N2 < N1);
85  ASSERT(dRho > 0. && dRho < 1.);
86  ASSERT(dFact > 0. && dFact < 1.);
87  ASSERT(dkRef > 0. && dkRef < 1.);
88  ASSERT(dkLim > dkRef && dkLim <= 1.);
89 
91 
92  pdErrS = pdErrM+N1;
93  for (integer i = 0; i < 2*N1; i++) {
94  pdErrM[i] = 0.;
95  }
96 }
97 
99  /* pdErr must be non-null */
101 }
102 
104  /* setta i riferimenti */
105  if (++iRef1 == N1) {
106  iRef1 = 0;
107  }
108  if (++iRef2 == N1) {
109  iRef2 = 0;
110  }
111 
112  /* calcola la media e la norma dell'errore */
113  doublereal dM = 0.;
114  doublereal dS = 0.;
115  for (integer i = iNumErr; i--;) {
116  dS += pErr[i]*pErr[i];
117  dM += pErr[i];
118  }
119 
120  /* somma agli errori la norma dell'errore corrente, poi sottrae
121  * l'errore al limite posteriore delle rispettive finestre */
122  dErr1M += dM;
123  dErr1M -= pdErrM[iRef1];
124  dErr1S += dS;
125  dErr1S -= pdErrS[iRef1];
126 
127  dErr2M += dM;
128  dErr2M -= pdErrM[iRef2];
129  dErr2S += dS;
130  dErr2S -= pdErrS[iRef2];
131 
132  /* aggiorna il limite superiore della finestra "lunga" */
133  pdErrM[iRef1] = dM;
134  pdErrS[iRef1] = dS;
135 
136  /* differenza tra la varianza dell'errore "corto"
137  * rispetto a quello "lungo" */
138  doublereal r = (dErr1S/N1-pow(dErr1M/N1,2));
139  if (fabs(r) < 1.e-6) {
140  return;
141  }
142  r = 1.-(dErr2S/N2-pow(dErr2M/N2,2))/r;
143 
144  /* se viene superato il limite, il forgetting factor scatta al valore
145  * minimo (massima dimenticanza) */
146  if (fabs(r) > dFact) {
147  dk = dkRef;
148  } else {
149 
150  /* il forgetting factor viene fatto variare con una sua dinamica */
151  dk = dRho*dk+(1-dRho)*dkLim;
152  }
153 }
154 
155 
157  integer n2,
158  integer i,
159  doublereal r,
160  doublereal f,
161  doublereal kr,
162  doublereal kl)
163 : ForgettingFactor(i),
164 N1(n1), N2(n2), dRho(r), dFact(f), dkRef(kr), dkLim(kl),
165 dk(kr), pdErr(NULL), ppdErr(NULL), iRef1(n1-1), iRef2(n1-n2-1),
166 pdErr1M(NULL), pdErr1S(NULL), pdErr2M(NULL), pdErr2S(NULL) {
167  ASSERT(N1 > 0);
168  ASSERT(N2 > 0);
169  ASSERT(N2 < N1);
170  ASSERT(dRho > 0. && dRho < 1.);
171  ASSERT(dFact > 0. && dFact < 1.);
172  ASSERT(dkRef > 0. && dkRef < 1.);
173  ASSERT(dkLim > dkRef && dkLim <= 1.);
174 
175  integer sz = (N1+4)*iNumErr;
176 
179 
184  for (integer i = sz; i-- > 0; ) {
185  pdErr[i] = 0.;
186  }
187  for (integer i = N1; i-- > 0; ) {
188  ppdErr[i] = pdErr+i*iNumErr;
189  }
190 }
191 
193  /* pdErr must be non-null */
196 }
197 
199  /* setta i riferimenti */
200  if (++iRef1 == N1) {
201  iRef1 = 0;
202  }
203  if (++iRef2 == N1) {
204  iRef2 = 0;
205  }
206 
207  /* somma agli errori la norma dell'errore corrente, poi sottrae
208  * l'errore al limite posteriore delle rispettive finestre */
209  doublereal dS1 = 0.;
210  doublereal dS2 = 0.;
211  for (integer i = iNumErr; i-- > 0; ) {
212  doublereal d = pE[i]*pE[i];
213  pdErr1S[i] += d;
214  pdErr1S[i] -= ppdErr[iRef1][i]*ppdErr[iRef1][i];
215  pdErr1M[i] += pE[i];
216  pdErr1M[i] -= ppdErr[iRef1][i];
217  dS1 += pdErr1S[i]/N1-pow(pdErr1M[i]/N1,2);
218  pdErr2S[i] += d;
219  pdErr2S[i] -= ppdErr[iRef2][i]*ppdErr[iRef2][i];
220  pdErr2M[i] += pE[i];
221  pdErr2M[i] -= ppdErr[iRef2][i];
222  dS2 += pdErr2S[i]/N2-pow(pdErr2M[i]/N2,2);
223 
224  /* aggiorna il limite superiore della finestra "lunga" */
225  ppdErr[iRef1][i] = pE[i];
226  }
227 
228 
229 
230  /* per debug
231  cout << setw(16) << dS1 << setw(16) << dS2 << setw(16)
232  << (dS1 != 0. ? 1.-dS2/dS1 : 0.) << endl;
233  */
234 
235 
236 
237  /* differenza tra la varianza dell'errore "corto"
238  * rispetto a quello "lungo"
239  * se viene superato il limite, il forgetting factor scatta al valore
240  * minimo (massima dimenticanza) */
241  doublereal d = 0.;
242  if (fabs(dS1) > std::numeric_limits<doublereal>::epsilon() && (d = fabs(1.-dS2/dS1)) > dFact) {
243 #if 0
244  dk = (dkLim-dkRef)*exp(-d)+dkRef;
245 #else
246  dk = dkRef;
247 #endif
248  } else {
249  /* il forgetting factor viene fatto variare con una sua dinamica */
250  dk = dRho*dk+(1-dRho)*dkLim;
251  }
252 }
253 
GradientExpression< UnaryExpr< FuncExp, Expr > > exp(const GradientExpression< Expr > &u)
Definition: gradient.h:2975
integer iNumErr
Definition: forgfact.h:47
doublereal * pdErr
Definition: forgfact.h:127
GradientExpression< BinaryExpr< FuncPow, LhsExpr, RhsExpr > > pow(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
Definition: gradient.h:2961
doublereal * pdErrS
Definition: forgfact.h:89
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
void Update(const doublereal *=NULL)
Definition: forgfact.cc:65
ForgettingFactor(integer i)
Definition: forgfact.cc:39
doublereal ** ppdErr
Definition: forgfact.h:128
#define NO_OP
Definition: myassert.h:74
GradientExpression< UnaryExpr< FuncFabs, Expr > > fabs(const GradientExpression< Expr > &u)
Definition: gradient.h:2973
~ConstForgettingFactor(void)
Definition: forgfact.cc:59
DynamicForgettingFactor(integer n1, integer n2, integer i, doublereal r, doublereal f, doublereal kr, doublereal kl)
Definition: forgfact.cc:71
virtual ~ForgettingFactor(void)
Definition: forgfact.cc:46
~DynamicForgettingFactor(void)
Definition: forgfact.cc:98
doublereal * pdErr1S
Definition: forgfact.h:133
#define ASSERT(expression)
Definition: colamd.c:977
void Update(const doublereal *pErr=NULL)
Definition: forgfact.cc:198
void Update(const doublereal *pErr=NULL)
Definition: forgfact.cc:103
const doublereal dS
Definition: beamslider.cc:71
doublereal * pdErrM
Definition: forgfact.h:88
#define SAFENEWARR(pnt, item, sz)
Definition: mynewmem.h:701
doublereal * pdErr2S
Definition: forgfact.h:135
ConstForgettingFactor(doublereal d)
Definition: forgfact.cc:52
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
doublereal * pdErr1M
Definition: forgfact.h:132
doublereal * pdErr2M
Definition: forgfact.h:134
DynamicForgettingFactor2(integer n1, integer n2, integer i, doublereal r, doublereal f, doublereal kr, doublereal kl)
Definition: forgfact.cc:156