testmultiga.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 "BinaryTranscribe.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 = 20;
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 = 20;
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         BinaryTranscribe Transcribe(basevalues,stepsizes,genesizes);
00056         StandardPropagation Propagator(&Select,&Population,&Random);
00057         tprobabilityv probabilities(popsize);
00058         double sum=0;
00059         
00060         const double mutationprob = 0.1;
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                 
00088                 GA.DoIteration(i,i==(maxgen -1));
00089                 cout << "Misfit: " << endl;
00090                 GA.PrintMisfit(cout);
00091                 GA.PrintRanks(rankfile);
00092         }
00093         ofstream frontfile("test.front");
00094         GA.PrintFront(frontfile);
00095         ofstream modelfile("test.model");
00096         for (int j = 0; j < Population.GetPopsize(); ++j)
00097         {
00098                 for (int k = 0; k < nparam; ++k)
00099                 {
00100                         modelfile << Transcribe.GetValues(row(Population.GetPopulation(),j))(k) << " ";
00101                 }
00102                 modelfile << endl;
00103         }
00104         //Population.PrintPopulation(cout);
00105         //cout << "Bestindex: " << Population.bestindex << endl << flush;
00106         //transcribed = Transcribe.GetValues(row(Population.Population,Population.bestindex));
00107         
00108 }

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