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(1.e-10);
92 Sol.Perform(F, ax, xx, bx);
94 Standard_Real Scale = Sol.Location();
95 Result = Sol.Minimum();
101 return Standard_False;
104 //=======================================================================
105 //function : math_FRPR
106 //purpose : Constructor
107 //=======================================================================
108 math_FRPR::math_FRPR(const math_MultipleVarFunctionWithGradient& theFunction,
109 const Standard_Real theTolerance,
110 const Standard_Integer theNbIterations,
111 const Standard_Real theZEPS)
113 : TheLocation(1, theFunction.NbVariables()),
114 TheGradient(1, theFunction.NbVariables()),
116 PreviousMinimum(0.0),
119 Done (Standard_False),
122 TheStatus (math_NotBracketed),
123 Itermax (theNbIterations)
127 //=======================================================================
128 //function : ~math_FRPR
129 //purpose : Destructor
130 //=======================================================================
131 math_FRPR::~math_FRPR()
136 //=======================================================================
139 //=======================================================================
140 void math_FRPR::Perform(math_MultipleVarFunctionWithGradient& F,
141 const math_Vector& StartingPoint)
143 Standard_Boolean Good;
144 Standard_Integer n = TheLocation.Length();
145 Standard_Integer j, its;
146 Standard_Real gg, gam, dgg;
148 math_Vector g(1, n), h(1, n);
150 math_Vector Temp1(1, n);
151 math_Vector Temp2(1, n);
152 math_Vector Temp3(1, n);
153 DirFunctionTer F_Dir(Temp1, Temp2, Temp3, F);
155 TheLocation = StartingPoint;
156 Good = F.Values(TheLocation, PreviousMinimum, TheGradient);
158 Done = Standard_False;
159 TheStatus = math_FunctionError;
167 for(its = 1; its <= Itermax; its++) {
170 Standard_Boolean IsGood = MinimizeDirection(TheLocation,
171 TheGradient, TheMinimum, F_Dir);
173 Done = Standard_False;
174 TheStatus = math_DirectionSearchError;
177 if(IsSolutionReached(F)) {
178 Done = Standard_True;
179 State = F.GetStateNumber();
183 Good = F.Values(TheLocation, PreviousMinimum, TheGradient);
185 Done = Standard_False;
186 TheStatus = math_FunctionError;
193 for(j = 1; j<= n; j++) {
195 // dgg += TheGradient(j)*TheGradient(j); //for Fletcher-Reeves
196 dgg += (TheGradient(j)+g(j)) * TheGradient(j); //for Polak-Ribiere
200 //Unlikely. If gradient is exactly 0 then we are already done.
201 Done = Standard_False;
202 TheStatus = math_FunctionError;
208 TheGradient = g + gam*h;
211 Done = Standard_False;
212 TheStatus = math_TooManyIterations;
216 //=======================================================================
219 //=======================================================================
220 void math_FRPR::Dump(Standard_OStream& o) const
224 o << " Status = Done \n";
225 o << " Location Vector = "<< TheLocation << "\n";
226 o << " Minimum value = " << TheMinimum <<"\n";
227 o << " Number of iterations = " << Iter <<"\n";
230 o << " Status = not Done because " << (Standard_Integer)TheStatus << "\n";