00001 /*************************************************************************** 00002 * Copyright (C) 2005-2009 by Robot Group Leipzig * 00003 * martius@informatik.uni-leipzig.de * 00004 * fhesse@informatik.uni-leipzig.de * 00005 * der@informatik.uni-leipzig.de * 00006 * guettler@informatik.uni-leipzig.de * 00007 * jhoffmann@informatik.uni-leipzig.de * 00008 * joergweide84@aol.com (robot12) * 00009 * * 00010 * This program is free software; you can redistribute it and/or modify * 00011 * it under the terms of the GNU General Public License as published by * 00012 * the Free Software Foundation; either version 2 of the License, or * 00013 * (at your option) any later version. * 00014 * * 00015 * This program is distributed in the hope that it will be useful, * 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00018 * GNU General Public License for more details. * 00019 * * 00020 * You should have received a copy of the GNU General Public License * 00021 * along with this program; if not, write to the * 00022 * Free Software Foundation, Inc., * 00023 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00024 *************************************************************************** 00025 * * 00026 * This class is a implementation for the IValue interface. It is for * 00027 * standard data types concepted as a template class. * 00028 * * 00029 * $Log: TemplateValue.h,v $ 00030 * Revision 1.9 2009/10/23 10:48:02 robot12 00031 * bugfix in store and restore 00032 * 00033 * Revision 1.8 2009/10/21 14:08:19 robot12 00034 * add restore and store functions to the ga package 00035 * 00036 * Revision 1.7 2009/07/21 08:37:59 robot12 00037 * add some comments 00038 * 00039 * Revision 1.6 2009/06/26 13:08:25 robot12 00040 * finishing Values and add some comments 00041 * 00042 * Revision 1.5 2009/05/14 15:29:54 robot12 00043 * bugfix: mutation change the oldGen, not the new!!! now fixed 00044 * 00045 * Revision 1.4 2009/05/12 13:29:24 robot12 00046 * some new function 00047 * -> toString methodes 00048 * 00049 * Revision 1.3 2009/05/11 14:08:51 robot12 00050 * patch some bugfix.... 00051 * 00052 * Revision 1.2 2009/05/06 13:28:22 robot12 00053 * some implements... Finish 00054 * 00055 * Revision 1.1 2009/05/04 15:27:57 robot12 00056 * rename of some files and moving files to other positions 00057 * - SingletonGenAlgAPI has one error!!! --> is not ready now 00058 * 00059 * Revision 1.2 2009/05/04 09:20:52 robot12 00060 * some implements.. Finish --> first compile 00061 * 00062 * Revision 1.1 2009/04/29 14:32:29 robot12 00063 * some implements... Part4 00064 * 00065 * 00066 * 00067 ***************************************************************************/ 00068 00069 #ifndef TEMPLATEVALUE_H_ 00070 #define TEMPLATEVALUE_H_ 00071 00072 //includes 00073 #include <string> 00074 00075 //ga_tools includes 00076 #include "IValue.h" 00077 #include "restore.h" 00078 00079 /** 00080 * general function to converrt a double value to a string 00081 * @param value (double) the value wath should convert 00082 * @return 00083 */ 00084 inline std::string doubleToString(double value) { 00085 char buffer[128]; 00086 sprintf(buffer,"% .12lf",value); 00087 return buffer; 00088 } 00089 00090 /** 00091 * template class for a IValue standard data type 00092 * needs the data type and a methode for string converting as template argument 00093 * 00094 * All parts are declared and defined in the header because the needed template implementations 00095 * are needed to the compiling time of the lib. To create new classes in an using program it 00096 * must be in the header. 00097 */ 00098 template<class Typ, std::string toString(Typ)=doubleToString> 00099 class TemplateValue : public IValue { 00100 public: 00101 /** 00102 * constructor 00103 * needs the value and a name (for IValue -> is default implemented as "templateValue") 00104 * @param value (Typ) the value of this IValue 00105 * @param name (string) the name 00106 */ 00107 TemplateValue(Typ value, std::string name = "templateValue") : IValue(name), m_value(value) {} 00108 00109 /** 00110 * default destructor 00111 */ 00112 virtual ~TemplateValue() {} 00113 00114 /** 00115 * this function can be used to read the standard data type. 00116 * @return (Typ) the value 00117 */ 00118 inline Typ getValue(void)const {return m_value;} 00119 00120 /** 00121 * this function is to change the value. 00122 * @param value 00123 */ 00124 inline void setValue(Typ value) {m_value=value;} 00125 00126 /** 00127 * the implementation of the mul operator, what is part of the interface. 00128 * This function only accept TemplateValues or the same type like "Typ" 00129 * @param value (const IValue&) the other part of the operation 00130 * @return (IValue*) the result 00131 */ 00132 virtual IValue* operator*(const IValue& value)const { 00133 TemplateValue<Typ,toString>* newValue; 00134 00135 //cast the IValue to TemplateValue of the same type like "Typ" 00136 const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value); 00137 if(castValue==0) 00138 return 0; 00139 00140 //multiplicate the values 00141 const Typ typeValue = castValue->getValue(); 00142 newValue = new TemplateValue<Typ,toString>(m_value*typeValue); 00143 00144 //return result 00145 return newValue; 00146 } 00147 00148 /** 00149 * the implementation of the add operator what is part of the interface. 00150 * This function only accept TemplateValues or the same type like "Typ" 00151 * @param value (const IValue&) the other part of the operation 00152 * @return (IValue*) the result 00153 */ 00154 virtual IValue* operator+(const IValue& value)const { 00155 TemplateValue<Typ,toString>* newValue; 00156 00157 //cast the IValue to TemplateValue of the same type like "Typ" 00158 const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value); 00159 if(castValue==0) 00160 return 0; 00161 00162 //add the values 00163 const Typ typeValue = castValue->getValue(); 00164 newValue = new TemplateValue<Typ,toString>(m_value+typeValue); 00165 00166 //return the result 00167 return newValue; 00168 } 00169 00170 /** 00171 * cast operatot to string 00172 * use the convert methode. 00173 * @return (string) the cast result 00174 */ 00175 virtual operator std::string(void)const { 00176 return toString(m_value); 00177 } 00178 00179 /** 00180 * store the value in a file 00181 * @param f (FILE*) the file to store 00182 * @return (bool) true if all ok. 00183 */ 00184 virtual bool store(FILE* f) const { 00185 RESTORE_GA_TEMPLATE<Typ> temp; 00186 00187 //test 00188 if(f==NULL) { 00189 printf("\n\n\t>>> [ERROR] <<<\nNo File to store GA [temp value].\n\t>>> [END] <<<\n\n\n"); 00190 return false; 00191 } 00192 00193 temp.value = m_value; 00194 00195 fprintf(f,"%i\n%s",(int)m_name.length(),m_name.c_str()); 00196 00197 for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) { 00198 fprintf(f,"%c",temp.buffer[x]); 00199 } 00200 00201 return true; 00202 } 00203 00204 /** 00205 * restore the value from a file 00206 * @param f (FILE*) the file where the value inside 00207 * @return (bool) true if all ok. 00208 */ 00209 virtual bool restore(FILE* f) { 00210 RESTORE_GA_TEMPLATE<Typ> temp; 00211 char* buffer; 00212 int toread; 00213 00214 //test 00215 if(f==NULL) { 00216 printf("\n\n\t>>> [ERROR] <<<\nNo File to restore GA [temp value].\n\t>>> [END] <<<\n\n\n"); 00217 return false; 00218 } 00219 00220 fscanf(f,"%i\n",&toread); 00221 buffer=new char[toread]; 00222 for(int y=0;y<toread;y++){ 00223 fscanf(f,"%c",&buffer[y]); 00224 } 00225 buffer[toread]='\0'; 00226 m_name=buffer; 00227 delete[] buffer; 00228 00229 for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) { 00230 fscanf(f,"%c",&temp.buffer[x]); 00231 } 00232 00233 m_value = temp.value; 00234 00235 return true; 00236 } 00237 00238 protected: 00239 /** 00240 * the real value 00241 */ 00242 Typ m_value; 00243 00244 private: 00245 /** 00246 * disable the default constructor 00247 * @return 00248 */ 00249 TemplateValue() : IValue() {} 00250 }; 00251 00252 #endif /* TEMPLATEVALUE_H_ */