[Insight-developers] Re: Conjugate Gradient Algorithm !

Ashish Poddar ahpoddar at gmail.com
Wed Jun 29 18:29:11 EDT 2005


I guess the deletion of old m_VnlOptimizer was done with the point of
view being introduction of a new costFunction, which is not so in my
case.. and thus is causing the whole trouble for me..

my suggestion for the change is

//---------old function----------------

void
ConjugateGradientOptimizer
::SetCostFunction( SingleValuedCostFunction * costFunction )
{

  const unsigned int numberOfParameters = 
    costFunction->GetNumberOfParameters();

  CostFunctionAdaptorType * adaptor = 
    new CostFunctionAdaptorType( numberOfParameters );
       
  adaptor->SetCostFunction( costFunction );

  if( m_OptimizerInitialized )
    { 
    delete m_VnlOptimizer;
    }
    
  this->SetCostFunctionAdaptor( adaptor );

  m_VnlOptimizer = new vnl_conjugate_gradient( *adaptor );
  m_OptimizerInitialized = true;

}


//----------new function---------------

void
ConjugateGradientOptimizer
::SetCostFunction( SingleValuedCostFunction * costFunction )
{

  if( m_OptimizerInitialized )
    { 
    if (this->GetCostFunctionAdaptor()->GetCostFunction() ==
costFunction) return;
    delete m_VnlOptimizer;
    }

  const unsigned int numberOfParameters = 
    costFunction->GetNumberOfParameters();

  CostFunctionAdaptorType * adaptor = 
    new CostFunctionAdaptorType( numberOfParameters );
       
  adaptor->SetCostFunction( costFunction );

  this->SetCostFunctionAdaptor( adaptor );

  m_VnlOptimizer = new vnl_conjugate_gradient( *adaptor );
  m_OptimizerInitialized = true;

}

with regards,
Ashish.


On 6/29/05, Ashish Poddar <ahpoddar at gmail.com> wrote:
> from the function
> "void ConjugateGradientOptimizer::SetCostFunction(
> SingleValuedCostFunction * costFunction )"
> 
> 
>   if( m_OptimizerInitialized )
>     {
>     delete m_VnlOptimizer;
>     }
> 
>   this->SetCostFunctionAdaptor( adaptor );
> 
>   m_VnlOptimizer = new vnl_conjugate_gradient( *adaptor );
>   m_OptimizerInitialized = true;
> 
> here if it was initialized, then that is basically deleted and a new
> instance is created, as can be seen. this is where the values
> initialized by me are getting lost..
> 
> regards,
> ashish.
> 
> On 6/29/05, Karthik Krishnan <Karthik.Krishnan at kitware.com> wrote:
> > Doesn't the boolean ivar m_OptimizerInitialized take care of that ?
> >
> > karthik
> >
> > Ashish Poddar wrote:
> >
> > >in Conjugate Gradient Optimizer.cxx around Line number 81 in function
> > >
> > >void ConjugateGradientOptimizer::SetCostFunction(
> > >SingleValuedCostFunction * costFunction )
> > >
> > >    m_VnlOptimizer = new vnl_conjugate_gradient( *adaptor );
> > >// should be replaced by this
> > >    if (!m_VnlOptimizer) m_VnlOptimizer = new vnl_conjugate_gradient(
> > >*adaptor );
> > >
> > >This was causing the initialization which I did to disappear and that
> > >was probably the reason why my iterations were not terminating !!
> > >
> > >regards,
> > >Ashish.
> > >
> > >On 6/28/05, Ashish Poddar <ahpoddar at gmail.com> wrote:
> > >
> > >
> > >>in Conjugate Grandient algorithm [cg.c] i think there is a bug..
> > >>
> > >>L190:
> > >>   ++na;
> > >>   if (na > 25) {
> > >>       goto L630;
> > >>   }
> > >>   q *= a4;
> > >>   p = fv_(&q, x, h, n, value);
> > >>   ins_(&q, &p, &a, &b, &c, &fa, &fb, &fc, &j, y, z);
> > >>   if (p - f > v * q) {
> > >>       goto L190;
> > >>   }
> > >>   goto L250;
> > >>
> > >>
> > >>   if (p - f > v * q) {
> > >>//should be replaced by something like this
> > >>   if ( (p - f) - (v * q) >= (1e-8) ) {
> > >>
> > >>when i run conjugate algorithm it starts repeating the metric value
> > >>for a long time and then suddenly starts moving is some extreme
> > >>direction, on debugging, I traced it back to this loop where it was
> > >>stuck !
> > >>
> > >>similar change needs to be carried at more places in the same file !
> > >>
> > >>I just want to confirm if this change is correct or not !!
> > >>
> > >>regards,
> > >>Ashish.
> > >>
> > >>--
> > >>Ashish Poddar
> > >>Have an acceptable reason for accepting anything.
> > >>Y:ashish_poddar | MSN:ashish_poddar at yahoo.com
> > >>
> > >>
> > >>
> > >
> > >
> > >
> > >
> >
> 
> 
> --
> Ashish Poddar
> Have an acceptable reason for accepting anything.
> Y:ashish_poddar | MSN:ashish_poddar at yahoo.com
> 


-- 
Ashish Poddar
Have an acceptable reason for accepting anything.
Y:ashish_poddar | MSN:ashish_poddar at yahoo.com


More information about the Insight-developers mailing list