ANIMA  4.0
animaBisectionRootFindingAlgorithm.cxx
Go to the documentation of this file.
2 
3 namespace anima
4 {
5 
7 {
8  bool continueLoop = true;
9  unsigned int nbIterations = 0;
10 
11  unsigned int numParameters = this->GetRootFindingFunction()->GetNumberOfParameters();
12  if (numParameters > 1)
13  throw itk::ExceptionObject(__FILE__, __LINE__, "Bisection algorithm does not implement multi-dimensional optimization. Only one parameter allowed.");
14 
15  ParametersType p(1);
16 
17  double internalLowerBound = this->GetLowerBound();
18  double internalUpperBound = this->GetUpperBound();
19 
20  while (continueLoop)
21  {
22  ++nbIterations;
23 
24  p[0] = (internalLowerBound + internalUpperBound) / 2.0;
25  double zeroValue = this->GetRootFindingFunction()->GetValue(p);
26 
27  continueLoop = (std::abs(zeroValue) >= this->GetCostFunctionTolerance());
28 
29  if (zeroValue < 0.0)
30  internalUpperBound = p[0];
31  else
32  internalLowerBound = p[0];
33 
34  if ((nbIterations >= this->GetMaximumNumberOfIterations()) ||
35  (std::abs(internalUpperBound - internalLowerBound) < this->GetRootRelativeTolerance() * (internalLowerBound + internalUpperBound) / 2.0))
36  continueLoop = false;
37  }
38 
39  return p[0];
40 }
41 
42 } // end namespace anima
BaseCostFunctionType::ParametersType ParametersType