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 | #include <math_GlobOptMin.hxx> |
17 | |
18 | #include <math_BFGS.hxx> |
19 | #include <math_Matrix.hxx> |
20 | #include <math_MultipleVarFunctionWithGradient.hxx> |
21 | #include <math_MultipleVarFunctionWithHessian.hxx> |
22 | #include <math_NewtonMinimum.hxx> |
23 | #include <math_Powell.hxx> |
4bbaf12b |
24 | #include <Standard_Integer.hxx> |
25 | #include <Standard_Real.hxx> |
e8746a26 |
26 | #include <Precision.hxx> |
4bbaf12b |
27 | |
4bbaf12b |
28 | |
29 | //======================================================================= |
30 | //function : math_GlobOptMin |
31 | //purpose : Constructor |
32 | //======================================================================= |
33 | math_GlobOptMin::math_GlobOptMin(math_MultipleVarFunction* theFunc, |
34 | const math_Vector& theA, |
35 | const math_Vector& theB, |
5493d334 |
36 | const Standard_Real theC, |
37 | const Standard_Real theDiscretizationTol, |
38 | const Standard_Real theSameTol) |
4bbaf12b |
39 | : myN(theFunc->NbVariables()), |
40 | myA(1, myN), |
41 | myB(1, myN), |
42 | myGlobA(1, myN), |
43 | myGlobB(1, myN), |
1907fb9a |
44 | myIsConstLocked(Standard_False), |
4bbaf12b |
45 | myX(1, myN), |
46 | myTmp(1, myN), |
5493d334 |
47 | myV(1, myN), |
3f733bb1 |
48 | myMaxV(1, myN), |
4b65fc77 |
49 | myExpandCoeff(1, myN), |
50 | myCellSize(0, myN - 1), |
51 | myFilter(theFunc->NbVariables()) |
4bbaf12b |
52 | { |
53 | Standard_Integer i; |
54 | |
55 | myFunc = theFunc; |
56 | myC = theC; |
1907fb9a |
57 | myInitC = theC; |
78e7cada |
58 | myIsFindSingleSolution = Standard_False; |
836d7b64 |
59 | myFunctionalMinimalValue = -Precision::Infinite(); |
4bbaf12b |
60 | myZ = -1; |
61 | mySolCount = 0; |
62 | |
63 | for(i = 1; i <= myN; i++) |
64 | { |
65 | myGlobA(i) = theA(i); |
66 | myGlobB(i) = theB(i); |
67 | |
68 | myA(i) = theA(i); |
69 | myB(i) = theB(i); |
70 | } |
71 | |
5493d334 |
72 | for(i = 1; i <= myN; i++) |
73 | { |
74 | myMaxV(i) = (myB(i) - myA(i)) / 3.0; |
75 | } |
76 | |
3f733bb1 |
77 | myExpandCoeff(1) = 1.0; |
78 | for(i = 2; i <= myN; i++) |
79 | { |
80 | myExpandCoeff(i) = (myB(i) - myA(i)) / (myB(i - 1) - myA(i - 1)); |
81 | } |
82 | |
5493d334 |
83 | myTol = theDiscretizationTol; |
84 | mySameTol = theSameTol; |
85 | |
4b65fc77 |
86 | const Standard_Integer aMaxSquareSearchSol = 200; |
87 | Standard_Integer aSolNb = Standard_Integer(Pow(3.0, Standard_Real(myN))); |
88 | myMinCellFilterSol = Max(2 * aSolNb, aMaxSquareSearchSol); |
89 | initCellSize(); |
1907fb9a |
90 | ComputeInitSol(); |
4b65fc77 |
91 | |
4bbaf12b |
92 | myDone = Standard_False; |
93 | } |
94 | |
95 | //======================================================================= |
96 | //function : SetGlobalParams |
1907fb9a |
97 | //purpose : Set parameters without memory allocation. |
4bbaf12b |
98 | //======================================================================= |
99 | void math_GlobOptMin::SetGlobalParams(math_MultipleVarFunction* theFunc, |
100 | const math_Vector& theA, |
101 | const math_Vector& theB, |
5493d334 |
102 | const Standard_Real theC, |
103 | const Standard_Real theDiscretizationTol, |
104 | const Standard_Real theSameTol) |
4bbaf12b |
105 | { |
106 | Standard_Integer i; |
107 | |
108 | myFunc = theFunc; |
109 | myC = theC; |
1907fb9a |
110 | myInitC = theC; |
4bbaf12b |
111 | myZ = -1; |
112 | mySolCount = 0; |
113 | |
114 | for(i = 1; i <= myN; i++) |
115 | { |
116 | myGlobA(i) = theA(i); |
117 | myGlobB(i) = theB(i); |
118 | |
119 | myA(i) = theA(i); |
120 | myB(i) = theB(i); |
121 | } |
122 | |
3f733bb1 |
123 | for(i = 1; i <= myN; i++) |
124 | { |
125 | myMaxV(i) = (myB(i) - myA(i)) / 3.0; |
126 | } |
127 | |
128 | myExpandCoeff(1) = 1.0; |
129 | for(i = 2; i <= myN; i++) |
130 | { |
131 | myExpandCoeff(i) = (myB(i) - myA(i)) / (myB(i - 1) - myA(i - 1)); |
132 | } |
133 | |
5493d334 |
134 | myTol = theDiscretizationTol; |
135 | mySameTol = theSameTol; |
136 | |
4b65fc77 |
137 | initCellSize(); |
1907fb9a |
138 | ComputeInitSol(); |
4b65fc77 |
139 | |
4bbaf12b |
140 | myDone = Standard_False; |
141 | } |
142 | |
143 | //======================================================================= |
144 | //function : SetLocalParams |
1907fb9a |
145 | //purpose : Set parameters without memory allocation. |
4bbaf12b |
146 | //======================================================================= |
147 | void math_GlobOptMin::SetLocalParams(const math_Vector& theLocalA, |
148 | const math_Vector& theLocalB) |
149 | { |
150 | Standard_Integer i; |
151 | |
152 | myZ = -1; |
4bbaf12b |
153 | for(i = 1; i <= myN; i++) |
154 | { |
155 | myA(i) = theLocalA(i); |
156 | myB(i) = theLocalB(i); |
157 | } |
158 | |
5493d334 |
159 | for(i = 1; i <= myN; i++) |
160 | { |
161 | myMaxV(i) = (myB(i) - myA(i)) / 3.0; |
162 | } |
163 | |
3f733bb1 |
164 | myExpandCoeff(1) = 1.0; |
165 | for(i = 2; i <= myN; i++) |
166 | { |
167 | myExpandCoeff(i) = (myB(i) - myA(i)) / (myB(i - 1) - myA(i - 1)); |
168 | } |
169 | |
4bbaf12b |
170 | myDone = Standard_False; |
171 | } |
172 | |
5493d334 |
173 | //======================================================================= |
174 | //function : SetTol |
175 | //purpose : Set algorithm tolerances. |
176 | //======================================================================= |
177 | void math_GlobOptMin::SetTol(const Standard_Real theDiscretizationTol, |
178 | const Standard_Real theSameTol) |
179 | { |
180 | myTol = theDiscretizationTol; |
181 | mySameTol = theSameTol; |
182 | } |
183 | |
184 | //======================================================================= |
185 | //function : GetTol |
186 | //purpose : Get algorithm tolerances. |
187 | //======================================================================= |
188 | void math_GlobOptMin::GetTol(Standard_Real& theDiscretizationTol, |
189 | Standard_Real& theSameTol) |
190 | { |
191 | theDiscretizationTol = myTol; |
192 | theSameTol = mySameTol; |
193 | } |
194 | |
4bbaf12b |
195 | //======================================================================= |
196 | //function : ~math_GlobOptMin |
197 | //purpose : |
198 | //======================================================================= |
199 | math_GlobOptMin::~math_GlobOptMin() |
200 | { |
201 | } |
202 | |
203 | //======================================================================= |
204 | //function : Perform |
205 | //purpose : Compute Global extremum point |
206 | //======================================================================= |
207 | // In this algo indexes started from 1, not from 0. |
78e7cada |
208 | void math_GlobOptMin::Perform(const Standard_Boolean isFindSingleSolution) |
4bbaf12b |
209 | { |
210 | Standard_Integer i; |
211 | |
212 | // Compute parameters range |
213 | Standard_Real minLength = RealLast(); |
214 | Standard_Real maxLength = RealFirst(); |
215 | for(i = 1; i <= myN; i++) |
216 | { |
217 | Standard_Real currentLength = myB(i) - myA(i); |
218 | if (currentLength < minLength) |
219 | minLength = currentLength; |
220 | if (currentLength > maxLength) |
221 | maxLength = currentLength; |
222 | } |
223 | |
e8746a26 |
224 | if (minLength < Precision::PConfusion()) |
225 | { |
226 | #ifdef OCCT_DEBUG |
227 | cout << "math_GlobOptMin::Perform(): Degenerated parameters space" << endl; |
228 | #endif |
229 | |
230 | return; |
231 | } |
232 | |
1907fb9a |
233 | if (!myIsConstLocked) |
234 | { |
235 | // Compute initial value for myC. |
236 | computeInitialValues(); |
237 | } |
e8746a26 |
238 | |
797d11c6 |
239 | myE1 = minLength * myTol; |
240 | myE2 = maxLength * myTol; |
78e7cada |
241 | |
242 | myIsFindSingleSolution = isFindSingleSolution; |
243 | if (isFindSingleSolution) |
244 | { |
1907fb9a |
245 | // Run local optimization if current value better than optimal. |
78e7cada |
246 | myE3 = 0.0; |
247 | } |
797d11c6 |
248 | else |
78e7cada |
249 | { |
250 | if (myC > 1.0) |
251 | myE3 = - maxLength * myTol / 4.0; |
252 | else |
253 | myE3 = - maxLength * myTol * myC / 4.0; |
254 | } |
4bbaf12b |
255 | |
1907fb9a |
256 | // Search single solution and current solution in its neighborhood. |
836d7b64 |
257 | if (CheckFunctionalStopCriteria()) |
258 | { |
259 | myDone = Standard_True; |
260 | return; |
261 | } |
262 | |
1907fb9a |
263 | myLastStep = 0.0; |
4b65fc77 |
264 | isFirstCellFilterInvoke = Standard_True; |
4bbaf12b |
265 | computeGlobalExtremum(myN); |
266 | |
267 | myDone = Standard_True; |
4bbaf12b |
268 | } |
269 | |
270 | //======================================================================= |
271 | //function : computeLocalExtremum |
272 | //purpose : |
273 | //======================================================================= |
274 | Standard_Boolean math_GlobOptMin::computeLocalExtremum(const math_Vector& thePnt, |
275 | Standard_Real& theVal, |
276 | math_Vector& theOutPnt) |
277 | { |
278 | Standard_Integer i; |
279 | |
280 | //Newton method |
281 | if (dynamic_cast<math_MultipleVarFunctionWithHessian*>(myFunc)) |
282 | { |
747f90db |
283 | math_MultipleVarFunctionWithHessian* aTmp = |
4bbaf12b |
284 | dynamic_cast<math_MultipleVarFunctionWithHessian*> (myFunc); |
747f90db |
285 | math_NewtonMinimum newtonMinimum(*aTmp); |
91806b90 |
286 | newtonMinimum.SetBoundary(myGlobA, myGlobB); |
747f90db |
287 | newtonMinimum.Perform(*aTmp, thePnt); |
859a47c3 |
288 | |
4bbaf12b |
289 | if (newtonMinimum.IsDone()) |
290 | { |
291 | newtonMinimum.Location(theOutPnt); |
292 | theVal = newtonMinimum.Minimum(); |
293 | } |
294 | else return Standard_False; |
295 | } else |
296 | |
297 | // BFGS method used. |
298 | if (dynamic_cast<math_MultipleVarFunctionWithGradient*>(myFunc)) |
299 | { |
747f90db |
300 | math_MultipleVarFunctionWithGradient* aTmp = |
4bbaf12b |
301 | dynamic_cast<math_MultipleVarFunctionWithGradient*> (myFunc); |
747f90db |
302 | math_BFGS bfgs(aTmp->NbVariables()); |
303 | bfgs.Perform(*aTmp, thePnt); |
4bbaf12b |
304 | if (bfgs.IsDone()) |
305 | { |
306 | bfgs.Location(theOutPnt); |
307 | theVal = bfgs.Minimum(); |
308 | } |
309 | else return Standard_False; |
310 | } else |
311 | |
312 | // Powell method used. |
313 | if (dynamic_cast<math_MultipleVarFunction*>(myFunc)) |
314 | { |
315 | math_Matrix m(1, myN, 1, myN, 0.0); |
316 | for(i = 1; i <= myN; i++) |
317 | m(1, 1) = 1.0; |
318 | |
859a47c3 |
319 | math_Powell powell(*myFunc, 1e-10); |
320 | powell.Perform(*myFunc, thePnt, m); |
4bbaf12b |
321 | |
322 | if (powell.IsDone()) |
323 | { |
324 | powell.Location(theOutPnt); |
325 | theVal = powell.Minimum(); |
326 | } |
327 | else return Standard_False; |
328 | } |
329 | |
330 | if (isInside(theOutPnt)) |
331 | return Standard_True; |
332 | else |
333 | return Standard_False; |
334 | } |
335 | |
797d11c6 |
336 | //======================================================================= |
337 | //function : computeInitialValues |
338 | //purpose : |
339 | //======================================================================= |
340 | void math_GlobOptMin::computeInitialValues() |
341 | { |
342 | Standard_Integer i; |
343 | math_Vector aCurrPnt(1, myN); |
344 | math_Vector aBestPnt(1, myN); |
e8746a26 |
345 | math_Vector aParamStep(1, myN); |
797d11c6 |
346 | Standard_Real aCurrVal = RealLast(); |
797d11c6 |
347 | |
1907fb9a |
348 | // Lipchitz const approximation. |
e8746a26 |
349 | Standard_Real aLipConst = 0.0, aPrevValDiag, aPrevValProj; |
797d11c6 |
350 | Standard_Integer aPntNb = 13; |
e8746a26 |
351 | myFunc->Value(myA, aPrevValDiag); |
352 | aPrevValProj = aPrevValDiag; |
797d11c6 |
353 | Standard_Real aStep = (myB - myA).Norm() / aPntNb; |
e8746a26 |
354 | aParamStep = (myB - myA) / aPntNb; |
797d11c6 |
355 | for(i = 1; i <= aPntNb; i++) |
356 | { |
e8746a26 |
357 | aCurrPnt = myA + aParamStep * i; |
797d11c6 |
358 | |
e8746a26 |
359 | // Walk over diagonal. |
360 | myFunc->Value(aCurrPnt, aCurrVal); |
361 | aLipConst = Max (Abs(aCurrVal - aPrevValDiag), aLipConst); |
362 | aPrevValDiag = aCurrVal; |
797d11c6 |
363 | |
e8746a26 |
364 | // Walk over diag in projected space aPnt(1) = myA(1) = const. |
365 | aCurrPnt(1) = myA(1); |
366 | myFunc->Value(aCurrPnt, aCurrVal); |
367 | aLipConst = Max (Abs(aCurrVal - aPrevValProj), aLipConst); |
368 | aPrevValProj = aCurrVal; |
797d11c6 |
369 | } |
e8746a26 |
370 | |
1907fb9a |
371 | myC = myInitC; |
e8746a26 |
372 | aLipConst *= Sqrt(myN) / aStep; |
797d11c6 |
373 | if (aLipConst < myC * 0.1) |
797d11c6 |
374 | myC = Max(aLipConst * 0.1, 0.01); |
1907fb9a |
375 | else if (aLipConst > myC * 5) |
376 | myC = Min(myC * 5, 50.0); |
377 | |
378 | // Clear all solutions except one. |
379 | if (myY.Size() != myN) |
797d11c6 |
380 | { |
1907fb9a |
381 | for(i = 1; i <= myN; i++) |
382 | aBestPnt(i) = myY(i); |
383 | myY.Clear(); |
384 | for(i = 1; i <= myN; i++) |
385 | myY.Append(aBestPnt(i)); |
797d11c6 |
386 | } |
1907fb9a |
387 | mySolCount = 1; |
797d11c6 |
388 | } |
389 | |
4bbaf12b |
390 | //======================================================================= |
391 | //function : ComputeGlobalExtremum |
392 | //purpose : |
393 | //======================================================================= |
394 | void math_GlobOptMin::computeGlobalExtremum(Standard_Integer j) |
395 | { |
396 | Standard_Integer i; |
397 | Standard_Real d; // Functional in moved point. |
398 | Standard_Real val = RealLast(); // Local extrema computed in moved point. |
3f733bb1 |
399 | Standard_Real aStepBestValue = RealLast(); |
3f733bb1 |
400 | math_Vector aStepBestPoint(1, myN); |
4bbaf12b |
401 | Standard_Boolean isInside = Standard_False; |
402 | Standard_Real r; |
debc95ee |
403 | Standard_Boolean isReached = Standard_False; |
4bbaf12b |
404 | |
1907fb9a |
405 | |
836d7b64 |
406 | for(myX(j) = myA(j) + myE1; |
debc95ee |
407 | (myX(j) < myB(j) + myE1) && (!isReached); |
408 | myX(j) += myV(j)) |
4bbaf12b |
409 | { |
410 | if (myX(j) > myB(j)) |
debc95ee |
411 | { |
4bbaf12b |
412 | myX(j) = myB(j); |
debc95ee |
413 | isReached = Standard_True; |
414 | } |
4bbaf12b |
415 | |
836d7b64 |
416 | if (CheckFunctionalStopCriteria()) |
417 | return; // Best possible value is obtained. |
418 | |
4bbaf12b |
419 | if (j == 1) |
420 | { |
421 | isInside = Standard_False; |
422 | myFunc->Value(myX, d); |
1907fb9a |
423 | r = (d + myZ * myC * myLastStep - myF) * myZ; |
4bbaf12b |
424 | if(r > myE3) |
425 | { |
426 | isInside = computeLocalExtremum(myX, val, myTmp); |
427 | } |
3f733bb1 |
428 | aStepBestValue = (isInside && (val < d))? val : d; |
429 | aStepBestPoint = (isInside && (val < d))? myTmp : myX; |
4bbaf12b |
430 | |
78e7cada |
431 | // Solutions are close to each other |
432 | // and it is allowed to have more than one solution. |
433 | if (Abs(aStepBestValue - myF) < mySameTol * 0.01 && |
434 | !myIsFindSingleSolution) |
4bbaf12b |
435 | { |
3f733bb1 |
436 | if (!isStored(aStepBestPoint)) |
4bbaf12b |
437 | { |
3f733bb1 |
438 | if ((aStepBestValue - myF) * myZ > 0.0) |
439 | myF = aStepBestValue; |
4bbaf12b |
440 | for(i = 1; i <= myN; i++) |
3f733bb1 |
441 | myY.Append(aStepBestPoint(i)); |
4bbaf12b |
442 | mySolCount++; |
443 | } |
444 | } |
445 | |
78e7cada |
446 | // New best solution: |
447 | // new point is out of (mySameTol * 0.01) surrounding or |
448 | // new point is better than old + single point search. |
449 | Standard_Real aFunctionalDelta = (aStepBestValue - myF) * myZ; |
450 | if (aFunctionalDelta > mySameTol * 0.01 || |
451 | (aFunctionalDelta > 0.0 && myIsFindSingleSolution)) |
4bbaf12b |
452 | { |
453 | mySolCount = 0; |
3f733bb1 |
454 | myF = aStepBestValue; |
4bbaf12b |
455 | myY.Clear(); |
456 | for(i = 1; i <= myN; i++) |
3f733bb1 |
457 | myY.Append(aStepBestPoint(i)); |
4bbaf12b |
458 | mySolCount++; |
4b65fc77 |
459 | |
460 | isFirstCellFilterInvoke = Standard_True; |
4bbaf12b |
461 | } |
462 | |
836d7b64 |
463 | if (CheckFunctionalStopCriteria()) |
464 | return; // Best possible value is obtained. |
465 | |
1907fb9a |
466 | myV(1) = Min(myE2 + Abs(myF - d) / myC, myMaxV(1)); |
467 | myLastStep = myV(1); |
4bbaf12b |
468 | } |
469 | else |
470 | { |
471 | myV(j) = RealLast() / 2.0; |
472 | computeGlobalExtremum(j - 1); |
3f733bb1 |
473 | |
474 | // Nullify steps on lower dimensions. |
475 | for(i = 1; i < j; i++) |
476 | myV(i) = 0.0; |
4bbaf12b |
477 | } |
3f733bb1 |
478 | // Compute step in (j + 1) dimension according to scale. |
479 | if (j < myN) |
4bbaf12b |
480 | { |
3f733bb1 |
481 | Standard_Real aUpperDimStep = myV(j) * myExpandCoeff(j + 1); |
482 | if (myV(j + 1) > aUpperDimStep) |
483 | { |
484 | if (aUpperDimStep > myMaxV(j + 1)) // Case of too big step. |
485 | myV(j + 1) = myMaxV(j + 1); |
486 | else |
487 | myV(j + 1) = aUpperDimStep; |
488 | } |
4bbaf12b |
489 | } |
490 | } |
491 | } |
492 | |
493 | //======================================================================= |
494 | //function : IsInside |
495 | //purpose : |
496 | //======================================================================= |
497 | Standard_Boolean math_GlobOptMin::isInside(const math_Vector& thePnt) |
498 | { |
499 | Standard_Integer i; |
500 | |
501 | for(i = 1; i <= myN; i++) |
502 | { |
503 | if (thePnt(i) < myGlobA(i) || thePnt(i) > myGlobB(i)) |
504 | return Standard_False; |
505 | } |
506 | |
507 | return Standard_True; |
508 | } |
509 | //======================================================================= |
510 | //function : IsStored |
511 | //purpose : |
512 | //======================================================================= |
513 | Standard_Boolean math_GlobOptMin::isStored(const math_Vector& thePnt) |
514 | { |
515 | Standard_Integer i,j; |
516 | Standard_Boolean isSame = Standard_True; |
20a216fe |
517 | math_Vector aTol(1, myN); |
518 | aTol = (myB - myA) * mySameTol; |
4bbaf12b |
519 | |
4b65fc77 |
520 | // C1 * n^2 = C2 * 3^dim * n |
521 | if (mySolCount < myMinCellFilterSol) |
4bbaf12b |
522 | { |
4b65fc77 |
523 | for(i = 0; i < mySolCount; i++) |
4bbaf12b |
524 | { |
4b65fc77 |
525 | isSame = Standard_True; |
526 | for(j = 1; j <= myN; j++) |
4bbaf12b |
527 | { |
4b65fc77 |
528 | if ((Abs(thePnt(j) - myY(i * myN + j))) > aTol(j)) |
529 | { |
530 | isSame = Standard_False; |
531 | break; |
532 | } |
4bbaf12b |
533 | } |
4b65fc77 |
534 | if (isSame == Standard_True) |
535 | return Standard_True; |
4bbaf12b |
536 | } |
4b65fc77 |
537 | } |
538 | else |
539 | { |
50bc8f96 |
540 | NCollection_CellFilter_Inspector anInspector(myN, Precision::PConfusion()); |
4b65fc77 |
541 | if (isFirstCellFilterInvoke) |
542 | { |
543 | myFilter.Reset(myCellSize); |
4bbaf12b |
544 | |
4b65fc77 |
545 | // Copy initial data into cell filter. |
546 | for(Standard_Integer aSolIdx = 0; aSolIdx < mySolCount; aSolIdx++) |
547 | { |
548 | math_Vector aVec(1, myN); |
549 | for(Standard_Integer aSolDim = 1; aSolDim <= myN; aSolDim++) |
550 | aVec(aSolDim) = myY(aSolIdx * myN + aSolDim); |
551 | |
552 | myFilter.Add(aVec, aVec); |
553 | } |
554 | } |
555 | |
556 | isFirstCellFilterInvoke = Standard_False; |
557 | |
558 | math_Vector aLow(1, myN), anUp(1, myN); |
559 | anInspector.Shift(thePnt, myCellSize, aLow, anUp); |
560 | |
561 | anInspector.ClearFind(); |
562 | anInspector.SetCurrent(thePnt); |
563 | myFilter.Inspect(aLow, anUp, anInspector); |
564 | if (!anInspector.isFind()) |
565 | { |
566 | // Point is out of close cells, add new one. |
567 | myFilter.Add(thePnt, thePnt); |
568 | } |
4bbaf12b |
569 | } |
570 | return Standard_False; |
571 | } |
572 | |
573 | //======================================================================= |
574 | //function : NbExtrema |
575 | //purpose : |
576 | //======================================================================= |
577 | Standard_Integer math_GlobOptMin::NbExtrema() |
578 | { |
579 | return mySolCount; |
580 | } |
581 | |
582 | //======================================================================= |
583 | //function : GetF |
584 | //purpose : |
585 | //======================================================================= |
586 | Standard_Real math_GlobOptMin::GetF() |
587 | { |
588 | return myF; |
589 | } |
590 | |
836d7b64 |
591 | //======================================================================= |
592 | //function : SetFunctionalMinimalValue |
593 | //purpose : |
594 | //======================================================================= |
595 | void math_GlobOptMin::SetFunctionalMinimalValue(const Standard_Real theMinimalValue) |
596 | { |
597 | myFunctionalMinimalValue = theMinimalValue; |
598 | } |
599 | |
600 | //======================================================================= |
601 | //function : GetFunctionalMinimalValue |
602 | //purpose : |
603 | //======================================================================= |
604 | Standard_Real math_GlobOptMin::GetFunctionalMinimalValue() |
605 | { |
606 | return myFunctionalMinimalValue; |
607 | } |
608 | |
4bbaf12b |
609 | //======================================================================= |
610 | //function : IsDone |
611 | //purpose : |
612 | //======================================================================= |
613 | Standard_Boolean math_GlobOptMin::isDone() |
614 | { |
615 | return myDone; |
616 | } |
617 | |
618 | //======================================================================= |
619 | //function : Points |
620 | //purpose : |
621 | //======================================================================= |
622 | void math_GlobOptMin::Points(const Standard_Integer theIndex, math_Vector& theSol) |
623 | { |
624 | Standard_Integer j; |
625 | |
626 | for(j = 1; j <= myN; j++) |
627 | theSol(j) = myY((theIndex - 1) * myN + j); |
628 | } |
4b65fc77 |
629 | |
630 | //======================================================================= |
631 | //function : initCellSize |
632 | //purpose : |
633 | //======================================================================= |
634 | void math_GlobOptMin::initCellSize() |
635 | { |
636 | for(Standard_Integer anIdx = 1; anIdx <= myN; anIdx++) |
637 | { |
638 | myCellSize(anIdx - 1) = (myGlobB(anIdx) - myGlobA(anIdx)) |
639 | * Precision::PConfusion() / (2.0 * Sqrt(2.0)); |
640 | } |
641 | } |
836d7b64 |
642 | |
643 | //======================================================================= |
644 | //function : CheckFunctionalStopCriteria |
645 | //purpose : |
646 | //======================================================================= |
647 | Standard_Boolean math_GlobOptMin::CheckFunctionalStopCriteria() |
648 | { |
1907fb9a |
649 | // Search single solution and current solution in its neighborhood. |
836d7b64 |
650 | if (myIsFindSingleSolution && |
651 | Abs (myF - myFunctionalMinimalValue) < mySameTol * 0.01) |
652 | return Standard_True; |
653 | |
654 | return Standard_False; |
655 | } |
1907fb9a |
656 | |
657 | //======================================================================= |
658 | //function : ComputeInitSol |
659 | //purpose : |
660 | //======================================================================= |
661 | void math_GlobOptMin::ComputeInitSol() |
662 | { |
663 | Standard_Real aCurrVal, aBestVal; |
664 | math_Vector aCurrPnt(1, myN); |
665 | math_Vector aBestPnt(1, myN); |
666 | math_Vector aParamStep(1, myN); |
667 | // Check functional value in midpoint, lower and upper border points and |
668 | // in each point try to perform local optimization. |
669 | aBestPnt = (myGlobA + myGlobB) * 0.5; |
670 | myFunc->Value(aBestPnt, aBestVal); |
671 | |
672 | Standard_Integer i; |
673 | for(i = 1; i <= 3; i++) |
674 | { |
675 | aCurrPnt = myA + (myB - myA) * (i - 1) / 2.0; |
676 | |
677 | if(computeLocalExtremum(aCurrPnt, aCurrVal, aCurrPnt)) |
678 | { |
679 | // Local search tries to find better solution than current point. |
680 | if (aCurrVal < aBestVal) |
681 | { |
682 | aBestVal = aCurrVal; |
683 | aBestPnt = aCurrPnt; |
684 | } |
685 | } |
686 | } |
687 | |
688 | myF = aBestVal; |
689 | myY.Clear(); |
690 | for(i = 1; i <= myN; i++) |
691 | myY.Append(aBestPnt(i)); |
692 | mySolCount = 1; |
693 | |
694 | myDone = Standard_False; |
695 | } |
696 | |
697 | //======================================================================= |
698 | //function : SetLipConstState |
699 | //purpose : |
700 | //======================================================================= |
701 | void math_GlobOptMin::SetLipConstState(const Standard_Boolean theFlag) |
702 | { |
703 | myIsConstLocked = theFlag; |
704 | } |