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