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 }