EvolvingObjects
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 
00054 class eoRealBaseVectorBounds : public std::vector<eoRealBounds *>
00055 {
00056 public:
00057   // virtual desctructor (to avoid warning?)
00058   virtual ~eoRealBaseVectorBounds(){}
00059 
00062   eoRealBaseVectorBounds() : std::vector<eoRealBounds *>(0) {}
00063 
00066   eoRealBaseVectorBounds(unsigned _dim, eoRealBounds & _bounds) :
00067     std::vector<eoRealBounds *>(_dim, &_bounds)
00068   {}
00069 
00072   eoRealBaseVectorBounds(eoRealBounds & _xbounds, eoRealBounds & _ybounds) :
00073     std::vector<eoRealBounds *>(0)
00074   {
00075         push_back( &_xbounds);
00076         push_back( &_ybounds);
00077   }
00078 
00081   virtual bool isBounded(unsigned _i)
00082   {
00083     return (*this)[_i]->isBounded();
00084   }
00085 
00088   virtual bool isBounded(void)
00089   {
00090     for (unsigned i=0; i<size(); i++)
00091       if (! (*this)[i]->isBounded())
00092         return false;
00093     return true;
00094   }
00095 
00098   virtual bool hasNoBoundAtAll(unsigned _i)
00099   {
00100     return (*this)[_i]->hasNoBoundAtAll();
00101   }
00102 
00105   virtual bool hasNoBoundAtAll(void)
00106   {
00107     for (unsigned i=0; i<size(); i++)
00108       if (! (*this)[i]->hasNoBoundAtAll())
00109         return false;
00110     return true;
00111   }
00112 
00113   virtual bool isMinBounded(unsigned _i)
00114   { return (*this)[_i]->isMinBounded();} ;
00115 
00116   virtual bool isMaxBounded(unsigned _i)
00117   { return (*this)[_i]->isMaxBounded();} ;
00118 
00121   virtual void foldsInBounds(unsigned _i, double & _r)
00122   {
00123     (*this)[_i]->foldsInBounds(_r);
00124   }
00125 
00128   virtual void foldsInBounds(std::vector<double> & _v)
00129   {
00130    for (unsigned i=0; i<size(); i++)
00131      {
00132        (*this)[i]->foldsInBounds(_v[i]);
00133      }
00134   }
00135 
00138   virtual void truncate(unsigned _i, double & _r)
00139   {
00140     (*this)[_i]->truncate(_r);
00141   }
00142 
00145   virtual void truncate(std::vector<double> & _v)
00146   {
00147    for (unsigned i=0; i<size(); i++)
00148      {
00149        (*this)[i]->truncate(_v[i]);
00150      }
00151   }
00152 
00155   virtual bool isInBounds(unsigned _i, double _r)
00156   { return (*this)[_i]->isInBounds(_r); }
00157 
00160   virtual bool isInBounds(std::vector<double> _v)
00161   {
00162     for (unsigned i=0; i<size(); i++)
00163       if (! isInBounds(i, _v[i]))
00164         return false;
00165     return true;
00166   }
00167 
00170   virtual double minimum(unsigned _i) {return (*this)[_i]->minimum();}
00171   virtual double maximum(unsigned _i) {return (*this)[_i]->maximum();}
00172   virtual double range(unsigned _i) {return (*this)[_i]->range();}
00173 
00177   virtual double averageRange()
00178   {
00179     double r=0.0;
00180     for (unsigned i=0; i<size(); i++)
00181       r += range(i);
00182     return r/size();
00183   }
00184 
00188   virtual double uniform(unsigned _i, eoRng & _rng = eo::rng)
00189   {
00190     (void)_rng;
00191 
00192     double r= (*this)[_i]->uniform();
00193     return r;
00194   }
00195 
00199   void uniform(std::vector<double> & _v, eoRng & _rng = eo::rng)
00200   {
00201     _v.resize(size());
00202     for (unsigned i=0; i<size(); i++)
00203       {
00204       _v[i] = uniform(i, _rng);
00205       }
00206   }
00207 
00212   virtual void printOn(std::ostream& _os) const
00213   {
00214     for (unsigned i=0; i<size(); i++)
00215       {
00216         operator[](i)->printOn(_os);
00217         _os << ";";
00218       }
00219   }
00220 };
00221 
00223 
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 
00354 class eoRealVectorNoBounds: public eoRealVectorBounds
00355 {
00356 public:
00357   // virtual desctructor (to avoid warning?)
00358   virtual ~eoRealVectorNoBounds(){}
00359 
00363   eoRealVectorNoBounds(unsigned _dim) :
00364     eoRealVectorBounds( (_dim?_dim:1), eoDummyRealNoBounds)
00365   {}
00366 
00367 
00368   virtual bool isBounded(unsigned)  {return false;}
00369   virtual bool isBounded(void)   {return false;}
00370 
00371   virtual bool hasNoBoundAtAll(unsigned)  {return true;}
00372   virtual bool hasNoBoundAtAll(void)  {return true;}
00373 
00374   virtual bool isMinBounded(unsigned)   {return false;}
00375   virtual bool isMaxBounded(unsigned)   {return false;}
00376 
00377   virtual void foldsInBounds(unsigned, double &) {return;}
00378   virtual void foldsInBounds(std::vector<double> &) {return;}
00379 
00380   virtual void truncate(unsigned, double &) {return;}
00381   virtual void truncate(std::vector<double> &) {return;}
00382 
00383   virtual bool isInBounds(unsigned, double) {return true;}
00384   virtual bool isInBounds(std::vector<double>) {return true;}
00385 
00386   // accessors
00387   virtual double minimum(unsigned)
00388   {
00389     throw std::logic_error("Trying to get minimum of eoRealVectorNoBounds");
00390   }
00391   virtual double maximum(unsigned)
00392   {
00393     throw std::logic_error("Trying to get maximum of eoRealVectorNoBounds");
00394   }
00395   virtual double range(unsigned)
00396   {
00397     throw std::logic_error("Trying to get range of eoRealVectorNoBounds");
00398   }
00399 
00400   virtual double averageRange()
00401   {
00402     throw std::logic_error("Trying to get average range of eoRealVectorNoBounds");
00403   }
00404 
00405   // random generators
00406   virtual double uniform(unsigned, eoRng & _rng = eo::rng)
00407   {
00408     (void)_rng;
00409 
00410     throw std::logic_error("No uniform distribution on eoRealVectorNoBounds");
00411   }
00412 
00413   // fills a std::vector with uniformly chosen variables in bounds
00414   void uniform(std::vector<double> &, eoRng & _rng = eo::rng)
00415   {
00416     (void)_rng;
00417 
00418     throw std::logic_error("No uniform distribution on eoRealVectorNoBounds");
00419   }
00420 
00421 };
00422 
00423 
00424 
00428 extern eoRealVectorNoBounds eoDummyVectorNoBounds;
00429 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends