0026687: SIGSEGV in BRepBuilderAPI_MakeFace
[occt.git] / src / math / math_GlobOptMin.hxx
CommitLineData
4bbaf12b 1// Created on: 2014-01-20
2// Created by: Alexaner Malyshev
4b65fc77 3// Copyright (c) 2014-2015 OPEN CASCADE SAS
4bbaf12b 4//
5// This file is part of Open CASCADE Technology software library.
6//
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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#ifndef _math_GlobOptMin_HeaderFile
17#define _math_GlobOptMin_HeaderFile
18
4b65fc77 19#include <gp_Pnt.hxx>
20#include <gp_Pnt2d.hxx>
50bc8f96 21#include <NCollection_CellFilter.hxx>
4bbaf12b 22#include <math_MultipleVarFunction.hxx>
23#include <NCollection_Sequence.hxx>
24#include <Standard_Type.hxx>
25
50bc8f96 26class NCollection_CellFilter_Inspector
4b65fc77 27{
28public:
29
30 //! Points and target type
31 typedef math_Vector Point;
32 typedef math_Vector Target;
33
50bc8f96 34 NCollection_CellFilter_Inspector(const Standard_Integer theDim,
35 const Standard_Real theTol)
4b65fc77 36 : myCurrent(1, theDim)
37 {
38 myTol = theTol * theTol;
39 myIsFind = Standard_False;
40 Dimension = theDim;
41 }
42
43 //! Access to co-ordinate
44 static Standard_Real Coord (int i, const Point &thePnt)
45 {
46 return thePnt(i + 1);
47 }
48
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,
53 Point& theLowPnt,
54 Point& theUppPnt) const
55 {
56 for(Standard_Integer anIdx = 1; anIdx <= Dimension; anIdx++)
57 {
58 theLowPnt(anIdx) = thePnt(anIdx) - theTol(anIdx - 1);
59 theUppPnt(anIdx) = thePnt(anIdx) + theTol(anIdx - 1);
60 }
61 }
62
63 void ClearFind()
64 {
65 myIsFind = Standard_False;
66 }
67
68 Standard_Boolean isFind()
69 {
70 return myIsFind;
71 }
72
73 //! Set current point to search for coincidence
74 void SetCurrent (const math_Vector& theCurPnt)
75 {
76 myCurrent = theCurPnt;
77 }
78
79 //! Implementation of inspection method
80 NCollection_CellFilter_Action Inspect (const Target& theObject)
81 {
82 Standard_Real aSqDist = (myCurrent - theObject).Norm2();
83
84 if(aSqDist < myTol)
85 {
86 myIsFind = Standard_True;
87 }
88
89 return CellFilter_Keep;
90 }
91
92private:
93 Standard_Real myTol;
94 math_Vector myCurrent;
95 Standard_Boolean myIsFind;
96 Standard_Integer Dimension;
97};
98
4bbaf12b 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.
102
103class math_GlobOptMin
104{
105public:
106
107 Standard_EXPORT math_GlobOptMin(math_MultipleVarFunction* theFunc,
3f733bb1 108 const math_Vector& theLowerBorder,
109 const math_Vector& theUpperBorder,
5493d334 110 const Standard_Real theC = 9,
111 const Standard_Real theDiscretizationTol = 1.0e-2,
112 const Standard_Real theSameTol = 1.0e-7);
4bbaf12b 113
114 Standard_EXPORT void SetGlobalParams(math_MultipleVarFunction* theFunc,
3f733bb1 115 const math_Vector& theLowerBorder,
116 const math_Vector& theUpperBorder,
5493d334 117 const Standard_Real theC = 9,
118 const Standard_Real theDiscretizationTol = 1.0e-2,
119 const Standard_Real theSameTol = 1.0e-7);
4bbaf12b 120
121 Standard_EXPORT void SetLocalParams(const math_Vector& theLocalA,
122 const math_Vector& theLocalB);
123
5493d334 124 Standard_EXPORT void SetTol(const Standard_Real theDiscretizationTol,
125 const Standard_Real theSameTol);
126
127 Standard_EXPORT void GetTol(Standard_Real& theDiscretizationTol,
128 Standard_Real& theSameTol);
129
4bbaf12b 130 Standard_EXPORT ~math_GlobOptMin();
131
78e7cada 132 //! @param isFindSingleSolution - defines whether to find single solution or all solutions.
133 Standard_EXPORT void Perform(const Standard_Boolean isFindSingleSolution = Standard_False);
4bbaf12b 134
135 //! Get best functional value.
136 Standard_EXPORT Standard_Real GetF();
137
797d11c6 138 //! Return count of global extremas.
4bbaf12b 139 Standard_EXPORT Standard_Integer NbExtrema();
140
3f733bb1 141 //! Return solution theIndex, 1 <= theIndex <= NbExtrema.
4bbaf12b 142 Standard_EXPORT void Points(const Standard_Integer theIndex, math_Vector& theSol);
143
5493d334 144 Standard_Boolean isDone();
145
4bbaf12b 146private:
147
4b65fc77 148 // Compute cell size.
149 void initCellSize();
150
4bbaf12b 151 math_GlobOptMin & operator = (const math_GlobOptMin & theOther);
152
153 Standard_Boolean computeLocalExtremum(const math_Vector& thePnt, Standard_Real& theVal, math_Vector& theOutPnt);
154
155 void computeGlobalExtremum(Standard_Integer theIndex);
156
797d11c6 157 //! Computes starting value / approximation:
3f733bb1 158 //! myF - initial best value.
159 //! myY - initial best point.
160 //! myC - approximation of Lipschitz constant.
161 //! to imporve convergence speed.
797d11c6 162 void computeInitialValues();
163
3f733bb1 164 //! Check that myA <= thePnt <= myB
4bbaf12b 165 Standard_Boolean isInside(const math_Vector& thePnt);
166
3f733bb1 167 //! Check presence of thePnt in GlobOpt sequence.
4bbaf12b 168 Standard_Boolean isStored(const math_Vector &thePnt);
4bbaf12b 169
170 // Input.
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.
5493d334 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
78e7cada 182 Standard_Boolean myIsFindSingleSolution; // Default value is false.
4bbaf12b 183
184 // Output.
185 Standard_Boolean myDone;
186 NCollection_Sequence<Standard_Real> myY;// Current solutions.
187 Standard_Integer mySolCount; // Count of solutions.
188
189 // Algorithm data.
190 Standard_Real myZ;
4bbaf12b 191 Standard_Real myE1; // Border coeff.
192 Standard_Real myE2; // Minimum step size.
193 Standard_Real myE3; // Local extrema starting parameter.
194
5493d334 195 math_Vector myX; // Current modified solution.
196 math_Vector myTmp; // Current modified solution.
4bbaf12b 197 math_Vector myV; // Steps array.
5493d334 198 math_Vector myMaxV; // Max Steps array.
3f733bb1 199 math_Vector myExpandCoeff; // Define expand coefficient between neighboring indiced dimensions.
4bbaf12b 200
4b65fc77 201 NCollection_Array1<Standard_Real> myCellSize;
202 Standard_Integer myMinCellFilterSol;
203 Standard_Boolean isFirstCellFilterInvoke;
50bc8f96 204 NCollection_CellFilter<NCollection_CellFilter_Inspector> myFilter;
4b65fc77 205
4bbaf12b 206 Standard_Real myF; // Current value of Global optimum.
207};
208
4bbaf12b 209#endif