GAChromosome.C

Go to the documentation of this file.
00001 /*!@file GA/GAChromosome.C A chromosome class for genetic algorithm. */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GA/GAChromosome.C $
00035 // $Id: GAChromosome.C 6005 2005-11-29 18:49:14Z rjpeters $
00036 //
00037 
00038 #include "GA/GAChromosome.H"
00039 
00040 #include "Util/Assert.H"
00041 #include "Util/Types.H"
00042 #include <cmath>
00043 #include <cstdlib>
00044 #include <cstring>
00045 #include <istream>
00046 #include <ostream>
00047 
00048 GAChromosome::GAChromosome() :
00049   fitness(0.0f), linear_fitness(0.0f), breedings(0), size(0), genes(NULL)
00050 { }
00051 
00052 GAChromosome::GAChromosome(const int N) :
00053   fitness(0.0f), linear_fitness(0.0f), breedings(0), size(0), genes(NULL)
00054 {
00055   init(N);
00056 }
00057 
00058 GAChromosome::GAChromosome(const int N, const int *a) :
00059   fitness(0.0f), linear_fitness(0.0f), breedings(0), size(0), genes(NULL)
00060 {
00061   init(N, a);
00062 }
00063 
00064 GAChromosome::GAChromosome(const GAChromosome& c) :
00065   fitness(c.fitness), linear_fitness(c.linear_fitness),
00066   breedings(c.breedings), size(0), genes(NULL)
00067 {
00068   init(c.size, c.genes);
00069 }
00070 
00071 void GAChromosome::resize(const int N)
00072 {
00073   if (size) delete [] genes;
00074   genes = new int[N];
00075   size = N;
00076 }
00077 
00078 void GAChromosome::init(const int N, const int *a)
00079 {
00080   resize(N);
00081   memcpy(genes, a, N * sizeof(int));
00082 }
00083 
00084 void GAChromosome::init(const int N)
00085 {
00086   resize(N);
00087   for (int i = 0; i < N; i++)
00088     genes[i] = (rand() % 3) - 1;
00089 }
00090 
00091 GAChromosome::~GAChromosome()
00092 {
00093   if (size) delete [] genes;
00094 }
00095 
00096 int GAChromosome::get_size() const
00097 {
00098   return size;
00099 }
00100 
00101 void GAChromosome::set_gene(const int i, const int a)
00102 {
00103   ASSERT(i >= 0 && i < size && abs(a) < 2);
00104   genes[i] = a;
00105 }
00106 
00107 int GAChromosome::get_gene(const int i) const
00108 {
00109   ASSERT(i >= 0 && i < size);
00110   return genes[i];
00111 }
00112 
00113 void GAChromosome::set_fitness(const float a)
00114 {
00115   ASSERT(a >= 0);
00116   fitness = a;
00117 }
00118 
00119 float GAChromosome::get_fitness() const
00120 {
00121   return fitness;
00122 }
00123 
00124 void GAChromosome::set_linear_fitness(const float a)
00125 {
00126   ASSERT(a >= 0);
00127   linear_fitness = a;
00128 }
00129 
00130 float GAChromosome::get_linear_fitness() const
00131 {
00132   return linear_fitness;
00133 }
00134 
00135 void GAChromosome::set_breedings(const int a)
00136 {
00137   ASSERT(a >= 0);
00138   breedings = a;
00139 }
00140 
00141 int GAChromosome::get_breedings() const
00142 {
00143   return breedings;
00144 }
00145 
00146 GAChromosome& GAChromosome::operator=(const GAChromosome& c)
00147 {
00148   init(c.size, c.genes);
00149   fitness = c.fitness;
00150   linear_fitness = c.linear_fitness;
00151   breedings = c.breedings;
00152   return *this;
00153 }
00154 
00155 bool GAChromosome::operator<(const GAChromosome& c) const
00156 {
00157   return breedings < c.breedings;
00158 }
00159 
00160 void GAChromosome::mutation()
00161 {
00162   int i = rand() % size;
00163   genes[i] = ((genes[i] + 2) % 3) - 1;
00164 }
00165 
00166 void GAChromosome::add_breeding()
00167 {
00168   breedings++;
00169 }
00170 
00171 void GAChromosome::use_breeding()
00172 {
00173   ASSERT(breedings > 0);
00174   breedings--;
00175 }
00176 
00177 std::istream& operator>> (std::istream& in, GAChromosome& c)
00178 {
00179   int s, f, l, b;
00180   in >> s >> f >> l >> b;
00181   ASSERT(f >=0 && l >= 0 && b >= 0);
00182   c.resize(s);
00183   c.fitness = f;
00184   c.linear_fitness = l;
00185   c.breedings = b;
00186   for (int i = 0; i < c.size; i++)
00187     {
00188       int g;
00189       in >> g;
00190       ASSERT(abs(g) < 2);
00191       c.genes[i] = g;
00192     }
00193   return in;
00194 }
00195 
00196 std::ostream& operator<< (std::ostream& out, GAChromosome& c)
00197 {
00198   out << c.size << '\n';
00199   out << c.fitness << ' ' << c.linear_fitness << ' ';
00200   out << c.breedings << '\n';
00201   for (int i = 0; i < c.size; i++)
00202     {
00203       out << c.genes[i] << '\n';
00204     }
00205   return out;
00206 }
00207 
00208 // ######################################################################
00209 /* So things look consistent in everyone's emacs... */
00210 /* Local Variables: */
00211 /* indent-tabs-mode: nil */
00212 /* End: */
Generated on Sun May 8 08:40:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3