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_Function.hxx>
25 #include <math_Matrix.hxx>
26 #include <math_MultipleVarFunction.hxx>
27 #include <math_Powell.hxx>
28 #include <Standard_DimensionError.hxx>
29 #include <StdFail_NotDone.hxx>
32 static inline Standard_Real SQR (const Standard_Real a)
38 class DirFunctionBis : public math_Function {
43 math_MultipleVarFunction *F;
46 DirFunctionBis(math_Vector& V1,
49 math_MultipleVarFunction& f);
51 void Initialize(const math_Vector& p0, const math_Vector& dir);
53 virtual Standard_Boolean Value(const Standard_Real x, Standard_Real& fval);
56 DirFunctionBis::DirFunctionBis(math_Vector& V1,
59 math_MultipleVarFunction& f) {
67 void DirFunctionBis::Initialize(const math_Vector& p0,
68 const math_Vector& dir) {
74 Standard_Boolean DirFunctionBis::Value(const Standard_Real x, Standard_Real& fval) {
83 static Standard_Boolean MinimizeDirection(math_Vector& P,
85 Standard_Real& Result,
95 math_BracketMinimum Bracket(F, 0.0, 1.0);
96 if (Bracket.IsDone()) {
97 Bracket.Values(ax, xx, bx);
98 math_BrentMinimum Sol(1.0e-10);
99 Sol.Perform(F, ax, xx, bx);
101 Standard_Real Scale = Sol.Location();
102 Result = Sol.Minimum();
105 return Standard_True;
108 return Standard_False;
111 //=======================================================================
112 //function : math_Powell
113 //purpose : Constructor
114 //=======================================================================
115 math_Powell::math_Powell(const math_MultipleVarFunction& theFunction,
116 const Standard_Real theTolerance,
117 const Standard_Integer theNbIterations,
118 const Standard_Real theZEPS)
119 : TheLocation (1, theFunction.NbVariables()),
120 TheMinimum (RealLast()),
121 TheLocationError(RealLast()),
122 PreviousMinimum (RealLast()),
125 Done (Standard_False),
127 TheStatus (math_NotBracketed),
128 TheDirections (1, theFunction.NbVariables(), 1, theFunction.NbVariables()),
130 Itermax (theNbIterations)
134 //=======================================================================
135 //function : ~math_Powell
136 //purpose : Destructor
137 //=======================================================================
138 math_Powell::~math_Powell()
142 //=======================================================================
145 //=======================================================================
146 void math_Powell::Perform(math_MultipleVarFunction& F,
147 const math_Vector& StartingPoint,
148 const math_Matrix& StartingDirections)
150 Done = Standard_False;
151 Standard_Integer i, ibig, j;
152 Standard_Real t, fptt, del;
153 Standard_Integer n = TheLocation.Length();
155 math_Vector ptt(1,n);
156 math_Vector xit(1,n);
157 math_Vector Temp1(1, n);
158 math_Vector Temp2(1, n);
159 math_Vector Temp3(1, n);
160 DirFunctionBis F_Dir(Temp1, Temp2, Temp3, F);
162 TheLocation = StartingPoint;
163 TheDirections = StartingDirections;
164 pt = TheLocation; //sauvegarde du point initial
167 for(Iter = 1; Iter<= Itermax; Iter++) {
168 F.Value(TheLocation, PreviousMinimum);
171 for (i = 1; i <= n; i++){
172 for(j =1; j<= n; j++) xit(j) = TheDirections(j,i);
173 F.Value(TheLocation, fptt);
174 Standard_Boolean IsGood = MinimizeDirection(TheLocation, xit,
178 Done = Standard_False;
179 TheStatus = math_DirectionSearchError;
183 if (fabs(fptt - TheMinimum)> del) {
184 del = fabs(fptt- TheMinimum);
189 if (IsSolutionReached(F)) {
190 //Termination criterion
191 State = F.GetStateNumber();
192 Done = Standard_True;
197 if (Iter == Itermax) {
198 Done = Standard_False;
199 TheStatus = math_TooManyIterations;
203 ptt = 2.0 * TheLocation - pt;
204 xit = TheLocation - pt;
207 // Valeur de la fonction au point extrapole:
211 if (fptt < PreviousMinimum) {
212 t = 2.0 *(PreviousMinimum -2.0*TheMinimum +fptt)*
213 SQR(PreviousMinimum-TheMinimum -del)-del*
214 SQR(PreviousMinimum-fptt);
216 //Minimisation along the direction
217 Standard_Boolean IsGood = MinimizeDirection(TheLocation, xit,
220 Done = Standard_False;
221 TheStatus = math_FunctionError;
225 for(j =1; j <= n; j++) {
226 TheDirections(j, ibig)=xit(j);
233 //=======================================================================
236 //=======================================================================
237 void math_Powell::Dump(Standard_OStream& o) const
239 o << "math_Powell resolution:";
241 o << " Status = Done \n";
242 o << " Location Vector = "<< TheLocation << "\n";
243 o << " Minimum value = " << TheMinimum <<"\n";
244 o << " Number of iterations = " << Iter <<"\n";
247 o << " Status = not Done because " << (Standard_Integer)TheStatus << "\n";