1 /* 2 ================================================================================ 3 PROJECT: 4 5 John Eddy's Genetic Algorithms (JEGA) 6 7 CONTENTS: 8 9 Definition of class FitnessRecord. 10 11 NOTES: 12 13 See notes under Class Definition section of this file. 14 15 PROGRAMMERS: 16 17 John Eddy (jpeddy@sandia.gov) (JE) 18 19 ORGANIZATION: 20 21 Sandia National Laboratories 22 23 COPYRIGHT: 24 25 See the LICENSE file in the top level JEGA directory. 26 27 VERSION: 28 29 2.0.0 30 31 CHANGES: 32 33 Wed Dec 21 15:49:27 2005 - Original Version (JE) 34 35 ================================================================================ 36 */ 37 38 39 40 41 /* 42 ================================================================================ 43 Document This File 44 ================================================================================ 45 */ 46 /** \file 47 * \brief Contains the definition of the FitnessRecord class. 48 */ 49 50 51 52 53 /* 54 ================================================================================ 55 Prevent Multiple Inclusions 56 ================================================================================ 57 */ 58 #ifndef JEGA_ALGORITHMS_FITNESSRECORD_HPP 59 #define JEGA_ALGORITHMS_FITNESSRECORD_HPP 60 61 62 63 64 65 66 67 /* 68 ================================================================================ 69 Includes 70 ================================================================================ 71 */ 72 // JEGAConfig.hpp should be the first include in all JEGA files. 73 #include <../Utilities/include/JEGAConfig.hpp> 74 75 #include <../Utilities/include/Design.hpp> 76 #include <../Utilities/include/DesignValueMap.hpp> 77 78 79 80 81 82 83 84 /* 85 ================================================================================ 86 Pre-Namespace Forward Declares 87 ================================================================================ 88 */ 89 90 91 92 93 94 95 96 97 /* 98 ================================================================================ 99 Namespace Aliases 100 ================================================================================ 101 */ 102 103 104 105 106 107 108 109 110 /* 111 ================================================================================ 112 Begin Namespace 113 ================================================================================ 114 */ 115 namespace JEGA { 116 namespace Algorithms { 117 118 119 120 121 122 /* 123 ================================================================================ 124 In-Namespace Forward Declares 125 ================================================================================ 126 */ 127 class FitnessRecord; 128 129 130 131 132 133 134 135 /* 136 ================================================================================ 137 In-Namespace File Scope Typedefs 138 ================================================================================ 139 */ 140 141 142 143 144 145 146 147 148 /* 149 ================================================================================ 150 Class Definition 151 ================================================================================ 152 */ 153 /** 154 * \brief A simple implementation of the FitnessRecord interface. 155 * 156 * This fitness record keeps a DesignValueMap for mapping Designs to fitness 157 * values. 158 */ 159 class FitnessRecord 160 { 161 /* 162 ============================================================================ 163 Class Scope Typedefs 164 ============================================================================ 165 */ 166 public: 167 168 typedef 169 JEGA::Utilities::DesignDoubleMap::const_iterator 170 const_iterator; 171 172 protected: 173 174 175 private: 176 177 178 /* 179 ============================================================================ 180 Member Data Declarations 181 ============================================================================ 182 */ 183 private: 184 185 /// The main data structure for this fitness record. 186 /** 187 * This is used to map Design*'s to fitness values. 188 */ 189 JEGA::Utilities::DesignDoubleMap _data; 190 191 /* 192 ============================================================================ 193 Mutators 194 ============================================================================ 195 */ 196 public: 197 198 199 protected: 200 201 202 private: 203 204 205 /* 206 ============================================================================ 207 Accessors 208 ============================================================================ 209 */ 210 public: 211 212 213 protected: 214 215 216 private: 217 218 219 /* 220 ============================================================================ 221 Public Methods 222 ============================================================================ 223 */ 224 public: 225 226 inline 227 void 228 SuspendStatistics( 229 ); 230 231 inline 232 void 233 ResumeStatistics( 234 bool performUpdate = true 235 ); 236 237 inline 238 bool 239 UpdatingStatistics( 240 ); 241 242 inline 243 void 244 UpdateTotals( 245 ); 246 247 /// Adds a fitness value for \a des to this record. 248 /** 249 * This method updates the max, min, and total fitness after inserting 250 * the design/value mapping to the main data structure. 251 * 252 * \param des The design to which \a fitness is to be mapped. 253 * \param fitness The value of the fitness to assign to \a des 254 * \return True if the fitness record is added and false otherwise. 255 * Failure to add would result if \a des is already mapped to a 256 * fitness value. 257 */ 258 inline 259 bool 260 AddFitness( 261 const JEGA::Utilities::Design* des, 262 double fitness 263 ); 264 265 /// Computes and returns the average fitness value in this record. 266 /** 267 * If there are no records, the return is -limits::max() 268 * 269 * \return The average fitness of all designs in this record or 270 * -limits::max() if no fitnesses are on record. 271 */ 272 inline 273 double 274 GetAverageFitness( 275 ) const; 276 277 inline 278 const_iterator 279 begin( 280 ) const; 281 282 inline 283 const_iterator 284 end( 285 ) const; 286 287 /* 288 ============================================================================ 289 Subclass Visible Methods 290 ============================================================================ 291 */ 292 protected: 293 294 295 296 297 298 /* 299 ============================================================================ 300 Subclass Overridable Methods 301 ============================================================================ 302 */ 303 public: 304 305 /// Required override of the FitnessRecord base class. 306 /** 307 * This implementation uses the DesignValueMap base class for retrieval 308 * of the fitnesses associated with supplied designs. 309 * 310 * If the fitness of des cannot be determined from this record, then 311 * -limits::max is returned. 312 * 313 * \param des The Design to retrieve the fitness of. 314 * \return The fitness of \a des or -limits::max if \a des not found. 315 */ 316 inline 317 double 318 GetFitness( 319 const JEGA::Utilities::Design& des 320 ) const; 321 322 /// Required override of the FitnessRecord base class. 323 /** 324 * This method returns the stored max fitness value. This is the 325 * numerical maximum which would typically be the best fitness. 326 * 327 * \return The largest fitness value known to this fitness record. 328 */ 329 inline 330 double 331 GetMaxFitness( 332 ) const; 333 334 /// Required override of the FitnessRecord base class. 335 /** 336 * This method returns the stored min fitness value. This is the 337 * numerical minimum which would typically be the worst fitness. 338 * 339 * \return The smallest fitness value known to this fitness record. 340 */ 341 inline 342 double 343 GetMinFitness( 344 ) const; 345 346 347 /// Required override of the FitnessRecord base class. 348 /** 349 * This method returns the stored sum of fitnesses. 350 * 351 * \return The sum of all fitnesses known to this fitness record. 352 */ 353 inline 354 double 355 GetTotalFitness( 356 ) const; 357 358 /// Required override of the FitnessRecord base class. 359 /** 360 * This method returns the number of stored fitness records. 361 * 362 * \return The average of all fitnesses known to this fitness record. 363 */ 364 inline 365 std::size_t 366 GetSize( 367 ) const; 368 369 protected: 370 371 372 private: 373 374 375 /* 376 ============================================================================ 377 Private Methods 378 ============================================================================ 379 */ 380 private: 381 382 383 384 385 386 /* 387 ============================================================================ 388 Structors 389 ============================================================================ 390 */ 391 public: 392 393 /// Default constructs a FitnessRecord object. 394 FitnessRecord( 395 std::size_t initSize = 0 396 ); 397 398 /// Destructs a FitnessRecord object. 399 virtual 400 ~FitnessRecord( 401 ); 402 403 404 405 }; // class FitnessRecord 406 407 408 409 /* 410 ================================================================================ 411 End Namespace 412 ================================================================================ 413 */ 414 } // namespace Algorithms 415 } // namespace JEGA 416 417 418 419 420 421 422 423 /* 424 ================================================================================ 425 Include Inlined Functions File 426 ================================================================================ 427 */ 428 #include "./inline/FitnessRecord.hpp.inl" 429 430 431 432 /* 433 ================================================================================ 434 End of Multiple Inclusion Check 435 ================================================================================ 436 */ 437 #endif // JEGA_ALGORITHMS_FITNESSRECORD_HPP 438