yanera_ool.c

Go to the documentation of this file.
00001 /*============================================================================
00002 Copyright 2006 Thad Harroun, Kevin Yager
00003 
00004 This file is part of "yanera 2.0".
00005 
00006 "yanera 2.0" is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
00007 
00008 "yanera 2.0" is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00009 
00010 You should have received a copy of the GNU General Public License along with "yanera 2.0"; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
00011 =============================================================================*/
00018 #ifdef HAVE_OOL
00019 
00020 #include "yanera.h"
00021 #include "yanera_ool.h"
00022 #include "yanera_mdmin.h"
00023 #include "yanera_util.h"
00024 /*============================================================================*/
00025 ool_diff_Hv_accel *Hv_accel;
00026 /*============================================================================*/
00027 int yanera_ool_main(yanera_container *yanera)
00028 { 
00029   int                   i, count, status;
00030   ool_conmin_function     ool_func;
00031   ool_conmin_constraint   ool_constraint;
00032   ool_conmin_minimizer   *solvr;
00033   gsl_vector             *guess;
00034   ool_conmin_spg_parameters    spgP;
00035   ool_conmin_gencan_parameters gencanP;
00036  
00037   ool_func.n   = yanera->parameters.n;
00038   ool_func.f   = &ool_f;
00039   ool_func.df  = &ool_df;
00040   ool_func.fdf = &ool_fdf;
00041   ool_func.Hv  = &ool_Hv;
00042   ool_func.params = (void *)yanera;
00043   Hv_accel = ool_diff_Hv_accel_alloc(yanera->parameters.n);
00044   
00045   /* Set the constraints */
00046   ool_constraint.n = yanera->parameters.n;
00047   ool_constraint.L = gsl_vector_alloc( ool_constraint.n );
00048   ool_constraint.U = gsl_vector_alloc( ool_constraint.n );
00049   for (i=0; i<yanera->parameters.n; i++) 
00050   {
00051     gsl_vector_set(ool_constraint.L, i, yanera->parameters.min[i]);
00052     gsl_vector_set(ool_constraint.U, i, yanera->parameters.max[i]);
00053     if (yanera->parameters.f[i] == YES)
00054     {
00055       gsl_vector_set(ool_constraint.L, i, yanera->parameters.p[i]);
00056       gsl_vector_set(ool_constraint.U, i, yanera->parameters.p[i]);
00057     }
00058   }
00059   
00060   /* Inital guess is the paramters as they were passed into here */
00061   guess = gsl_vector_alloc(yanera->parameters.n);
00062   for (i=0; i<yanera->parameters.n; i++) 
00063   {
00064     gsl_vector_set(guess, i, yanera->parameters.p[i]);
00065   }
00066 
00067   solvr = ool_conmin_minimizer_alloc(yanera->misc.fit_method_ool, \
00068                                      yanera->parameters.n);
00069   if (yanera->misc.fit_method == OOL_SPG)
00070   {
00071     ool_conmin_parameters_default(yanera->misc.fit_method_ool, \
00072                                  (void*)(&spgP));
00073     /*spgP.alphamax = 1.0e+3;
00074     spgP.alphamin = 1.0e-3;
00075     spgP.tol      = 1.0e-5;*/
00076   }  
00077   else if (yanera->misc.fit_method == OOL_GENCAN)
00078   {
00079     ool_conmin_parameters_default(yanera->misc.fit_method_ool, \
00080                                  (void*)(&gencanP));
00081   }
00082   
00083   /*==================================================*/
00084   count = 0;
00085   if (yanera->misc.fit_method == OOL_SPG)
00086   {
00087     ool_conmin_minimizer_set(solvr, &ool_func, &ool_constraint, guess, \
00088                              (void*)(&spgP) );
00089   }
00090   else if (yanera->misc.fit_method == OOL_GENCAN)
00091   {
00092     ool_conmin_minimizer_set(solvr, &ool_func, &ool_constraint, guess, \
00093                              (void*)(&gencanP) );
00094   }
00095   yanera_report(yanera,count,solvr);
00096 
00097   do 
00098   {
00099     count++;
00100      
00101     ool_conmin_minimizer_iterate(solvr);
00102     status = ool_conmin_is_optimal(solvr);
00103     if (status == OOL_SUCCESS) 
00104     {
00105       fprintf(stderr,"STATUS: SUCCESS\n");
00106     }
00107     
00108     yanera_report(yanera, count, solvr);
00109 
00110   } while((status == GSL_CONTINUE) && (count < yanera->misc.fit_iterations));
00111   
00112   yanera_report(yanera, -1, solvr);
00113 
00114   ool_conmin_minimizer_free(solvr);
00115   gsl_vector_free(guess);
00116   gsl_vector_free(ool_constraint.L);
00117   gsl_vector_free(ool_constraint.U);
00118   ool_diff_Hv_accel_free (Hv_accel);
00119   return OOL_SUCCESS;
00120 }
00121 /*============================================================================*/
00122 void ool_fdf(const gsl_vector *parameters, void *yanera_pointer,
00123              double *f, gsl_vector *df )
00124 {
00125   *f = ool_f(parameters, yanera_pointer);
00126   ool_diff_g(ool_f, parameters, yanera_pointer, df, 1.E-6);
00127 }
00128 /*============================================================================*/
00129 double ool_f(const gsl_vector *parameters, void *yanera_pointer)
00130 {
00131   int     i;
00132   double  tmp;
00133   yanera_container  *yanera;
00134   /*
00135   ** r =>> residual
00136   */
00137   yanera = (yanera_container *)yanera_pointer;
00138   /*
00139   **  Load the params back into the parameter array.
00140   */
00141   for(i=0; i<yanera->parameters.n; i++)
00142   {
00143     yanera->parameters.p[i] = gsl_vector_get(parameters, i);
00144   }
00145   
00146   tmp = mdmin_chisq(yanera);
00147 
00148   return tmp;
00149 }
00150 /*============================================================================*/
00151 void ool_df(const gsl_vector *parameters, void *yanera_pointer, gsl_vector *df)
00152 {
00153   ool_diff_g(ool_f, parameters, yanera_pointer, df, 1.E-6);
00154 }
00155 /*============================================================================*/
00156 void ool_Hv(const gsl_vector *parameters, void *yanera_pointer, \
00157             const gsl_vector *V, gsl_vector *hv)
00158 {
00159   ool_diff_Hv(Hv_accel, ool_df, parameters, yanera_pointer, V, hv, 1.E-6);
00160 }
00161 
00162 #endif
00163 

Generated on Thu May 29 10:56:33 2008 by  doxygen 1.5.5