Line data Source code
1 : /** 2 : Bojan Nikolic <bojan@bnikolic.co.uk> 3 : Initial version 2003 4 : 5 : This file is part of BNMin1 and is licensed under GNU General 6 : Public License version 2 7 : 8 : \file minim.hxx 9 : Renamed to minim.h 2023 10 : 11 : */ 12 : #ifndef __BNMIN__ALGO__MINIM_HXX 13 : #define __BNMIN__ALGO__MINIM_HXX 14 : 15 : #include <string> 16 : #include <vector> 17 : 18 : 19 : #include "paramctr.h" 20 : #include "minimmodel.h" 21 : 22 : /// Namespace for the BNMin1 library 23 : namespace Minim { 24 : 25 : // Forward declarations 26 : class Monitor; 27 : 28 : 29 : /** \brief Description of a model for an obseration, providing 30 : functions for handling parameters 31 : 32 : This class can hold the model description but can not do the 33 : actuall minimisation as it does not demand that the model has a 34 : figure-of-merit function. 35 : 36 : */ 37 : class ModelDesc { 38 : 39 : protected : 40 : 41 : Model & mod; 42 : 43 : public: 44 : 45 : /* --------- Public data ---------------------- */ 46 : std::vector <DParamCtr> pars ; 47 : 48 : /* --------- Constructors / Desctructors ------- */ 49 : 50 : ModelDesc( Model &mod ); 51 : 52 : virtual ~ModelDesc(void); 53 : 54 : // ---------- Public interface -------------------------- 55 : 56 : // Routines to access the model parameters 57 : DParamCtr * operator[] (const std::string &name); 58 0 : DParamCtr * getbyname (const std::string &name) { return this->operator[](name);}; 59 : 60 : // Routines to access the model parameters by id number 61 0 : DParamCtr * operator[] (unsigned i) { return &pars[i]; } 62 : DParamCtr * getbynumb (unsigned i) { return this->operator[](i); }; 63 : 64 : /*! Returns the total number of params, both fitted and not 65 : * fitted */ 66 0 : unsigned NTotParam(void) const { return pars.size() ; } 67 : 68 : /*! Copies the parameter values from the supplied ModelDesc */ 69 : void CopyParsFrom ( ModelDesc & mod2 ); 70 : 71 : /** Returns the number of parameters to be fitted, i.e. excluding 72 : those with do fit set to false 73 : */ 74 : unsigned NParam(void) const; 75 : 76 : /** Copy supplied values to parameters 77 : 78 : Only fitted-for parameters are copied to 79 : */ 80 : void copytopars(const double *x) ; 81 : 82 : /** Like copytopars but take a vector 83 : */ 84 158113058 : void put(const std::vector<double> & x) 85 : { 86 158113058 : if (x.size() != NParam()) 87 : { 88 0 : std::string errmsg = "In function ModelDesc::Put expected "+std::to_string(NParam())+" but received "+std::to_string(x.size())+" pars "; 89 0 : throw BaseErr(errmsg); 90 0 : } 91 158113058 : copytopars(&x[0]); 92 158113058 : } 93 : 94 : /** Copy parameters to supplied vector 95 : */ 96 : void copyfrompars(double *x) ; 97 : 98 1542129 : void get(std::vector<double> & x) 99 : { 100 1542129 : copyfrompars(&x[0]) ; 101 1542129 : } 102 : 103 : 104 : 105 : }; 106 : 107 : 108 : /** \brief Provides the "solve" interface function 109 : 110 : Demands that a Minimisable is passed to the constructor, hence 111 : provides the "solve" function interface 112 : */ 113 : class Minimiser: 114 : public ModelDesc 115 : { 116 : 117 : private: 118 : //unsigned iter; 119 : Minimisable &m; 120 : std::vector< double > res; 121 : protected: 122 : bool MonitorChi; 123 : int MonitorChi_stride; 124 : int MonitorChi_cno; 125 : 126 : public: 127 : 128 : // Monitor class is called every iteration 129 : std::vector<Monitor *> mons; 130 : void AddMon (Monitor * mon); 131 : 132 : // --------- Constructors / Desctructors ------------------- 133 : 134 : Minimiser (Minimisable &pm) ; 135 0 : virtual ~Minimiser(void ) {} ; 136 : 137 : 138 : /// Evaluate the residuals for the current model 139 : void ResEval (void ) ; 140 : 141 : /// Calculats the ChiSquared i.e. the sum of the squares of the 142 : /// values stored in the res vector 143 : double ChiSquared(void) ; 144 : 145 : /// Initialises the residue vector to enough space for all the residues 146 : void InitRes (void); 147 : 148 : /// Collects all the parameters which can be minimised 149 : void CollectParams ( Model & m ); 150 : 151 : 152 : 153 0 : unsigned NRes(void) const { return res.size() ; }; 154 : 155 : virtual void solve(void) = 0; 156 : 157 : 158 0 : void copyres (double *x) { std::copy( res.begin() , res.end(), x) ; } 159 : 160 : 161 : }; 162 : 163 : 164 : 165 : 166 : 167 : } 168 : 169 : 170 : #endif // __BNMIN__ALGO__MINIM_HXX