1 /* _______________________________________________________________________ 2 3 DAKOTA: Design Analysis Kit for Optimization and Terascale Applications 4 Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS). 5 This software is distributed under the GNU Lesser General Public License. 6 For more information, see the README file in the top Dakota directory. 7 _______________________________________________________________________ */ 8 9 /* 10 =============================================================================== 11 PROJECT: 12 13 Genetic Algorithm for Sandia National Laboratories 14 15 CONTENTS: 16 17 Definition of class JEGAOptimizer. 18 19 NOTES: 20 21 See notes under section "Class Definition" of this file. 22 23 PROGRAMMERS: 24 25 John Eddy (jpeddy@sandia.gov) (JE) 26 Brian Adams (briadam@sandia.gov) (BA) 27 28 ORGANIZATION: 29 30 Sandia National Laboratories 31 32 COPYRIGHT: 33 34 This library is free software; you can redistribute it and/or 35 modify it under the terms of the GNU Lesser General Public 36 License as published by the Free Software Foundation; either 37 version 2.1 of the License, or (at your option) any later version. 38 39 This library is distributed in the hope that it will be useful, 40 but WITHOUT ANY WARRANTY; without even the implied warranty of 41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 42 GNU Lesser General Public License for more details. 43 44 For a copy of the GNU Lesser General Public License, write to: 45 Free Software Foundation, Inc. 46 59 Temple Place, Suite 330 47 Boston, MA 02111-1307 USA 48 49 VERSION: 50 51 2.0.0 52 53 CHANGES: 54 55 Mon Jun 09 09:48:34 2003 - Original Version (JE) 56 Wed Dec 07 15:00:00 2005 - Added ParameterDatabase subclass to wrap 57 ProblemDescDB for dependency removal (JE) 58 59 =============================================================================== 60 */ 61 62 63 64 /* 65 =============================================================================== 66 Document This File 67 =============================================================================== 68 */ 69 /** \file 70 * \brief Contains the definition of the JEGAOptimizer class. 71 */ 72 73 74 75 76 77 78 /* 79 =============================================================================== 80 Prevent Multiple Inclusions 81 =============================================================================== 82 */ 83 #ifndef DAKOTA_JEGAOPTIMIZER_H 84 #define DAKOTA_JEGAOPTIMIZER_H 85 86 87 88 89 90 91 92 /* 93 =============================================================================== 94 Includes 95 =============================================================================== 96 */ 97 // JEGAConfig.hpp should be the first include in all JEGA files. 98 #include <../Utilities/include/JEGAConfig.hpp> 99 100 #include <DakotaOptimizer.hpp> 101 #include <../Utilities/include/JEGATypes.hpp> 102 103 104 105 106 107 108 /* 109 =============================================================================== 110 Pre-Namespace Forward Declares 111 =============================================================================== 112 */ 113 namespace JEGA 114 { 115 namespace Utilities 116 { 117 class Design; 118 class DesignOFSortSet; 119 class ParameterDatabase; 120 } 121 122 namespace FrontEnd 123 { 124 class ProblemConfig; 125 class AlgorithmConfig; 126 } 127 128 namespace Algorithms 129 { 130 class GeneticAlgorithm; 131 } 132 } 133 134 /* 135 =============================================================================== 136 Namespace Using Directives - DISCOURAGED!! 137 =============================================================================== 138 */ 139 140 141 142 143 144 145 /* 146 =============================================================================== 147 Begin Namespace 148 =============================================================================== 149 */ 150 namespace Dakota { 151 152 153 154 155 156 157 158 /* 159 =============================================================================== 160 Forward Declares 161 =============================================================================== 162 */ 163 class JEGAOptimizer; 164 class JEGATraits; 165 166 167 168 169 170 /* 171 =============================================================================== 172 Class Definition 173 =============================================================================== 174 */ 175 176 /** 177 * \brief A version of Dakota::Optimizer for instantiation of John Eddy's 178 * Genetic Algorithms (JEGA). 179 * 180 * This class encapsulates the necessary functionality for creating and 181 * properly initializing the JEGA algorithms (MOGA and SOGA). 182 */ 183 class JEGAOptimizer : 184 public Optimizer 185 { 186 /* 187 =========================================================================== 188 Inner Class Forward Declares 189 =========================================================================== 190 */ 191 private: 192 193 /// This evaluator uses Sandia National Laboratories Dakota software. 194 /** 195 * Evaluations are carried out using a Model which is known by 196 * reference to this class. This provides the advantage of execution 197 * on massively parallel computing architectures. 198 */ 199 class Evaluator; 200 201 /** 202 * \brief A specialization of the JEGA::FrontEnd::EvaluatorCreator that 203 * creates a new instance of a JEGAEvaluator. 204 */ 205 class EvaluatorCreator; 206 207 /** 208 * \brief A subclass of the JEGA front end driver that exposes the 209 * individual protected methods to execute the algorithm. 210 * 211 * This is necessary because DAKOTA requires that all problem 212 * information be extracted from the problem description DB at the 213 * time of Optimizer construction and the front end does it all in 214 * the execute algorithm method which must be called in core_run. 215 */ 216 class Driver; 217 218 /* 219 =========================================================================== 220 Member Data Declarations 221 =========================================================================== 222 */ 223 private: 224 225 /** 226 * \brief A pointer to an EvaluatorCreator used to create the evaluator 227 * used by JEGA in Dakota (a JEGAEvaluator). 228 */ 229 EvaluatorCreator* _theEvalCreator; 230 231 /** 232 * \brief A pointer to the ParameterDatabase from which all parameters 233 * are retrieved by the created algorithms. 234 */ 235 JEGA::Utilities::ParameterDatabase* _theParamDB; 236 237 /// An array of initial points to use as an initial population. 238 /** 239 * This member is here to help support the use of JEGA algorithms in 240 * Dakota strategies. If this array is populated, then whatever 241 * initializer is specified will be ignored and the DoubleMatrix 242 * initializer will be used instead on a matrix created from the data 243 * in this array. 244 */ 245 VariablesArray _initPts; 246 247 248 /* 249 =========================================================================== 250 Mutators 251 =========================================================================== 252 */ 253 public: 254 255 256 257 258 259 /* 260 =========================================================================== 261 Accessors 262 =========================================================================== 263 */ 264 public: 265 266 267 268 269 270 271 272 /* 273 =========================================================================== 274 Public Methods 275 =========================================================================== 276 */ 277 278 public: 279 280 281 282 283 284 285 /* 286 =========================================================================== 287 Subclass Visible Methods 288 =========================================================================== 289 */ 290 protected: 291 292 /** 293 * \brief Loads the JEGA-style Design class into equivalent 294 * Dakota-style Variables and Response objects. 295 * 296 * This version is meant for the case where a Variables and a Response 297 * object exist and just need to be loaded. 298 * 299 * \param from The JEGA Design class object from which to extract the 300 * variable and response information for Dakota. 301 * \param vars The Dakota::Variables object into which to load the 302 * design variable values of \a from. 303 * \param resp The Dakota::Response object into which to load the 304 * objective function and constraint values of \a from. 305 */ 306 void 307 LoadDakotaResponses( 308 const JEGA::Utilities::Design& from, 309 Variables& vars, 310 Response& resp 311 ) const; 312 313 /** 314 * \brief Destroys the current parameter database and creates a new 315 * empty one. 316 */ 317 void 318 ReCreateTheParameterDatabase( 319 ); 320 321 /** 322 * \brief Reads information out of the known Dakota::ProblemDescDB and 323 * puts it into the current parameter database. 324 * 325 * This should be called from the JEGAOptimizer constructor since it 326 * is the only time when the problem description database is certain to 327 * be configured to supply data for this optimizer. 328 */ 329 void 330 LoadTheParameterDatabase( 331 ); 332 333 /** 334 * \brief Completely initializes the supplied algorithm configuration. 335 * 336 * This loads the supplied configuration object with appropriate data 337 * retrieved from the parameter database. 338 * 339 * \param aConfig The algorithm configuration object to load. 340 */ 341 void 342 LoadAlgorithmConfig( 343 JEGA::FrontEnd::AlgorithmConfig& aConfig 344 ); 345 346 /** 347 * \brief Completely initializes the supplied problem configuration. 348 * 349 * This loads the fresh configuration object using the 350 * LoadTheDesignVariables, LoadTheObjectiveFunctions, and 351 * LoadTheConstraints methods. 352 * 353 * \param pConfig The problem configuration object to load. 354 */ 355 void 356 LoadProblemConfig( 357 JEGA::FrontEnd::ProblemConfig& pConfig 358 ); 359 360 /** 361 * \brief Adds DesignVariableInfo objects into the problem 362 * configuration object. 363 * 364 * This retrieves design variable information from the 365 * ParameterDatabase and creates DesignVariableInfo's from it. 366 * 367 * \param pConfig The problem configuration object to load. 368 */ 369 void 370 LoadTheDesignVariables( 371 JEGA::FrontEnd::ProblemConfig& pConfig 372 ); 373 374 /** 375 * \brief Adds ObjectiveFunctionInfo objects into the problem 376 * configuration object. 377 * 378 * This retrieves objective function information from the 379 * ParameterDatabase and creates ObjectiveFunctionInfo's from it. 380 * 381 * \param pConfig The problem configuration object to load. 382 */ 383 void 384 LoadTheObjectiveFunctions( 385 JEGA::FrontEnd::ProblemConfig& pConfig 386 ); 387 388 /** 389 * \brief Adds ConstraintInfo objects into the problem 390 * configuration object. 391 * 392 * This retrieves constraint function information from the 393 * ParameterDatabase and creates ConstraintInfo's from it. 394 * 395 * \param pConfig The problem configuration object to load. 396 */ 397 void 398 LoadTheConstraints( 399 JEGA::FrontEnd::ProblemConfig& pConfig 400 ); 401 402 /** 403 * \brief Returns up to _numBest designs sorted by DAKOTA's 404 * fitness (L2 constraint violation, then utopia or 405 * objective), taking into account the algorithm type. 406 * The front of the returned map can be viewed as a 407 * single "best". 408 * 409 * \param from The full set of designs returned by the solver. 410 * 411 * \param theGA The GA used to generate this set; needed for 412 * its weights in the SO case, provided to both 413 * for consistency 414 * 415 * \param designSortMap Map of best solutions with key 416 * pair<constraintViolation, fitness> 417 * 418 * eventually this functionality must be moved into a separate 419 * post-processing application for MO datasets. 420 */ 421 void 422 GetBestSolutions( 423 const JEGA::Utilities::DesignOFSortSet& from, 424 const JEGA::Algorithms::GeneticAlgorithm& theGA, 425 std::multimap<RealRealPair, JEGA::Utilities::Design*>& designSortMap 426 ); 427 428 /** 429 * \brief Retreive the best Designs from a set of solutions assuming 430 * that they are generated by a multi objective algorithm. 431 * 432 * eventually this functionality must be moved into a separate 433 * post-processing application for MO datasets. 434 */ 435 void 436 GetBestMOSolutions( 437 const JEGA::Utilities::DesignOFSortSet& from, 438 const JEGA::Algorithms::GeneticAlgorithm& theGA, 439 std::multimap<RealRealPair, JEGA::Utilities::Design*>& designSortMap 440 ); 441 442 /** 443 * \brief Retreive the best Designs from a set of solutions assuming 444 * that they are generated by a single objective algorithm. 445 * 446 * eventually this functionality must be moved into a separate 447 * post-processing application for MO datasets. 448 */ 449 void 450 GetBestSOSolutions( 451 const JEGA::Utilities::DesignOFSortSet& from, 452 const JEGA::Algorithms::GeneticAlgorithm& theGA, 453 std::multimap<RealRealPair, JEGA::Utilities::Design*>& designSortMap 454 ); 455 456 /** 457 * \brief Converts the items in a VariablesArray into a DoubleMatrix 458 * whereby the items in the matrix are the design variables. 459 * 460 * The matrix will not contain responses but when being used by Dakota, 461 * this doesn't matter. JEGA will attempt to re-evaluate these points 462 * but Dakota will recognize that they do not require re-evaluation and 463 * thus it will be a cheap operation. 464 * 465 * \param variables The array of DakotaVariables objects to use as the 466 * contents of the returned matrix. 467 * \return The matrix created using the supplied VariablesArray. 468 */ 469 JEGA::DoubleMatrix 470 ToDoubleMatrix( 471 const VariablesArray& variables 472 ) const; 473 474 475 /* 476 =========================================================================== 477 Subclass Overridable Methods 478 =========================================================================== 479 */ 480 public: 481 482 /// Performs the iterations to determine the optimal set of solutions. 483 /** 484 * Override of pure virtual method in Optimizer base class. 485 * 486 * The extraction of parameter values actually occurs in this method 487 * when the JEGA::FrontEnd::Driver::ExecuteAlgorithm is called. Also 488 * the loading of the problem and algorithm configurations occurs in 489 * this method. That way, if it is called more than once and the 490 * algorithm or problem has changed, it will be accounted for. 491 */ 492 virtual 493 void 494 core_run( 495 ); 496 497 /** 498 * \brief Overridden to return true since JEGA algorithms can accept 499 * multiple initial points. 500 * 501 * \return true, always. 502 */ 503 virtual 504 bool 505 accepts_multiple_points( 506 ) const; 507 508 /** 509 * \brief Overridden to return true since JEGA algorithms can return 510 * multiple final points. 511 * 512 * \return true, always. 513 */ 514 virtual 515 bool 516 returns_multiple_points( 517 ) const; 518 519 /** 520 * \brief Overridden to assign the _initPts member variable to the 521 * passed in collection of Dakota::Variables. 522 * 523 * \param pts The array of initial points for the JEGA algorithm created 524 * and run by this JEGAOptimizer. 525 */ 526 virtual 527 void 528 initial_points( 529 const VariablesArray& pts 530 ); 531 532 /** 533 * \brief Overridden to return the collection of initial points for the 534 * JEGA algorithm created and run by this JEGAOptimizer. 535 * 536 * \return The collection of initial points for the JEGA algorithm 537 * created and run by this JEGAOptimizer. 538 */ 539 virtual 540 const VariablesArray& 541 initial_points( 542 ) const; 543 544 protected: 545 546 547 private: 548 549 550 551 /* 552 =========================================================================== 553 Private Methods 554 =========================================================================== 555 */ 556 private: 557 558 559 560 561 /* 562 =========================================================================== 563 Structors 564 =========================================================================== 565 */ 566 public: 567 568 /// Constructs a JEGAOptimizer class object. 569 /** 570 * This method does some of the initialization work for the algorithm. 571 * In particular, it initialized the JEGA core. 572 * 573 * \param problem_db The Dakota::ProblemDescDB with information on how the 574 * algorithm controls should be set. 575 * 576 * \param model The Dakota::Model that will be used by this optimizer 577 * for problem information, etc. 578 */ 579 JEGAOptimizer( 580 ProblemDescDB& problem_db, Model& model 581 ); 582 583 /// Destructs a JEGAOptimizer 584 ~JEGAOptimizer( 585 ); 586 587 }; // class JEGAOptimizer 588 589 590 591 /* 592 =============================================================================== 593 Include Inlined Methods File 594 =============================================================================== 595 */ 596 // Not using an Inlined Functions File. 597 598 599 /* 600 =============================================================================== 601 Class Definition 602 =============================================================================== 603 */ 604 605 /** 606 * \brief A version of TraitsBase specialized for John Eddy's 607 * Genetic Algorithms (JEGA). 608 * 609 */ 610 611 class JEGATraits: public TraitsBase 612 { 613 public: 614 615 /// default constructor JEGATraits()616 JEGATraits() { } 617 618 /// destructor ~JEGATraits()619 virtual ~JEGATraits() { } 620 621 /// A temporary query used in the refactor is_derived()622 virtual bool is_derived() { return true; } 623 624 /// Return the flag indicating whether method supports continuous variables supports_continuous_variables()625 bool supports_continuous_variables() { return true; } 626 627 /// Return the flag indicating whether method supports continuous variables supports_discrete_variables()628 bool supports_discrete_variables() { return true; } 629 630 /// Return the flag indicating whether method supports linear equalities supports_linear_equality()631 bool supports_linear_equality() { return true; } 632 633 /// Return the flag indicating whether method supports linear inequalities supports_linear_inequality()634 bool supports_linear_inequality() { return true; } 635 636 /// Return the flag indicating whether method supports nonlinear equalities supports_nonlinear_equality()637 bool supports_nonlinear_equality() { return true; } 638 639 /// Return the flag indicating whether method supports nonlinear inequalities supports_nonlinear_inequality()640 bool supports_nonlinear_inequality() { return true; } 641 642 /// Return the format used for nonlinear inequality constraints nonlinear_inequality_format()643 NONLINEAR_INEQUALITY_FORMAT nonlinear_inequality_format() 644 { return NONLINEAR_INEQUALITY_FORMAT::TWO_SIDED; } 645 646 }; 647 648 649 /* 650 =============================================================================== 651 End Namespace 652 =============================================================================== 653 */ 654 } // namespace Dakota 655 656 657 /* 658 =============================================================================== 659 End of Multiple Inclusion Check 660 =============================================================================== 661 */ 662 #endif // DAKOTA_JEGAOPTIMIZER_H 663