mtanisoga.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <algorithm>
00004 #include <numeric>
00005 #include <sstream>
00006 #include <string>
00007 #include "SimpleSelect.h"
00008 #include "BinaryTournamentSelect.h"
00009 #include "UniformRNG.h"
00010 #include "gentypes.h"
00011 #include "BinaryPopulation.h"
00012 #include "StandardPropagation.h"
00013 #include "GrayTranscribe.h"
00014 #include "AnnealingGA.h"
00015 #include "ParetoGA.h"
00016 #include "Aniso1DMTObjective.h"
00017 #include "PTensor1DMTObjective.h"
00018 #include "MTStation.h"
00019 #include "C1dInvGaConf.h"
00020 #include "Adaptors.h"
00021 #include "MTFitSetup.h"
00022 #include "PTensorMTStation.h"
00023 #include <boost/bind.hpp>
00024 #include <boost/shared_ptr.hpp>
00025 #include <boost/filesystem.hpp>
00026 #include <boost/date_time/posix_time/posix_time.hpp>
00027 #include <complex>
00028 #include "Util.h"
00029 #include "MTAnisoRoughness.h"
00030 #include "MTAnisoGAConf.h"
00031 
00032 using namespace std;
00033 using namespace gplib;
00034 
00035 enum tgatype
00036   {
00037   pareto, anneal
00038   };
00039 typedef boost::shared_ptr<GeneralObjective> pCGeneralObjective;
00040 
00041 
00042 
00043 void SetupAnnealingGA(boost::shared_ptr<GeneralGA> &GA,
00044     const MTAnisoGAConf Configuration)
00045   {
00046     double InitTemperature = Configuration.inittemp;
00047     if (Configuration.inittemp < 0)
00048       {
00049         double avgcombfit = 0;
00050         GA->DoIteration(0, false);
00051         /*for (int i = 0; i < GA->GetAvgFit().size(); ++i)
00052          avgcombfit += GA->GetAvgFit().at(i);*/
00053         avgcombfit = accumulate(GA->GetAvgFit().begin(), GA->GetAvgFit().end(),
00054             0.0);
00055         InitTemperature = avgcombfit * abs(Configuration.inittemp);
00056       }
00057     AnnealingGA *AnnealGA = dynamic_cast<AnnealingGA*> (GA.get());
00058     AnnealGA->SetParams(InitTemperature, Configuration.annealinggeneration,
00059         Configuration.coolingratio);
00060   }
00061 
00062 //! Program to invert MT data for 1D anisotropic structure with a genetic algorithm
00063 int main(int argc, char *argv[])
00064   {
00065     boost::posix_time::ptime starttime =
00066         boost::posix_time::microsec_clock::local_time();
00067     string version = "$Id: mtanisoga.cpp 1844 2010-04-12 11:34:25Z mmoorkamp $";
00068     cout << endl << endl;
00069     cout << "Program " << version << endl;
00070     cout << "Genetic algorithm to jointly invert MT data for 1D anisotropy"
00071         << endl;
00072     cout << "look at mtanisoga.conf for configuration and setup " << endl;
00073     cout << "The root name of log-files can be overwritten by using " << endl;
00074     cout << "1dinvga newrootname " << endl;
00075 
00076     MTAnisoGAConf Configuration;
00077 
00078     UniformRNG Random;
00079     try
00080       {
00081         Configuration.GetData("mtanisoga.conf");
00082 
00083         ttranscribed transcribed;
00084         tgatype GAtype = pareto;
00085 
00086         string outputbase;
00087 
00088         if (argc > 1)
00089           outputbase = argv[1];
00090         else
00091           outputbase = Configuration.outputbase;
00092 
00093         ofstream misfitfile;
00094         ofstream valuefile;
00095         ofstream probfile;
00096         ofstream distancefile;
00097         ofstream fitstat((outputbase + ".ftst").c_str());
00098         ofstream uniquepop((outputbase + ".unpop").c_str());
00099         ofstream rankfile((outputbase + ".rank").c_str());
00100         ofstream frontfile((outputbase + ".front").c_str());
00101         ofstream bestfile((outputbase + ".best").c_str());
00102 
00103         if (Configuration.verbose) // in most cases we do not need all of these values
00104           {
00105             misfitfile.open((outputbase + ".msft").c_str());
00106             valuefile.open((outputbase + ".vals").c_str());
00107             probfile.open((outputbase + ".probs").c_str());
00108             distancefile.open((outputbase + ".dist").c_str());
00109           }
00110 
00111         if ((Configuration.thickbase.size() != Configuration.resbase.size())
00112             || (Configuration.thickbase.size()
00113                 != Configuration.anisobase.size())
00114             || (Configuration.thickbase.size()
00115                 != Configuration.strikebase.size()))
00116           {
00117             cerr << "Configuration of genes is not consistent ! ";
00118             return 100;
00119           }
00120         const int nparam = 4;
00121         const int genes = 4 * Configuration.thickbase.size();
00122         const int popsize = Configuration.popsize;
00123         const int maxgen = Configuration.generations;
00124         const int paramlength = genes / nparam;
00125         ttranscribed basevalues(genes);
00126         ttranscribed stepsizes(genes);
00127         tsizev genesizes(genes);
00128 
00129         misfitfile << popsize << " " << maxgen << " " << endl;
00130         valuefile << genes << " " << popsize << " " << maxgen << endl;
00131 
00132         copy(Configuration.resbase.begin(), Configuration.resbase.end(),
00133             basevalues.begin());
00134         copy(Configuration.resstep.begin(), Configuration.resstep.end(),
00135             stepsizes.begin());
00136         copy(Configuration.ressizes.begin(), Configuration.ressizes.end(),
00137             genesizes.begin());
00138 
00139         copy(Configuration.thickbase.begin(), Configuration.thickbase.end(),
00140             basevalues.begin() + paramlength);
00141         copy(Configuration.thickstep.begin(), Configuration.thickstep.end(),
00142             stepsizes.begin() + paramlength);
00143         copy(Configuration.thicksizes.begin(), Configuration.thicksizes.end(),
00144             genesizes.begin() + paramlength);
00145 
00146         copy(Configuration.anisobase.begin(), Configuration.anisobase.end(),
00147             basevalues.begin() + 2 * paramlength);
00148         copy(Configuration.anisostep.begin(), Configuration.anisostep.end(),
00149             stepsizes.begin() + 2 * paramlength);
00150         copy(Configuration.anisosizes.begin(), Configuration.anisosizes.end(),
00151             genesizes.begin() + 2 * paramlength);
00152 
00153         copy(Configuration.strikebase.begin(), Configuration.strikebase.end(),
00154             basevalues.begin() + 3 * paramlength);
00155         copy(Configuration.strikestep.begin(), Configuration.strikestep.end(),
00156             stepsizes.begin() + 3 * paramlength);
00157         copy(Configuration.strikesizes.begin(),
00158             Configuration.strikesizes.end(), genesizes.begin() + 3
00159                 * paramlength);
00160 
00161         const int totalgenesize = accumulate(genesizes.begin(),
00162             genesizes.end(), 0);
00163         BinaryPopulation Population(popsize, totalgenesize, Random, true);
00164         BinaryTournamentSelect Select(Random, boost::bind(
00165             &GeneralPopulation::GetProbabilities, boost::ref(Population)),
00166             boost::bind(&GeneralPopulation::GetCrowdingDistances, boost::ref(
00167                 Population)));
00168         StandardPropagation Propagator(&Select, &Population, &Random);
00169         double indmutationprob = 1.0 - std::pow(1.0 - Configuration.mutationprob, 1.
00170             / totalgenesize); // we specify the overall probability for mutation in config file
00171         cout << "Individual Mutation set to: " << indmutationprob << endl;
00172         Propagator.SetParams(indmutationprob, Configuration.crossoverprob);
00173         GrayTranscribe Transcribe(basevalues, stepsizes, genesizes);
00174 
00175         //read in data
00176 
00177 
00178         //setup MT Objective function
00179         boost::shared_ptr<PlottableObjective> MTObjective;
00180 
00181         if (Configuration.mtfit == "ptensor")
00182           {
00183             PTensorMTStation PTData;
00184             PTData.GetData(Configuration.ptensordata);
00185             boost::shared_ptr<PTensor1DMTObjective> PTObjective(
00186                 new PTensor1DMTObjective(PTData));
00187             PTObjective->SetErrorLevel(Configuration.phaseerror);
00188             MTObjective = PTObjective;
00189           }
00190         else
00191           {
00192             MTStation MTData;
00193             MTData.GetData(Configuration.mtinputdata);
00194             boost::shared_ptr<Aniso1DMTObjective> AnisoObjective(
00195                 new Aniso1DMTObjective(MTData));
00196             SetupMTFitParameters(Configuration, *AnisoObjective);
00197             MTObjective = AnisoObjective;
00198           }
00199         MTObjective->SetFitExponent(Configuration.mtfitexponent);
00200 
00201         //setup regularization
00202 
00203         boost::shared_ptr<MTAnisoRoughness> MTRegularization(
00204             new MTAnisoRoughness());
00205         MTRegularization->SetCondDiffWeight(Configuration.conddiffweight);
00206         MTRegularization->SetAnisotropyWeight(Configuration.anisotropyweight);
00207         MTRegularization->SetStrikeDiffWeight(Configuration.strikediffweight);
00208         vector<pCGeneralObjective> ObjVector;
00209         ObjVector.push_back(MTObjective);
00210         ObjVector.push_back(MTRegularization);
00211 
00212         boost::shared_ptr<GeneralGA> GA;
00213         if (Configuration.gatype == "pareto")
00214           {
00215             GAtype = pareto;
00216             boost::shared_ptr<ParetoGA> Pareto(new ParetoGA(&Propagator,
00217                 &Population, &Transcribe, ObjVector, Configuration.threads));
00218             GA = Pareto;
00219           }
00220         else if (Configuration.gatype == "anneal")
00221           {
00222             GAtype = anneal;
00223             boost::shared_ptr<AnnealingGA> Annealing(new AnnealingGA(
00224                 &Propagator, &Population, &Transcribe, ObjVector,
00225                 Configuration.threads));
00226             GA = Annealing;
00227           }
00228         else
00229           {
00230             cerr << "Invalid GA type in configuration file." << endl;
00231             cerr << " Has to be either 'pareto' or 'anneal' !" << endl;
00232             return 100;
00233           }
00234         GA->SetWeights(Configuration.weights);
00235         //Setting up indices for forward modelling
00236         GeneralGA::tparamindv ParamIndices;
00237         const int nmtparam = 4 * Configuration.resbase.size();
00238         std::vector<int> LocalIndices;
00239         generate_n(back_inserter(LocalIndices), nmtparam, IntSequence(0)); //generate Indices 0 .. nmtparam for MT Objective function
00240         ParamIndices.push_back(LocalIndices);//both data misfit and regularization
00241         ParamIndices.push_back(LocalIndices); //work on all parts of the model vector
00242 
00243         GA->SetParameterIndices(ParamIndices);
00244         GA->SetElitist(Configuration.elitist);
00245         //we have to do a few things that are specific to the type of genetic algorithm
00246         if (GAtype == anneal)
00247           {
00248             SetupAnnealingGA(GA, Configuration);
00249             Population.InitPop();
00250           }
00251         //now comes the core loop
00252         for (int i = 0; i < maxgen; ++i) //iterate over the specified number of generattions
00253           {
00254             GA->DoIteration(i, i == (maxgen - 1)); // pass iteration number to GA and tell whether it's the final iteration
00255             if (Configuration.verbose) // in most cases we do not need all of these values
00256               {
00257                 GA->PrintMisfit(misfitfile);
00258                 GA->PrintTranscribed(valuefile);
00259                 Population.PrintProbabilities(probfile);
00260                 Population.PrintDistances(distancefile);
00261               }
00262             GA->PrintBestMisfit(frontfile);
00263 
00264             fitstat << i << " ";
00265             GA->PrintFitStat(fitstat);
00266           }
00267 
00268         //the following is to organize the output of the best models
00269         int bestcount = 0;
00270         UniquePop FinalUnique; //we only want to write each model once, even if it is several times in the population
00271         tfitvec bestfit(ObjVector.size());
00272         tpopmember member(Population.GetGenesize());
00273 
00274         vector<int> BestIndices(GA->GetBestModelIndices()); // store the indices of the best models
00275         const unsigned int noutmodels = GA->GetNBestmodels(); // and how many of them we have
00276         assert(BestIndices.size() == noutmodels);
00277         const unsigned int nobjective = ObjVector.size();
00278         GeneralGA::tparamvector LocalParameters(nobjective);
00279         for (unsigned int i = 0; i < nobjective; ++i)
00280           LocalParameters.at(i).resize(ParamIndices.at(i).size(), false);
00281         ofstream finalmodels((outputbase + ".final").c_str());
00282         for (unsigned int h = 0; h < noutmodels; ++h) //for each best model
00283           {
00284             member = row(Population.GetPopulation(), BestIndices.at(h)); //get the genetic string
00285 
00286 
00287             transcribed = Transcribe.GetValues(member); //transcribe the genetic string to model parameters
00288             GA->SetupParams(transcribed, LocalParameters); //copy the right parameters for each objective function
00289 
00290             for (unsigned int f = 0; f < nobjective; ++f) //calculate the performance for each function
00291               {
00292                 if (Configuration.weights.at(f) > 0)
00293                   bestfit(f) = ObjVector.at(f)->CalcPerformance(
00294                       LocalParameters.at(f));
00295                 else
00296                   bestfit(f) = 0;
00297 
00298               }
00299 
00300             if (FinalUnique.Insert(bestfit, transcribed)) //if we can insert it, it is a new model that we didn't output yet
00301               {
00302                 copy(bestfit.begin(), bestfit.end(), ostream_iterator<double> (
00303                     finalmodels, " ")); //print the misfits in the .final file
00304                 finalmodels << "  " << bestcount << endl; // write the index in the last column for easier identification
00305                 ostringstream filename;
00306                 filename << "best_" << bestcount;
00307                 cout << "Best: " << h << " Index : " << BestIndices.at(h)
00308                     << " "; //write some info to screen
00309                 for (unsigned int f = 0; f < nobjective; ++f)
00310                   cout << bestfit(f) << " ";
00311                 cout << " Saved as : " << filename.str();
00312 
00313                 MTObjective->WriteModel(filename.str() + "_mt.mod"); //write model, plotting file and synthetic data
00314                 MTObjective->WritePlot(filename.str() + "_mt.plot");
00315                 MTObjective->WriteData(filename.str());
00316 
00317                 filename.str("");
00318                 bestcount++;
00319                 cout << endl;
00320               }
00321 
00322           }
00323 
00324         if (Configuration.verbose) // in most cases we do not need all of these values
00325           {
00326             GA->PrintUniquePop(uniquepop);
00327             FinalUnique.PrintAll(bestfile);
00328           }
00329         boost::posix_time::ptime endtime =
00330             boost::posix_time::microsec_clock::local_time();
00331         std::cout << "Total Runtime: " << endtime - starttime << std::endl;
00332       } catch (FatalException &e)
00333       {
00334         cerr << e.what() << endl;
00335         return -1;
00336       }
00337   }

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8