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
22 #include <math_BracketMinimum.hxx>
23 #include <math_BrentMinimum.hxx>
24 #include <math_FRPR.hxx>
25 #include <math_Function.hxx>
26 #include <math_MultipleVarFunction.hxx>
27 #include <math_MultipleVarFunctionWithGradient.hxx>
28 #include <Standard_DimensionError.hxx>
29 #include <StdFail_NotDone.hxx>
31 // l'utilisation de math_BrentMinumim pur trouver un minimum dans une direction
32 // donnee n'est pas du tout optimale. voir peut etre interpolation cubique
33 // classique et aussi essayer "recherche unidimensionnelle economique"
34 // PROGRAMMATION MATHEMATIQUE (theorie et algorithmes) tome1 page 82.
35 class DirFunctionTer : public math_Function {
40 math_MultipleVarFunction *F;
44 DirFunctionTer(math_Vector& V1,
47 math_MultipleVarFunction& f);
49 void Initialize(const math_Vector& p0, const math_Vector& dir);
51 virtual Standard_Boolean Value(const Standard_Real x, Standard_Real& fval);
54 DirFunctionTer::DirFunctionTer(math_Vector& V1,
57 math_MultipleVarFunction& f) {
65 void DirFunctionTer::Initialize(const math_Vector& p0,
66 const math_Vector& dir) {
72 Standard_Boolean DirFunctionTer::Value(const Standard_Real x, Standard_Real& fval) {
78 return F->Value(*P, fval);
81 static Standard_Boolean MinimizeDirection(math_Vector& P,
83 Standard_Real& Result,
86 Standard_Real ax, xx, bx;
89 math_BracketMinimum Bracket(F, 0.0, 1.0);
90 if(Bracket.IsDone()) {
91 Bracket.Values(ax, xx, bx);
92 math_BrentMinimum Sol(1.e-10);
93 Sol.Perform(F, ax, xx, bx);
95 Standard_Real Scale = Sol.Location();
96 Result = Sol.Minimum();
102 return Standard_False;
105 //=======================================================================
106 //function : math_FRPR
107 //purpose : Constructor
108 //=======================================================================
109 math_FRPR::math_FRPR(const math_MultipleVarFunctionWithGradient& theFunction,
110 const Standard_Real theTolerance,
111 const Standard_Integer theNbIterations,
112 const Standard_Real theZEPS)
114 : TheLocation(1, theFunction.NbVariables()),
115 TheGradient(1, theFunction.NbVariables()),
117 PreviousMinimum(0.0),
120 Done (Standard_False),
123 TheStatus (math_NotBracketed),
124 Itermax (theNbIterations)
128 //=======================================================================
129 //function : ~math_FRPR
130 //purpose : Destructor
131 //=======================================================================
132 math_FRPR::~math_FRPR()
137 //=======================================================================
140 //=======================================================================
141 void math_FRPR::Perform(math_MultipleVarFunctionWithGradient& F,
142 const math_Vector& StartingPoint)
144 Standard_Boolean Good;
145 Standard_Integer n = TheLocation.Length();
146 Standard_Integer j, its;
147 Standard_Real gg, gam, dgg;
149 math_Vector g(1, n), h(1, n);
151 math_Vector Temp1(1, n);
152 math_Vector Temp2(1, n);
153 math_Vector Temp3(1, n);
154 DirFunctionTer F_Dir(Temp1, Temp2, Temp3, F);
156 TheLocation = StartingPoint;
157 Good = F.Values(TheLocation, PreviousMinimum, TheGradient);
159 Done = Standard_False;
160 TheStatus = math_FunctionError;
168 for(its = 1; its <= Itermax; its++) {
171 Standard_Boolean IsGood = MinimizeDirection(TheLocation,
172 TheGradient, TheMinimum, F_Dir);
174 Done = Standard_False;
175 TheStatus = math_DirectionSearchError;
178 if(IsSolutionReached(F)) {
179 Done = Standard_True;
180 State = F.GetStateNumber();
184 Good = F.Values(TheLocation, PreviousMinimum, TheGradient);
186 Done = Standard_False;
187 TheStatus = math_FunctionError;
194 for(j = 1; j<= n; j++) {
196 // dgg += TheGradient(j)*TheGradient(j); //for Fletcher-Reeves
197 dgg += (TheGradient(j)+g(j)) * TheGradient(j); //for Polak-Ribiere
201 //Unlikely. If gradient is exactly 0 then we are already done.
202 Done = Standard_False;
203 TheStatus = math_FunctionError;
209 TheGradient = g + gam*h;
212 Done = Standard_False;
213 TheStatus = math_TooManyIterations;
217 //=======================================================================
220 //=======================================================================
221 void math_FRPR::Dump(Standard_OStream& o) const
225 o << " Status = Done \n";
226 o << " Location Vector = "<< TheLocation << "\n";
227 o << " Minimum value = " << TheMinimum <<"\n";
228 o << " Number of iterations = " << Iter <<"\n";
231 o << " Status = not Done because " << (Standard_Integer)TheStatus << "\n";