Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

testenv.cc

Go to the documentation of this file.
00001 
00025 #include <ctype.h>
00026 #include <magic/mmath.h>
00027 #include <magic/mstream.h>
00028 #include <magic/mtextstream.h>
00029 #include "nhp/genetics.h"
00030 #include "nhp/genes.h"
00031 #include "nhp/individual.h"
00032 #include "nhp/gaenvrnmt.h"
00033 #include "nhp/testenv.h"
00034 
00036 // -----                  ----   _   -----             o                                      //
00037 //   |    ___   ____  |  |      / \  |       _                      _          ___    _    |  //
00038 //   |   /   ) (     -+- | --- /   \ |---  |/ \  |   | | |/\  __  |/ \  |/|/| /   ) |/ \  -+- //
00039 //   |   |---   \__   |  |   \ |---| |     |   |  \ /  | |   /  \ |   | | | | |---  |   |  |  //
00040 //   |    \__  ____)   \ |___/ |   | |____ |   |   V   | |   \__/ |   | | | |  \__  |   |   \ //
00042 
00043 BinaryTestEAEnv::BinaryTestEAEnv (int dim, int ntargets) {
00044     targets.make (ntargets, dim);
00045     changeObjective (0);
00046 }
00047 
00048 void BinaryTestEAEnv::changeObjective (int target) {
00049     mObjective = target;
00050 
00051     for (int t=0; t<targets.rows; t++)
00052         for (int i=0; i<targets.cols; i++)
00053             targets.get (t, i) = mObjective;
00054 }
00055 
00056 void BinaryTestEAEnv::addFeaturesTo (Genome& genome) const {
00057     for (int i=0; i<targets.cols; i++)
00058         genome.add (new BinaryGene (format ("x%d", i), 1.0));
00059 }
00060 
00061 double dist (double x0, double y0, double x1, double y1) {
00062     return sqrt ((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));
00063 }
00064 
00065 double BinaryTestEAEnv::evaluateg (const Individual& indiv) {
00066     double err = 0.0;
00067     for (int i=0; i<targets.cols; i++) {
00068         int x = static_cast<const BinaryGene&> (*indiv.getGene(format ("x%d", i))).getvalue();
00069         err += (x==targets.get (0, i))? 0.0:1.0;
00070         //      printf ("%g ", x);
00071         //      fflush (stdout);
00072     }
00073     //  printf ("%g ", err);
00074     //  fflush (stdout);
00075 
00076     return err;
00077 }
00078 
00079 void BinaryTestEAEnv::cycle_report (OStream& log, OStream& out) {
00080     ;
00081 }
00082 
00083 
00084 
00086 //                                                                              //
00087 //  ----- |                -----                  ----   _   -----              //
00088 //  |     |       ___   |    |    ___   ____  |  |      / \  |       _          //
00089 //  |---  |  __   ___| -+-   |   /   ) (     -+- | --- /   \ |---  |/ \  |   |  //
00090 //  |     | /  \ (   |  |    |   |---   \__   |  |   \ |---| |     |   |  \ /   //
00091 //  |     | \__/  \__|   \   |    \__  ____)   \ |___/ |   | |____ |   |   V   O//
00092 //                                                                              //
00094 
00095 FloatTestEAEnv::FloatTestEAEnv (const StringMap& params, int d, int f) : mParams (params) {
00096     dim = d;
00097     func = f;
00098     mObjective = 0;
00099     mGeneType = ESFLOAT;
00100  }
00101 
00102 void FloatTestEAEnv::addFeaturesTo (Genome& genome) const {
00103     for (int i=0; i<dim; i++)
00104         if (mGeneType==BITFLOAT)
00105             genome.add (new BitFloatGene (format ("x%d", i), -4.0, 4.0, 16, mParams));
00106         else
00107             genome.add (new FloatGene (format ("x%d", i), -4.0, 4.0, 1.0));
00108 }
00109 
00111 //                                                     o                     //
00112 //     |   ___   ____  |      __         _    ___   |           _    ____    //
00113 //    -+- /   ) (     -+-    /   |   | |/ \  |   \ -+- |  __  |/ \  (        //
00114 //     |  |---   \__   |     +-- |   | |   | |      |  | /  \ |   |  \__     //
00115 //      \  \__  ____)   \    |    \__! |   |  \__/   \ | \__/ |   | ____)    //
00116 //                           |                                               //
00118 
00119 #define pi 3.14159
00120 
00121 #define Sum(fx)\
00122     double sum=0;\
00123     for (int i=0; i<x.size(); i++)\
00124         sum += fx;
00125 
00126 #define Mul(fx)\
00127     double mul=1;\
00128     for (int i=0; i<x.size(); i++)\
00129         mul *= fx;
00130 
00131 inline double sphereTF (const PackArray<double>& x) {
00132     Sum (sqr(x[i]));
00133     return sum;
00134 }
00135 
00136 inline double ellipsoidTF (const PackArray<double>& x) {
00137     Sum (sqr(double(i+1))*sqr(x[i]));
00138     return sum;
00139 }
00140 
00141 inline double negsphereTF (const PackArray<double>& x) {
00142     Sum (-sqr(x[i]));
00143     return sum;
00144 }
00145 
00146 inline double zerominTF (const PackArray<double>& x) {
00147     Sum (fabs(x[i]));
00148     return sum;
00149 }
00150 
00151 inline double TF4 (const PackArray<double>& x) {
00152     double k=4;
00153     Sum (sqr(k*x[i])-k*cos(2*pi*x[i]*k));
00154     return (x.size()*10 + sum)/50.0;
00155 }
00156 
00157 inline double TF5 (const PackArray<double>& x) {
00158     double k=-1000;
00159     Sum (-x[i]*sin(sqrt(fabs(k*x[i]))));
00160     return sum;
00161 }
00162 
00163 // Rastrigin's function
00164 inline double TF6 (const PackArray<double>& x) {
00165     double k=20;
00166     Sum (sqr(k*4*x[i])/4000);
00167     Mul (cos(k*x[i]/sqrt(i+1.0)));
00168     return (sum-mul+1)/4.0;
00169 }
00170 
00171 // Generalized Rastrigin's function
00172 inline double GenRastriginTF (const PackArray<double>& x) {
00173     double A=10, w=2*M_PI;
00174     Sum (sqr(x[i])-A*cos(w*x[i]));
00175     return A*x.size() + sum;
00176 }
00177 
00178 // Schwefel's function
00179 inline double TF7 (const PackArray<double>& x) {
00180     Sum (fabs(x[i]));
00181     return sum;
00182 }
00183 
00184 // Griewangk's function
00185 inline double TF8 (const PackArray<double>& x) {
00186     Sum (fabs(x[i]));
00187     return sum;
00188 }
00189 
00190 
00192 
00193 double FloatTestEAEnv::evaluateg (const Individual& indiv) {
00194     PackArray<double> x (dim);
00195     for (int i=0; i<dim; i++)
00196         x[i] = dynamic_cast<const AnyFloatGene&> (*indiv.getGene(format ("x%d", i))).getvalue();
00197     
00198     return calc (x, func);
00199 }
00200 
00201 double FloatTestEAEnv::calc (const Vector& x0, int func) {
00202     ASSERT (func>=0 && func<functions);
00203     ASSERT (mObjective==0 || mObjective==1);
00204 
00205     Vector x = x0;
00206     if (mObjective==1)
00207         for (int i=0; i<x.size(); i++)
00208             x[i] = 1-x[i];
00209     
00210     double result=0;
00211     switch (func) {
00212       case Sphere:      result = sphereTF       (x); break;
00213       case Ellipsoid:   result = ellipsoidTF    (x); break;
00214       case NegSphere:   result = negsphereTF    (x); break;
00215       case ZeroMin:     result = zerominTF      (x); break;
00216       case F4:          result = TF4            (x); break;
00217       case F5:          result = TF5            (x); break;
00218       case F6:          result = TF6            (x); break;
00219       case F7:          result = TF7            (x); break;
00220       case F8:          result = TF8            (x); break;
00221     };
00222 
00223     return result;
00224 }
00225 
00226 void FloatTestEAEnv::printMathematica2D () {
00227     TextOStream out;
00228     out.autoFlush ();
00229 
00230     Vector vec (2);
00231     for (int f=4; f<functions-2; f++) {
00232         out.printf ("testfunc%d := {", f);
00233         for (double y=-1.0; y<=1.0; y+=0.02) {
00234             if (y>-1.0)
00235                 out << ",";
00236             out << "{";
00237             for (double x=-1.0; x<=1.0; x+=0.02) {
00238                 if (x>-1.0)
00239                     out << ",";
00240                 vec[0] = x;
00241                 vec[1] = y;
00242                 double r = calc (vec, f) + gaussrnd (0.1);
00243                 out.printf ("%f", r);
00244             }
00245             out << "}";
00246         }
00247         out.printf ("}\n"
00248                     "ListDensityPlot[testfunc%d, Mesh -> False]\n\n",
00249                     f);
00250     }
00251 }
00252 
00253 
00255 //                                                                           //
00256 //      |   |       |     o |   | o        ----   _   -----                  //
00257 //      |\ /|       |  |    |\ /|     _   |      / \  |       _              //
00258 //      | V | |   | | -+- | | V | | |/ \  | --- /   \ |---  |/ \  |   |      //
00259 //      | | | |   | |  |  | | | | | |   | |   \ |---| |     |   |  \ /       //
00260 //      |   |  \__! |   \ | |   | | |   | |___/ |   | |____ |   |   V        //
00261 //                                                                           //
00263 
00264 void MultiMinEAEnv::addFeaturesTo (Genome& genome) const {
00265     genome.add (new FloatGene ("x", 0.0, 1.0, 1.0));
00266     genome.add (new FloatGene ("y", 0.0, 1.0, 1.0));
00267 }
00268 
00269 double MultiMinEAEnv::evaluateg (const Individual& genome) {
00270     double  x   = static_cast<const FloatGene&> (genome["x"]).getvalue();
00271     double  y   = static_cast<const FloatGene&> (genome["y"]).getvalue();
00272 
00273     double d[4];
00274     d[0] = dist (x, y, 0.6, 0.75);
00275     d[1] = dist (x, y, 0.25, 0.25);
00276     d[2] = dist (x, y, 0.0, 1.0);
00277     d[3] = dist (x, y, 0.9, 0.4);
00278     
00279     double nearv=d[0];
00280     for (int i=1; i<4; i++)
00281         if (d[i]<nearv)
00282             nearv = d[i];
00283 
00284     return nearv;
00285 }
00286 

Generated on Thu Feb 10 20:12:01 2005 for NeHeP by doxygen1.2.18