0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / math / math_BFGS.hxx
1 // Created on: 1991-05-14
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _math_BFGS_HeaderFile
18 #define _math_BFGS_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
23
24 #include <Standard_Boolean.hxx>
25 #include <math_Status.hxx>
26 #include <math_Vector.hxx>
27 #include <Standard_Real.hxx>
28 #include <Standard_Integer.hxx>
29 #include <Standard_OStream.hxx>
30 class StdFail_NotDone;
31 class Standard_DimensionError;
32 class math_MultipleVarFunctionWithGradient;
33
34
35
36 //! This class implements the Broyden-Fletcher-Goldfarb-Shanno variant of
37 //! Davidson-Fletcher-Powell minimization algorithm of a function of
38 //! multiple variables.Knowledge of the function's gradient is required.
39 //!
40 //! It is possible to solve conditional optimization problem on hyperparallelepiped.
41 //! Method SetBoundary is used to define hyperparallelepiped borders. With boundaries
42 //! defined, the algorithm will not make evaluations of the function outside of the
43 //! borders.
44 class math_BFGS 
45 {
46 public:
47
48   DEFINE_STANDARD_ALLOC
49
50   
51   //! Initializes the computation of the minimum of a function with
52   //! NbVariables.
53   //! Tolerance, ZEPS and NbIterations are described in the method Perform.
54   //! Warning:
55   //! A call to the Perform method must be made after this
56   //! initialization to effectively compute the minimum of the
57   //! function F.
58   Standard_EXPORT math_BFGS(const Standard_Integer NbVariables, const Standard_Real Tolerance = 1.0e-8, const Standard_Integer NbIterations = 200, const Standard_Real ZEPS = 1.0e-12);
59
60   Standard_EXPORT virtual ~math_BFGS();
61
62   //! Set boundaries for conditional optimization.
63   //! The expected indices range of vectors is [1, NbVariables].
64   Standard_EXPORT void SetBoundary(const math_Vector& theLeftBorder, const math_Vector& theRightBorder);
65
66   //! Given the starting point StartingPoint,
67   //! minimization is done on the function F.
68   //! The solution F = Fi is found when :
69   //! 2.0 * abs(Fi - Fi-1) <= Tolerance * (abs(Fi) + abs(Fi-1) + ZEPS).
70   //! Tolerance, ZEPS and maximum number of iterations are given
71   //! in the constructor.
72   Standard_EXPORT void Perform (math_MultipleVarFunctionWithGradient& F, const math_Vector& StartingPoint);
73   
74
75   //! This method is called at the end of each iteration to check if the
76   //! solution is found.
77   //! It can be redefined in a sub-class to implement a specific test to
78   //! stop the iterations.
79   Standard_EXPORT virtual Standard_Boolean IsSolutionReached (math_MultipleVarFunctionWithGradient& F) const;
80   
81   //! Returns true if the computations are successful, otherwise returns false.
82     Standard_Boolean IsDone() const;
83   
84   //! returns the location vector of the minimum.
85   //! Exception NotDone is raised if the minimum was not found.
86     const math_Vector& Location() const;
87   
88   //! outputs the location vector of the minimum in Loc.
89   //! Exception NotDone is raised if the minimum was not found.
90   //! Exception DimensionError is raised if the range of Loc is not
91   //! equal to the range of the StartingPoint.
92     void Location (math_Vector& Loc) const;
93   
94   //! returns the value of the minimum.
95   //! Exception NotDone is raised if the minimum was not found.
96     Standard_Real Minimum() const;
97   
98   //! Returns the gradient vector at the minimum.
99   //! Exception NotDone is raised if the minimum was not found.
100     const math_Vector& Gradient() const;
101   
102   //! Returns the value of the gradient vector at the minimum in Grad.
103   //! Exception NotDone is raised if the minimum was not found.
104   //! Exception DimensionError is raised if the range of Grad is not
105   //! equal to the range of the StartingPoint.
106     void Gradient (math_Vector& Grad) const;
107   
108   //! Returns the number of iterations really done in the
109   //! calculation of the minimum.
110   //! The exception NotDone is raised if the minimum was not found.
111     Standard_Integer NbIterations() const;
112   
113   //! Prints on the stream o information on the current state
114   //! of the object.
115   //! Is used to redefine the operator <<.
116   Standard_EXPORT void Dump (Standard_OStream& o) const;
117
118
119
120
121 protected:
122
123
124
125   math_Status TheStatus;
126   math_Vector TheLocation;
127   math_Vector TheGradient;
128   Standard_Real PreviousMinimum;
129   Standard_Real TheMinimum;
130   Standard_Real XTol;
131   Standard_Real EPSZ;
132   Standard_Integer nbiter;
133   Standard_Boolean myIsBoundsDefined;
134   math_Vector myLeft;
135   math_Vector myRight;
136
137
138 private:
139
140
141
142   Standard_Boolean Done;
143   Standard_Integer Itermax;
144
145
146 };
147
148
149 #include <math_BFGS.lxx>
150
151
152
153
154
155 #endif // _math_BFGS_HeaderFile