[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] conjugate gradient problem

From: Debashree.Sen
Subject: [Help-gsl] conjugate gradient problem
Date: Sun, 29 Jan 2006 00:39:18 +0530

    i am using GSL for doing multidimensional minimization of a non-linear 
functions of dimensions ( max 20-30). I was trying to minimize a quadratic 
function by employing exponential barrier method on it ( for non-linear 
programming problem of operation research).
The function to be minimised is 'xy'. this is how i have defined my function:

/*This is the multidimensional quadratic function being minimised*/

double my_f (const gsl_vector *v, void *params)
        printf("\ninside my_f\n");
           double *dp = (double *)params;

            /*Variable declarations*/
           double temp = 0;
           double temp0 = 0, temp2 = 0;
           double temp1 = 0, temp3 = 0;
           int i=0, j=0, k;

           /*Initialising the multi-dim array*/
                         arr[i] = gsl_vector_get(v, i);
                        temp3+=exp(-LARGENUMBER*term[i]); // Largenumber can be 
anything say 10-20--here                
                                                             this is the 
exponential barrier 
            printf("temp= %lf",temp3);

            temp1 = arr[0]*arr[1]+temp3;                  // a function of the 
type , say , x*y
            printf("func value: %lf", temp1); -------------> at this stage 
value calculated is correct

            return temp1;                    //The final function value


this 'temp1' returned is supposed to be printed by s->f in main(). 's' is 
actually passed to 'gsl_multimin_fdfminimizer_iterate (s)'; in each iteration 
But a different value is printed in main. To be precise its printing the value 
of arr[0]* arr[1] & NOT arr[0]*arr[1]+ temp3.

Now temp3 uses term[i] array, which is calculated like exp(-20*linear 
constraint1) + exp(-20*linearconstraint2) + ..... + exp(-20*linearconstraintN). 
in another function.

Now my another 2 problems are:
2> for this constrained case the output never prints the 'Minima Found'.

3> After some 10 iterations the function value & variable values does not 
change any more with iterations. & at the same time for each iteration it 
enters into my_f(ie, the function where the function 'xy' is calculated). 
Initially for each iteration it enters my_f only once (as is expected)
but as th function & variable values stops changing it enters the my_f function 
succesively more no. of times for each iteration. ... why should it be like 
that ??

This is the code snippet used for main which is taken from GSL reference manual.

int main (void)
  size_t iter = 0;
  int status;

  const gsl_multimin_fdfminimizer_type *T;
  gsl_multimin_fdfminimizer *s;

  /* Position of the minimum (1,2). */
  double par[2] = { 1.0, 2.0 };

  gsl_vector *x;
  gsl_multimin_function_fdf my_func;

  my_func.f = &my_f;
  my_func.df = &my_df;
  my_func.fdf = &my_fdf;
  my_func.n = 2;
  my_func.params = &par;

  /* Starting point, x = (5,7) */
  x = gsl_vector_alloc (2);
  gsl_vector_set (x, 0, 5.0);
  gsl_vector_set (x, 1, 7.0);

  T = gsl_multimin_fdfminimizer_conjugate_fr;
  s = gsl_multimin_fdfminimizer_alloc (T, 2);

  gsl_multimin_fdfminimizer_set (s, &my_func, x, 0.01, 1e-4);

      status = gsl_multimin_fdfminimizer_iterate (s);

      if (status)

      status = gsl_multimin_test_gradient (s->gradient, 1e-3);

      if (status == GSL_SUCCESS)
        printf ("Minimum found at:\n");

      printf ("%5d %.5f %.5f %10.5f\n", iter,
              gsl_vector_get (s->x, 0), 
              gsl_vector_get (s->x, 1), 
              s->f);   -----------------------> printing wrong value not same 
as temp1 in my_f.

  while (status == GSL_CONTINUE && iter < 100);

  gsl_multimin_fdfminimizer_free (s);
  gsl_vector_free (x);

  return 0;

               The problem may seem to be very trivial but I will be greatful, 
if someone helps me out.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]