18 #ifndef __itkPSMGradientDescentOptimizer_hxx 19 #define __itkPSMGradientDescentOptimizer_hxx 20 #include "itkPSMGradientDescentOptimizer.h" 24 template <
class TGradientNumericType,
unsigned int VDimension>
25 PSMGradientDescentOptimizer<TGradientNumericType, VDimension>
26 ::PSMGradientDescentOptimizer()
28 m_StopOptimization =
false;
29 m_NumberOfIterations = 0;
30 m_MaximumNumberOfIterations = 0;
33 m_OptimizationMode = 0;
37 template <
class TGradientNumericType,
unsigned int VDimension>
39 PSMGradientDescentOptimizer<TGradientNumericType, VDimension>
40 ::StartGaussSeidelOptimization()
44 m_StopOptimization =
false;
45 m_NumberOfIterations = 0;
46 m_CostFunction->SetParticleSystem(m_ParticleSystem);
51 while (m_StopOptimization ==
false)
53 m_CostFunction->BeforeIteration();
55 double movement = 0.0f;
58 for (
unsigned int dom = 0; dom < m_ParticleSystem->GetNumberOfDomains(); dom++)
61 if (m_ParticleSystem->GetDomainFlag(dom) ==
false)
64 m_CostFunction->SetDomainNumber(dom);
68 typename ParticleSystemType::PointContainerType::ConstIterator endit =
69 m_ParticleSystem->GetPositions(dom)->GetEnd();
70 for (
typename ParticleSystemType::PointContainerType::ConstIterator it
71 = m_ParticleSystem->GetPositions(dom)->GetBegin(); it != endit; it++, k++)
74 m_CostFunction->BeforeEvaluate(it.GetIndex(), dom, m_ParticleSystem);
75 gradient = m_CostFunction->Evaluate(it.GetIndex(), dom, m_ParticleSystem,
79 dynamic_cast<DomainType *
>(m_ParticleSystem->GetDomain(dom))
80 ->ApplyVectorConstraints(gradient,
81 m_ParticleSystem->GetPosition(it.GetIndex(), dom), maxdt);
84 if (gradient.magnitude() > maxdt)
85 { gradient = (gradient / gradient.magnitude()) * maxdt; }
88 for (
unsigned int i = 0; i < VDimension; i++)
90 newpoint[i] = (*it)[i] - gradient[i] * m_TimeStep;
94 if ( gradient.magnitude() > movement) movement = gradient.magnitude();
97 m_ParticleSystem->SetPosition(newpoint, it.GetIndex(), dom);
102 m_NumberOfIterations++;
103 m_CostFunction->AfterIteration();
104 this->InvokeEvent(itk::IterationEvent());
107 if ((m_MaximumNumberOfIterations != 0) && (m_NumberOfIterations >= m_MaximumNumberOfIterations))
109 m_StopOptimization =
true;
112 if (movement <= m_Tolerance) m_StopOptimization =
true;
118 template <
class TGradientNumericType,
unsigned int VDimension>
120 PSMGradientDescentOptimizer<TGradientNumericType, VDimension>
121 ::StartJacobiOptimization()
125 m_StopOptimization =
false;
126 m_NumberOfIterations = 0;
127 m_CostFunction->SetParticleSystem(m_ParticleSystem);
130 std::vector<std::vector<PointType> > updates(m_ParticleSystem->GetNumberOfDomains());
132 for (
unsigned int d = 0; d < m_ParticleSystem->GetNumberOfDomains(); d++)
134 updates[d].resize(m_ParticleSystem->GetPositions(d)->GetSize());
139 while (m_StopOptimization ==
false)
141 m_CostFunction->BeforeIteration();
143 double movement = 0.0f;
146 for (
unsigned int dom = 0; dom < m_ParticleSystem->GetNumberOfDomains(); dom++)
149 if (m_ParticleSystem->GetDomainFlag(dom) ==
false)
152 m_CostFunction->SetDomainNumber(dom);
156 typename ParticleSystemType::PointContainerType::ConstIterator endit =
157 m_ParticleSystem->GetPositions(dom)->GetEnd();
158 for (
typename ParticleSystemType::PointContainerType::ConstIterator it
159 = m_ParticleSystem->GetPositions(dom)->GetBegin(); it != endit; it++, k++)
162 m_CostFunction->BeforeEvaluate(it.GetIndex(), dom, m_ParticleSystem);
163 gradient = m_CostFunction->Evaluate(it.GetIndex(), dom, m_ParticleSystem,
166 dynamic_cast<DomainType *
>(m_ParticleSystem->GetDomain(dom))
167 ->ApplyVectorConstraints(gradient, m_ParticleSystem->GetPosition(it.GetIndex(), dom), maxdt);
170 if (gradient.magnitude() > maxdt)
172 gradient = (gradient / gradient.magnitude()) * maxdt;
176 for (
unsigned int i = 0; i < VDimension; i++)
178 newpoint[i] = (*it)[i] - gradient[i] * m_TimeStep;
183 if ( gradient.magnitude() > movement) movement = gradient.magnitude();
186 updates[dom][k] = newpoint;
191 for (
unsigned int dom = 0; dom < m_ParticleSystem->GetNumberOfDomains(); dom++)
194 if (m_ParticleSystem->GetDomainFlag(dom) ==
false)
197 typename ParticleSystemType::PointContainerType::ConstIterator endit =
198 m_ParticleSystem->GetPositions(dom)->GetEnd();
199 for (
typename ParticleSystemType::PointContainerType::ConstIterator it
200 = m_ParticleSystem->GetPositions(dom)->GetBegin(); it != endit; it++, k++)
202 m_ParticleSystem->SetPosition(updates[dom][k], it.GetIndex(), dom);
207 m_NumberOfIterations++;
208 m_CostFunction->AfterIteration();
209 this->InvokeEvent(itk::IterationEvent());
212 if ((m_MaximumNumberOfIterations != 0) && (m_NumberOfIterations >= m_MaximumNumberOfIterations))
214 m_StopOptimization =
true;
218 if (movement <= m_Tolerance) m_StopOptimization =
true;