1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #define No_Standard_RangeError
17 #define No_Standard_OutOfRange
18 #define No_Standard_DimensionError
21 #include <math_FRPR.ixx>
23 #include <math_BracketMinimum.hxx>
24 #include <math_BrentMinimum.hxx>
25 #include <math_Function.hxx>
26 #include <math_MultipleVarFunction.hxx>
27 #include <math_MultipleVarFunctionWithGradient.hxx>
29 // l'utilisation de math_BrentMinumim pur trouver un minimum dans une direction
30 // donnee n'est pas du tout optimale. voir peut etre interpolation cubique
31 // classique et aussi essayer "recherche unidimensionnelle economique"
32 // PROGRAMMATION MATHEMATIQUE (theorie et algorithmes) tome1 page 82.
34 class DirFunctionTer : public math_Function {
39 math_MultipleVarFunction *F;
43 DirFunctionTer(math_Vector& V1,
46 math_MultipleVarFunction& f);
48 void Initialize(const math_Vector& p0, const math_Vector& dir);
50 virtual Standard_Boolean Value(const Standard_Real x, Standard_Real& fval);
53 DirFunctionTer::DirFunctionTer(math_Vector& V1,
56 math_MultipleVarFunction& f) {
64 void DirFunctionTer::Initialize(const math_Vector& p0,
65 const math_Vector& dir) {
71 Standard_Boolean DirFunctionTer::Value(const Standard_Real x, Standard_Real& fval) {
80 static Standard_Boolean MinimizeDirection(math_Vector& P,
82 Standard_Real& Result,
85 Standard_Real ax, xx, bx;
88 math_BracketMinimum Bracket(F, 0.0, 1.0);
89 if(Bracket.IsDone()) {
90 Bracket.Values(ax, xx, bx);
91 math_BrentMinimum Sol(F, ax, xx, bx, 1.0e-10, 100);
93 Standard_Real Scale = Sol.Location();
94 Result = Sol.Minimum();
100 return Standard_False;
104 void math_FRPR::Perform(math_MultipleVarFunctionWithGradient& F,
105 const math_Vector& StartingPoint) {
107 Standard_Boolean Good;
108 Standard_Integer n = TheLocation.Length();
109 Standard_Integer j, its;
110 Standard_Real gg, gam, dgg;
112 math_Vector g(1, n), h(1, n);
114 math_Vector Temp1(1, n);
115 math_Vector Temp2(1, n);
116 math_Vector Temp3(1, n);
117 DirFunctionTer F_Dir(Temp1, Temp2, Temp3, F);
119 TheLocation = StartingPoint;
120 Good = F.Values(TheLocation, PreviousMinimum, TheGradient);
122 Done = Standard_False;
123 TheStatus = math_FunctionError;
131 for(its = 1; its <= Itermax; its++) {
134 Standard_Boolean IsGood = MinimizeDirection(TheLocation,
135 TheGradient, TheMinimum, F_Dir);
137 Done = Standard_False;
138 TheStatus = math_DirectionSearchError;
141 if(IsSolutionReached(F)) {
142 Done = Standard_True;
143 State = F.GetStateNumber();
147 Good = F.Values(TheLocation, PreviousMinimum, TheGradient);
149 Done = Standard_False;
150 TheStatus = math_FunctionError;
157 for(j = 1; j<= n; j++) {
159 // dgg += TheGradient(j)*TheGradient(j); //for Fletcher-Reeves
160 dgg += (TheGradient(j)+g(j)) * TheGradient(j); //for Polak-Ribiere
164 //Unlikely. If gradient is exactly 0 then we are already done.
165 Done = Standard_False;
166 TheStatus = math_FunctionError;
172 TheGradient = g + gam*h;
175 Done = Standard_False;
176 TheStatus = math_TooManyIterations;
183 Standard_Boolean math_FRPR::IsSolutionReached(
184 // math_MultipleVarFunctionWithGradient& F) {
185 math_MultipleVarFunctionWithGradient& ) {
187 return (2.0 * fabs(TheMinimum - PreviousMinimum)) <=
188 XTol * (fabs(TheMinimum) + fabs(PreviousMinimum) + EPSZ);
191 math_FRPR::math_FRPR(math_MultipleVarFunctionWithGradient& F,
192 const math_Vector& StartingPoint,
193 const Standard_Real Tolerance,
194 const Standard_Integer NbIterations,
195 const Standard_Real ZEPS)
196 : TheLocation(1, StartingPoint.Length()),
197 TheGradient(1, StartingPoint.Length()) {
201 Itermax = NbIterations;
202 Perform(F, StartingPoint);
206 math_FRPR::math_FRPR(math_MultipleVarFunctionWithGradient& F,
207 const Standard_Real Tolerance,
208 const Standard_Integer NbIterations,
209 const Standard_Real ZEPS)
210 : TheLocation(1, F.NbVariables()),
211 TheGradient(1, F.NbVariables()) {
215 Itermax = NbIterations;
219 math_FRPR::~math_FRPR()
223 void math_FRPR::Dump(Standard_OStream& o) const {
227 o << " Status = Done \n";
228 o << " Location Vector = "<< TheLocation << "\n";
229 o << " Minimum value = " << TheMinimum <<"\n";
230 o << " Number of iterations = " << Iter <<"\n";
233 o << " Status = not Done because " << (Standard_Integer)TheStatus << "\n";