Levenberg-Marquardt Optimization

Synopsis

Optimize a function using the LevenbergMarquardtOptimization class.

Results

Output:

Position: [5.762595813691092, 0.4800781816497411]
Value: [0.18761225810035853, 0.22149805874930717, 0.19626506063745364, 0.008440292123000681, -0.2246669049722252, -0.06499867890559052, -0.03009462205999469, 0.037078071678616986, 0.1179535364891624, -0.20197691757229475, 0.23114283424137572, -0.09633797060122617, -0.1181447420933539, -0.016296554008191855, 0.15351231349591643, 0.04282427169413161, -0.011028322317460315, -0.014874778904555797, 0.026054762464067238, 0.08282156821610176, -0.18170363558960823, 0.014342598340990165, -0.05669067326929067, -0.19960985461710923, 0.07906076620078828, 0.053467861432614505, -0.04393281305375307, -0.21198352299156475, 0.13281781677553717, 0.19836431320892967, -0.05863022207617963, 0.04153068096802581, 0.027536776558543252, -0.2217222950679556, -0.052451620797857146, 0.0037302084610484343, 0.10780178216799818, 0.03909757445646189, -0.19500845421763202, 0.13552303389775222, 0.12067588331830059, -0.1646559748487526, -0.17905579039027675, -0.2047507980917569, 0.13126702388154232, 0.2038583258729716, 0.2315159987359916, 0.03741545669200619, 0.0640979627527889, -0.2180880841666788, 0.09985426440562328, 0.05599101173689913, 0.018079402649374465, -0.11392806561205493, 0.12335904899417738, -0.1946137987174792, -0.11640787926864604, 0.02577531924348353, -0.20075160702307215, -0.06148859349321256, 0.13543279691396393, -0.017197866393147798, -0.034013725658296856, -0.057410320040360396, 0.03428199478561922, 0.12079221893803638, -0.16693885394333918, 0.21030957837668662, -0.11858742760250163, -0.05222840443865451, -0.20187684802252903, 0.13116194362153877, -0.04916327491714512, 0.1116844217460855, -0.05268803531855326, 0.18635323207406884, -0.2359521189116638, -0.03685975982932632, -0.1408713799105108, 0.04996752375214619, -0.1635648070609097, 0.2208479366940992, 0.000060936209241191364, 0.12473423700590125, -0.061608327743822855, -0.20618061051637326, -0.012960406675505354, 0.1520753363006424, -0.006297055122873374, 0.1324731913025463, 0.2377413446210319, -0.13626735303743231, -0.005897280453501352, -0.09485834355640499, 0.19074286440427457, -0.19759247327942475, 0.159875584380206, 0.18354609840799085, 0.13071872295595632, -0.24643784133671076]

Code

C++

// Include the Levenberg-Marquardt optimizer and a custom cost function
#include "itkLevenbergMarquardtOptimizer.h"
#include "itkExampleCostFunction.h"

// Typedef the optimizer and cost function, for convenience
using OptimizerType = itk::LevenbergMarquardtOptimizer;
using CostType = itk::ExampleCostFunction;

int
main(int, char *[])
{

  // Instantiate the cost function and optimizer
  CostType::Pointer      cost = CostType::New();
  OptimizerType::Pointer optimizer = OptimizerType::New();

  optimizer->SetNumberOfIterations(100);
  optimizer->UseCostFunctionGradientOff();
  optimizer->SetCostFunction(cost.GetPointer());

  // This is the initial guess for the parameter values, which we set to one
  CostType::ParametersType p(cost->GetNumberOfParameters());
  p.Fill(1);
  optimizer->SetInitialPosition(p);
  optimizer->StartOptimization();

  // Print out some information about the optimization
  // The parameters come out to be near to, but not exactly [5.5, 0.5]
  std::cout << "Position: " << optimizer->GetCurrentPosition() << std::endl;
  std::cout << "Value: " << optimizer->GetValue() << std::endl;

  return EXIT_SUCCESS;
}

Classes demonstrated