MOGAL

doublevectorgenome.h

00001 /*
00002     
00003   This file is a part of MOGAL, a Multi-Objective Genetic Algorithm
00004   Library.
00005   
00006   Copyright (C) 2008-2012 Jori Liesenborgs
00007 
00008   Contact: jori.liesenborgs@gmail.com
00009 
00010   This library is free software; you can redistribute it and/or
00011   modify it under the terms of the GNU Lesser General Public
00012   License as published by the Free Software Foundation; either
00013   version 2.1 of the License, or (at your option) any later version.
00014 
00015   This library 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 GNU
00018   Lesser General Public License for more details.
00019 
00020   You should have received a copy of the GNU Lesser General Public
00021   License along with this library; if not, write to the Free Software
00022   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00023   USA
00024 
00025 */
00026 
00027 #ifndef MOGAL_DOUBLEVECTORGENOME_H
00028 
00029 #define MOGAL_DOUBLEVECTORGENOME_H
00030 
00031 #include "mogalconfig.h"
00032 #include "genome.h"
00033 #include <vector>
00034 
00035 namespace mogal
00036 {
00037 
00038 class DoubleVectorGAFactory;
00039 
00040 class MOGAL_IMPORTEXPORT DoubleVectorGenome : public Genome
00041 {
00042 public:
00043         DoubleVectorGenome(DoubleVectorGAFactory *pFactory, int numParams, int numFitnessValues);
00044         ~DoubleVectorGenome();
00045 
00046         bool calculateFitness();
00047         void setActiveFitnessComponent(int componentNumber)                                                     { m_fitnessComponent = componentNumber; }
00048         bool isFitterThan(const mogal::Genome *pGenome) const;
00049         Genome *reproduce(const mogal::Genome *pGenome) const;
00050         mogal::Genome *clone() const;
00051         void mutate();
00052         
00053         std::string getFitnessDescription() const;
00054 
00055         void setParameters(const double *pValues)                                                               { for (int i = 0 ; i < m_params.size() ; i++) m_params[i] = pValues[i]; }
00056         const double *getParameters() const                                                                     { return &(m_params[0]); }
00057         int getNumberOfParameters() const                                                                       { return m_params.size(); }
00058 
00059         void setFitnessValues(const double *pValues)                                                            { for (int i = 0 ; i < m_fitness.size() ; i++) m_fitness[i] = pValues[i]; }
00060         const double *getFitnessValues() const                                                                  { return &(m_fitness[0]); }
00061         int getNumberOfFitnessValues() const                                                                    { return m_fitness.size(); }
00062 
00063         void setCalculatedFitness(bool f = true)                                                                { m_calculatedFitness = f; }
00064         bool hasCalculatedFitness() const                                                                       { return m_calculatedFitness; }
00065 
00066         DoubleVectorGAFactory *getFactory()                                                                     { return m_pFactory; }
00067 protected:
00068         virtual bool calculateFitness(const double *pParams, int numParams, double *pFitnessValues, int numFitnessValues) = 0;
00069 private:
00070         DoubleVectorGAFactory *m_pFactory;
00071 
00072         std::vector<double> m_params;
00073         std::vector<double> m_fitness;
00074         bool m_calculatedFitness;
00075         int m_fitnessComponent;
00076 };
00077 
00078 } // end namespace
00079 
00080 #endif // MOGAL_DOUBLEVECTORGENOME_H
00081