testga.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include "SimpleSelect.h"
00003 #include "UniformRNG.h"
00004 #include "gentypes.h"
00005 #include "BinaryPopulation.h"
00006 #include "StandardPropagation.h"
00007 #include "GrayTranscribe.h"
00008 #include "AnnealingGA.h"
00009 #include "ParetoGA.h"
00010 #include "TestObjective.h"
00011 #include "TestObjective2.h"
00012 #include "BinaryTournamentSelect.h"
00013 #include <boost/numeric/ublas/vector.hpp>
00014 #include <boost/numeric/ublas/io.hpp>
00015 #include <boost/numeric/ublas/matrix_proxy.hpp>
00016 #include <boost/bind.hpp>
00017 #include <boost/shared_ptr.hpp>
00018 #include <fstream>
00019 
00020 using namespace std;
00021 int main()
00022 {
00023         UniformRNG Random;
00024         const int popsize = 25;
00025         typedef boost::shared_ptr<GeneralObjective> pCGeneralObjective;
00026         const double inittemp = 1;
00027         const int annealgen = 5;
00028         const double ratio = 10;
00029         const int maxgen = 10;
00030         const int nparam = 2;
00031         
00032         ttranscribed basevalues(nparam);
00033         ttranscribed stepsizes(nparam);
00034         tsizev genesizes(nparam);
00035         basevalues(0) = -10;
00036         stepsizes(0) = 0.02;
00037         genesizes(0) = 10;
00038         basevalues(1) = -10;
00039         stepsizes(1) = 0.02;
00040         genesizes(1) = 10;
00041         const int genesize = ublas::sum(genesizes);
00042         boost::shared_ptr<TestObjective> Objective(new TestObjective());
00043         boost::shared_ptr<TestObjective2> Objective2(new TestObjective2());
00044 
00045         std::vector<int> Indices(nparam,0);
00046         for (int i = 0; i < nparam; ++i)
00047                 Indices.at(i) = i;
00048         vector<pCGeneralObjective> ObjVector;
00049         ObjVector.push_back(Objective);
00050         ObjVector.push_back(Objective2);
00051         
00052         BinaryPopulation Population(popsize,genesize,Random,true);
00053         BinaryTournamentSelect Select(Random,boost::bind(&GeneralPopulation::GetProbabilities,boost::ref(Population)),
00054                 boost::bind(&GeneralPopulation::GetCrowdingDistances,boost::ref(Population)));
00055         GrayTranscribe Transcribe(basevalues,stepsizes,genesizes);
00056         StandardPropagation Propagator(&Select,&Population,&Random);
00057         tprobabilityv probabilities(popsize);
00058         double sum=0;
00059         
00060         const double mutationprob = 0.2;
00061         const double crossoverprob = 0.6;
00062         Propagator.SetParams(mutationprob,crossoverprob);
00063         
00064         ttranscribed transcribed;
00065         ParetoGA GA(&Propagator,&Population,&Transcribe,ObjVector);
00066         
00067         GeneralGA::tparamindv temp(2,Indices);
00068         
00069         GA.SetParameterIndices(temp);
00070         GA.SetElitist(true);
00071         cout << "Population initialized" << endl <<flush;
00072         
00073         //GA.SetParams(inittemp,annealgen,ratio);
00074         
00075         cout << "Pointers set" << endl <<flush;
00076         
00077         ofstream rankfile("test.ranks");
00078         
00079         for (int i = 0; i < maxgen; ++i)
00080         {
00081                 cout << endl << "Iteration: " << i << endl << endl; 
00082                 /*cout << "Transcribed: " << endl;
00083                 for (int j = 0; j < Population.GetPopsize(); ++j)
00084                 {
00085                         cout << row(Population.GetPopulation(),j) << " " << Transcribe.GetValues(row(Population.GetPopulation(),j)) << endl;
00086                 }*/
00087                 //cout << "Population: " << endl;
00088                 //Population.PrintPopulation(cout);
00089                 GA.DoIteration(i,i==(maxgen -1));
00090                 cout << "Transcribed: " << endl;
00091                 //GA.PrintTranscribed(cout);
00092                 //cout << "Probabilities: " << endl;
00093                 //Population.PrintProbabilities(cout);
00094                 //cout << "Distances: " << endl;
00095                 //Population.PrintDistances(cout);
00096                 //cout << "Misfit: " << endl;
00097                 //GA.PrintMisfit(cout);
00098                 GA.PrintRanks(rankfile);
00099         }
00100         ofstream frontfile("test.front");
00101         ofstream modelfile("test.models");
00102         GA.PrintFront(frontfile);
00103         
00104         tpopmember member(Population.GetGenesize());
00105         vector<int> BestIndices(GA.GetBestModelIndices());
00106         int noutmodels = GA.GetNBestmodels();
00107         for (int h= 0; h < noutmodels; ++h)
00108         {
00109                 member = row(Population.GetPopulation(),BestIndices.at(h));
00110                 transcribed = Transcribe.GetValues(member);
00111                 for (size_t i = 0; i < transcribed.size(); ++i)
00112                         modelfile << transcribed(i) << " ";
00113                 modelfile << endl;
00114         }
00115         //Population.PrintPopulation(cout);
00116         //cout << "Bestindex: " << Population.bestindex << endl << flush;
00117         //transcribed = Transcribe.GetValues(row(Population.Population,Population.bestindex));
00118         
00119 }

Generated on Mon Sep 15 12:54:34 2008 for GPLIB++ by  doxygen 1.5.5