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 // 00032 // montecarlo.h 00033 // simple Monte Carlo optimizer class 00034 // 00035 // Defines class "montecarlo", which can attempt to find a global minimum 00036 // using a random search in parameter space. This class can run either an 00037 // internal powell local optimizer or a user-specified local optimizer on 00038 // each test point in parameter space. It returns the best local minimum 00039 // found. 00040 // 00041 // The output of montecarlo defaults to "display(false).verbose()", which 00042 // outputs a report of the error function and parameter values each time 00043 // an improved minimum is found. The effect of the display() member function 00044 // is described in the comments near its declaration below. Calling 00045 // very_verbose() additionally sets verbose() on the local optimizer called 00046 // for every candidate point. 00047 // 00048 // When montecarlo::minimize() exits, it sets the parameter values to the 00049 // best values found and calculates the error function one last time, 00050 // returning the resulting error fution value. It does this so that the 00051 // error fution's internal state will be consistent with the value 00052 // returned. 00053 // 00054 // Also included is class "do_nothing_optimizer", which can be given to 00055 // montecarlo in order to just evaluate test points without running a 00056 // local optimizer on them. 00057 // 00058 // 3/21/00 FRR and JSW 00059 // 00060 // 9/22/00: Added virtual destructor, so derived classes can change stop() 00061 // 6/13/00: Some changes to verbose behavior. 00062 // 00063 // ************************************************************************ 00064 00065 00066 #ifndef MONTECARLO_H 00067 #define MONTECARLO_H 00068 00069 #include "powell.h" 00070 00071 class montecarlo : public minimizer 00072 { 00073 public: 00074 // NOTE: Constructors have the side effect of seeding the random number 00075 // generator used by drand48() (defined in <stdlib.h>). 00076 00077 // Constructor needs the error function to be minimized: 00078 montecarlo(abstract_error_func & aef); 00079 00080 // This contructor lets you specify the subordinate minimizer to call: 00081 montecarlo(abstract_error_func & aef, minimizer & method); 00082 00083 // How many random starting points to try (default 100): 00084 montecarlo & npoints(unsigned); 00085 00086 // Type of verbose output on each iteration (default is false): 00087 // set to true: output each local minimum found 00088 // set to false: output only if a better minimum found 00089 montecarlo & display(bool); 00090 00091 // Type of verbose output on each iteration. Calling this function 00092 // causes verbose output to display those local minima whose value 00093 // is no greater than 1+r times the value of the best minimum seen 00094 // so far. Example: display(0.1) will cause all minima whose values 00095 // are no greater than 1.1 of the best seen so far to be displayed. 00096 // If the best minimum has a negative value, uses 1-r times the value 00097 // rather than 1+r times. If r <= 0.0, the function's effect is the 00098 // same as calling display(false). 00099 montecarlo & display(double r); 00100 00101 // The internal powell minimizer used if none specified by the constructor. 00102 // You can access the powell minimizer using this function so that you 00103 // may modify its conversion properties. See powell.h for details. 00104 powell & p() { return p_ ; } 00105 00106 // Performs the global minimization: 00107 double minimize(); 00108 00109 // A virtual destructor ensures proper subclass destruction. 00110 virtual ~montecarlo() { } 00111 00112 private: 00113 powell p_; // local minimizer 00114 minimizer * m; // user-specified minimizer 00115 unsigned n; // number of random points 00116 bool display_f; // display flag for the type of verbose output 00117 double display_r; // display ratio for the type of verbose output 00118 double best_e; // holds best error function result seen 00119 real_vector best_x; // holds parameter values yielding best_e 00120 void randomize(); // randomize the error function parameters 00121 }; 00122 00123 inline montecarlo & montecarlo::npoints(unsigned np) 00124 { n = np; return *this; } 00125 00126 inline montecarlo & montecarlo::display(bool f) 00127 { display_f = f; display_r = 0.0; return *this; } 00128 00129 inline montecarlo & montecarlo::display(double r) 00130 { display_f = false; display_r = (r <= 0.0) ? 0.0 : r; return *this; } 00131 00132 // ************************************************************************ 00133 00134 class do_nothing_optimizer : public minimizer 00135 { 00136 public: 00137 // Constructor needs the error function to be minimized: 00138 do_nothing_optimizer(abstract_error_func & aef) : minimizer(aef) { }; 00139 00140 // Don't do anything! 00141 double minimize() { return erf(); } 00142 }; 00143 00144 #endif /* MONTECARLO_H */
Please direct comments and corrections to
supermix@submm.caltech.edu
Go to the supermix home page
Generated by
1.2.7