ANIMA  4.0
animaVoxelExhaustiveOptimizer.cxx
Go to the documentation of this file.
2 #include <itkCommand.h>
3 #include <itkEventObject.h>
4 
5 namespace anima
6 {
7 
13 {
14  m_CurrentIteration = 0;
15  m_CurrentValue = 0;
16  m_CurrentParameter = 0;
17  m_CurrentIndex.Fill(0);
18  m_Stop = false;
19  m_NumberOfSteps.Fill(0);
20 
21  m_Geometry.set_size(3,3);
22  m_Geometry.fill(0);
23  m_Geometry.fill_diagonal(1);
24 
25  m_StopConditionDescription.str("");
26 }
27 
28 const VoxelExhaustiveOptimizer::MeasureType& VoxelExhaustiveOptimizer
30 {
31  if (this->m_Maximize)
32  return this->GetMaximumMetricValue();
33  else
34  return this->GetMinimumMetricValue();
35 }
36 
42 {
43  this->StartWalking();
44 
45  if (this->m_Maximize)
46  {
48  this->m_CurrentPosition = this->GetMaximumMetricValuePosition();
49  }
50  else
51  {
52  this->m_CurrentValue = this->GetMinimumMetricValue();
53  this->m_CurrentPosition = this->GetMinimumMetricValuePosition();
54  }
55 }
56 
57 
58 void
61 {
62  this->InvokeEvent( itk::StartEvent() );
63  m_StopConditionDescription.str("");
64  m_StopConditionDescription << this->GetNameOfClass() << ": Running";
65 
66  ParametersType initialPos = this->GetInitialPosition();
67  m_MinimumMetricValuePosition = initialPos;
68  m_MaximumMetricValuePosition = initialPos;
69 
70  MeasureType initialValue = this->GetValue( this->GetInitialPosition() );
71  m_MaximumMetricValue = initialValue;
72  m_MinimumMetricValue = initialValue;
73 
76 
77  const unsigned int spaceDimension = this->GetInitialPosition().GetSize();
78 
79  for (unsigned int i=0; i< spaceDimension; i++)
81 
82  m_CurrentIndex.SetSize(spaceDimension);
83  m_CurrentIndex.Fill(0);
84 
85  ScalesType scales = this->GetScales();
86 
87  // Make sure the scales have been set properly
88  if (scales.size() != spaceDimension)
89  {
90  itkExceptionMacro(<< "The size of Scales is "
91  << scales.size()
92  << ", but the NumberOfParameters is "
93  << spaceDimension
94  << ".");
95  }
96 
97  if (m_Geometry.rows() != spaceDimension)
98  {
99  itkExceptionMacro(<< "The size of the geometry matrix is "
100  << m_Geometry.rows()
101  << ", but the NumberOfParameters is "
102  << spaceDimension
103  << ".");
104  }
105 
106  // Setup first grid position.
107  ParametersType position( spaceDimension );
108  for(unsigned int i=0; i<spaceDimension; i++)
109  position[i] = this->GetInitialPosition()[i] - m_NumberOfSteps[i] * scales[i];
110 
111  position = m_Geometry * position;
112  this->SetCurrentPosition( position );
113 
114  this->ResumeWalking();
115 }
116 
120 void
123 {
124  m_Stop = false;
125 
126  const unsigned int spaceDimension = this->GetInitialPosition().GetSize();
127  ParametersType currentPosition(spaceDimension), newPosition(spaceDimension);
128 
129  while( !m_Stop )
130  {
131  currentPosition = this->GetCurrentPosition();
132 
133  if( m_Stop )
134  {
135  StopWalking();
136  break;
137  }
138 
139  m_CurrentValue = this->GetValue( currentPosition );
140 
142  {
144  m_MaximumMetricValuePosition = currentPosition;
145  }
146 
148  {
150  m_MinimumMetricValuePosition = currentPosition;
151  }
152 
153  if( m_Stop )
154  {
155  this->StopWalking();
156  break;
157  }
158 
159  m_StopConditionDescription.str("");
160  m_StopConditionDescription << this->GetNameOfClass() << ": Running. ";
161  m_StopConditionDescription << "@ index " << this->GetCurrentIndex() << " value is " << this->GetCurrentValue();
162 
163  this->InvokeEvent( itk::IterationEvent() );
164 
165  IncrementIndex(newPosition);
166  newPosition = m_Geometry * newPosition;
167 
168  this->SetCurrentPosition(newPosition);
169 
171  }
172 }
173 
174 
175 void
178 {
179  m_Stop = true;
180  this->InvokeEvent( itk::EndEvent() );
181 }
182 
183 
184 void
186 ::IncrementIndex( ParametersType &newPosition )
187 {
188  unsigned int idx = 0;
189  const unsigned int spaceDimension = m_CostFunction->GetNumberOfParameters();
190 
191  while( idx < spaceDimension )
192  {
193  m_CurrentIndex[idx]++;
194 
195  if( m_CurrentIndex[idx] > (2*m_NumberOfSteps[idx]))
196  {
197  m_CurrentIndex[idx]=0;
198  idx++;
199  }
200  else
201  {
202  break;
203  }
204  }
205 
206  if( idx==spaceDimension )
207  {
208  m_Stop = true;
209  m_StopConditionDescription.str("");
210  m_StopConditionDescription << this->GetNameOfClass() << ": ";
211  m_StopConditionDescription << "Completed sampling of parametric space of size " << spaceDimension;
212  }
213 
214  for(unsigned int i=0; i<spaceDimension; i++)
215  newPosition[i] = (m_CurrentIndex[i]-m_NumberOfSteps[i]) * this->GetScales()[i] + this->GetInitialPosition()[i];
216 }
217 
218 
219 const std::string
222 {
223  return m_StopConditionDescription.str();
224 }
225 
226 void
228 ::PrintSelf(std::ostream& os, itk::Indent indent) const
229 {
230  Superclass::PrintSelf(os,indent);
231 
232  os << indent << "CurrentValue = " << m_CurrentValue << std::endl;
233  os << indent << "NumberOfSteps = " << m_NumberOfSteps << std::endl;
234  os << indent << "CurrentIteration = " << m_CurrentIteration << std::endl;
235  os << indent << "Stop = " << m_Stop << std::endl;
236  os << indent << "CurrentParameter = " << m_CurrentParameter << std::endl;
237  os << indent << "CurrentIndex = " << m_CurrentIndex << std::endl;
238  os << indent << "Geometry = " << m_Geometry << std::endl;
239  os << indent << "MaximumNumberOfIterations = " << m_MaximumNumberOfIterations << std::endl;
240  os << indent << "MaximumMetricValue = " << m_MaximumMetricValue << std::endl;
241  os << indent << "MinimumMetricValue = " << m_MinimumMetricValue << std::endl;
242  os << indent << "MinimumMetricValuePosition = " << m_MinimumMetricValuePosition << std::endl;
243  os << indent << "MaximumMetricValuePosition = " << m_MaximumMetricValuePosition << std::endl;
244 }
245 
246 } // end of namespace anima
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
virtual const ParametersType & GetCurrentIndex() const
virtual const ParametersType & GetMaximumMetricValuePosition() const
void IncrementIndex(ParametersType &newPosition)
virtual const ParametersType & GetMinimumMetricValuePosition() const
virtual const MeasureType & GetMinimumMetricValue() const
virtual const char * GetNameOfClass() const
const std::string GetStopConditionDescription() const ITK_OVERRIDE
virtual const MeasureType & GetCurrentValue() const
virtual const MeasureType & GetMaximumMetricValue() const