10 bool continueLoop =
true;
11 unsigned int nbIterations = 0;
14 if (numParameters > 1)
15 throw itk::ExceptionObject(__FILE__, __LINE__,
"Brent algorithm does not implement multi-dimensional optimization. Only one parameter allowed.");
25 p[0] = internalLowerBound;
32 p[0] = internalUpperBound;
36 if (std::abs(fValAtLowerBound) < std::abs(fValAtUpperBound))
38 double workValue = internalLowerBound;
39 internalLowerBound = internalUpperBound;
40 internalUpperBound = workValue;
41 workValue = fValAtLowerBound;
42 fValAtLowerBound = fValAtUpperBound;
43 fValAtUpperBound = workValue;
46 double cValue = internalLowerBound;
47 double fValAtCValue = fValAtLowerBound;
56 double fDiffAC = fValAtLowerBound - fValAtCValue;
57 double fDiffBC = fValAtUpperBound - fValAtCValue;
58 double fDiffAB = fValAtLowerBound - fValAtUpperBound;
59 if (fDiffAC == 0.0 || fDiffBC == 0.0)
62 p[0] = internalUpperBound + fValAtUpperBound * (internalUpperBound - internalLowerBound) / fDiffAB;
67 double firstTerm = internalLowerBound * fValAtUpperBound * fValAtCValue / (fDiffAB * fDiffAC);
68 double secondTerm = internalUpperBound * fValAtLowerBound * fValAtCValue / (-fDiffAB * fDiffBC);
69 double thirdTerm = cValue * fValAtLowerBound * fValAtUpperBound / (fDiffAC * fDiffBC);
70 p[0] = firstTerm + secondTerm + thirdTerm;
73 bool condition1 = (p[0] < (3.0 * internalLowerBound + internalUpperBound) / 4.0) || (p[0] > internalUpperBound);
74 bool condition2 = (mFlag) && (std::abs(p[0] - internalUpperBound) >= std::abs(internalUpperBound - cValue) / 2.0);
75 bool condition3 = (!mFlag) && (std::abs(p[0] - internalUpperBound) >= std::abs(cValue - dValue) / 2.0);
76 bool condition4 = (mFlag) && (std::abs(internalUpperBound - cValue) < deltaValue);
77 bool condition5 = (!mFlag) && (std::abs(cValue - dValue) < deltaValue);
79 if (condition1 || condition2 || condition3 || condition4 || condition5)
82 p[0] = (internalLowerBound + internalUpperBound) / 2.0;
93 cValue = internalUpperBound;
94 fValAtCValue = fValAtUpperBound;
96 if (fValAtLowerBound * tentativeCost < 0.0)
98 internalUpperBound = p[0];
99 fValAtUpperBound = tentativeCost;
103 internalLowerBound = p[0];
104 fValAtLowerBound = tentativeCost;
107 if (std::abs(fValAtLowerBound) < std::abs(fValAtUpperBound))
109 double workValue = internalLowerBound;
110 internalLowerBound = internalUpperBound;
111 internalUpperBound = workValue;
112 workValue = fValAtLowerBound;
113 fValAtLowerBound = fValAtUpperBound;
114 fValAtUpperBound = workValue;
120 (std::abs(internalUpperBound - internalLowerBound) < this->
GetRootRelativeTolerance() * (internalLowerBound + internalUpperBound) / 2.0))
121 continueLoop =
false;
double GetFunctionValueAtInitialLowerBound()
bool GetProvidedFunctionValueAtInitialLowerBound()
double GetRootRelativeTolerance()
bool GetProvidedFunctionValueAtInitialUpperBound()
unsigned int GetMaximumNumberOfIterations()
double GetCostFunctionTolerance()
BaseCostFunctionType::ParametersType ParametersType
double GetFunctionValueAtInitialUpperBound()
double Optimize() ITK_OVERRIDE
BaseCostFunctionType * GetRootFindingFunction()