Commit | Line | Data |
---|---|---|
b311480e | 1 | // Copyright (c) 1997-1999 Matra Datavision |
973c2be1 | 2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e | 3 | // |
973c2be1 | 4 | // This file is part of Open CASCADE Technology software library. |
b311480e | 5 | // |
d5f74e42 | 6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
10 | // distribution for complete text of the license and disclaimer of any warranty. | |
b311480e | 11 | // |
973c2be1 | 12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. | |
b311480e | 14 | |
7fd59977 | 15 | // pmn 15/05/97 pas de Gauss avec des pivot trop petit. SVD fait mieux |
16 | // l'affaire + limitation de la longeur du pas + qq comentaire issus d'EUCLID3 | |
17 | // pmn 10/06/97 refonte totale du traitement des bords + ajustement des init | |
18 | // et des tolerances pour brent... | |
19 | ||
0797d9d3 | 20 | //#ifndef OCCT_DEBUG |
7fd59977 | 21 | #define No_Standard_RangeError |
22 | #define No_Standard_OutOfRange | |
23 | #define No_Standard_DimensionError | |
24 | //#endif | |
25 | ||
26 | //math_FunctionSetRoot.cxx | |
27 | ||
28 | ||
29 | #include <math_FunctionSetRoot.ixx> | |
30 | #include <Standard_DimensionError.hxx> | |
31 | #include <math_Gauss.hxx> | |
32 | #include <math_SVD.hxx> | |
33 | #include <math_GaussLeastSquare.hxx> | |
34 | #include <math_IntegerVector.hxx> | |
35 | #include <math_Function.hxx> | |
36 | #include <math_BrentMinimum.hxx> | |
37 | #include <math_FunctionSetWithDerivatives.hxx> | |
38 | #include <Precision.hxx> | |
39 | ||
40 | ||
41 | //=========================================================================== | |
42 | // - A partir d une solution de depart, recherche d une direction.( Newton la | |
43 | // plupart du temps, gradient si Newton echoue. | |
7fd59977 | 44 | // - Recadrage au niveau des bornes avec recalcul de la direction si une |
45 | // inconnue a une valeur imposee. | |
7fd59977 | 46 | // -Si On ne sort pas des bornes |
47 | // Tant que (On ne progresse pas assez ou on ne change pas de direction) | |
48 | // . Si (Progression encore possible) | |
49 | // Si (On ne sort pas des bornes) | |
50 | // On essaye de progresser dans cette meme direction. | |
51 | // Sinon | |
52 | // On diminue le pas d'avancement ou on change de direction. | |
53 | // Sinon | |
54 | // Si on depasse le minimum | |
55 | // On fait une interpolation parabolique. | |
7fd59977 | 56 | // - Si on a progresse sur F |
57 | // On fait les tests d'arret | |
58 | // Sinon | |
59 | // On change de direction | |
60 | //============================================================================ | |
61 | ||
3e42bd70 J |
62 | #define FSR_DEBUG(arg) |
63 | // Uncomment the following code to have debug output to cout | |
89d8607f | 64 | //========================================================== |
65 | //static Standard_Boolean mydebug = Standard_True; | |
66 | //#undef FSR_DEBUG | |
67 | //#define FSR_DEBUG(arg) {if (mydebug) { cout << arg << endl; }} | |
68 | //=========================================================== | |
fbadd2cc | 69 | |
7fd59977 | 70 | class MyDirFunction : public math_Function |
71 | { | |
72 | ||
89d8607f | 73 | math_Vector *P0; |
74 | math_Vector *Dir; | |
75 | math_Vector *P; | |
76 | math_Vector *FV; | |
77 | math_FunctionSetWithDerivatives *F; | |
7fd59977 | 78 | |
79 | public : | |
80 | ||
89d8607f | 81 | MyDirFunction(math_Vector& V1, |
82 | math_Vector& V2, | |
83 | math_Vector& V3, | |
84 | math_Vector& V4, | |
85 | math_FunctionSetWithDerivatives& f) ; | |
86 | ||
87 | void Initialize(const math_Vector& p0, const math_Vector& dir) const; | |
88 | //For hp : | |
89 | Standard_Boolean Value(const math_Vector& Sol, math_Vector& FF, | |
90 | math_Matrix& DF, math_Vector& GH, | |
91 | Standard_Real& F2, Standard_Real& Gnr1); | |
92 | // Standard_Boolean MyDirFunction::Value(const math_Vector& Sol, math_Vector& FF, | |
93 | // math_Matrix& DF, math_Vector& GH, | |
94 | // Standard_Real& F2, Standard_Real& Gnr1); | |
95 | Standard_Boolean Value(const Standard_Real x, Standard_Real& fval) ; | |
96 | ||
7fd59977 | 97 | }; |
98 | ||
99 | MyDirFunction::MyDirFunction(math_Vector& V1, | |
89d8607f | 100 | math_Vector& V2, |
101 | math_Vector& V3, | |
102 | math_Vector& V4, | |
103 | math_FunctionSetWithDerivatives& f) { | |
104 | ||
105 | P0 = &V1; | |
106 | Dir = &V2; | |
107 | P = &V3; | |
108 | FV = &V4; | |
109 | F = &f; | |
7fd59977 | 110 | } |
111 | ||
112 | void MyDirFunction::Initialize(const math_Vector& p0, | |
89d8607f | 113 | const math_Vector& dir) const |
7fd59977 | 114 | { |
115 | *P0 = p0; | |
116 | *Dir = dir; | |
117 | } | |
118 | ||
119 | ||
120 | Standard_Boolean MyDirFunction::Value(const Standard_Real x, | |
89d8607f | 121 | Standard_Real& fval) |
7fd59977 | 122 | { |
123 | Standard_Real p; | |
124 | for(Standard_Integer i = P->Lower(); i <= P->Upper(); i++) { | |
125 | p = Dir->Value(i); | |
126 | P->Value(i) = p * x + P0->Value(i); | |
127 | } | |
e93e4230 | 128 | if( F->Value(*P, *FV) ) |
129 | { | |
7fd59977 | 130 | |
e93e4230 | 131 | Standard_Real aVal = 0.0; |
7fd59977 | 132 | |
e93e4230 | 133 | for(Standard_Integer i = FV->Lower(); i <= FV->Upper(); i++) |
134 | { | |
7fd59977 | 135 | aVal = FV->Value(i); |
e93e4230 | 136 | if(aVal <= -1.e+100) // Precision::HalfInfinite() later |
137 | return Standard_False; | |
7fd59977 | 138 | else if(aVal >= 1.e+100) // Precision::HalfInfinite() later |
89d8607f | 139 | return Standard_False; |
7fd59977 | 140 | } |
141 | ||
142 | fval = 0.5 * (FV->Norm2()); | |
143 | return Standard_True; | |
144 | } | |
145 | return Standard_False; | |
146 | } | |
147 | ||
148 | Standard_Boolean MyDirFunction::Value(const math_Vector& Sol, | |
89d8607f | 149 | math_Vector& FF, |
150 | math_Matrix& DF, | |
151 | math_Vector& GH, | |
152 | Standard_Real& F2, | |
153 | Standard_Real& Gnr1) | |
7fd59977 | 154 | { |
155 | if( F->Values(Sol, FF, DF) ) { | |
156 | ||
157 | Standard_Real aVal = 0.; | |
158 | ||
159 | for(Standard_Integer i = FF.Lower(); i <= FF.Upper(); i++) { | |
89d8607f | 160 | // modified by NIZHNY-MKK Mon Oct 3 17:56:50 2005.BEGIN |
7fd59977 | 161 | aVal = FF.Value(i); |
162 | if(aVal < 0.) { | |
89d8607f | 163 | if(aVal <= -1.e+100) // Precision::HalfInfinite() later |
164 | // if(Precision::IsInfinite(Abs(FF.Value(i)))) { | |
165 | // F2 = Precision::Infinite(); | |
166 | // Gnr1 = Precision::Infinite(); | |
167 | return Standard_False; | |
7fd59977 | 168 | } |
169 | else if(aVal >= 1.e+100) // Precision::HalfInfinite() later | |
89d8607f | 170 | return Standard_False; |
171 | // modified by NIZHNY-MKK Mon Oct 3 17:57:05 2005.END | |
7fd59977 | 172 | } |
173 | ||
174 | ||
175 | F2 = 0.5 * (FF.Norm2()); | |
176 | GH.TMultiply(DF, FF); | |
12f139fd | 177 | for(Standard_Integer i = GH.Lower(); i <= GH.Upper(); i++) |
178 | { | |
179 | if(Precision::IsInfinite((GH.Value(i)))) | |
180 | { | |
181 | return Standard_False; | |
182 | } | |
183 | } | |
7fd59977 | 184 | Gnr1 = GH.Norm2(); |
185 | return Standard_True; | |
186 | } | |
187 | return Standard_False; | |
188 | } | |
189 | ||
190 | ||
191 | //-------------------------------------------------------------- | |
192 | static Standard_Boolean MinimizeDirection(const math_Vector& P0, | |
89d8607f | 193 | const math_Vector& P1, |
194 | const math_Vector& P2, | |
195 | const Standard_Real F1, | |
196 | math_Vector& Delta, | |
197 | const math_Vector& Tol, | |
198 | MyDirFunction& F) | |
199 | // Purpose : minimisation a partir de 3 points | |
200 | //------------------------------------------------------- | |
7fd59977 | 201 | { |
202 | // (1) Evaluation d'un tolerance parametrique 1D | |
203 | Standard_Real tol1d = 2.1 , invnorme, tsol; | |
204 | Standard_Real Eps = 1.e-16; | |
205 | Standard_Real ax, bx, cx; | |
206 | ||
207 | for (Standard_Integer ii =1; ii<=Tol.Length(); ii++) { | |
208 | invnorme = Abs(Delta(ii)); | |
209 | if (invnorme>Eps) tol1d = Min(tol1d, Tol(ii) / invnorme); | |
210 | } | |
211 | if (tol1d > 1.9) return Standard_False; //Pas la peine de se fatiguer | |
212 | tol1d /= 3; | |
213 | ||
89d8607f | 214 | //JR/Hp : |
7fd59977 | 215 | math_Vector PP0 = P0 ; |
216 | math_Vector PP1 = P1 ; | |
217 | Delta = PP1 - PP0; | |
89d8607f | 218 | // Delta = P1 - P0; |
7fd59977 | 219 | invnorme = Delta.Norm(); |
220 | if (invnorme <= Eps) return Standard_False; | |
221 | invnorme = ((Standard_Real) 1) / invnorme; | |
222 | ||
223 | F.Initialize(P1, Delta); | |
224 | ||
225 | // (2) On minimise | |
3e42bd70 | 226 | FSR_DEBUG (" minimisation dans la direction") |
89d8607f | 227 | ax = -1; bx = 0; |
7fd59977 | 228 | cx = (P2-P1).Norm()*invnorme; |
859a47c3 | 229 | if (cx < 1.e-2) |
230 | return Standard_False; | |
231 | ||
232 | math_BrentMinimum Sol(tol1d, 100, tol1d); | |
233 | Sol.Perform(F, ax, bx, cx); | |
234 | ||
7fd59977 | 235 | if(Sol.IsDone()) { |
236 | tsol = Sol.Location(); | |
237 | if (Sol.Minimum() < F1) { | |
238 | Delta.Multiply(tsol); | |
239 | return Standard_True; | |
240 | } | |
241 | } | |
242 | return Standard_False; | |
243 | } | |
244 | ||
245 | //---------------------------------------------------------------------- | |
246 | static Standard_Boolean MinimizeDirection(const math_Vector& P, | |
89d8607f | 247 | math_Vector& Dir, |
248 | const Standard_Real& PValue, | |
249 | const Standard_Real& PDirValue, | |
250 | const math_Vector& Gradient, | |
251 | const math_Vector& DGradient, | |
252 | const math_Vector& Tol, | |
253 | MyDirFunction& F) | |
254 | // Purpose: minimisation a partir de 2 points et une derives | |
255 | //---------------------------------------------------------------------- | |
7fd59977 | 256 | |
257 | { | |
258 | // (0) Evaluation d'un tolerance parametrique 1D | |
259 | Standard_Boolean good = Standard_False; | |
260 | Standard_Real Eps = 1.e-20; | |
261 | Standard_Real tol1d = 1.1, Result = PValue, absdir; | |
262 | ||
263 | for (Standard_Integer ii =1; ii<=Tol.Length(); ii++) { | |
264 | absdir = Abs(Dir(ii)); | |
265 | if (absdir >Eps) tol1d = Min(tol1d, Tol(ii) / absdir); | |
266 | } | |
267 | if (tol1d > 0.9) return Standard_False; | |
89d8607f | 268 | |
7fd59977 | 269 | // (1) On realise une premiere interpolation quadratique |
270 | Standard_Real ax, bx, cx, df1, df2, Delta, tsol, fsol, tsolbis; | |
89d8607f | 271 | FSR_DEBUG(" essai d interpolation"); |
fbadd2cc | 272 | |
7fd59977 | 273 | df1 = Gradient*Dir; |
274 | df2 = DGradient*Dir; | |
275 | ||
276 | if (df1<-Eps && df2>Eps) { // cuvette | |
277 | tsol = - df1 / (df2 - df1); | |
278 | } | |
279 | else { | |
280 | cx = PValue; | |
281 | bx = df1; | |
282 | ax = PDirValue - (bx+cx); | |
283 | ||
284 | if (Abs(ax) <= Eps) { // cas lineaire | |
285 | if ((Abs(bx) >= Eps)) tsol = - cx/bx; | |
286 | else tsol = 0; | |
287 | } | |
288 | else { // cas quadratique | |
289 | Delta = bx*bx - 4*ax*cx; | |
290 | if (Delta > 1.e-9) { | |
89d8607f | 291 | // il y a des racines, on prend la plus proche de 0 |
292 | Delta = Sqrt(Delta); | |
293 | tsol = -(bx + Delta); | |
294 | tsolbis = (Delta - bx); | |
295 | if (Abs(tsolbis) < Abs(tsol)) tsol = tsolbis; | |
296 | tsol /= 2*ax; | |
7fd59977 | 297 | } |
298 | else { | |
89d8607f | 299 | // pas ou peu de racine : on "extremise" |
300 | tsol = -(0.5*bx)/ax; | |
7fd59977 | 301 | } |
302 | } | |
303 | } | |
304 | ||
305 | if (Abs(tsol) >= 1) return Standard_False; //resultat sans interet | |
306 | ||
307 | F.Initialize(P, Dir); | |
308 | F.Value(tsol, fsol); | |
309 | ||
310 | if (fsol<PValue) { | |
311 | good = Standard_True; | |
312 | Result = fsol; | |
89d8607f | 313 | FSR_DEBUG("t= "<<tsol<<" F = " << fsol << " OldF = "<<PValue); |
7fd59977 | 314 | } |
315 | ||
316 | // (2) Si l'on a pas assez progresser on realise une recherche | |
317 | // en bonne et due forme, a partir des inits precedents | |
e93e4230 | 318 | if ((fsol > 0.2*PValue) && (tol1d < 0.5)) |
319 | { | |
89d8607f | 320 | |
7fd59977 | 321 | if (tsol <0) { |
322 | ax = tsol; bx = 0.0; cx = 1.0; | |
323 | } | |
324 | else { | |
325 | ax = 0.0; bx = tsol; cx = 1.0; | |
326 | } | |
89d8607f | 327 | FSR_DEBUG(" minimisation dans la direction"); |
859a47c3 | 328 | |
329 | math_BrentMinimum Sol(tol1d, 100, tol1d); | |
859a47c3 | 330 | |
e93e4230 | 331 | // Base invocation. |
332 | Sol.Perform(F, ax, bx, cx); | |
333 | if(Sol.IsDone()) | |
334 | { | |
335 | if (Sol.Minimum() <= Result) | |
336 | { | |
3e42bd70 J |
337 | tsol = Sol.Location(); |
338 | good = Standard_True; | |
e93e4230 | 339 | Result = Sol.Minimum(); |
340 | ||
341 | // Objective function changes too fast -> | |
342 | // perform additional computations. | |
343 | if (Gradient.Norm2() > 1.0 / Precision::SquareConfusion() && | |
344 | tsol > ax && | |
345 | tsol < cx) // Solution inside of (ax, cx) interval. | |
346 | { | |
347 | // First and second part invocation. | |
348 | Sol.Perform(F, ax, (ax + tsol) / 2.0, tsol); | |
349 | if(Sol.IsDone()) | |
350 | { | |
351 | if (Sol.Minimum() <= Result) | |
352 | { | |
353 | tsol = Sol.Location(); | |
354 | good = Standard_True; | |
355 | Result = Sol.Minimum(); | |
356 | } | |
357 | } | |
358 | ||
359 | Sol.Perform(F, tsol, (cx + tsol) / 2.0, cx); | |
360 | if(Sol.IsDone()) | |
361 | { | |
362 | if (Sol.Minimum() <= Result) | |
363 | { | |
364 | tsol = Sol.Location(); | |
365 | good = Standard_True; | |
366 | Result = Sol.Minimum(); | |
367 | } | |
368 | } | |
369 | } | |
370 | } // if (Sol.Minimum() <= Result) | |
371 | } // if(Sol.IsDone()) | |
7fd59977 | 372 | } |
e93e4230 | 373 | |
374 | if (good) | |
375 | { | |
7fd59977 | 376 | // mise a jour du Delta |
377 | Dir.Multiply(tsol); | |
378 | } | |
379 | return good; | |
380 | } | |
381 | ||
382 | //------------------------------------------------------ | |
383 | static void SearchDirection(const math_Matrix& DF, | |
89d8607f | 384 | const math_Vector& GH, |
385 | const math_Vector& FF, | |
386 | Standard_Boolean ChangeDirection, | |
387 | const math_Vector& InvLengthMax, | |
388 | math_Vector& Direction, | |
389 | Standard_Real& Dy) | |
7fd59977 | 390 | |
391 | { | |
392 | Standard_Integer Ninc = DF.ColNumber(), Neq = DF.RowNumber(); | |
393 | Standard_Real Eps = 1.e-32; | |
394 | if (!ChangeDirection) { | |
395 | if (Ninc == Neq) { | |
396 | for (Standard_Integer i = FF.Lower(); i <= FF.Upper(); i++) { | |
89d8607f | 397 | Direction(i) = -FF(i); |
7fd59977 | 398 | } |
399 | math_Gauss Solut(DF, 1.e-9); | |
400 | if (Solut.IsDone()) Solut.Solve(Direction); | |
401 | else { // we have to "forget" singular directions. | |
89d8607f | 402 | FSR_DEBUG(" Matrice singuliere : On prend SVD"); |
3e42bd70 | 403 | math_SVD SolvebySVD(DF); |
7fd59977 | 404 | if (SolvebySVD.IsDone()) SolvebySVD.Solve(-1*FF, Direction); |
3e42bd70 J |
405 | else ChangeDirection = Standard_True; |
406 | } | |
7fd59977 | 407 | } |
408 | else if (Ninc > Neq) { | |
409 | math_SVD Solut(DF); | |
410 | if (Solut.IsDone()) Solut.Solve(-1*FF, Direction); | |
411 | else ChangeDirection = Standard_True; | |
412 | } | |
413 | else if (Ninc < Neq) { // Calcul par GaussLeastSquare | |
414 | math_GaussLeastSquare Solut(DF); | |
415 | if (Solut.IsDone()) Solut.Solve(-1*FF, Direction); | |
416 | else ChangeDirection = Standard_True; | |
417 | } | |
418 | } | |
419 | // Il vaut mieux interdire des directions trops longue | |
420 | // Afin de blinder les cas trop mal conditionne | |
421 | // PMN 12/05/97 Traitement des singularite dans les conges | |
422 | // Sur des surfaces periodiques | |
89d8607f | 423 | |
7fd59977 | 424 | Standard_Real ratio = Abs( Direction(Direction.Lower()) |
89d8607f | 425 | *InvLengthMax(Direction.Lower()) ); |
7fd59977 | 426 | Standard_Integer i; |
427 | for (i = Direction.Lower()+1; i<=Direction.Upper(); i++) { | |
428 | ratio = Max(ratio, Abs( Direction(i)*InvLengthMax(i)) ); | |
429 | } | |
430 | if (ratio > 1) { | |
431 | Direction /= ratio; | |
432 | } | |
89d8607f | 433 | |
7fd59977 | 434 | Dy = Direction*GH; |
435 | if (Dy >= -Eps) { // newton "ne descend pas" on prend le gradient | |
436 | ChangeDirection = Standard_True; | |
437 | } | |
438 | if (ChangeDirection) { // On va faire un gradient ! | |
439 | for (i = Direction.Lower(); i <= Direction.Upper(); i++) { | |
440 | Direction(i) = - GH(i); | |
441 | } | |
442 | Dy = - (GH.Norm2()); | |
443 | } | |
444 | } | |
445 | ||
446 | ||
447 | //===================================================================== | |
448 | static void SearchDirection(const math_Matrix& DF, | |
89d8607f | 449 | const math_Vector& GH, |
450 | const math_Vector& FF, | |
451 | const math_IntegerVector& Constraints, | |
452 | // const math_Vector& X, // Le point d'init | |
453 | const math_Vector& , // Le point d'init | |
454 | Standard_Boolean ChangeDirection, | |
7fd59977 | 455 | const math_Vector& InvLengthMax, |
89d8607f | 456 | math_Vector& Direction, |
457 | Standard_Real& Dy) | |
458 | //Purpose : Recherche une direction (et un pas si Newton Fonctionne) le long | |
459 | // d'une frontiere | |
460 | //===================================================================== | |
7fd59977 | 461 | { |
462 | Standard_Integer Ninc = DF.ColNumber(), Neq = DF.RowNumber(); | |
463 | Standard_Integer i, j, k, Cons = 0; | |
464 | ||
465 | // verification sur les bornes imposees: | |
466 | ||
467 | for (i = 1; i <= Ninc; i++) { | |
468 | if (Constraints(i) != 0) Cons++; | |
469 | // sinon le systeme a resoudre ne change pas. | |
470 | } | |
471 | ||
472 | if (Cons == 0) { | |
473 | SearchDirection(DF, GH, FF, ChangeDirection, InvLengthMax, | |
89d8607f | 474 | Direction, Dy); |
7fd59977 | 475 | } |
476 | else if (Cons == Ninc) { // il n'y a plus rien a faire... | |
477 | for(Standard_Integer i = Direction.Lower(); i <= Direction.Upper(); i++) { | |
89d8607f | 478 | Direction(i) = 0; |
479 | } | |
7fd59977 | 480 | Dy = 0; |
481 | } | |
482 | else { //(1) Cas general : On definit un sous probleme | |
483 | math_Matrix DF2(1, Neq, 1, Ninc-Cons); | |
484 | math_Vector MyGH(1, Ninc-Cons); | |
485 | math_Vector MyDirection(1, Ninc-Cons); | |
486 | math_Vector MyInvLengthMax(1, Ninc); | |
487 | ||
488 | for (k=1, i = 1; i <= Ninc; i++) { | |
489 | if (Constraints(i) == 0) { | |
89d8607f | 490 | MyGH(k) = GH(i); |
491 | MyInvLengthMax(k) = InvLengthMax(i); | |
492 | MyDirection(k) = Direction(i); | |
493 | for (j = 1; j <= Neq; j++) { | |
494 | DF2(j, k) = DF(j, i); | |
495 | } | |
496 | k++; //on passe a l'inconnue suivante | |
497 | } | |
7fd59977 | 498 | } |
499 | //(2) On le resoud | |
500 | SearchDirection(DF2, MyGH, FF, ChangeDirection, MyInvLengthMax, | |
89d8607f | 501 | MyDirection, Dy); |
7fd59977 | 502 | |
503 | // (3) On l'interprete... | |
504 | // Reconstruction de Direction: | |
505 | for (i = 1, k = 1; i <= Ninc; i++) { | |
506 | if (Constraints(i) == 0) { | |
89d8607f | 507 | if (!ChangeDirection) { |
508 | Direction(i) = MyDirection(k); | |
509 | } | |
510 | else Direction(i) = - GH(i); | |
511 | k++; | |
7fd59977 | 512 | } |
513 | else { | |
89d8607f | 514 | Direction(i) = 0.0; |
7fd59977 | 515 | } |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | ||
521 | ||
522 | //==================================================== | |
3e42bd70 J |
523 | Standard_Boolean Bounds(const math_Vector& InfBound, |
524 | const math_Vector& SupBound, | |
525 | const math_Vector& Tol, | |
526 | math_Vector& Sol, | |
527 | const math_Vector& SolSave, | |
528 | math_IntegerVector& Constraints, | |
529 | math_Vector& Delta, | |
530 | Standard_Boolean& theIsNewSol) | |
89d8607f | 531 | // |
532 | // Purpose: Trims an initial solution Sol to be within a domain defined by | |
533 | // InfBound and SupBound. Delta will contain a distance between final Sol and | |
534 | // SolSave. | |
535 | // IsNewSol returns False, if final Sol fully coincides with SolSave, i.e. | |
536 | // if SolSave already lied on a boundary and initial Sol was fully beyond it | |
537 | //====================================================== | |
7fd59977 | 538 | { |
539 | Standard_Boolean Out = Standard_False; | |
540 | Standard_Integer i, Ninc = Sol.Length(); | |
541 | Standard_Real monratio = 1; | |
89d8607f | 542 | |
3e42bd70 J |
543 | theIsNewSol = Standard_True; |
544 | ||
7fd59977 | 545 | // Calcul du ratio de recadrage |
546 | for (i = 1; i <= Ninc; i++) { | |
547 | Constraints(i) = 0; | |
548 | Delta(i) = Sol(i) - SolSave(i); | |
549 | if (InfBound(i) == SupBound(i)) { | |
550 | Constraints(i) = 1; | |
551 | Out = Standard_True; // Ok mais, cela devrait etre eviter | |
552 | } | |
553 | else if(Sol(i) < InfBound(i)) { | |
554 | Constraints(i) = 1; | |
555 | Out = Standard_True; | |
3e42bd70 J |
556 | // Delta(i) is negative |
557 | if (-Delta(i) > Tol(i)) // Afin d'eviter des ratio nulles pour rien | |
558 | monratio = Min(monratio, (InfBound(i) - SolSave(i))/Delta(i) ); | |
7fd59977 | 559 | } |
560 | else if (Sol(i) > SupBound(i)) { | |
561 | Constraints(i) = 1; | |
562 | Out = Standard_True; | |
3e42bd70 J |
563 | // Delta(i) is positive |
564 | if (Delta(i) > Tol(i)) | |
565 | monratio = Min(monratio, (SupBound(i) - SolSave(i))/Delta(i) ); | |
7fd59977 | 566 | } |
567 | } | |
568 | ||
569 | if (Out){ // Troncature et derniers recadrage pour blinder (pb numeriques) | |
3e42bd70 J |
570 | if (monratio == 0.0) { |
571 | theIsNewSol = Standard_False; | |
572 | Sol = SolSave; | |
573 | Delta.Init (0.0); | |
574 | } else { | |
575 | Delta *= monratio; | |
576 | Sol = SolSave+Delta; | |
577 | for (i = 1; i <= Ninc; i++) { | |
578 | if(Sol(i) < InfBound(i)) { | |
579 | Sol(i) = InfBound(i); | |
580 | Delta(i) = Sol(i) - SolSave(i); | |
581 | } | |
582 | else if (Sol(i) > SupBound(i)) { | |
583 | Sol(i) = SupBound(i); | |
584 | Delta(i) = Sol(i) - SolSave(i); | |
585 | } | |
7fd59977 | 586 | } |
587 | } | |
588 | } | |
589 | return Out; | |
590 | } | |
591 | ||
592 | ||
593 | ||
594 | ||
859a47c3 | 595 | //======================================================================= |
596 | //function : math_FunctionSetRoot | |
597 | //purpose : Constructor | |
598 | //======================================================================= | |
599 | math_FunctionSetRoot::math_FunctionSetRoot( | |
600 | math_FunctionSetWithDerivatives& theFunction, | |
601 | const math_Vector& theTolerance, | |
602 | const Standard_Integer theNbIterations) | |
603 | ||
604 | : Delta(1, theFunction.NbVariables()), | |
605 | Sol (1, theFunction.NbVariables()), | |
606 | DF (1, theFunction.NbEquations() , 1, theFunction.NbVariables()), | |
607 | Tol (1, theFunction.NbVariables()), | |
608 | Done (Standard_False), | |
609 | Kount (0), | |
610 | State (0), | |
611 | Itermax (theNbIterations), | |
612 | InfBound(1, theFunction.NbVariables(), RealFirst()), | |
613 | SupBound(1, theFunction.NbVariables(), RealLast ()), | |
614 | SolSave (1, theFunction.NbVariables()), | |
615 | GH (1, theFunction.NbVariables()), | |
616 | DH (1, theFunction.NbVariables()), | |
617 | DHSave (1, theFunction.NbVariables()), | |
618 | FF (1, theFunction.NbEquations()), | |
619 | PreviousSolution(1, theFunction.NbVariables()), | |
620 | Save (0, theNbIterations), | |
621 | Constraints(1, theFunction.NbVariables()), | |
622 | Temp1 (1, theFunction.NbVariables()), | |
623 | Temp2 (1, theFunction.NbVariables()), | |
624 | Temp3 (1, theFunction.NbVariables()), | |
625 | Temp4 (1, theFunction.NbEquations()), | |
626 | myIsDivergent(Standard_False) | |
7fd59977 | 627 | { |
859a47c3 | 628 | SetTolerance(theTolerance); |
7fd59977 | 629 | } |
630 | ||
859a47c3 | 631 | //======================================================================= |
632 | //function : math_FunctionSetRoot | |
633 | //purpose : Constructor | |
634 | //======================================================================= | |
635 | math_FunctionSetRoot::math_FunctionSetRoot(math_FunctionSetWithDerivatives& theFunction, | |
636 | const Standard_Integer theNbIterations) | |
637 | ||
638 | : Delta(1, theFunction.NbVariables()), | |
639 | Sol (1, theFunction.NbVariables()), | |
640 | DF (1, theFunction.NbEquations() , 1, theFunction.NbVariables()), | |
641 | Tol (1, theFunction.NbVariables()), | |
642 | Done (Standard_False), | |
643 | Kount (0), | |
644 | State (0), | |
645 | Itermax (theNbIterations), | |
646 | InfBound(1, theFunction.NbVariables(), RealFirst()), | |
647 | SupBound(1, theFunction.NbVariables(), RealLast ()), | |
648 | SolSave (1, theFunction.NbVariables()), | |
649 | GH (1, theFunction.NbVariables()), | |
650 | DH (1, theFunction.NbVariables()), | |
651 | DHSave (1, theFunction.NbVariables()), | |
652 | FF (1, theFunction.NbEquations()), | |
653 | PreviousSolution(1, theFunction.NbVariables()), | |
654 | Save (0, theNbIterations), | |
655 | Constraints(1, theFunction.NbVariables()), | |
656 | Temp1 (1, theFunction.NbVariables()), | |
657 | Temp2 (1, theFunction.NbVariables()), | |
658 | Temp3 (1, theFunction.NbVariables()), | |
659 | Temp4 (1, theFunction.NbEquations()), | |
660 | myIsDivergent(Standard_False) | |
7fd59977 | 661 | { |
7fd59977 | 662 | } |
663 | ||
859a47c3 | 664 | //======================================================================= |
665 | //function : ~math_FunctionSetRoot | |
666 | //purpose : Destructor | |
667 | //======================================================================= | |
668 | math_FunctionSetRoot::~math_FunctionSetRoot() | |
89d8607f | 669 | { |
859a47c3 | 670 | Delete(); |
7fd59977 | 671 | } |
672 | ||
859a47c3 | 673 | //======================================================================= |
674 | //function : SetTolerance | |
675 | //purpose : | |
676 | //======================================================================= | |
677 | void math_FunctionSetRoot::SetTolerance(const math_Vector& theTolerance) | |
6da30ff1 | 678 | { |
859a47c3 | 679 | for (Standard_Integer i = 1; i <= Tol.Length(); ++i) |
680 | Tol(i) = theTolerance(i); | |
6da30ff1 | 681 | } |
7fd59977 | 682 | |
859a47c3 | 683 | //======================================================================= |
684 | //function : Perform | |
685 | //purpose : | |
686 | //======================================================================= | |
687 | void math_FunctionSetRoot::Perform(math_FunctionSetWithDerivatives& theFunction, | |
688 | const math_Vector& theStartingPoint, | |
689 | const Standard_Boolean theStopOnDivergent) | |
7fd59977 | 690 | { |
859a47c3 | 691 | Perform(theFunction, theStartingPoint, InfBound, SupBound, theStopOnDivergent); |
7fd59977 | 692 | } |
693 | ||
859a47c3 | 694 | //======================================================================= |
695 | //function : Perform | |
696 | //purpose : | |
697 | //======================================================================= | |
7fd59977 | 698 | void math_FunctionSetRoot::Perform(math_FunctionSetWithDerivatives& F, |
89d8607f | 699 | const math_Vector& StartingPoint, |
75259fc5 | 700 | const math_Vector& theInfBound, |
701 | const math_Vector& theSupBound, | |
89d8607f | 702 | Standard_Boolean theStopOnDivergent) |
7fd59977 | 703 | { |
7fd59977 | 704 | Standard_Integer Ninc = F.NbVariables(), Neq = F.NbEquations(); |
89d8607f | 705 | |
7fd59977 | 706 | if ((Neq <= 0) || |
89d8607f | 707 | (StartingPoint.Length()!= Ninc) || |
75259fc5 | 708 | (theInfBound.Length() != Ninc) || |
709 | (theSupBound.Length() != Ninc)) { Standard_DimensionError:: Raise(); } | |
7fd59977 | 710 | |
711 | Standard_Integer i; | |
3e42bd70 | 712 | Standard_Boolean ChangeDirection = Standard_False, Sort = Standard_False, isNewSol = Standard_False; |
7fd59977 | 713 | Standard_Boolean Good, Verif; |
714 | Standard_Boolean Stop; | |
3e42bd70 | 715 | const Standard_Real EpsSqrt = 1.e-16, Eps = 1.e-32, Eps2 = 1.e-64, Progres = 0.005; |
7fd59977 | 716 | Standard_Real F2, PreviousMinimum, Dy, OldF; |
717 | Standard_Real Ambda, Ambda2, Gnr1, Oldgr; | |
718 | math_Vector InvLengthMax(1, Ninc); // Pour bloquer les pas a 1/4 du domaine | |
75259fc5 | 719 | math_IntegerVector aConstraints(1, Ninc); // Pour savoir sur quels bord on se trouve |
7fd59977 | 720 | for (i = 1; i <= Ninc ; i++) { |
89d8607f | 721 | // modified by NIZHNY-MKK Mon Oct 3 18:03:50 2005 |
722 | // InvLengthMax(i) = 1. / Max(Abs(SupBound(i) - InfBound(i))/4, 1.e-9); | |
75259fc5 | 723 | InvLengthMax(i) = 1. / Max((theSupBound(i) - theInfBound(i))/4, 1.e-9); |
89d8607f | 724 | } |
7fd59977 | 725 | |
726 | MyDirFunction F_Dir(Temp1, Temp2, Temp3, Temp4, F); | |
727 | Standard_Integer DescenteIter; | |
728 | ||
729 | Done = Standard_False; | |
730 | Sol = StartingPoint; | |
731 | Kount = 0; | |
732 | ||
b659a6dc | 733 | // |
734 | myIsDivergent = Standard_False; | |
735 | for (i = 1; i <= Ninc; i++) | |
736 | { | |
75259fc5 | 737 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
b659a6dc | 738 | } |
739 | if (theStopOnDivergent & myIsDivergent) | |
740 | { | |
741 | return; | |
742 | } | |
7fd59977 | 743 | // Verification de la validite des inconnues par rapport aux bornes. |
744 | // Recentrage sur les bornes si pas valide. | |
745 | for ( i = 1; i <= Ninc; i++) { | |
75259fc5 | 746 | if (Sol(i) <= theInfBound(i)) Sol(i) = theInfBound(i); |
747 | else if (Sol(i) > theSupBound(i)) Sol(i) = theSupBound(i); | |
7fd59977 | 748 | } |
749 | ||
750 | // Calcul de la premiere valeur de F et de son gradient | |
751 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { | |
752 | Done = Standard_False; | |
b659a6dc | 753 | if (!theStopOnDivergent || !myIsDivergent) |
754 | { | |
755 | State = F.GetStateNumber(); | |
756 | } | |
7fd59977 | 757 | return; |
758 | } | |
759 | Ambda2 = Gnr1; | |
760 | // Le rang 0 de Save ne doit servir q'au test accelarteur en fin de boucle | |
761 | // s'il on est dejas sur la solution, il faut leurer ce test pour eviter | |
762 | // de faire une seconde iteration... | |
3e42bd70 | 763 | Save(0) = Max (F2, EpsSqrt); |
5368adff | 764 | Standard_Real aTol_Func = Epsilon(F2); |
89d8607f | 765 | FSR_DEBUG("=== Mode Debug de Function Set Root" << endl); |
766 | FSR_DEBUG(" F2 Initial = " << F2); | |
7fd59977 | 767 | |
3e42bd70 | 768 | if ((F2 <= Eps) || (Gnr1 <= Eps2)) { |
b659a6dc | 769 | Done = Standard_False; |
770 | if (!theStopOnDivergent || !myIsDivergent) | |
771 | { | |
772 | Done = Standard_True; | |
773 | State = F.GetStateNumber(); | |
774 | } | |
7fd59977 | 775 | return; |
776 | } | |
777 | ||
778 | for (Kount = 1; Kount <= Itermax; Kount++) { | |
779 | PreviousMinimum = F2; | |
780 | Oldgr = Gnr1; | |
781 | PreviousSolution = Sol; | |
782 | SolSave = Sol; | |
783 | ||
784 | SearchDirection(DF, GH, FF, ChangeDirection, InvLengthMax, DH, Dy); | |
785 | if (Abs(Dy) <= Eps) { | |
b659a6dc | 786 | Done = Standard_False; |
787 | if (!theStopOnDivergent || !myIsDivergent) | |
788 | { | |
789 | Done = Standard_True; | |
790 | ////modified by jgv, 31.08.2011//// | |
791 | F.Value(Sol, FF); //update F before GetStateNumber | |
792 | /////////////////////////////////// | |
793 | State = F.GetStateNumber(); | |
794 | } | |
7fd59977 | 795 | return; |
796 | } | |
797 | if (ChangeDirection) { | |
3e42bd70 | 798 | Ambda = Ambda2 / Sqrt(Abs(Dy)); |
7fd59977 | 799 | if (Ambda > 1.0) Ambda = 1.0; |
800 | } | |
801 | else { | |
802 | Ambda = 1.0; | |
803 | Ambda2 = 0.5*Ambda/DH.Norm(); | |
804 | } | |
805 | ||
806 | for( i = Sol.Lower(); i <= Sol.Upper(); i++) { | |
807 | Sol(i) = Sol(i) + Ambda * DH(i); | |
808 | } | |
b659a6dc | 809 | // |
810 | for (i = 1; i <= Ninc; i++) | |
811 | { | |
75259fc5 | 812 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
b659a6dc | 813 | } |
814 | if (theStopOnDivergent & myIsDivergent) | |
815 | { | |
816 | return; | |
817 | } | |
818 | // | |
75259fc5 | 819 | Sort = Bounds(theInfBound, theSupBound, Tol, Sol, SolSave, |
820 | aConstraints, Delta, isNewSol); | |
89d8607f | 821 | |
7fd59977 | 822 | |
7fd59977 | 823 | DHSave = GH; |
3e42bd70 | 824 | if (isNewSol) { |
89d8607f | 825 | // F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1); |
3e42bd70 J |
826 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { |
827 | Done = Standard_False; | |
b659a6dc | 828 | if (!theStopOnDivergent || !myIsDivergent) |
829 | { | |
830 | State = F.GetStateNumber(); | |
831 | } | |
3e42bd70 J |
832 | return; |
833 | } | |
7fd59977 | 834 | } |
89d8607f | 835 | |
836 | FSR_DEBUG("Kount = " << Kount); | |
837 | FSR_DEBUG("Le premier F2 = " << F2); | |
838 | FSR_DEBUG("Direction = " << ChangeDirection); | |
7fd59977 | 839 | |
3e42bd70 | 840 | if ((F2 <= Eps) || (Gnr1 <= Eps2)) { |
b659a6dc | 841 | Done = Standard_False; |
842 | if (!theStopOnDivergent || !myIsDivergent) | |
843 | { | |
844 | Done = Standard_True; | |
845 | ////modified by jgv, 31.08.2011//// | |
846 | F.Value(Sol, FF); //update F before GetStateNumber | |
847 | /////////////////////////////////// | |
848 | State = F.GetStateNumber(); | |
849 | } | |
7fd59977 | 850 | return; |
851 | } | |
852 | ||
853 | if (Sort || (F2/PreviousMinimum > Progres)) { | |
854 | Dy = GH*DH; | |
855 | OldF = PreviousMinimum; | |
856 | Stop = Standard_False; | |
857 | Good = Standard_False; | |
858 | DescenteIter = 0; | |
859 | Standard_Boolean Sortbis; | |
860 | ||
861 | // ------------------------------------------------- | |
862 | // Traitement standard sans traitement des bords | |
863 | // ------------------------------------------------- | |
864 | if (!Sort) { // si l'on est pas sortie on essaye de progresser en avant | |
89d8607f | 865 | while((F2/PreviousMinimum > Progres) && !Stop) { |
866 | if (F2 < OldF && (Dy < 0.0)) { | |
867 | // On essaye de progresser dans cette direction. | |
868 | FSR_DEBUG(" iteration de descente = " << DescenteIter); | |
869 | DescenteIter++; | |
870 | SolSave = Sol; | |
871 | OldF = F2; | |
872 | for( i = Sol.Lower(); i <= Sol.Upper(); i++) { | |
873 | Sol(i) = Sol(i) + Ambda * DH(i); | |
874 | } | |
875 | // | |
876 | for (i = 1; i <= Ninc; i++) | |
877 | { | |
75259fc5 | 878 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
89d8607f | 879 | } |
880 | if (theStopOnDivergent & myIsDivergent) | |
881 | { | |
882 | return; | |
883 | } | |
884 | // | |
75259fc5 | 885 | Stop = Bounds(theInfBound, theSupBound, Tol, Sol, SolSave, |
886 | aConstraints, Delta, isNewSol); | |
89d8607f | 887 | FSR_DEBUG(" Augmentation de lambda"); |
888 | Ambda *= 1.7; | |
b659a6dc | 889 | } |
89d8607f | 890 | else { |
891 | if ((F2 >= OldF)||(F2 >= PreviousMinimum)) { | |
892 | Good = Standard_False; | |
893 | if (DescenteIter == 0) { | |
894 | // C'est le premier pas qui flanche, on fait une interpolation. | |
895 | // et on minimise si necessaire. | |
896 | DescenteIter++; | |
897 | Good = MinimizeDirection(SolSave, Delta, OldF, F2, DHSave, GH, | |
898 | Tol, F_Dir); | |
899 | } | |
900 | else if (ChangeDirection || (DescenteIter>1) | |
901 | || (OldF>PreviousMinimum) ) { | |
902 | // La progression a ete utile, on minimise... | |
903 | DescenteIter++; | |
904 | Good = MinimizeDirection(PreviousSolution, SolSave, Sol, | |
905 | OldF, Delta, Tol, F_Dir); | |
906 | } | |
907 | if (!Good) { | |
908 | Sol = SolSave; | |
909 | F2 = OldF; | |
910 | } | |
911 | else { | |
912 | Sol = SolSave+Delta; | |
913 | // | |
914 | for (i = 1; i <= Ninc; i++) | |
915 | { | |
75259fc5 | 916 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
89d8607f | 917 | } |
918 | if (theStopOnDivergent & myIsDivergent) | |
919 | { | |
920 | return; | |
921 | } | |
922 | // | |
75259fc5 | 923 | Sort = Bounds(theInfBound, theSupBound, Tol, Sol, SolSave, |
924 | aConstraints, Delta, isNewSol); | |
89d8607f | 925 | } |
926 | Sort = Standard_False; // On a rejete le point sur la frontiere | |
927 | } | |
928 | Stop = Standard_True; // et on sort dans tous les cas... | |
b659a6dc | 929 | } |
89d8607f | 930 | DHSave = GH; |
3e42bd70 | 931 | if (isNewSol) { |
89d8607f | 932 | // F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1); |
3e42bd70 J |
933 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { |
934 | Done = Standard_False; | |
b659a6dc | 935 | if (!theStopOnDivergent || !myIsDivergent) |
936 | { | |
937 | State = F.GetStateNumber(); | |
938 | } | |
3e42bd70 J |
939 | return; |
940 | } | |
941 | } | |
89d8607f | 942 | Dy = GH*DH; |
943 | if (Abs(Dy) <= Eps) { | |
944 | if (F2 > OldF) | |
3e42bd70 | 945 | Sol = SolSave; |
89d8607f | 946 | Done = Standard_False; |
947 | if (!theStopOnDivergent || !myIsDivergent) | |
948 | { | |
949 | Done = Standard_True; | |
b659a6dc | 950 | ////modified by jgv, 31.08.2011//// |
951 | F.Value(Sol, FF); //update F before GetStateNumber | |
952 | /////////////////////////////////// | |
89d8607f | 953 | State = F.GetStateNumber(); |
954 | } | |
955 | return; | |
956 | } | |
957 | if (DescenteIter >= 100) { | |
958 | Stop = Standard_True; | |
959 | } | |
960 | } | |
961 | FSR_DEBUG("--- Sortie du Traitement Standard"); | |
962 | FSR_DEBUG(" DescenteIter = "<<DescenteIter << " F2 = " << F2); | |
7fd59977 | 963 | } |
964 | // ------------------------------------ | |
965 | // on passe au traitement des bords | |
966 | // ------------------------------------ | |
967 | if (Sort) { | |
89d8607f | 968 | Stop = (F2 > 1.001*OldF); // Pour ne pas progresser sur le bord |
969 | Sortbis = Sort; | |
970 | DescenteIter = 0; | |
971 | while (Sortbis && ((F2<OldF)|| (DescenteIter == 0)) | |
972 | && (!Stop)) { | |
973 | DescenteIter++; | |
974 | // On essaye de progresser sur le bord | |
975 | SolSave = Sol; | |
976 | OldF = F2; | |
75259fc5 | 977 | SearchDirection(DF, GH, FF, aConstraints, Sol, |
89d8607f | 978 | ChangeDirection, InvLengthMax, DH, Dy); |
979 | FSR_DEBUG(" Conditional Direction = " << ChangeDirection); | |
980 | if (Dy<-Eps) { //Pour eviter des calculs inutiles et des /0... | |
981 | if (ChangeDirection) { | |
982 | ||
983 | // Ambda = Ambda2 / Sqrt(Abs(Dy)); | |
984 | Ambda = Ambda2 / Sqrt(-Dy); | |
985 | if (Ambda > 1.0) Ambda = 1.0; | |
986 | } | |
987 | else { | |
988 | Ambda = 1.0; | |
989 | Ambda2 = 0.5*Ambda/DH.Norm(); | |
990 | } | |
991 | ||
992 | for( i = Sol.Lower(); i <= Sol.Upper(); i++) { | |
993 | Sol(i) = Sol(i) + Ambda * DH(i); | |
994 | } | |
995 | // | |
996 | for (i = 1; i <= Ninc; i++) | |
997 | { | |
75259fc5 | 998 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
89d8607f | 999 | } |
1000 | if (theStopOnDivergent & myIsDivergent) | |
1001 | { | |
3e42bd70 J |
1002 | return; |
1003 | } | |
89d8607f | 1004 | // |
75259fc5 | 1005 | Sortbis = Bounds(theInfBound, theSupBound, Tol, Sol, SolSave, |
1006 | aConstraints, Delta, isNewSol); | |
89d8607f | 1007 | |
1008 | DHSave = GH; | |
1009 | if (isNewSol) { | |
1010 | // F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1); | |
1011 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { | |
1012 | Done = Standard_False; | |
1013 | if (!theStopOnDivergent || !myIsDivergent) | |
1014 | { | |
1015 | State = F.GetStateNumber(); | |
1016 | } | |
1017 | return; | |
b659a6dc | 1018 | } |
3e42bd70 | 1019 | } |
89d8607f | 1020 | Ambda2 = Gnr1; |
1021 | FSR_DEBUG("--- Iteration au bords : " << DescenteIter); | |
1022 | FSR_DEBUG("--- F2 = " << F2); | |
3e42bd70 | 1023 | } |
89d8607f | 1024 | else { |
1025 | Stop = Standard_True; | |
1026 | } | |
1027 | ||
1028 | while((F2/PreviousMinimum > Progres) && (F2<OldF) && (!Stop) ) { | |
1029 | DescenteIter++; | |
1030 | FSR_DEBUG("--- Iteration de descente conditionnel = " << DescenteIter); | |
1031 | if (F2 < OldF && Dy < 0.0) { | |
1032 | // On essaye de progresser dans cette direction. | |
1033 | SolSave = Sol; | |
1034 | OldF = F2; | |
1035 | for( i = Sol.Lower(); i <= Sol.Upper(); i++) { | |
1036 | Sol(i) = Sol(i) + Ambda * DH(i); | |
1037 | } | |
1038 | // | |
1039 | for (i = 1; i <= Ninc; i++) | |
b659a6dc | 1040 | { |
75259fc5 | 1041 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
b659a6dc | 1042 | } |
89d8607f | 1043 | if (theStopOnDivergent & myIsDivergent) |
1044 | { | |
1045 | return; | |
1046 | } | |
1047 | // | |
75259fc5 | 1048 | Sortbis = Bounds(theInfBound, theSupBound, Tol, Sol, SolSave, |
1049 | aConstraints, Delta, isNewSol); | |
89d8607f | 1050 | } |
1051 | DHSave = GH; | |
1052 | if (isNewSol) { | |
1053 | // F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1); | |
1054 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { | |
1055 | Done = Standard_False; | |
1056 | if (!theStopOnDivergent || !myIsDivergent) | |
1057 | { | |
1058 | State = F.GetStateNumber(); | |
1059 | } | |
1060 | return; | |
1061 | } | |
1062 | } | |
1063 | Ambda2 = Gnr1; | |
1064 | Dy = GH*DH; | |
1065 | Stop = ((Dy >=0) || (DescenteIter >= 10) || Sortbis); | |
1066 | } | |
1067 | Stop = ((Dy >=0) || (DescenteIter >= 10)); | |
1068 | } | |
1069 | if (((F2/PreviousMinimum > Progres) && | |
1070 | (F2>=OldF))||(F2>=PreviousMinimum)) { | |
1071 | // On minimise par Brent | |
1072 | DescenteIter++; | |
1073 | Good = MinimizeDirection(SolSave, Delta, OldF, F2, | |
1074 | DHSave, GH, Tol, F_Dir); | |
1075 | if (!Good) { | |
1076 | Sol = SolSave; | |
1077 | Sort = Standard_False; | |
1078 | } | |
1079 | else { | |
1080 | Sol = SolSave + Delta; | |
1081 | // | |
1082 | for (i = 1; i <= Ninc; i++) | |
1083 | { | |
75259fc5 | 1084 | myIsDivergent |= (Sol(i) < theInfBound(i)) | (theSupBound(i) < Sol(i)); |
89d8607f | 1085 | } |
1086 | if (theStopOnDivergent & myIsDivergent) | |
1087 | { | |
3e42bd70 J |
1088 | return; |
1089 | } | |
89d8607f | 1090 | // |
75259fc5 | 1091 | Sort = Bounds(theInfBound, theSupBound, Tol, Sol, SolSave, |
1092 | aConstraints, Delta, isNewSol); | |
89d8607f | 1093 | if (isNewSol) { |
1094 | // F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1); | |
1095 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { | |
1096 | Done = Standard_False; | |
1097 | if (!theStopOnDivergent || !myIsDivergent) | |
1098 | { | |
1099 | State = F.GetStateNumber(); | |
1100 | } | |
1101 | return; | |
1102 | } | |
1103 | } | |
1104 | } | |
1105 | Dy = GH*DH; | |
1106 | } | |
1107 | FSR_DEBUG("--- Sortie du Traitement des Bords"); | |
1108 | FSR_DEBUG("--- DescenteIter = "<<DescenteIter << " F2 = " << F2); | |
7fd59977 | 1109 | } |
1110 | } | |
1111 | ||
1112 | // --------------------------------------------- | |
1113 | // on passe aux tests d'ARRET | |
1114 | // --------------------------------------------- | |
1115 | Save(Kount) = F2; | |
1116 | // Est ce la solution ? | |
1117 | if (ChangeDirection) Verif = Standard_True; | |
89d8607f | 1118 | // Gradient : Il faut eviter de boucler |
7fd59977 | 1119 | else { |
1120 | Verif = Standard_False; | |
1121 | if (Kount > 1) { // Pour accelerer les cas quasi-quadratique | |
89d8607f | 1122 | if (Save(Kount-1)<1.e-4*Save(Kount-2)) Verif = Standard_True; |
7fd59977 | 1123 | } |
1124 | else Verif = (F2 < 1.e-6*Save(0)); //Pour les cas dejas solutions | |
1125 | } | |
1126 | if (Verif) { | |
1127 | for(i = Delta.Lower(); i <= Delta.Upper(); i++) { | |
89d8607f | 1128 | Delta(i) = PreviousSolution(i) - Sol(i); |
7fd59977 | 1129 | } |
89d8607f | 1130 | |
7fd59977 | 1131 | if (IsSolutionReached(F)) { |
89d8607f | 1132 | if (PreviousMinimum < F2) { |
1133 | Sol = SolSave; | |
1134 | } | |
1135 | Done = Standard_False; | |
1136 | if (!theStopOnDivergent || !myIsDivergent) | |
1137 | { | |
1138 | Done = Standard_True; | |
b659a6dc | 1139 | ////modified by jgv, 31.08.2011//// |
1140 | F.Value(Sol, FF); //update F before GetStateNumber | |
1141 | /////////////////////////////////// | |
89d8607f | 1142 | State = F.GetStateNumber(); |
1143 | } | |
1144 | return; | |
7fd59977 | 1145 | } |
1146 | } | |
1147 | //fin du test solution | |
89d8607f | 1148 | |
7fd59977 | 1149 | // Analyse de la progression... |
5368adff | 1150 | //comparison of current minimum and previous minimum |
1151 | if ((F2 - PreviousMinimum) <= aTol_Func){ | |
7fd59977 | 1152 | if (Kount > 5) { |
89d8607f | 1153 | // L'historique est il bon ? |
1154 | if (F2 >= 0.95*Save(Kount - 5)) { | |
1155 | if (!ChangeDirection) ChangeDirection = Standard_True; | |
1156 | else | |
1157 | { | |
1158 | Done = Standard_False; | |
1159 | if (!theStopOnDivergent || !myIsDivergent) | |
1160 | { | |
1161 | Done = Standard_True; | |
1162 | State = F.GetStateNumber(); | |
1163 | } | |
1164 | return; // si un gain inf a 5% on sort | |
1165 | } | |
1166 | } | |
1167 | else ChangeDirection = Standard_False; //Si oui on recommence | |
7fd59977 | 1168 | } |
1169 | else ChangeDirection = Standard_False; //Pas d'historique on continue | |
1170 | // Si le gradient ne diminue pas suffisemment par newton on essaie | |
1171 | // le gradient sauf si f diminue (aussi bizarre que cela puisse | |
1172 | // paraitre avec NEWTON le gradient de f peut augmenter alors que f | |
1173 | // diminue: dans ce cas il faut garder NEWTON) | |
1174 | if ((Gnr1 > 0.9*Oldgr) && | |
89d8607f | 1175 | (F2 > 0.5*PreviousMinimum)) { |
1176 | ChangeDirection = Standard_True; | |
7fd59977 | 1177 | } |
1178 | ||
1179 | // Si l'on ne decide pas de changer de strategie, on verifie, | |
1180 | // si ce n'est dejas fait | |
1181 | if ((!ChangeDirection) && (!Verif)) { | |
89d8607f | 1182 | for(i = Delta.Lower(); i <= Delta.Upper(); i++) { |
1183 | Delta(i) = PreviousSolution(i) - Sol(i); | |
1184 | } | |
1185 | if (IsSolutionReached(F)) { | |
1186 | Done = Standard_False; | |
1187 | if (!theStopOnDivergent || !myIsDivergent) | |
1188 | { | |
1189 | Done = Standard_True; | |
b659a6dc | 1190 | ////modified by jgv, 31.08.2011//// |
1191 | F.Value(Sol, FF); //update F before GetStateNumber | |
1192 | /////////////////////////////////// | |
89d8607f | 1193 | State = F.GetStateNumber(); |
1194 | } | |
1195 | return; | |
1196 | } | |
7fd59977 | 1197 | } |
1198 | } | |
1199 | else { // Cas de regression | |
1200 | if (!ChangeDirection) { // On passe au gradient | |
89d8607f | 1201 | ChangeDirection = Standard_True; |
1202 | Sol = PreviousSolution; | |
1203 | // F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1); | |
1204 | if(!F_Dir.Value(Sol, FF, DF, GH, F2, Gnr1)) { | |
1205 | Done = Standard_False; | |
1206 | if (!theStopOnDivergent || !myIsDivergent) | |
1207 | { | |
1208 | State = F.GetStateNumber(); | |
1209 | } | |
1210 | return; | |
1211 | } | |
7fd59977 | 1212 | } |
5368adff | 1213 | else |
1214 | { | |
89d8607f | 1215 | |
b659a6dc | 1216 | if (!theStopOnDivergent || !myIsDivergent) |
1217 | { | |
1218 | State = F.GetStateNumber(); | |
1219 | } | |
5368adff | 1220 | return; // y a plus d'issues |
1221 | } | |
7fd59977 | 1222 | } |
1223 | } | |
b659a6dc | 1224 | if (!theStopOnDivergent || !myIsDivergent) |
1225 | { | |
1226 | State = F.GetStateNumber(); | |
1227 | } | |
7fd59977 | 1228 | } |
1229 | ||
859a47c3 | 1230 | //======================================================================= |
1231 | //function : Dump | |
1232 | //purpose : | |
1233 | //======================================================================= | |
1234 | void math_FunctionSetRoot::Dump(Standard_OStream& o) const | |
1235 | { | |
1236 | o << " math_FunctionSetRoot"; | |
89d8607f | 1237 | if (Done) { |
1238 | o << " Status = Done\n"; | |
1239 | o << " Location value = " << Sol << "\n"; | |
1240 | o << " Number of iterations = " << Kount << "\n"; | |
1241 | } | |
1242 | else { | |
859a47c3 | 1243 | o << "Status = Not Done\n"; |
89d8607f | 1244 | } |
7fd59977 | 1245 | } |
1246 | ||
859a47c3 | 1247 | //======================================================================= |
1248 | //function : Root | |
1249 | //purpose : | |
1250 | //======================================================================= | |
1251 | void math_FunctionSetRoot::Root(math_Vector& Root) const | |
1252 | { | |
7fd59977 | 1253 | StdFail_NotDone_Raise_if(!Done, " "); |
1254 | Standard_DimensionError_Raise_if(Root.Length() != Sol.Length(), " "); | |
1255 | Root = Sol; | |
1256 | } | |
1257 | ||
859a47c3 | 1258 | //======================================================================= |
1259 | //function : FunctionSetErrors | |
1260 | //purpose : | |
1261 | //======================================================================= | |
1262 | void math_FunctionSetRoot::FunctionSetErrors(math_Vector& Err) const | |
1263 | { | |
7fd59977 | 1264 | StdFail_NotDone_Raise_if(!Done, " "); |
1265 | Standard_DimensionError_Raise_if(Err.Length() != Sol.Length(), " "); | |
1266 | Err = Delta; | |
1267 | } |