Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

montecarlo.cc

Go to the documentation of this file.
00001 // SuperMix version 1.0  C++ source file
00002 //
00003 // Copyright (c) 1999 California Institute of Technology.
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms for noncommercial
00007 // purposes are permitted provided that the above copyright notice and
00008 // this paragraph are duplicated in all such forms and that any
00009 // documentation and other materials related to such distribution and
00010 // use acknowledge that the software was developed by California
00011 // Institute of Technology. Redistribution and/or use in source or
00012 // binary forms is not permitted for any commercial purpose. Use of
00013 // this software does not include a permitted use of the Institute's
00014 // name or trademark for any purpose.
00015 //
00016 // DISCLAIMER:
00017 // THIS SOFTWARE AND/OR RELATED MATERIALS ARE PROVIDED "AS-IS" WITHOUT
00018 // WARRANTY OF ANY KIND INCLUDING ANY WARRANTIES OF PERFORMANCE OR
00019 // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE OR PURPOSE (AS SET
00020 // FORTH IN UCC 23212-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
00021 // LICENSED PRODUCT, HOWEVER USED.  IN NO EVENT SHALL CALTECH/JPL BE
00022 // LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING BUT NOT LIMITED TO
00023 // INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING ECONOMIC
00024 // DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF
00025 // WHETHER CALTECH/JPL SHALL BE ADVISED, HAVE REASON TO KNOW, OR IN
00026 // FACT SHALL KNOW OF THE POSSIBILITY.  THE USER BEARS ALL RISK
00027 // RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE AND/OR RELATED
00028 // MATERIALS.
00029 //
00030 // ************************************************************************
00031 // montecarlo.cc
00032 //
00033 // 9/25/00:  Added one final error function calc to minimize()
00034 // 9/22/00:  Added calls to minimizer::stop() in minimize()
00035 // 9/8/00:   Simple efficiency improvement to randomize()
00036 // 6/13/00:  Added verbose output improvements
00037 //
00038 
00039 #include <stdlib.h>
00040 #include <time.h>
00041 #include "error.h"
00042 #include "montecarlo.h"
00043 
00044 montecarlo::montecarlo(abstract_error_func & aef) :
00045   minimizer(aef), p_(aef), m(0), n(100), display_f(false), display_r(0.0)
00046 { srand48(time(0)); verbose(); }
00047 
00048 montecarlo::montecarlo(abstract_error_func & aef, minimizer & method) :
00049   minimizer(aef), p_(aef), m(&method), n(100), display_f(false), display_r(0.0)
00050 { srand48(time(0)); verbose(); }
00051 
00052 void montecarlo::randomize()
00053 {
00054   static real_vector x, y;     // only allocate once
00055   x = erf.get_min_parms();     // x gets sized here
00056   y = erf.get_max_parms();     // y gets sized here
00057 
00058   for (int i = x.minindex(); i <= x.maxindex(); ++i) {
00059     x[i] += drand48()*(y[i] - x[i]);
00060   }
00061 
00062   erf.set_parms(x);
00063 }
00064 
00065 double montecarlo::minimize()
00066 {
00067   // set up local minimizer
00068   if(target_on) {
00069     if(m) m->set_target(target);
00070     else  p_.set_target(target);
00071   }
00072   else {
00073     if(m) m->no_target();
00074     else  p_.no_target();
00075   }
00076   if(is_very_verbose) {
00077     if(m) m->verbose();
00078     else  p_.verbose();
00079   }
00080   else {
00081     if(m) m->quiet();
00082     else  p_.quiet();
00083   }
00084 
00085   // the first iteration will use the provided initial point:
00086   erf.set_parms(erf.get_initial_parms());
00087   best_e = (m) ? m->minimize() : p_.minimize();
00088   best_x = erf.get_parms();
00089   if(is_verbose)
00090     error::stream() << 1 << " : " << best_e << " : " 
00091                     << erf.get_parms_user() << endl;
00092 
00093   // check for early termination
00094   if(stop(1)) return best_e;
00095 
00096   // the rest of the iterations:
00097   for( unsigned i = 1; i < n; ++i) {
00098     randomize();
00099     double e = (m) ? m->minimize() : p_.minimize();
00100     if(e < best_e) {
00101       best_e = e;
00102       best_x = erf.get_parms();
00103       if(is_verbose)
00104         error::stream() << i+1 << " : " << best_e << " : " 
00105                         << erf.get_parms_user() << endl;
00106     }
00107     // some other tests in case of verbose output:
00108     else if(display_f && is_verbose) 
00109       error::stream() << i+1 << " : " << e << " : " 
00110                       << erf.get_parms_user() << endl;
00111     else if(display_r > 0.0 && is_verbose) {
00112       if((best_e > 0.0 && e <= (1+display_r)*best_e)||
00113          (best_e < 0.0 && e <= (1-display_r)*best_e))
00114         error::stream() << i+1 << " : " << e << " : " 
00115                         << erf.get_parms_user() << endl;
00116     }
00117 
00118     // check for early termination
00119     if(stop(i+1)) break;
00120   }
00121 
00122   // set error function parameters to the best seen and
00123   // recalculate error fuction one last time to make its
00124   // internal state consistent with the return value.
00125   erf.set_parms(best_x);
00126   return erf();
00127 }

Please direct comments and corrections to supermix@submm.caltech.edu
Go to the supermix home page
Generated by doxygen1.2.7