Skip to content

Commit

Permalink
STYLE: Simplify creating string representation of stop conditions
Browse files Browse the repository at this point in the history
Replaced `break` statements in `switch` cases by `return` statements, directly returning the appropriate string, in Optimizers.

Using Notepad++, Replace in Files:

    Find what: ^  ([ ]+)stopcondition = "(.+)";\r\n  \1break;\r\n
    Replace with: $1return "$2";
    (*) Regular expression
  • Loading branch information
N-Dekker committed Feb 13, 2025
1 parent 17eb1ca commit ff26cc7
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 408 deletions.
33 changes: 13 additions & 20 deletions Components/Optimizers/AdaGrad/elxAdaGrad.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -324,26 +324,19 @@ AdaGrad<TElastix>::AfterEachResolution()
* MetricError,
* MinimumStepSize } ;
*/
std::string stopcondition;

switch (this->GetStopCondition())
{
case MaximumNumberOfIterations:
stopcondition = "Maximum number of iterations has been reached";
break;

case MetricError:
stopcondition = "Error in metric";
break;

case MinimumStepSize:
stopcondition = "The minimum step length has been reached";
break;

default:
stopcondition = "Unknown";
break;
}
const std::string stopcondition = [this] {
switch (this->GetStopCondition())
{
case MaximumNumberOfIterations:
return "Maximum number of iterations has been reached";
case MetricError:
return "Error in metric";
case MinimumStepSize:
return "The minimum step length has been reached";
default:
return "Unknown";
}
}();

/** Print the stopping condition. */
log::info(std::ostringstream{} << "Stopping condition: " << stopcondition << ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,26 +304,19 @@ AdaptiveStochasticGradientDescent<TElastix>::AfterEachResolution()
* MetricError,
* MinimumStepSize };
*/
std::string stopcondition;

switch (this->GetStopCondition())
{
case MaximumNumberOfIterations:
stopcondition = "Maximum number of iterations has been reached";
break;

case MetricError:
stopcondition = "Error in metric";
break;

case MinimumStepSize:
stopcondition = "The minimum step length has been reached";
break;

default:
stopcondition = "Unknown";
break;
}
const std::string stopcondition = [this] {
switch (this->GetStopCondition())
{
case MaximumNumberOfIterations:
return "Maximum number of iterations has been reached";
case MetricError:
return "Error in metric";
case MinimumStepSize:
return "The minimum step length has been reached";
default:
return "Unknown";
}
}();

/** Print the stopping condition. */
log::info(std::ostringstream{} << "Stopping condition: " << stopcondition << ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,34 +375,23 @@ AdaptiveStochasticLBFGS<TElastix>::AfterEachResolution()
* MetricError,
* MinimumStepSize };
*/
std::string stopcondition;

switch (this->GetStopCondition())
{
case MetricError:
stopcondition = "Error in metric";
break;

case MaximumNumberOfIterations:
stopcondition = "Maximum number of iterations has been reached";
break;

case InvalidDiagonalMatrix:
stopcondition = "The InvalidDiagonalMatrix";
break;

case GradientMagnitudeTolerance:
stopcondition = "The gradient magnitude has (nearly) vanished";
break;

case MinimumStepSize:
stopcondition = "The last step size was (nearly) zero";
break;

default:
stopcondition = "Unknown";
break;
}
const std::string stopcondition = [this] {
switch (this->GetStopCondition())
{
case MetricError:
return "Error in metric";
case MaximumNumberOfIterations:
return "Maximum number of iterations has been reached";
case InvalidDiagonalMatrix:
return "The InvalidDiagonalMatrix";
case GradientMagnitudeTolerance:
return "The gradient magnitude has (nearly) vanished";
case MinimumStepSize:
return "The last step size was (nearly) zero";
default:
return "Unknown";
}
}();

/** Print the stopping condition. */
log::info(std::ostringstream{} << "Stopping condition: " << stopcondition << ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,22 +327,17 @@ AdaptiveStochasticVarianceReducedGradient<TElastix>::AfterEachResolution()
* MetricError,
* MinimumStepSize };
*/
std::string stopcondition;

switch (this->GetStopCondition())
{
case MetricError:
stopcondition = "Error in metric";
break;

case MinimumStepSize:
stopcondition = "The minimum step length has been reached";
break;

default:
stopcondition = "Unknown";
break;
}
const std::string stopcondition = [this] {
switch (this->GetStopCondition())
{
case MetricError:
return "Error in metric";
case MinimumStepSize:
return "The minimum step length has been reached";
default:
return "Unknown";
}
}();

/** Print the stopping condition. */
log::info(std::ostringstream{} << "Stopping condition: " << stopcondition << ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,38 +240,25 @@ CMAEvolutionStrategy<TElastix>::AfterEachResolution()
ZeroStepLength,
Unknown }; */

std::string stopcondition;

switch (this->GetStopCondition())
{
case MetricError:
stopcondition = "Error in metric";
break;

case MaximumNumberOfIterations:
stopcondition = "Maximum number of iterations has been reached";
break;

case PositionToleranceMin:
stopcondition = "The minimum step length condition has been reached";
break;

case PositionToleranceMax:
stopcondition = "The maximum step length condition has been reached";
break;

case ValueTolerance:
stopcondition = "Almost no decrease in function value anymore";
break;

case ZeroStepLength:
stopcondition = "The step length is 0";
break;

default:
stopcondition = "Unknown";
break;
}
const std::string stopcondition = [this] {
switch (this->GetStopCondition())
{
case MetricError:
return "Error in metric";
case MaximumNumberOfIterations:
return "Maximum number of iterations has been reached";
case PositionToleranceMin:
return "The minimum step length condition has been reached";
case PositionToleranceMax:
return "The maximum step length condition has been reached";
case ValueTolerance:
return "Almost no decrease in function value anymore";
case ZeroStepLength:
return "The step length is 0";
default:
return "Unknown";
}
}();

/** Print the stopping condition */
log::info(std::ostringstream{} << "Stopping condition: " << stopcondition << ".");
Expand Down
92 changes: 29 additions & 63 deletions Components/Optimizers/ConjugateGradient/elxConjugateGradient.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -402,37 +402,26 @@ ConjugateGradient<TElastix>::AfterEachResolution()
}
else
{
switch (this->GetStopCondition())
{

case StopConditionType::MetricError:
stopcondition = "Error in metric";
break;

case StopConditionType::LineSearchError:
stopcondition = "Error in LineSearch";
break;

case StopConditionType::MaximumNumberOfIterations:
stopcondition = "Maximum number of iterations has been reached";
break;

case StopConditionType::GradientMagnitudeTolerance:
stopcondition = "The gradient magnitude has (nearly) vanished";
break;

case StopConditionType::ValueTolerance:
stopcondition = "Almost no decrease in function value anymore";
break;

case StopConditionType::InfiniteBeta:
stopcondition = "The beta factor became infinite";
break;
stopcondition = [this] {
switch (this->GetStopCondition())
{

default:
stopcondition = "Unknown";
break;
}
case StopConditionType::MetricError:
return "Error in metric";
case StopConditionType::LineSearchError:
return "Error in LineSearch";
case StopConditionType::MaximumNumberOfIterations:
return "Maximum number of iterations has been reached";
case StopConditionType::GradientMagnitudeTolerance:
return "The gradient magnitude has (nearly) vanished";
case StopConditionType::ValueTolerance:
return "Almost no decrease in function value anymore";
case StopConditionType::InfiniteBeta:
return "The beta factor became infinite";
default:
return "Unknown";
}
}();
} // end else

/** Print the stopping condition */
Expand Down Expand Up @@ -509,52 +498,29 @@ ConjugateGradient<TElastix>::GetLineSearchStopCondition() const
Unknown
};

std::string stopcondition;

auto lineSearchStopCondition = static_cast<LineSearchStopConditionType>(this->m_LineOptimizer->GetStopCondition());

switch (lineSearchStopCondition)
switch (static_cast<LineSearchStopConditionType>(this->m_LineOptimizer->GetStopCondition()))
{

case LineSearchStopConditionType::StrongWolfeConditionsSatisfied:
stopcondition = "WolfeSatisfied";
break;

return "WolfeSatisfied";
case LineSearchStopConditionType::MetricError:
stopcondition = "MetricError";
break;

return "MetricError";
case LineSearchStopConditionType::MaximumNumberOfIterations:
stopcondition = "MaxNrIterations";
break;

return "MaxNrIterations";
case LineSearchStopConditionType::StepTooSmall:
stopcondition = "StepTooSmall";
break;

return "StepTooSmall";
case LineSearchStopConditionType::StepTooLarge:
stopcondition = "StepTooLarge";
break;

return "StepTooLarge";
case LineSearchStopConditionType::IntervalTooSmall:
stopcondition = "IntervalTooSmall";
break;

return "IntervalTooSmall";
case LineSearchStopConditionType::RoundingError:
stopcondition = "RoundingError";
break;

return "RoundingError";
case LineSearchStopConditionType::AscentSearchDirection:
stopcondition = "AscentSearchDir";
break;

return "AscentSearchDir";
default:
stopcondition = "Unknown";
break;
return "Unknown";
}

return stopcondition;

} // end GetLineSearchStopCondition


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,26 +203,19 @@ ConjugateGradientFRPR<TElastix>::AfterEachResolution()

/** \todo StopConditions are not yet implemented in the FRPROptimizer;
* Uncomment the following code when they are implemented.
std::string stopcondition = [this] {
switch (this->GetStopCondition())
{
std::string stopcondition;
switch( this->GetStopCondition() )
{
case MaximumNumberOfIterations :
stopcondition = "Maximum number of iterations has been reached";
break;
case MetricError :
stopcondition = "Error in metric";
break;
default:
stopcondition = "Unknown";
break;
}
*/
case MaximumNumberOfIterations:
return "Maximum number of iterations has been reached";
case MetricError:
return "Error in metric";
default:
return "Unknown";
}
}();
*/
/** Print the stopping condition */
// elxout << "Stopping condition: " << stopcondition << "." << std::endl;

Expand Down
Loading

0 comments on commit ff26cc7

Please # to comment.