eoRealVectorBounds.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoRealVectorBounds.h
00005 // (c) Marc Schoenauer 2001, Maarten Keijzer 2000, GeNeura Team, 1998
00006 /* 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00022              Marc.Schoenauer@polytechnique.fr
00023              mak@dhi.dk
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _eoRealVectorBounds_h
00028 #define _eoRealVectorBounds_h
00029 
00030 #include <stdexcept>               // std::exceptions!
00031 #include <utils/eoRNG.h>
00032 #include <utils/eoRealBounds.h>
00033 
00058 class eoRealBaseVectorBounds : public std::vector<eoRealBounds *>
00059 { 
00060 public:
00061   // virtual desctructor (to avoid warning?)
00062   virtual ~eoRealBaseVectorBounds(){}
00063 
00066   eoRealBaseVectorBounds() : std::vector<eoRealBounds *>(0) {}
00067 
00070   eoRealBaseVectorBounds(unsigned _dim, eoRealBounds & _bounds) : 
00071     std::vector<eoRealBounds *>(_dim, &_bounds)
00072   {}
00073   
00076   eoRealBaseVectorBounds(eoRealBounds & _xbounds, eoRealBounds & _ybounds) : 
00077     std::vector<eoRealBounds *>(0)
00078   {
00079         push_back( &_xbounds);
00080         push_back( &_ybounds);
00081   }
00082   
00085   virtual bool isBounded(unsigned _i) 
00086   { 
00087     return (*this)[_i]->isBounded();
00088   }
00089  
00092   virtual bool isBounded(void) 
00093   {
00094     for (unsigned i=0; i<size(); i++)
00095       if (! (*this)[i]->isBounded())
00096         return false;
00097     return true;
00098   }
00099 
00102   virtual bool hasNoBoundAtAll(unsigned _i) 
00103   { 
00104     return (*this)[_i]->hasNoBoundAtAll();
00105   }
00106  
00109   virtual bool hasNoBoundAtAll(void) 
00110   {
00111     for (unsigned i=0; i<size(); i++)
00112       if (! (*this)[i]->hasNoBoundAtAll())
00113         return false;
00114     return true;
00115   }
00116 
00117   virtual bool isMinBounded(unsigned _i) 
00118   { return (*this)[_i]->isMinBounded();} ;
00119 
00120   virtual bool isMaxBounded(unsigned _i) 
00121   { return (*this)[_i]->isMaxBounded();} ;
00122 
00125   virtual void foldsInBounds(unsigned _i, double & _r)
00126   {
00127     (*this)[_i]->foldsInBounds(_r);
00128   }
00129 
00132   virtual void foldsInBounds(std::vector<double> & _v)
00133   {
00134    for (unsigned i=0; i<size(); i++)
00135      {
00136        (*this)[i]->foldsInBounds(_v[i]);
00137      }    
00138   }
00139 
00142   virtual void truncate(unsigned _i, double & _r)
00143   {
00144     (*this)[_i]->truncate(_r);
00145   }
00146 
00149   virtual void truncate(std::vector<double> & _v)
00150   {
00151    for (unsigned i=0; i<size(); i++)
00152      {
00153        (*this)[i]->truncate(_v[i]);
00154      }    
00155   }
00156 
00159   virtual bool isInBounds(unsigned _i, double _r)
00160   { return (*this)[_i]->isInBounds(_r); }
00161 
00164   virtual bool isInBounds(std::vector<double> _v)
00165   {
00166     for (unsigned i=0; i<size(); i++)
00167       if (! isInBounds(i, _v[i]))
00168         return false;
00169     return true;
00170   }
00171 
00174   virtual double minimum(unsigned _i) {return (*this)[_i]->minimum();}
00175   virtual double maximum(unsigned _i) {return (*this)[_i]->maximum();}
00176   virtual double range(unsigned _i) {return (*this)[_i]->range();}
00177 
00181   virtual double averageRange() 
00182   {
00183     double r=0.0;
00184     for (unsigned i=0; i<size(); i++)
00185       r += range(i);
00186     return r/size();
00187   }
00188 
00192   virtual double uniform(unsigned _i, eoRng & _rng = eo::rng)
00193   { 
00194     double r= (*this)[_i]->uniform();
00195     return r;
00196   }
00197 
00201   void uniform(std::vector<double> & _v, eoRng & _rng = eo::rng)
00202   {
00203     _v.resize(size());
00204     for (unsigned i=0; i<size(); i++)
00205       {
00206       _v[i] = uniform(i, _rng);
00207       }
00208   }  
00209 
00214   virtual void printOn(std::ostream& _os) const
00215   {
00216     for (unsigned i=0; i<size(); i++)
00217       {
00218         operator[](i)->printOn(_os);
00219         _os << ";";
00220       }
00221   }
00222 };
00223 
00225 
00228 class eoRealVectorBounds : public eoRealBaseVectorBounds, public eoPersistent
00229 {
00230 public:
00233   eoRealVectorBounds():eoRealBaseVectorBounds() {}
00234 
00237   eoRealVectorBounds(unsigned _dim, eoRealBounds & _bounds) : 
00238     eoRealBaseVectorBounds(_dim, _bounds), factor(1,_dim), ownedBounds(0)
00239   {}
00240   
00243   eoRealVectorBounds(eoRealBounds & _xbounds, eoRealBounds & _ybounds) : 
00244     eoRealBaseVectorBounds(_xbounds, _ybounds), factor(2,1), ownedBounds(0)
00245   {}
00246   
00249   eoRealVectorBounds(unsigned _dim, double _min, double _max) : 
00250     eoRealBaseVectorBounds(), factor(1, _dim), ownedBounds(0)
00251   {
00252     if (_max-_min<=0)
00253       throw std::logic_error("Void range in eoRealVectorBounds");
00254     eoRealBounds *ptBounds = new eoRealInterval(_min, _max);
00255     // handle memory once
00256     ownedBounds.push_back(ptBounds);
00257     // same bound for everyone
00258     for (unsigned int i=0; i<_dim; i++)
00259       push_back(ptBounds);
00260   }
00261 
00264   eoRealVectorBounds(std::vector<double> _min, std::vector<double> _max) : 
00265     factor(_min.size(), 1), ownedBounds(0)
00266   {
00267     if (_max.size() != _min.size())
00268       throw std::logic_error("Dimensions don't match in eoRealVectorBounds");
00269     // the bounds
00270     eoRealBounds *ptBounds;
00271     for (unsigned i=0; i<_min.size(); i++)
00272       {
00273         ptBounds = new eoRealInterval(_min[i], _max[i]);
00274         ownedBounds.push_back(ptBounds);
00275         push_back(ptBounds);
00276       }
00277   }
00278 
00282   eoRealVectorBounds(std::string _s) : eoRealBaseVectorBounds()
00283   {
00284     readFrom(_s);
00285   }
00286 
00288   virtual ~eoRealVectorBounds()
00289   {
00290 //     std::cout << "Dtor, avec size = " << ownedBounds.size() << std::endl;
00291 //     for (unsigned i = 0; i < ownedBounds.size(); ++i)
00292 //     {
00293 //         delete ownedBounds[i];
00294 //     }
00295 }
00296 
00297 
00298   // methods from eoPersistent
00304   virtual void readFrom(std::istream& _is) ;
00305 
00310   virtual void readFrom(std::string _s) ;
00311 
00313   virtual void printOn(std::ostream& _os) const
00314   {
00315     if (factor[0]>1)
00316       _os << factor[0] ;
00317     operator[](0)->printOn(_os);
00318 
00319     // other bounds
00320     unsigned int index=factor[0];
00321     if (factor.size()>1)
00322       for (unsigned i=1; i<factor.size(); i++)
00323         {
00324           _os << ";";
00325           if (factor[i] > 1)
00326             _os << factor[i];
00327           operator[](index)->printOn(_os);
00328           index += factor[i];
00329         }
00330   }
00331 
00333   void adjust_size(unsigned _dim);
00334 
00337   eoRealVectorBounds(const eoRealVectorBounds &);
00338 
00339 private:// WARNING: there is no reason for both std::vector below 
00340         //to be synchronized in any manner
00341   std::vector<unsigned int> factor;        // std::list of nb of "grouped" bounds
00342   std::vector<eoRealBounds *> ownedBounds; 
00343 // keep this one private
00344   eoRealVectorBounds& operator=(const eoRealVectorBounds&);
00345   };
00346 
00348 
00352 class eoRealVectorNoBounds: public eoRealVectorBounds
00353 { 
00354 public:
00355   // virtual desctructor (to avoid warning?)
00356   virtual ~eoRealVectorNoBounds(){}
00357 
00361   eoRealVectorNoBounds(unsigned _dim) : 
00362     eoRealVectorBounds( (_dim?_dim:1), eoDummyRealNoBounds)
00363   {}
00364 
00365   
00366   virtual bool isBounded(unsigned)  {return false;}
00367   virtual bool isBounded(void)   {return false;}
00368 
00369   virtual bool hasNoBoundAtAll(unsigned)  {return true;}
00370   virtual bool hasNoBoundAtAll(void)  {return true;}
00371 
00372   virtual bool isMinBounded(unsigned)   {return false;}
00373   virtual bool isMaxBounded(unsigned)   {return false;}
00374 
00375   virtual void foldsInBounds(unsigned, double &) {return;}
00376   virtual void foldsInBounds(std::vector<double> &) {return;}
00377 
00378   virtual void truncate(unsigned, double &) {return;}
00379   virtual void truncate(std::vector<double> &) {return;}
00380 
00381   virtual bool isInBounds(unsigned, double) {return true;}
00382   virtual bool isInBounds(std::vector<double>) {return true;}
00383 
00384   // accessors  
00385   virtual double minimum(unsigned)
00386   {
00387     throw std::logic_error("Trying to get minimum of eoRealVectorNoBounds");
00388   }
00389   virtual double maximum(unsigned)
00390   {
00391     throw std::logic_error("Trying to get maximum of eoRealVectorNoBounds");
00392   }
00393   virtual double range(unsigned)
00394   {
00395     throw std::logic_error("Trying to get range of eoRealVectorNoBounds");
00396   }
00397 
00398   virtual double averageRange() 
00399   {
00400     throw std::logic_error("Trying to get average range of eoRealVectorNoBounds");
00401   }
00402 
00403   // random generators
00404   virtual double uniform(unsigned, eoRng & _rng = eo::rng)
00405   {
00406     throw std::logic_error("No uniform distribution on eoRealVectorNoBounds");
00407   }
00408 
00409   // fills a std::vector with uniformly chosen variables in bounds
00410   void uniform(std::vector<double> &, eoRng & _rng = eo::rng)
00411   {
00412     throw std::logic_error("No uniform distribution on eoRealVectorNoBounds");
00413   }
00414 
00415 };
00416 
00417 
00418 
00419 // one object for all - see eoRealBounds.cpp
00420 extern eoRealVectorNoBounds eoDummyVectorNoBounds;
00421 #endif

Generated on Thu Oct 19 05:06:37 2006 for EO by  doxygen 1.3.9.1