Skip to content

Commit 4faefbe

Browse files
committed
BUG: Honor StopOptimization from IterationEvent in v4 optimizers
Add an immediate m_Stop check after InvokeEvent(IterationEvent()) in ExhaustiveOptimizerv4, PowellOptimizerv4, and OnePlusOneEvolutionaryOptimizerv4. Previously these optimizers would continue advancing after an observer called StopOptimization() (or StopWalking()) during IterationEvent, making it impossible to halt from a callback. This is the companion change to cf929f2 which added the same pattern to the GradientDescentOptimizerv4 family. All v4 optimizers now consistently stop before taking another step when Stop is requested from an IterationEvent observer.
1 parent 12c0cb7 commit 4faefbe

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Modules/Numerics/Optimizersv4/include/itkExhaustiveOptimizerv4.hxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ ExhaustiveOptimizerv4<TInternalComputationValueType>::ResumeWalking()
134134
<< "@ index " << this->GetCurrentIndex() << " value is " << m_CurrentValue;
135135

136136
this->InvokeEvent(IterationEvent());
137+
if (m_Stop)
138+
{
139+
this->StopWalking();
140+
break;
141+
}
137142
this->AdvanceOneStep();
138143
this->m_CurrentIteration++;
139144
}

Modules/Numerics/Optimizersv4/include/itkOnePlusOneEvolutionaryOptimizerv4.hxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ OnePlusOneEvolutionaryOptimizerv4<TInternalComputationValueType>::StartOptimizat
257257
}
258258

259259
this->InvokeEvent(IterationEvent());
260+
if (m_Stop)
261+
{
262+
m_StopConditionDescription.str("");
263+
m_StopConditionDescription << this->GetNameOfClass() << ": "
264+
<< "StopOptimization() called";
265+
break;
266+
}
260267
itkDebugMacro("Current position: " << this->GetCurrentPosition());
261268
}
262269
if (this->m_CurrentIteration >= m_MaximumIteration)

Modules/Numerics/Optimizersv4/include/itkPowellOptimizerv4.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ PowellOptimizerv4<TInternalComputationValueType>::StartOptimization(bool /* doOn
498498
}
499499

500500
this->InvokeEvent(IterationEvent());
501+
if (m_Stop)
502+
{
503+
m_StopConditionDescription << "StopOptimization() called";
504+
this->InvokeEvent(EndEvent());
505+
return;
506+
}
501507
}
502508

503509
m_StopConditionDescription << "Maximum number of iterations exceeded. "

0 commit comments

Comments
 (0)