EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoParam.h 00005 // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000 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 mkeijzer@dhi.dk 00024 */ 00025 //----------------------------------------------------------------------------- 00026 00027 #ifndef eoParam_h 00028 #define eoParam_h 00029 00030 #include <cmath> 00031 #include <iterator> 00032 #include <stdexcept> 00033 #include <sstream> 00034 #include <string> 00035 #include <vector> 00036 #include <eoScalarFitness.h> 00037 00051 class eoParam 00052 { 00053 public: 00054 00056 eoParam () 00057 : repLongName(""), repDefault(""), repDescription(""), 00058 repShortHand(0), repRequired(false) 00059 {} 00060 00069 eoParam (std::string _longName, std::string _default, 00070 std::string _description, char _shortName = 0, bool _required = false) 00071 : repLongName(_longName), repDefault(_default), 00072 repDescription(_description ), 00073 repShortHand(_shortName), repRequired( _required) 00074 {} 00075 00079 virtual ~eoParam () {} 00080 00084 virtual std::string getValue () const = 0; 00085 00089 virtual void setValue(const std::string& _value) = 0 ; 00090 00094 char shortName() const { return repShortHand; }; 00095 00099 const std::string& longName() const { return repLongName; }; 00100 00104 const std::string& description() const { return repDescription; }; 00105 00109 const std::string& defValue() const { return repDefault; }; 00110 00114 void defValue( const std::string& str ) { repDefault = str; }; 00115 00119 void setLongName(std::string _longName) { repLongName = _longName;} 00120 00124 bool required() const { return repRequired; }; 00125 00126 private: 00127 std::string repLongName; 00128 std::string repDefault; 00129 std::string repDescription; 00130 char repShortHand; 00131 bool repRequired; 00132 }; 00133 00134 00135 00145 template <class ValueType> 00146 class eoValueParam : public eoParam 00147 { 00148 public : 00149 00151 eoValueParam(void) : eoParam() {} 00152 00161 eoValueParam(ValueType _defaultValue, 00162 std::string _longName, 00163 std::string _description = "No description", 00164 char _shortHand = 0, 00165 bool _required = false) 00166 : eoParam(_longName, "", _description, _shortHand, _required), 00167 repValue(_defaultValue) 00168 { 00169 eoParam::defValue(getValue()); 00170 } 00171 00176 ValueType& value() { return repValue; } 00177 00184 const ValueType& value() const { return repValue; } 00185 00186 00189 void value( ValueType val ) 00190 { 00191 // convert to string 00192 std::ostringstream os; 00193 os << val; 00194 00195 // convert to ValueType 00196 std::istringstream is( os.str() ); 00197 is >> repValue; 00198 } 00199 00200 00203 std::string getValue(void) const 00204 { 00205 std::ostringstream os; 00206 os << repValue; 00207 return os.str(); 00208 } 00209 00210 00224 void setValue(const std::string& _value) 00225 { 00226 std::istringstream is(_value); 00227 is >> repValue; 00228 } 00229 00230 protected: 00231 00232 ValueType repValue; 00233 }; 00234 00235 /* 00236 Specialization for std::string 00237 */ 00238 template <> 00239 inline std::string eoValueParam<std::string>::getValue() const 00240 { 00241 return repValue; 00242 } 00243 00244 00245 template <> 00246 inline void eoValueParam<bool>::setValue(const std::string& _value) 00247 { 00248 if (_value.empty()) 00249 { 00250 repValue = true; 00251 return; 00252 } 00253 std::istringstream is(_value); 00254 is >> repValue; 00255 } 00256 00257 00259 template <> 00260 inline std::string eoValueParam<std::pair<double, double> >::getValue(void) const 00261 { 00262 // use own buffer as MSVC's buffer leaks! 00263 std::ostringstream os; 00264 os << repValue.first << ' ' << repValue.second; 00265 return os.str(); 00266 } 00267 00269 template <> 00270 inline void eoValueParam<std::pair<double, double> >::setValue(const std::string& _value) 00271 { 00272 std::istringstream is(_value); 00273 is >> repValue.first; 00274 is >> repValue.second; 00275 } 00276 00277 // The std::vector<std::vector<double> > 00280 template <> 00281 inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(void) const 00282 { 00283 std::ostringstream os; 00284 os << repValue.size() << ' '; 00285 for (unsigned i = 0; i < repValue.size(); ++i) 00286 { 00287 os << repValue[i].size() << ' '; 00288 std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " ")); 00289 } 00290 return os.str(); 00291 } 00292 00294 template <> 00295 inline void eoValueParam<std::vector<std::vector<double> > >::setValue(const std::string& _value) 00296 { 00297 std::istringstream is(_value); 00298 unsigned i,j,sz; 00299 is >> sz; 00300 repValue.resize(sz); 00301 00302 for (i = 0; i < repValue.size(); ++i) 00303 { 00304 unsigned sz2; 00305 is >> sz2; 00306 repValue[i].resize(sz2); 00307 for (j = 0; j < sz2; ++j) 00308 { 00309 is >> repValue[i][j]; 00310 } 00311 } 00312 } 00313 00314 // The std::vector<double> 00317 template <> 00318 inline std::string eoValueParam<std::vector<double> >::getValue(void) const 00319 { 00320 std::ostringstream os; 00321 os << repValue.size() << ' '; 00322 std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " ")); 00323 return os.str(); 00324 } 00325 00327 template <> 00328 inline void eoValueParam<std::vector<double> >::setValue(const std::string& _value) 00329 { 00330 static const std::string delimiter(",;"); 00331 std::istringstream is(_value); 00332 unsigned sz; 00333 is >> sz; 00334 repValue.resize(sz); 00335 for(unsigned i=0; i<repValue.size(); ++i) { 00336 char c; 00337 do { 00338 is >> c; 00339 } while((std::string::npos != delimiter.find(c)) && (! is.eof())); 00340 is >> repValue[i]; 00341 } 00342 } 00343 00344 // The std::vector<eoMinimizingFitness> 00347 template <> 00348 inline std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(void) const 00349 { 00350 std::ostringstream os; 00351 os << repValue.size() << ' '; 00352 std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<eoMinimizingFitness>(os, " ")); 00353 return os.str(); 00354 } 00355 00357 // NOTE: g++ doesn support it either!!! 00358 template <> 00359 inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(const std::string& _value) 00360 { 00361 std::istringstream is(_value); 00362 unsigned sz; 00363 is >> sz; 00364 repValue.resize(sz); 00365 std::copy(std::istream_iterator<eoMinimizingFitness>(is), std::istream_iterator<eoMinimizingFitness>(), repValue.begin()); 00366 } 00367 00368 // The std::vector<const EOT*> 00370 template <> 00371 inline std::string eoValueParam<std::vector<void*> >::getValue(void) const 00372 { 00373 throw std::runtime_error("I cannot getValue for a std::vector<EOT*>"); 00374 return std::string(""); 00375 } 00376 00377 template <> 00378 inline void eoValueParam<std::vector<void*> >::setValue(const std::string&) 00379 { 00380 throw std::runtime_error("I cannot setValue for a std::vector<EOT*>"); 00381 return; 00382 } 00383 00384 /*template <class ContainerType> 00385 class eoContainerParam : public eoParam 00386 { 00387 public : 00388 eoContainerParam (ContainerType& value, std::string _shortName, std::string _longName, 00389 std::string _default, 00390 std::string _description, 00391 bool _required, 00392 bool _change ) 00393 : value(_value), eoParam(_shortName, _longName, _description, _default, _required, _change) 00394 {} 00395 00396 00397 // void setValue(const std::string & _value) 00398 // { 00399 // std::istd::stringstream is(_value); 00400 // copy(std::istream_iterator<Container::value_type>(is), std::istream_iterator<Container::value_type>(), back_inserter(value)); 00401 // } 00402 00403 private : 00404 ContainerType& value; 00405 };*/ 00406 00417 class eoParamParamType : public std::pair<std::string,std::vector<std::string> > 00418 { 00419 public: 00420 eoParamParamType(std::string _value) 00421 { 00422 readFrom(_value); 00423 } 00424 00425 std::ostream & printOn(std::ostream & _os) const 00426 { 00427 _os << first; 00428 unsigned narg = second.size(); 00429 if (!narg) 00430 return _os; 00431 00432 // Here, we do have args 00433 _os << "("; 00434 if (narg == 1) // 1 arg only 00435 { 00436 _os << second[0] << ")" ; 00437 return _os; 00438 } 00439 // and here more than 1 arg 00440 for (unsigned i=0; i<narg-1; i++) 00441 _os << second[i] << "," ; 00442 _os << second[narg-1] << ")"; 00443 return _os; 00444 } 00445 00446 std::istream & readFrom(std::istream & _is) 00447 { 00448 std::string value; 00449 _is >> value; 00450 readFrom(value); 00451 return _is; 00452 } 00453 00454 void readFrom(std::string & _value) 00455 { 00456 second.resize(0); // just in case 00457 size_t pos = _value.find('('); 00458 if (pos >= _value.size()) // no arguments 00459 { 00460 first = _value; 00461 return; 00462 } 00463 // so here we do have arguments 00464 std::string t = _value.substr(pos+1);// the arguments 00465 _value.resize(pos); 00466 first = _value; // done for the keyword (NOTE: may be empty std::string!) 00467 00468 // now all arguments 00469 std::string delim(" (),"); 00470 while ( (pos=t.find_first_not_of(delim)) < t.size()) 00471 { 00472 size_t posEnd = t.find_first_of(delim, pos); 00473 std::string u = t.substr(pos,posEnd);//(t, pos); 00474 /*u.resize(posEnd - pos);*/ 00475 second.push_back(u); 00476 t = t.substr(posEnd+1); 00477 } 00478 } 00479 }; 00480 00481 // at the moment, the following are defined in eoParser.cpp 00482 std::ostream & operator<<(std::ostream & _os, const eoParamParamType & _rate); 00483 std::istream & operator>>(std::istream & _is, eoParamParamType & _rate); 00484 00486 #endif