Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Evolution/Individual.cpp

Go to the documentation of this file.
00001 /**
00002 * @file Individual.cpp
00003 *
00004 * Implementation of class Individual.
00005 *
00006 * @author <a href=mailto:dueffert@informatik.hu-berlin.de>Uwe Düffert</a>
00007 * @author <a href="mailto:a.cesarz@gmx.de">Arthur Cesarz</a>
00008 * @author <a href="mailto:matthias.hebbel@uni-dortmund.de">Matthias Hebbel</a>
00009 */
00010 
00011 #include "Individual.h"
00012 #include "Tools/Math/Common.h"
00013 
00014 Individual::Individual():fitness(-1)
00015 {
00016 }
00017 
00018 void Individual::mutationOf(Individual* father, double rate, double amount, bool uniformNoise/*, other EvoParams*/)
00019 {
00020 /*
00021 FlipMutate: flip bits: not usefull here
00022 SwapMutate: swap two numbers: only usefull with data of same type
00023 */
00024   int max1,max2;
00025   getDimension(max1,max2);
00026   for (int i=0;i<max1;i++)
00027   {
00028     bool doMutate=((double)rand()/(RAND_MAX+1.0)<rate);
00029     for (int j=0;j<max2;j++)
00030     {
00031       double min,max,val;
00032       ValueType type;
00033       father->getValue(i,j,min,max,val,type);
00034       if (doMutate)
00035       {
00036         switch(type)
00037         {
00038           case valueInt:
00039             //2do: Sprung zum naechsten mit Wahrscheinlichkeit schaffen
00040           case valueDouble:
00041           case value2PiDouble:
00042             if (uniformNoise)
00043             {
00044               // add uniform distributed value
00045               val += (random()-0.5)*amount*(max-min);
00046             }
00047             else
00048             {
00049               // add gauss distributed value
00050               val += amount*(max-min)*sqrt(-2.0*log(random()))*sin(pi2*random());
00051             }
00052         }
00053         if (type==value2PiDouble)
00054         {
00055           val = (val<-pi)?val+pi2:((val>=pi)?val-pi2:val);
00056         }
00057         val = (val<min)?min:((val>max)?max:val);
00058       }
00059       setValue(i,j,val);
00060     }
00061   }
00062   fitness=-1;
00063 }
00064 
00065 void Individual::crossingOverOf(Individual* father, Individual* mother/*, other EvoParams*/)
00066 {
00067 /*UniformCrossover: For each bit we flip a coin to see if that bit should come from the mother or the father.
00068 EvenOddCrossover:
00069 OnePointCrossover:
00070 TwoPointCrossover:
00071 */
00072 
00073   int max1,max2;
00074   getDimension(max1,max2);
00075 
00076   //find random value around better parent or mixture of parents
00077   double fitDiff=father->fitness-mother->fitness;
00078   if (fitDiff<-10) {fitDiff=-10;}
00079   else if (fitDiff>10) {fitDiff=10;}
00080   double fatherFactor=(10+fitDiff)/20;
00081   for (int i=0;i<max1;i++)
00082   {
00083     //Roefer-like Crossover (with intra- and extrapolation)
00084     for (int j=0;j<max2;j++)
00085     {
00086       double minVal,maxVal,valF,valM;
00087       ValueType type;
00088       father->getValue(i,j,minVal,maxVal,valF,type);
00089       mother->getValue(i,j,minVal,maxVal,valM,type);
00090       double valMiddle=fatherFactor*valF+(1-fatherFactor)*valM;
00091       double val=valMiddle+(random()-0.5)*2*fabs(valF-valM);
00092       val=(val<minVal)?minVal:((val>maxVal)?maxVal:val);
00093       setValue(i,j,val);
00094     }
00095     /*
00096     //EvenOddCrossover
00097     bool fromFather=((i%2)==0);
00098     for (int j=0;j<max2;j++)
00099     {
00100     double min,max,val;
00101     ValueType type;
00102     if (fromFather)
00103     {
00104     father->getValue(i,j,min,max,val,type);
00105     }
00106     else
00107     {
00108     mother->getValue(i,j,min,max,val,type);
00109     }
00110     setValue(i,j,val);
00111     }
00112     */
00113   }
00114   fitness=-1;
00115 }
00116 
00117 /*
00118 * Change log :
00119 * 
00120 * $Log: Individual.cpp,v $
00121 * Revision 1.2  2004/05/26 17:32:53  dueffert
00122 * update from preWM CVS
00123 *
00124 * Revision 1.6  2004/05/24 13:06:01  dueffert
00125 * extrapolation added
00126 *
00127 * Revision 1.5  2004/03/24 13:47:10  dueffert
00128 * own new bug fixed
00129 *
00130 * Revision 1.4  2004/03/24 13:44:49  dueffert
00131 * support for uniform noise mutation readded
00132 *
00133 * Revision 1.3  2004/02/28 11:49:10  cesarz
00134 * changed uniformly distributed random value to gauss distributed value
00135 *
00136 * Revision 1.2  2004/02/26 18:07:28  cesarz
00137 * first version of evolution behavior
00138 *
00139 * Revision 1.1  2003/10/07 10:13:22  cvsadm
00140 * Created GT2004 (M.J.)
00141 *
00142 * Revision 1.1  2003/09/26 11:40:40  juengel
00143 * - sorted tools
00144 * - clean-up in DataTypes
00145 *
00146 * Revision 1.3  2003/09/01 15:57:42  dueffert
00147 * Genom and Individual merged
00148 *
00149 * Revision 1.2  2003/08/08 14:27:07  dueffert
00150 * implementation added
00151 *
00152 * Revision 1.1.1.1  2003/07/02 09:40:22  cvsadm
00153 * created new repository for the competitions in Padova from the 
00154 * tamara CVS (Tuesday 2:00 pm)
00155 *
00156 * removed unused solutions
00157 *
00158 * Revision 1.1  2003/02/10 15:26:58  dueffert
00159 * self made GA stuff
00160 *
00161 *
00162 */

Generated on Thu Sep 23 19:57:37 2004 for GT2004 by doxygen 1.3.6