# t-eobin.cpp

    00001 /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003     t-eobin.cpp
    00004       This program tests the the binary cromosomes and several genetic operators
    00005     (c) GeNeura Team, 1999
    00006
    00007     This program is free software; you can redistribute it and/or modify
    00008     it under the terms of the GNU General Public License as published by
    00009     the Free Software Foundation; either version 2 of the License, or
    00010     (at your option) any later version.
    00011
    00012     This program is distributed in the hope that it will be useful,
    00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015     GNU General Public License for more details.
    00016
    00017     You should have received a copy of the GNU General Public License
    00018     along with this program; if not, write to the Free Software
    00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00020
    00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00022
    00023 */
    00024 //-----------------------------------------------------------------------------
    00025 #ifdef HAVE_CONFIG_H
    00026 #include <config.h>
    00027 #endif
    00028
    00029 #include <iostream>   // std::cout
    00030 #include <sstream>
    00031
    00032 #include <eo>         // general EO
    00033 #include <ga.h>       // bitstring representation & operators
    00034 #include <utils/eoRndGenerators.h>
    00035 #include "binary_value.h"
    00036
    00037 //-----------------------------------------------------------------------------
    00038
    00039 typedef eoBit<double> Chrom;
    00040
    00041 //-----------------------------------------------------------------------------
    00042
    00043 void main_function()
    00044 {
    00045   const unsigned SIZE = 8;
    00046   unsigned i, j;
    00047   eoBooleanGenerator gen;
    00048
    00049   Chrom chrom(SIZE), chrom2;
    00050   chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
    00051
    00052   std::cout << "chrom:  " << chrom << std::endl;
    00053   chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
    00054   std::cout << "chrom:  " << chrom << std::endl;
    00055   chrom[0] = chrom[SIZE - 1] = false; chrom.fitness(binary_value(chrom));
    00056   std::cout << "chrom:  " << chrom << std::endl;
    00057   chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
    00058
    00059   std::cout << "chrom.className() = " << chrom.className() << std::endl;
    00060
    00061   std::cout << "chrom:  " << chrom << std::endl
    00062        << "chrom2: " << chrom2 << std::endl;
    00063
    00064   std::ostringstream os;
    00065   os << chrom;
    00066   std::istringstream is(os.str());
    00067   is >> chrom2; chrom.fitness(binary_value(chrom2));
    00068
    00069   std::cout << "\nTesting reading, writing\n";
    00070   std::cout << "chrom:  " << chrom << "\nchrom2: " << chrom2 << '\n';
    00071
    00072   std::fill(chrom.begin(), chrom.end(), false);
    00073   std::cout << "--------------------------------------------------"
    00074        << std::endl << "eoMonOp's aplied to .......... " << chrom << std::endl;
    00075
    00076   eoInitFixedLength<Chrom>
    00077       random(chrom.size(), gen);
    00078
    00079   random(chrom); chrom.fitness(binary_value(chrom));
    00080   std::cout << "after eoBinRandom ............ " << chrom << std::endl;
    00081
    00082   eoOneBitFlip<Chrom> bitflip;
    00083   bitflip(chrom); chrom.fitness(binary_value(chrom));
    00084   std::cout << "after eoBitFlip .............. " << chrom << std::endl;
    00085
    00086   eoBitMutation<Chrom> mutation(0.5);
    00087   mutation(chrom); chrom.fitness(binary_value(chrom));
    00088   std::cout << "after eoBinMutation(0.5) ..... " << chrom << std::endl;
    00089
    00090   eoBitInversion<Chrom> inversion;
    00091   inversion(chrom); chrom.fitness(binary_value(chrom));
    00092   std::cout << "after eoBinInversion ......... " << chrom << std::endl;
    00093
    00094   eoBitNext<Chrom> next;
    00095   next(chrom); chrom.fitness(binary_value(chrom));
    00096   std::cout << "after eoBinNext .............. " << chrom << std::endl;
    00097
    00098   eoBitPrev<Chrom> prev;
    00099   prev(chrom); chrom.fitness(binary_value(chrom));
    00100   std::cout << "after eoBinPrev .............. " << chrom << std::endl;
    00101
    00102   std::fill(chrom.begin(), chrom.end(), false); chrom.fitness(binary_value(chrom));
    00103   std::fill(chrom2.begin(), chrom2.end(), true); chrom2.fitness(binary_value(chrom2));
    00104   std::cout << "--------------------------------------------------"
    00105        << std::endl << "eoBinOp's aplied to ... "
    00106        << chrom << " " << chrom2 << std::endl;
    00107
    00108   eo1PtBitXover<Chrom> xover;
    00109   std::fill(chrom.begin(), chrom.end(), false);
    00110   std::fill(chrom2.begin(), chrom2.end(), true);
    00111   xover(chrom, chrom2);
    00112   chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
    00113   std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl;
    00114
    00115   for (i = 1; i < SIZE; i++)
    00116     {
    00117       eoNPtsBitXover<Chrom> nxover(i);
    00118       std::fill(chrom.begin(), chrom.end(), false);
    00119       std::fill(chrom2.begin(), chrom2.end(), true);
    00120       nxover(chrom, chrom2);
    00121       chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
    00122       std::cout << "eoBinNxOver(" << i << ") ........ "
    00123            << chrom << " " << chrom2 << std::endl;
    00124     }
    00125
    00126   for (i = 1; i < SIZE / 2; i++)
    00127     for (j = 1; j < SIZE / 2; j++)
    00128       {
    00129         eoBitGxOver<Chrom> gxover(i, j);
    00130         std::fill(chrom.begin(), chrom.end(), false);
    00131         std::fill(chrom2.begin(), chrom2.end(), true);
    00132         gxover(chrom, chrom2);
    00133         chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
    00134         std::cout  << "eoBinGxOver(" << i << ", " << j << ") ..... "
    00135               << chrom << " " << chrom2 << std::endl;
    00136       }
    00137
    00138     // test SGA algorithm
    00139     eoGenContinue<Chrom> continuator1(50);
    00140     eoFitContinue<Chrom> continuator2(65535.f);
    00141
    00142     eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
    00143
    00144     eoCheckPoint<Chrom> checkpoint(continuator);
    00145
    00146     eoStdoutMonitor monitor;
    00147
    00148     checkpoint.add(monitor);
    00149
    00150     eoSecondMomentStats<Chrom> stats;
    00151
    00152     monitor.add(stats);
    00153     checkpoint.add(stats);
    00154
    00155     eoProportionalSelect<Chrom> select;
    00156     eoEvalFuncPtr<Chrom>  eval(binary_value);
    00157
    00158     eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint);
    00159
    00160     eoInitFixedLength<Chrom> init(16, gen);
    00161     eoPop<Chrom> pop(100, init);
    00162
    00163     apply<Chrom>(eval, pop);
    00164
    00165     sga(pop);
    00166
    00167     pop.sort();
    00168
    00169     std::cout << "Population " << pop << std::endl;
    00170
    00171     std::cout << "\nBest: " << pop[0].fitness() << '\n';
    00172
    00173   /*
    00174
    00175     Commented this out, waiting for a definite decision what to do with the mOp's
    00176
    00177     // Check multiOps
    00178     eoMultiMonOp<Chrom> mOp( &next );
    00179     mOp.adOp( &bitflip );
    00180     std::cout << "before multiMonOp............  " << chrom << std::endl;
    00181     mOp( chrom );
    00182     std::cout << "after multiMonOp .............. " << chrom << std::endl;
    00183
    00184     eoBinGxOver<Chrom> gxover(2, 4);
    00185     eoMultiBinOp<Chrom> mbOp( &gxover );
    00186     mOp.adOp( &bitflip );
    00187     std::cout << "before multiBinOp............  " << chrom << " " << chrom2 << std::endl;
    00188     mbOp( chrom, chrom2 );
    00189     std::cout << "after multiBinOp .............. " << chrom << " " << chrom2 <<std::endl;
    00190   */
    00191 }
    00192
    00193 //-----------------------------------------------------------------------------
    00194 // For MSVC memory lead detection
    00195 #ifdef _MSC_VER
    00196 #include <crtdbg.h>
    00197 #endif
    00198
    00199 int main()
    00200 {
    00201 #ifdef _MSC_VER
    00202   //  rng.reseed(42);
    00203     int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
    00204      flag |= _CRTDBG_LEAK_CHECK_DF;
    00205     _CrtSetDbgFlag(flag);
    00206 //   _CrtSetBreakAlloc(100);
    00207 #endif
    00208
    00209     try
    00210     {
    00211         main_function();
    00212     }
    00213     catch(std::exception& e)
    00214     {
    00215         std::cout << "Exception: " << e.what() << '\n';
    00216     }
    00217
    00218 }