//--------------------------------------------------------------------- // ACOVEA -- Analysis of Compiler Options Via Evolution Algorithm // // acovea.h // // Class definitions for the ACOVEA algorithm //--------------------------------------------------------------------- // // Copyright 2003, 2004, 2005 Scott Robert Ladd // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the // Free Software Foundation, Inc. // 59 Temple Place - Suite 330 // Boston, MA 02111-1307, USA. // //----------------------------------------------------------------------- // // For more information on this software package, please visit // Scott's web site, Coyote Gulch Productions, at: // // http://www.coyotegulch.com // //----------------------------------------------------------------------- #if !defined(ACOVEA_H) #define ACOVEA_H #include "libevocosm/evocommon.h" #include "libevocosm/evocosm.h" #include "libevocosm/roulette.h" namespace acovea { using namespace libevocosm; using namespace std; //---------------------------------------------------------- // Fitness scaling type enum optimization_mode { OPTIMIZE_SPEED, OPTIMIZE_SIZE, OPTIMIZE_RETVAL, }; //---------------------------------------------------------- // objects global to several classes class common : protected libevocosm::globals { // class exists to provide a singular path to libevocosm::globals }; //---------------------------------------------------------- // settings tracker // an option with settings returns an object of this type containing // option-specific tracking data (value, for example); several trackers // can be accumulated to get cross-population totals class settings_tracker { public: // get a string representing this settings tracker virtual string get_settings_text() { return string("none"); } // accumulate information virtual settings_tracker & operator += (const settings_tracker & tracker) { return *this; } // virtual destructor (does nothing unless overridden) virtual ~settings_tracker() { // nada } }; //---------------------------------------------------------- // abstract definition of a application option or switch class option : public common { public: // creation constructor option(bool a_enabled = true); // copy constructor option(const option & a_source); // assignment option & operator = (const option & a_source); // clone virtual option * clone() = 0; // properties bool is_enabled() const { return m_enabled; } void set_enabled(bool a_enabled) { m_enabled = a_enabled; } // averaging virtual bool has_settings() { return false; } virtual settings_tracker * alloc_settings_tracker() { return new settings_tracker(); } // get the string for this option virtual string get() const = 0; // get the unadorned name of this option virtual vector get_choices() const = 0; // get the setting for this option virtual int get_setting() const = 0; // randomize settings of this option virtual void randomize(); // mutate this option virtual void mutate(); // virtual destructor (does nothing unless overridden) virtual ~option() = 0; protected: // is this option enabled (included in command line) bool m_enabled; }; //---------------------------------------------------------- // an option that is just a string class simple_option : public option { public: // creation constructor simple_option(const string & a_name, bool a_enabled = true); // creation constructor simple_option(const char * a_name, bool a_enabled = true); // copy constructor simple_option(const simple_option & a_source); // assignment operator simple_option & operator = (const simple_option & a_source); // clone virtual option * clone(); // get the string for this option virtual string get() const { return m_name; } // get the unadorned name of this option virtual vector get_choices() const { vector result; result.push_back(m_name); return result; } // get the setting for this option virtual int get_setting() const { return 0; } protected: // name of this option string m_name; }; //---------------------------------------------------------- // value settings tracker class tuning_option; class tuning_settings_tracker : public settings_tracker { public: // constructor tuning_settings_tracker(const tuning_option & option); // copy constructor tuning_settings_tracker(const tuning_settings_tracker & source); // assignment tuning_settings_tracker & operator = (const tuning_settings_tracker & source); // get a string representing this settings tracker virtual string get_settings_text(); // accumulate information virtual settings_tracker & operator += (const settings_tracker & tracker); // virtual destructor (does nothing unless overridden) virtual ~tuning_settings_tracker() { // nada } private: // list of values vector m_values; // sum of enabled options int m_sum; // count of options enabled int m_count; }; //---------------------------------------------------------- // an option or switch that requires an integer argument class tuning_option : public simple_option { public: // creation constructor tuning_option(const string & a_name, bool a_enabled, int a_default, int a_min_value, int a_max_value, int a_step, char a_separator = '='); // copy constructor tuning_option(const tuning_option & a_source); // assignment operator tuning_option & operator = (const tuning_option & a_source); // clone virtual option * clone(); // get the string for this option virtual string get() const; // get the value of this option int get_value() const { return m_value; } // get the value of this option int set_value(int a_value) { m_value = a_value; if (m_value < m_min_value) m_value = m_min_value; if (m_value > m_max_value) m_value = m_max_value; } // get the value of this option int get_default() const { return m_default; } // averaging virtual bool has_settings() { return true; } virtual settings_tracker * alloc_settings_tracker() { return new tuning_settings_tracker(*this); } // mutate this option virtual void mutate(); // virtual destructor (does nothing unless overridden) virtual ~tuning_option() { }; protected: // current value int m_value; // range of value int m_default; int m_min_value; int m_max_value; // maximum change in value per mutation int m_step; // separator between name and value char m_separator; }; //---------------------------------------------------------- // enum option and settings tracker class enum_option : public option { public: // creation constructor (vector of strings) enum_option(const vector & a_choices, bool a_enabled = true); // creation constructor (char * array) enum_option(const char ** a_choices, size_t a_num_choices, bool a_enabled = true); // creation constructor (delimited string) enum_option(const char * a_choices, bool a_enabled = true); // copy constructor enum_option(const enum_option & a_source); // assignment enum_option & operator = (const enum_option & a_source); // clone virtual option * clone(); // get the string for this option virtual string get() const; // get the unadorned name of this option virtual vector get_choices() const { return m_choices; } // get current setting virtual int get_setting() const { return m_setting; } // randomize settings of this option virtual void randomize(); // mutate this option virtual void mutate(); // virtual destructor (does nothing unless overridden) virtual ~enum_option(); protected: // which setting? int m_setting; vector m_choices; }; //---------------------------------------------------------- // a vector of options, defined as an object so that the // pointers will be automatically deallocated upon // object deletion class chromosome : private vector