1 // Created on: 2014-01-20
2 // Created by: Alexaner Malyshev
3 // Copyright (c) 2014-2015 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _math_GlobOptMin_HeaderFile
17 #define _math_GlobOptMin_HeaderFile
20 #include <gp_Pnt2d.hxx>
21 #include <NCollection_CellFilter.hxx>
22 #include <math_MultipleVarFunction.hxx>
23 #include <NCollection_Sequence.hxx>
24 #include <Standard_Type.hxx>
26 class NCollection_CellFilter_Inspector
30 //! Points and target type
31 typedef math_Vector Point;
32 typedef math_Vector Target;
34 NCollection_CellFilter_Inspector(const Standard_Integer theDim,
35 const Standard_Real theTol)
36 : myCurrent(1, theDim)
38 myTol = theTol * theTol;
39 myIsFind = Standard_False;
43 //! Access to co-ordinate
44 static Standard_Real Coord (int i, const Point &thePnt)
49 //! Auxiliary method to shift point by each coordinate on given value;
50 //! useful for preparing a points range for Inspect with tolerance
51 void Shift (const Point& thePnt,
52 const NCollection_Array1<Standard_Real> &theTol,
54 Point& theUppPnt) const
56 for(Standard_Integer anIdx = 1; anIdx <= Dimension; anIdx++)
58 theLowPnt(anIdx) = thePnt(anIdx) - theTol(anIdx - 1);
59 theUppPnt(anIdx) = thePnt(anIdx) + theTol(anIdx - 1);
65 myIsFind = Standard_False;
68 Standard_Boolean isFind()
73 //! Set current point to search for coincidence
74 void SetCurrent (const math_Vector& theCurPnt)
76 myCurrent = theCurPnt;
79 //! Implementation of inspection method
80 NCollection_CellFilter_Action Inspect (const Target& theObject)
82 Standard_Real aSqDist = (myCurrent - theObject).Norm2();
86 myIsFind = Standard_True;
89 return CellFilter_Keep;
94 math_Vector myCurrent;
95 Standard_Boolean myIsFind;
96 Standard_Integer Dimension;
99 //! This class represents Evtushenko's algorithm of global optimization based on nonuniform mesh.<br>
100 //! Article: Yu. Evtushenko. Numerical methods for finding global extreme (case of a non-uniform mesh). <br>
101 //! U.S.S.R. Comput. Maths. Math. Phys., Vol. 11, N 6, pp. 38-54.
103 class math_GlobOptMin
107 Standard_EXPORT math_GlobOptMin(math_MultipleVarFunction* theFunc,
108 const math_Vector& theLowerBorder,
109 const math_Vector& theUpperBorder,
110 const Standard_Real theC = 9,
111 const Standard_Real theDiscretizationTol = 1.0e-2,
112 const Standard_Real theSameTol = 1.0e-7);
114 Standard_EXPORT void SetGlobalParams(math_MultipleVarFunction* theFunc,
115 const math_Vector& theLowerBorder,
116 const math_Vector& theUpperBorder,
117 const Standard_Real theC = 9,
118 const Standard_Real theDiscretizationTol = 1.0e-2,
119 const Standard_Real theSameTol = 1.0e-7);
121 Standard_EXPORT void SetLocalParams(const math_Vector& theLocalA,
122 const math_Vector& theLocalB);
124 Standard_EXPORT void SetTol(const Standard_Real theDiscretizationTol,
125 const Standard_Real theSameTol);
127 Standard_EXPORT void GetTol(Standard_Real& theDiscretizationTol,
128 Standard_Real& theSameTol);
130 Standard_EXPORT ~math_GlobOptMin();
132 //! @param isFindSingleSolution - defines whether to find single solution or all solutions.
133 Standard_EXPORT void Perform(const Standard_Boolean isFindSingleSolution = Standard_False);
135 //! Get best functional value.
136 Standard_EXPORT Standard_Real GetF();
138 //! Return count of global extremas.
139 Standard_EXPORT Standard_Integer NbExtrema();
141 //! Return solution theIndex, 1 <= theIndex <= NbExtrema.
142 Standard_EXPORT void Points(const Standard_Integer theIndex, math_Vector& theSol);
144 Standard_Boolean isDone();
148 // Compute cell size.
151 math_GlobOptMin & operator = (const math_GlobOptMin & theOther);
153 Standard_Boolean computeLocalExtremum(const math_Vector& thePnt, Standard_Real& theVal, math_Vector& theOutPnt);
155 void computeGlobalExtremum(Standard_Integer theIndex);
157 //! Computes starting value / approximation:
158 //! myF - initial best value.
159 //! myY - initial best point.
160 //! myC - approximation of Lipschitz constant.
161 //! to imporve convergence speed.
162 void computeInitialValues();
164 //! Check that myA <= thePnt <= myB
165 Standard_Boolean isInside(const math_Vector& thePnt);
167 //! Check presence of thePnt in GlobOpt sequence.
168 Standard_Boolean isStored(const math_Vector &thePnt);
171 math_MultipleVarFunction* myFunc;
172 Standard_Integer myN;
173 math_Vector myA; // Left border on current C2 interval.
174 math_Vector myB; // Right border on current C2 interval.
175 math_Vector myGlobA; // Global left border.
176 math_Vector myGlobB; // Global right border.
177 Standard_Real myTol; // Discretization tolerance, default 1.0e-2.
178 Standard_Real mySameTol; // points with ||p1 - p2|| < mySameTol is equal,
179 // function values |val1 - val2| * 0.01 < mySameTol is equal,
180 // default value is 1.0e-7.
181 Standard_Real myC; //Lipschitz constant, default 9
182 Standard_Boolean myIsFindSingleSolution; // Default value is false.
185 Standard_Boolean myDone;
186 NCollection_Sequence<Standard_Real> myY;// Current solutions.
187 Standard_Integer mySolCount; // Count of solutions.
191 Standard_Real myE1; // Border coeff.
192 Standard_Real myE2; // Minimum step size.
193 Standard_Real myE3; // Local extrema starting parameter.
195 math_Vector myX; // Current modified solution.
196 math_Vector myTmp; // Current modified solution.
197 math_Vector myV; // Steps array.
198 math_Vector myMaxV; // Max Steps array.
199 math_Vector myExpandCoeff; // Define expand coefficient between neighboring indiced dimensions.
201 NCollection_Array1<Standard_Real> myCellSize;
202 Standard_Integer myMinCellFilterSol;
203 Standard_Boolean isFirstCellFilterInvoke;
204 NCollection_CellFilter<NCollection_CellFilter_Inspector> myFilter;
206 Standard_Real myF; // Current value of Global optimum.