00001 /** @file GeneticAlgorithm.h 00002 * @author Gabor Madl 00003 * @date Created 08/2005 00004 * @brief Genetic algorithm library. 00005 * 00006 * 00007 * ================================================================= 00008 * DREAM License v2.0 00009 * 00010 * DREAM - Distributed Real-time Embedded Analysis Method 00011 * http://dre.sourceforge.net. 00012 * Copyright (c) 2005-2007 Gabor Madl, All Rights Reserved. 00013 * 00014 * This file is part of DREAM. 00015 * 00016 * DREAM is free software; you can redistribute it and/or modify it 00017 * under the terms of the GNU General Public License version 2 as 00018 * published by the Free Software Foundation. No future versions of 00019 * the GPL license may be automatically applied to DREAM. It is in 00020 * the sole discretion of the copyright holder to determine whether 00021 * DREAM may be released under a different license or terms. There 00022 * are no restrictions on the use of DREAM for any purpose. 00023 * 00024 * DREAM is distributed in the hope that it will be useful, 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00027 * GNU General Public License for more details. 00028 * 00029 * You should have received a copy of the GNU General Public License 00030 * along with this program; if not, write to the Free Software 00031 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00032 * MA 02110-1301, USA. 00033 * 00034 * By submitting comments, suggestions, code, code snippets, 00035 * techniques (including that of usage), and algorithms, submitters 00036 * acknowledge that they have the right to do so, that any such 00037 * submissions are given freely and unreservedly, and that they 00038 * waive any claims to copyright or ownership. In addition, 00039 * submitters acknowledge that any such submission might become 00040 * part of the copyright maintained on the overall body of code, 00041 * which comprises DREAM. By making a submission, submitter agrees 00042 * to these terms. Furthermore, submitters acknowledge that the 00043 * incorporation or modification of such submissions is entirely 00044 * at the discretion of the moderators of the DREAM project. 00045 * 00046 * DREAM links to the Libxml2 third party library. Please see 00047 * COPYING-libxml for the copyright information of Libxml2. 00048 * ================================================================= 00049 */ 00050 00051 #ifndef DREAM_GENETICALGORITHM 00052 #define DREAM_GENETICALGORITHM 00053 00054 #include "../core/System.h" 00055 00056 namespace DREAM 00057 { 00058 00059 class Solution; 00060 struct sort_solutions; 00061 struct optimize_cpus; 00062 00063 /** Operator overloading for appending DREAM::Solution to std:string. */ 00064 std::string& operator<< (std::string& out, DREAM::Solution& solution); 00065 00066 /** A deterministic schedule for DREAM::System. */ 00067 class Solution 00068 { 00069 00070 friend class GeneticOptimize; 00071 friend class ModelCheck; 00072 friend std::ostream& operator<< (std::ostream& out, DREAM::Solution& solution); 00073 friend struct sort_solutions; 00074 00075 public: 00076 00077 /** Creates a solution corresponding to the System. 00078 * @param system_ptr is the pointer to the System to be used to generate the Solution. 00079 */ 00080 Solution (DREAM::System* system_ptr); 00081 00082 /** Destructor. */ 00083 virtual ~Solution (); 00084 00085 /** Returns true if the schedule corresponding to the Schedule is deterministic. 00086 * The function is conservative - it might return false for some deterministic systems but not the other way. 00087 * @return mostly true if the Solution is deterministic. 00088 */ 00089 bool deterministic (); 00090 00091 /** Calculates how good the current solution is. 00092 * @param verbose specifies whether simulation messages will be supressed or not. 00093 * @param deterministic specifies whether randomness is allowed. 00094 * @param endtoend updates the pointer's target with the end-to-end execution time of the model (unless the pointer is NULL). 00095 * @return the fitness. 00096 */ 00097 virtual double fitness (bool verbose, bool deterministic, double* endtoend = NULL); 00098 00099 /** Generates random solution for the System. */ 00100 virtual void generate_priorities () 00101 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING 00102 throw (DREAM::Exception) 00103 #endif 00104 ; 00105 00106 /** Generates random solution for the System. */ 00107 virtual void generate_subpriorities () 00108 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING 00109 throw (DREAM::Exception) 00110 #endif 00111 ; 00112 00113 /** Regenerates random solution. */ 00114 virtual void regenerate_priorities (); 00115 00116 /** Regenerates random solution. */ 00117 virtual void regenerate_subpriorities (); 00118 00119 /** Produces nice output from the Solution. 00120 * @param out is the message to be output. 00121 */ 00122 void visitor (std::string& out); 00123 00124 protected: 00125 00126 /** Flag which stores when the new task AVL tree copy is created. */ 00127 bool copy_; 00128 00129 /** Stores the fitness of the Solution. */ 00130 double fitness_; 00131 00132 /** Map storing all the Tasks in the System. */ 00133 DREAM::TASK_AVLTREE task_avltree_; 00134 00135 /** Pointer to the system */ 00136 DREAM::System* system_ptr_; 00137 }; 00138 00139 /** Optimizes the scheduling of DREAM::System. */ 00140 class GeneticOptimize 00141 { 00142 public: 00143 /** Creates a genetic algorithm. */ 00144 GeneticOptimize (); 00145 00146 /** Destructor. */ 00147 virtual ~GeneticOptimize () {}; 00148 00149 /** Single source mutation. 00150 * @param solution is the solution to be mutated. 00151 */ 00152 virtual void mutate (DREAM::Solution* solution) const; 00153 00154 /** Mutation from two parents. 00155 * @param father is one of the solution sources. 00156 * @param mother is the other solution source. 00157 * @param child is a pointer to the solution to be modified (it can be father or mother as well...). 00158 */ 00159 virtual void mutate (DREAM::Solution* father, DREAM::Solution* mother, DREAM::Solution* child) const; 00160 00161 /** Thread-level optimization of fixed-priority scheduling using genetic algorithms. 00162 * We partition the solution space into 8 zones. One zone is reserved for the best solutions. 00163 * Three zones are reserved for mutations from parents. Two zones are reserved for single mutations. 00164 * Two zones are reserved for random generation only. 00165 * @param system_ptr is a pointer to the System. 00166 */ 00167 virtual void thread_level (DREAM::System* system_ptr); 00168 00169 /** CPU-level optimization of fixed-priority scheduling using genetic algorithms. 00170 * Optimizes both priorities and subpriorities. 00171 * We partition the solution space into 8 zones. One zone is reserved for the best solutions. 00172 * Three zones are reserved for mutations from parents. Two zones are reserved for single mutations. 00173 * Two zones are reserved for random generation only. 00174 * @param system_ptr is a pointer to the System. 00175 */ 00176 virtual void CPU_level (DREAM::System* system_ptr); 00177 00178 protected: 00179 00180 /** AVLTree that stores the potential solutions with their fitness as key. */ 00181 std::vector<DREAM::Solution*> solution_vector_; 00182 00183 /** Pointer to the system */ 00184 DREAM::System* system_ptr_; 00185 }; 00186 00187 /** Used by genetic algorithms to sort solutions based on the fitness function. */ 00188 struct sort_solutions 00189 { 00190 bool operator () (DREAM::Solution* solution1, DREAM::Solution* solution2) 00191 { 00192 return (solution1->fitness_ < solution2->fitness_); 00193 } 00194 }; 00195 00196 }; 00197 00198 #endif 00199