https://public.kitware.com/Wiki/index.php?title=SPSAOptimizer&feed=atom&action=history
SPSAOptimizer - Revision history
2024-03-28T19:46:26Z
Revision history for this page on the wiki
MediaWiki 1.38.6
https://public.kitware.com/Wiki/index.php?title=SPSAOptimizer&diff=4268&oldid=prev
Andy at 20:41, 20 December 2005
2005-12-20T20:41:56Z
<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 20:41, 20 December 2005</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l403">Line 403:</td>
<td colspan="2" class="diff-lineno">Line 403:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>#endif</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>#endif</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></pre></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></pre></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">{{ITK/Template/Footer}}</ins></div></td></tr>
</table>
Andy
https://public.kitware.com/Wiki/index.php?title=SPSAOptimizer&diff=2787&oldid=prev
Blezek at 13:36, 17 March 2005
2005-03-17T13:36:37Z
<p></p>
<p><b>New page</b></p><div>Dan Blezek's implemetation of the SPSA optimizer. Because I couldn't upload the files as media, here they are:<br />
<br />
itkSPSAOptimizer.h<br />
<pre>/*=========================================================================<br />
<br />
Program: Insight Segmentation & Registration Toolkit<br />
Module: $RCSfile: itkSPSAOptimizer.h,v $<br />
Language: C++<br />
Date: $Date: 2005/03/10 13:32:40 $<br />
Version: $Revision: 1.1 $<br />
<br />
Copyright (c) Insight Software Consortium. All rights reserved.<br />
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.<br />
<br />
This software is distributed WITHOUT ANY WARRANTY; without even <br />
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR <br />
PURPOSE. See the above copyright notices for more information.<br />
<br />
=========================================================================*/<br />
#ifndef __itkSPSAOptimizer_h<br />
#define __itkSPSAOptimizer_h<br />
<br />
#include "itkSingleValuedNonLinearOptimizer.h"<br />
#include <log4cxx/logger.h><br />
<br />
namespace itk<br />
{<br />
<br />
/** \class SPSAOptimizer<br />
* \brief Implements the Simultaseous Perturbation Stochastic Approximation optimizer<br />
* \ingroup Numerics Optimizers<br />
*/ <br />
class ITK_EXPORT SPSAOptimizer : <br />
public SingleValuedNonLinearOptimizer<br />
{<br />
public:<br />
/** Standard class typedefs. */<br />
typedef SPSAOptimizer Self;<br />
typedef SingleValuedNonLinearOptimizer Superclass;<br />
typedef SmartPointer<Self> Pointer;<br />
typedef SmartPointer<const Self> ConstPointer;<br />
<br />
/** ParametersType typedef.<br />
* It defines a position in the optimization search space. */<br />
typedef Superclass::ParametersType ParametersType;<br />
<br />
/** Method for creation through the object factory. */<br />
itkNewMacro(Self);<br />
<br />
/** Run-time type information (and related methods). */<br />
itkTypeMacro( SPSAOptimizer, SingleValuedNonLinearOptimizer );<br />
<br />
/** Methods to configure the cost function. */<br />
itkGetConstMacro( Maximize, bool );<br />
itkSetMacro( Maximize, bool );<br />
itkBooleanMacro( Maximize );<br />
bool GetMinimize( ) const<br />
{ return !m_Maximize; }<br />
void SetMinimize(bool v)<br />
{ this->SetMaximize(!v); }<br />
void MinimizeOn()<br />
{ this->MaximizeOff(); }<br />
void MinimizeOff()<br />
{ this->MaximizeOn(); }<br />
<br />
/** Advance one step following the gradient direction. */<br />
virtual void AdvanceOneStep( void );<br />
<br />
/** Start optimization. */<br />
void StartOptimization( void );<br />
<br />
/** Resume previously stopped optimization with current parameters<br />
* \sa StopOptimization. */<br />
void ResumeOptimization( void );<br />
<br />
/** Stop optimization.<br />
* \sa ResumeOptimization */<br />
void StopOptimization( void );<br />
<br />
/** Set the number of iterations. */<br />
itkSetMacro( NumberOfIterations, unsigned long );<br />
<br />
/** Get the number of iterations. */<br />
itkGetConstMacro( NumberOfIterations, unsigned long );<br />
<br />
/** Get the current iteration number. */<br />
itkGetConstMacro( CurrentIteration, unsigned int );<br />
<br />
/** Get the current value. */<br />
itkGetConstMacro( Value, double );<br />
<br />
/** Get the current value. */<br />
itkSetMacro( Alpha, double );<br />
itkGetConstMacro( Alpha, double );<br />
<br />
/** Get the current value. */<br />
itkSetMacro( Gamma, double );<br />
itkGetConstMacro( Gamma, double );<br />
<br />
/** Get the current value. */<br />
itkSetMacro( a, double );<br />
itkGetConstMacro( a, double );<br />
<br />
/** Get the current value. */<br />
itkSetMacro( A, double );<br />
itkGetConstMacro( A, double );<br />
<br />
/** Get the current value. */<br />
itkSetMacro( c, double );<br />
itkGetConstMacro( c, double );<br />
<br />
protected:<br />
SPSAOptimizer();<br />
virtual ~SPSAOptimizer() {};<br />
void PrintSelf(std::ostream& os, Indent indent) const;<br />
<br />
// made protected so subclass can access<br />
bool m_Maximize;<br />
<br />
private:<br />
static log4cxx::LoggerPtr logger;<br />
<br />
SPSAOptimizer(const Self&); //purposely not implemented<br />
void operator=(const Self&); //purposely not implemented<br />
<br />
bool m_Stop;<br />
double m_Value;<br />
unsigned long m_NumberOfIterations;<br />
unsigned long m_CurrentIteration;<br />
double m_Alpha, m_Gamma, m_a, m_A, m_c;<br />
ParametersType m_CurrentPosition;<br />
<br />
};<br />
<br />
} // end namespace itk<br />
<br />
<br />
#endif<br />
<br />
<br />
<br />
</pre><br />
<br />
and itkSPSAOPtimizer.cxx<br />
<br />
<pre>/*=========================================================================<br />
<br />
Program: Insight Segmentation & Registration Toolkit<br />
Module: $RCSfile: itkSPSAOptimizer.cxx,v $<br />
Language: C++<br />
Date: $Date: 2005/03/10 13:32:40 $<br />
Version: $Revision: 1.1 $<br />
<br />
Copyright (c) Insight Software Consortium. All rights reserved.<br />
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.<br />
<br />
This software is distributed WITHOUT ANY WARRANTY; without even <br />
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR <br />
PURPOSE. See the above copyright notices for more information.<br />
<br />
=========================================================================*/<br />
#ifndef _itkSPSAOptimizer_txx<br />
#define _itkSPSAOptimizer_txx<br />
<br />
#include "itkSPSAOptimizer.h"<br />
#include "itkCommand.h"<br />
#include "itkEventObject.h"<br />
#include "itkExceptionObject.h"<br />
#include "vnl/vnl_math.h"<br />
#include "vnl/vnl_sample.h"<br />
<br />
<br />
namespace itk<br />
{<br />
log4cxx::LoggerPtr SPSAOptimizer::logger;<br />
<br />
/**<br />
* Constructor<br />
*/<br />
SPSAOptimizer<br />
::SPSAOptimizer()<br />
{<br />
if ( logger == NULL )<br />
{<br />
logger = log4cxx::Logger::getLogger ( "org.itk.Code.Numerics.SPSAOptimizer" );<br />
}<br />
logger->debug ( "Constructor" );<br />
itkDebugMacro("Constructor");<br />
<br />
m_NumberOfIterations = 100;<br />
m_CurrentIteration = 0;<br />
m_Maximize = false;<br />
m_Value = 0.0;<br />
m_Alpha = 0.602;<br />
m_Gamma = 0.101;<br />
m_a = 0.1;<br />
m_A = 0.1;<br />
m_c = 0.1;<br />
}<br />
<br />
<br />
<br />
void<br />
SPSAOptimizer<br />
::PrintSelf(std::ostream& os, Indent indent) const<br />
{<br />
Superclass::PrintSelf(os,indent);<br />
<br />
os << indent << "NunberOfIterations: "<br />
<< m_NumberOfIterations << std::endl;<br />
os << indent << "Maximize: "<br />
<< m_Maximize << std::endl;<br />
os << indent << "CurrentIteration: "<br />
<< m_CurrentIteration;<br />
os << indent << "Value: "<br />
<< m_Value;<br />
if (m_CostFunction)<br />
{<br />
os << indent << "CostFunction: "<br />
<< m_CostFunction;<br />
}<br />
os << indent << "Alpha: " << m_Alpha << std::endl;<br />
os << indent << "Gamma: " << m_Gamma << std::endl;<br />
os << indent << "a: " << m_a << std::endl;<br />
os << indent << "A: " << m_A << std::endl;<br />
os << indent << "c: " << m_c << std::endl;<br />
os << std::endl;<br />
}<br />
<br />
<br />
/**<br />
* Start the optimization<br />
*/<br />
void<br />
SPSAOptimizer<br />
::StartOptimization( void )<br />
{<br />
<br />
itkDebugMacro("StartOptimization");<br />
logger->debug ( "StartOptimization" ); <br />
m_CurrentIteration = 0;<br />
<br />
this->SetCurrentPosition( this->GetInitialPosition() );<br />
this->ResumeOptimization();<br />
<br />
}<br />
<br />
<br />
<br />
/**<br />
* Resume the optimization<br />
*/<br />
void<br />
SPSAOptimizer<br />
::ResumeOptimization( void )<br />
{<br />
<br />
itkDebugMacro("ResumeOptimization");<br />
<br />
m_Stop = false;<br />
<br />
InvokeEvent( StartEvent() );<br />
while( !m_Stop ) <br />
{<br />
<br />
try<br />
{<br />
m_Value = m_CostFunction->GetValue( this->GetCurrentPosition() );<br />
}<br />
catch( ExceptionObject& err )<br />
{<br />
// An exception has occurred. <br />
// Terminate immediately.<br />
StopOptimization();<br />
<br />
// Pass exception to caller<br />
throw err;<br />
}<br />
<br />
<br />
if( m_Stop )<br />
{<br />
break;<br />
}<br />
<br />
AdvanceOneStep();<br />
<br />
m_CurrentIteration++;<br />
<br />
if( m_CurrentIteration >= m_NumberOfIterations )<br />
{<br />
StopOptimization();<br />
break;<br />
}<br />
<br />
}<br />
<br />
<br />
}<br />
<br />
<br />
/**<br />
* Stop optimization<br />
*/<br />
void<br />
SPSAOptimizer<br />
::StopOptimization( void )<br />
{<br />
<br />
itkDebugMacro("StopOptimization");<br />
<br />
m_Stop = true;<br />
InvokeEvent( EndEvent() );<br />
}<br />
<br />
<br />
<br />
<br />
<br />
/**<br />
* Advance one Step following the gradient direction<br />
*/<br />
void<br />
SPSAOptimizer<br />
::AdvanceOneStep( void )<br />
{ <br />
char buffer[1024];<br />
itkDebugMacro("AdvanceOneStep");<br />
logger->debug ( "AdvanceOneStep" );<br />
double direction;<br />
if( this->m_Maximize ) <br />
{<br />
direction = 1.0;<br />
}<br />
else <br />
{<br />
direction = -1.0;<br />
}<br />
<br />
const unsigned int spaceDimension = <br />
m_CostFunction->GetNumberOfParameters();<br />
<br />
const ParametersType & currentPosition = this->GetCurrentPosition();<br />
<br />
ScalesType scales = this->GetScales();<br />
<br />
// Make sure the scales have been set properly<br />
if (scales.size() != spaceDimension)<br />
{<br />
itkExceptionMacro(<< "The size of Scales is "<br />
<< scales.size()<br />
<< ", but the NumberOfParameters for the CostFunction is "<br />
<< spaceDimension<br />
<< ".");<br />
}<br />
<br />
<br />
double ak, ck, yplus, yminus;<br />
double a = m_a, c = m_c, A = m_A;<br />
<br />
ak = a / pow (A + m_CurrentIteration, m_Alpha);<br />
ck = c / pow ( m_CurrentIteration+1, m_Gamma );<br />
ParametersType delta ( spaceDimension );<br />
ParametersType theta ( spaceDimension );<br />
<br />
sprintf ( buffer, "ak: %f ck: %f", ak, ck );<br />
logger->debug ( buffer );<br />
<br />
for ( unsigned int i = 0; i < spaceDimension; i++ ) {<br />
delta[i] = 2 * vnl_math_rnd ( vnl_sample_uniform ( 0.0f, 1.0f ) ) - 1;<br />
delta[i] = delta[i] / scales[i];<br />
}<br />
<br />
for ( unsigned int i = 0; i < spaceDimension; i++ ) {<br />
theta[i] = currentPosition[i] + ck * delta[i];<br />
}<br />
<br />
yplus = m_CostFunction->GetValue ( theta );<br />
<br />
for ( unsigned int i = 0; i < spaceDimension; i++ ) {<br />
theta[i] = currentPosition[i] - ck * delta[i];<br />
}<br />
<br />
yminus = m_CostFunction->GetValue ( theta );<br />
<br />
sprintf ( buffer, "New Position: " );<br />
double ydiff = direction * ( yplus - yminus );<br />
for ( unsigned int i = 0; i < spaceDimension; i++ ) {<br />
theta[i] = currentPosition[i] - ak * ydiff / ( 2.0 * ck * delta[i] );<br />
sprintf ( buffer, "%s %f, ", buffer, theta[i] );<br />
}<br />
logger->debug ( buffer ); <br />
this->SetCurrentPosition( theta );<br />
<br />
this->InvokeEvent( IterationEvent() );<br />
<br />
}<br />
<br />
<br />
<br />
} // end namespace itk<br />
<br />
#endif<br />
</pre></div>
Blezek