Skip to content

Commit

Permalink
STYLE: Declare m_VnlOptimizer from Optimizers as unique_ptr
Browse files Browse the repository at this point in the history
Also "defaulted" the destructors of those optimizer classes.
  • Loading branch information
N-Dekker authored and dzenanz committed Sep 27, 2022
1 parent 2b1b531 commit 7357008
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 79 deletions.
3 changes: 2 additions & 1 deletion Modules/Numerics/Optimizers/include/itkAmoebaOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkSingleValuedNonLinearVnlOptimizer.h"
#include "vnl/algo/vnl_amoeba.h"
#include "ITKOptimizersExport.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -175,7 +176,7 @@ class ITKOptimizers_EXPORT AmoebaOptimizer : public SingleValuedNonLinearVnlOpti
bool m_AutomaticInitialSimplex;
ParametersType m_InitialSimplexDelta;
bool m_OptimizeWithRestarts;
vnl_amoeba * m_VnlOptimizer;
std::unique_ptr<vnl_amoeba> m_VnlOptimizer;

std::ostringstream m_StopConditionDescription;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkSingleValuedNonLinearVnlOptimizer.h"
#include "vnl/algo/vnl_conjugate_gradient.h"
#include "ITKOptimizersExport.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -86,8 +87,8 @@ class ITKOptimizers_EXPORT ConjugateGradientOptimizer : public SingleValuedNonLi

private:
/** The vnl optimization method for conjugate gradient. */
bool m_OptimizerInitialized;
InternalOptimizerType * m_VnlOptimizer;
bool m_OptimizerInitialized;
std::unique_ptr<InternalOptimizerType> m_VnlOptimizer;
};
} // end namespace itk

Expand Down
9 changes: 5 additions & 4 deletions Modules/Numerics/Optimizers/include/itkLBFGSBOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkIntTypes.h"
#include "itkSingleValuedNonLinearVnlOptimizer.h"
#include "ITKOptimizersExport.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -216,10 +217,10 @@ class ITKOptimizers_EXPORT LBFGSBOptimizer : public SingleValuedNonLinearVnlOpti
unsigned int m_CurrentIteration{ 0 };
double m_InfinityNormOfProjectedGradient{ 0.0 };

InternalOptimizerType * m_VnlOptimizer{ nullptr };
BoundValueType m_LowerBound;
BoundValueType m_UpperBound;
BoundSelectionType m_BoundSelection;
std::unique_ptr<InternalOptimizerType> m_VnlOptimizer;
BoundValueType m_LowerBound;
BoundValueType m_UpperBound;
BoundSelectionType m_BoundSelection;
};
} // end namespace itk

Expand Down
7 changes: 4 additions & 3 deletions Modules/Numerics/Optimizers/include/itkLBFGSOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkSingleValuedNonLinearVnlOptimizer.h"
#include "vnl/algo/vnl_lbfgs.h"
#include "ITKOptimizersExport.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -179,9 +180,9 @@ class ITKOptimizers_EXPORT LBFGSOptimizer : public SingleValuedNonLinearVnlOptim
using CostFunctionAdaptorType = Superclass::CostFunctionAdaptorType;

private:
bool m_OptimizerInitialized;
InternalOptimizerType * m_VnlOptimizer;
mutable std::ostringstream m_StopConditionDescription;
bool m_OptimizerInitialized;
std::unique_ptr<InternalOptimizerType> m_VnlOptimizer;
mutable std::ostringstream m_StopConditionDescription;

bool m_Trace;
unsigned int m_MaximumNumberOfFunctionEvaluations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkMultipleValuedNonLinearVnlOptimizer.h"
#include "vnl/algo/vnl_levenberg_marquardt.h"
#include "ITKOptimizersExport.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -95,12 +96,12 @@ class ITKOptimizers_EXPORT LevenbergMarquardtOptimizer : public MultipleValuedNo
using CostFunctionAdaptorType = Superclass::CostFunctionAdaptorType;

private:
bool m_OptimizerInitialized;
InternalOptimizerType * m_VnlOptimizer;
unsigned int m_NumberOfIterations;
double m_ValueTolerance;
double m_GradientTolerance;
double m_EpsilonFunction;
bool m_OptimizerInitialized;
std::unique_ptr<InternalOptimizerType> m_VnlOptimizer;
unsigned int m_NumberOfIterations;
double m_ValueTolerance;
double m_GradientTolerance;
double m_EpsilonFunction;
};
} // end namespace itk

Expand Down
10 changes: 3 additions & 7 deletions Modules/Numerics/Optimizers/src/itkAmoebaOptimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ AmoebaOptimizer::AmoebaOptimizer()
}


AmoebaOptimizer::~AmoebaOptimizer()
{
delete m_VnlOptimizer;
}
AmoebaOptimizer::~AmoebaOptimizer() = default;


const std::string
Expand Down Expand Up @@ -95,7 +92,7 @@ AmoebaOptimizer::GetValue() const
vnl_amoeba *
AmoebaOptimizer::GetOptimizer() const
{
return this->m_VnlOptimizer;
return this->m_VnlOptimizer.get();
}

void
Expand Down Expand Up @@ -150,8 +147,7 @@ AmoebaOptimizer::StartOptimization()
CostFunctionAdaptorType * adaptor = GetNonConstCostFunctionAdaptor();
// get rid of previous instance of the internal optimizer and create a
// new one
delete m_VnlOptimizer;
m_VnlOptimizer = new vnl_amoeba(*adaptor);
m_VnlOptimizer = std::make_unique<vnl_amoeba>(*adaptor);
m_VnlOptimizer->set_max_iterations(static_cast<int>(m_MaximumNumberOfIterations));
m_VnlOptimizer->set_x_tolerance(m_ParametersConvergenceTolerance);
m_VnlOptimizer->set_f_tolerance(m_FunctionConvergenceTolerance);
Expand Down
14 changes: 3 additions & 11 deletions Modules/Numerics/Optimizers/src/itkConjugateGradientOptimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ ConjugateGradientOptimizer::ConjugateGradientOptimizer()
/**
* Destructor
*/
ConjugateGradientOptimizer::~ConjugateGradientOptimizer()
{
delete m_VnlOptimizer;
}
ConjugateGradientOptimizer::~ConjugateGradientOptimizer() = default;

/**
* Get the Optimizer
*/
vnl_conjugate_gradient *
ConjugateGradientOptimizer::GetOptimizer()
{
return m_VnlOptimizer;
return m_VnlOptimizer.get();
}

/**
Expand All @@ -58,14 +55,9 @@ ConjugateGradientOptimizer::SetCostFunction(SingleValuedCostFunction * costFunct

adaptor->SetCostFunction(costFunction);

if (m_OptimizerInitialized)
{
delete m_VnlOptimizer;
}

this->SetCostFunctionAdaptor(adaptor);

m_VnlOptimizer = new vnl_conjugate_gradient(*adaptor);
m_VnlOptimizer = std::make_unique<vnl_conjugate_gradient>(*adaptor);
m_OptimizerInitialized = true;
}

Expand Down
12 changes: 2 additions & 10 deletions Modules/Numerics/Optimizers/src/itkLBFGSBOptimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ LBFGSBOptimizer::LBFGSBOptimizer()
/**
* Destructor
*/
LBFGSBOptimizer::~LBFGSBOptimizer()
{
delete m_VnlOptimizer;
}
LBFGSBOptimizer::~LBFGSBOptimizer() = default;

/**
* PrintSelf
Expand Down Expand Up @@ -266,14 +263,9 @@ LBFGSBOptimizer::SetCostFunction(SingleValuedCostFunction * costFunction)

adaptor->SetCostFunction(costFunction);

if (m_OptimizerInitialized)
{
delete m_VnlOptimizer;
}

this->SetCostFunctionAdaptor(adaptor);

m_VnlOptimizer = new InternalOptimizerType(*adaptor, this);
m_VnlOptimizer = std::make_unique<InternalOptimizerType>(*adaptor, this);

// set the optimizer parameters
m_VnlOptimizer->set_lower_bound(m_LowerBound);
Expand Down
14 changes: 3 additions & 11 deletions Modules/Numerics/Optimizers/src/itkLBFGSOptimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ LBFGSOptimizer::LBFGSOptimizer()
/**
* Destructor
*/
LBFGSOptimizer::~LBFGSOptimizer()
{
delete m_VnlOptimizer;
}
LBFGSOptimizer::~LBFGSOptimizer() = default;

/**
* PrintSelf
Expand Down Expand Up @@ -185,14 +182,9 @@ LBFGSOptimizer::SetCostFunction(SingleValuedCostFunction * costFunction)

adaptor->SetCostFunction(costFunction);

if (m_OptimizerInitialized)
{
delete m_VnlOptimizer;
}

this->SetCostFunctionAdaptor(adaptor);

m_VnlOptimizer = new vnl_lbfgs(*adaptor);
m_VnlOptimizer = std::make_unique<vnl_lbfgs>(*adaptor);

// set the optimizer parameters
m_VnlOptimizer->set_trace(m_Trace);
Expand Down Expand Up @@ -265,7 +257,7 @@ LBFGSOptimizer::StartOptimization()
vnl_lbfgs *
LBFGSOptimizer::GetOptimizer()
{
return m_VnlOptimizer;
return m_VnlOptimizer.get();
}

const std::string
Expand Down
14 changes: 3 additions & 11 deletions Modules/Numerics/Optimizers/src/itkLevenbergMarquardtOptimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ LevenbergMarquardtOptimizer::LevenbergMarquardtOptimizer()
/**
* Destructor
*/
LevenbergMarquardtOptimizer::~LevenbergMarquardtOptimizer()
{
delete m_VnlOptimizer;
}
LevenbergMarquardtOptimizer::~LevenbergMarquardtOptimizer() = default;

/**
* Connect a Cost Function
Expand All @@ -54,14 +51,9 @@ LevenbergMarquardtOptimizer::SetCostFunction(MultipleValuedCostFunction * costFu

adaptor->SetCostFunction(costFunction);

if (m_OptimizerInitialized)
{
delete m_VnlOptimizer;
}

this->SetCostFunctionAdaptor(adaptor);

m_VnlOptimizer = new vnl_levenberg_marquardt(*adaptor);
m_VnlOptimizer = std::make_unique<vnl_levenberg_marquardt>(*adaptor);

this->SetNumberOfIterations(m_NumberOfIterations);
this->SetValueTolerance(m_ValueTolerance);
Expand Down Expand Up @@ -203,7 +195,7 @@ LevenbergMarquardtOptimizer::SetEpsilonFunction(double epsilon)
vnl_levenberg_marquardt *
LevenbergMarquardtOptimizer::GetOptimizer() const
{
return m_VnlOptimizer;
return m_VnlOptimizer.get();
}

const std::string
Expand Down
13 changes: 7 additions & 6 deletions Modules/Numerics/Optimizersv4/include/itkAmoebaOptimizerv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkSingleValuedNonLinearVnlOptimizerv4.h"
#include "vnl/algo/vnl_amoeba.h"
#include "ITKOptimizersv4Export.h"
#include <memory> // For unique_ptr.

namespace itk
{
Expand Down Expand Up @@ -153,12 +154,12 @@ class ITKOptimizersv4_EXPORT AmoebaOptimizerv4 : public SingleValuedNonLinearVnl
void
ValidateSettings();

ParametersType::ValueType m_ParametersConvergenceTolerance;
MeasureType m_FunctionConvergenceTolerance;
bool m_AutomaticInitialSimplex;
ParametersType m_InitialSimplexDelta;
bool m_OptimizeWithRestarts;
vnl_amoeba * m_VnlOptimizer;
ParametersType::ValueType m_ParametersConvergenceTolerance;
MeasureType m_FunctionConvergenceTolerance;
bool m_AutomaticInitialSimplex;
ParametersType m_InitialSimplexDelta;
bool m_OptimizeWithRestarts;
std::unique_ptr<vnl_amoeba> m_VnlOptimizer;

std::ostringstream m_StopConditionDescription;
};
Expand Down
10 changes: 3 additions & 7 deletions Modules/Numerics/Optimizersv4/src/itkAmoebaOptimizerv4.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ AmoebaOptimizerv4::AmoebaOptimizerv4()
}


AmoebaOptimizerv4::~AmoebaOptimizerv4()
{
delete m_VnlOptimizer;
}
AmoebaOptimizerv4::~AmoebaOptimizerv4() = default;


const std::string
Expand All @@ -62,7 +59,7 @@ AmoebaOptimizerv4::PrintSelf(std::ostream & os, Indent indent) const
vnl_amoeba *
AmoebaOptimizerv4::GetOptimizer() const
{
return this->m_VnlOptimizer;
return this->m_VnlOptimizer.get();
}

void
Expand Down Expand Up @@ -136,8 +133,7 @@ AmoebaOptimizerv4::StartOptimization(bool /* doOnlyInitialization */)
CostFunctionAdaptorType * adaptor = GetNonConstCostFunctionAdaptor();
// get rid of previous instance of the internal optimizer and create a
// new one
delete m_VnlOptimizer;
m_VnlOptimizer = new vnl_amoeba(*adaptor);
m_VnlOptimizer = std::make_unique<vnl_amoeba>(*adaptor);
m_VnlOptimizer->set_max_iterations(static_cast<int>(m_NumberOfIterations));
m_VnlOptimizer->set_x_tolerance(m_ParametersConvergenceTolerance);
m_VnlOptimizer->set_f_tolerance(m_FunctionConvergenceTolerance);
Expand Down

0 comments on commit 7357008

Please sign in to comment.