22 #include <boost/bind.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/filesystem.hpp>
25 #include <boost/date_time/posix_time/posix_time.hpp>
29 #include "MTAnisoGAConf.h"
32 using namespace gplib;
45 double InitTemperature = Configuration.inittemp;
46 if (Configuration.inittemp < 0)
48 double avgcombfit = 0;
49 GA->DoIteration(0,
false);
52 avgcombfit = accumulate(GA->GetAvgFit().begin(), GA->GetAvgFit().end(),
54 InitTemperature = avgcombfit * abs(Configuration.inittemp);
57 AnnealGA->
SetParams(InitTemperature, Configuration.annealinggeneration,
58 Configuration.coolingratio);
62 int main(
int argc,
char *argv[])
64 boost::posix_time::ptime starttime =
65 boost::posix_time::microsec_clock::local_time();
66 string version =
"$Id: mtanisoga.cpp 1849 2010-05-07 11:53:45Z mmoorkamp $";
68 cout <<
"Program " << version << endl;
69 cout <<
"Genetic algorithm to jointly invert MT data for 1D anisotropy"
71 cout <<
"look at mtanisoga.conf for configuration and setup " << endl;
72 cout <<
"The root name of log-files can be overwritten by using " << endl;
73 cout <<
"1dinvga newrootname " << endl;
80 Configuration.GetData(
"mtanisoga.conf");
90 outputbase = Configuration.outputbase;
95 ofstream distancefile;
96 ofstream fitstat((outputbase +
".ftst").c_str());
97 ofstream uniquepop((outputbase +
".unpop").c_str());
98 ofstream rankfile((outputbase +
".rank").c_str());
99 ofstream frontfile((outputbase +
".front").c_str());
100 ofstream bestfile((outputbase +
".best").c_str());
102 if (Configuration.verbose)
104 misfitfile.open((outputbase +
".msft").c_str());
105 valuefile.open((outputbase +
".vals").c_str());
106 probfile.open((outputbase +
".probs").c_str());
107 distancefile.open((outputbase +
".dist").c_str());
110 if ((Configuration.thickbase.size() != Configuration.resbase.size())
111 || (Configuration.thickbase.size()
112 != Configuration.anisobase.size())
113 || (Configuration.thickbase.size()
114 != Configuration.strikebase.size()))
116 cerr <<
"Configuration of genes is not consistent ! ";
119 const int nparam = 4;
120 const int genes = 4 * Configuration.thickbase.size();
121 const int popsize = Configuration.popsize;
122 const int maxgen = Configuration.generations;
123 const int paramlength = genes / nparam;
128 misfitfile << popsize <<
" " << maxgen <<
" " << endl;
129 valuefile << genes <<
" " << popsize <<
" " << maxgen << endl;
131 copy(Configuration.resbase.begin(), Configuration.resbase.end(),
133 copy(Configuration.resstep.begin(), Configuration.resstep.end(),
135 copy(Configuration.ressizes.begin(), Configuration.ressizes.end(),
138 copy(Configuration.thickbase.begin(), Configuration.thickbase.end(),
139 basevalues.begin() + paramlength);
140 copy(Configuration.thickstep.begin(), Configuration.thickstep.end(),
141 stepsizes.begin() + paramlength);
142 copy(Configuration.thicksizes.begin(), Configuration.thicksizes.end(),
143 genesizes.begin() + paramlength);
145 copy(Configuration.anisobase.begin(), Configuration.anisobase.end(),
146 basevalues.begin() + 2 * paramlength);
147 copy(Configuration.anisostep.begin(), Configuration.anisostep.end(),
148 stepsizes.begin() + 2 * paramlength);
149 copy(Configuration.anisosizes.begin(), Configuration.anisosizes.end(),
150 genesizes.begin() + 2 * paramlength);
152 copy(Configuration.strikebase.begin(), Configuration.strikebase.end(),
153 basevalues.begin() + 3 * paramlength);
154 copy(Configuration.strikestep.begin(), Configuration.strikestep.end(),
155 stepsizes.begin() + 3 * paramlength);
156 copy(Configuration.strikesizes.begin(),
157 Configuration.strikesizes.end(), genesizes.begin() + 3
160 const int totalgenesize = accumulate(genesizes.begin(),
164 &GeneralPopulation::GetProbabilities, boost::ref(Population)),
165 boost::bind(&GeneralPopulation::GetCrowdingDistances, boost::ref(
168 double indmutationprob = 1.0 - std::pow(1.0 - Configuration.mutationprob, 1.
170 cout <<
"Individual Mutation set to: " << indmutationprob << endl;
171 Propagator.
SetParams(indmutationprob, Configuration.crossoverprob);
180 if (Configuration.mtfit ==
"ptensor")
183 PTData.
GetData(Configuration.ptensordata);
184 boost::shared_ptr<PTensor1DMTObjective> PTObjective(
186 PTObjective->SetErrorLevel(Configuration.phaseerror);
187 MTObjective = PTObjective;
192 MTData.
GetData(Configuration.mtinputdata);
193 boost::shared_ptr<Aniso1DMTObjective> AnisoObjective(
196 MTObjective = AnisoObjective;
198 MTObjective->SetFitExponent(Configuration.mtfitexponent);
202 boost::shared_ptr<MTAnisoRoughness> MTRegularization(
204 MTRegularization->SetCondDiffWeight(Configuration.conddiffweight);
205 MTRegularization->SetAnisotropyWeight(Configuration.anisotropyweight);
206 MTRegularization->SetStrikeDiffWeight(Configuration.strikediffweight);
207 vector<pCGeneralObjective> ObjVector;
208 ObjVector.push_back(MTObjective);
209 ObjVector.push_back(MTRegularization);
211 boost::shared_ptr<GeneralGA> GA;
212 if (Configuration.gatype ==
"pareto")
215 boost::shared_ptr<ParetoGA> Pareto(
new ParetoGA(&Propagator,
216 &Population, &Transcribe, ObjVector, Configuration.threads));
219 else if (Configuration.gatype ==
"anneal")
222 boost::shared_ptr<AnnealingGA> Annealing(
new AnnealingGA(
223 &Propagator, &Population, &Transcribe, ObjVector,
224 Configuration.threads));
229 cerr <<
"Invalid GA type in configuration file." << endl;
230 cerr <<
" Has to be either 'pareto' or 'anneal' !" << endl;
233 GA->SetWeights(Configuration.weights);
236 const int nmtparam = 4 * Configuration.resbase.size();
237 std::vector<int> LocalIndices;
238 generate_n(back_inserter(LocalIndices), nmtparam, IntSequence(0));
239 ParamIndices.push_back(LocalIndices);
240 ParamIndices.push_back(LocalIndices);
242 GA->SetParameterIndices(ParamIndices);
243 GA->SetElitist(Configuration.elitist);
248 Population.InitPop();
251 for (
int i = 0; i < maxgen; ++i)
253 GA->DoIteration(i, i == (maxgen - 1));
254 if (Configuration.verbose)
256 GA->PrintMisfit(misfitfile);
257 GA->PrintTranscribed(valuefile);
258 Population.PrintProbabilities(probfile);
259 Population.PrintDistances(distancefile);
261 GA->PrintBestMisfit(frontfile);
264 GA->PrintFitStat(fitstat);
270 tfitvec bestfit(ObjVector.size());
273 vector<int> BestIndices(GA->GetBestModelIndices());
274 const unsigned int noutmodels = GA->GetNBestmodels();
275 assert(BestIndices.size() == noutmodels);
276 const unsigned int nobjective = ObjVector.size();
278 for (
unsigned int i = 0; i < nobjective; ++i)
279 LocalParameters.at(i).resize(ParamIndices.at(i).size(),
false);
280 ofstream finalmodels((outputbase +
".final").c_str());
281 for (
unsigned int h = 0;
h < noutmodels; ++
h)
283 member = row(Population.GetPopulation(), BestIndices.at(
h));
286 transcribed = Transcribe.
GetValues(member);
287 GA->SetupParams(transcribed, LocalParameters);
289 for (
unsigned int f = 0;
f < nobjective; ++
f)
291 if (Configuration.weights.at(
f) > 0)
292 bestfit(
f) = ObjVector.at(
f)->CalcPerformance(
293 LocalParameters.at(
f));
299 if (FinalUnique.
Insert(bestfit, transcribed))
301 copy(bestfit.begin(), bestfit.end(), ostream_iterator<double> (
303 finalmodels <<
" " << bestcount << endl;
304 ostringstream filename;
305 filename <<
"best_" << bestcount;
306 cout <<
"Best: " <<
h <<
" Index : " << BestIndices.at(
h)
308 for (
unsigned int f = 0;
f < nobjective; ++
f)
309 cout << bestfit(
f) <<
" ";
310 cout <<
" Saved as : " << filename.str();
312 MTObjective->WriteModel(filename.str() +
"_mt.mod");
313 MTObjective->WritePlot(filename.str() +
"_mt.plot");
314 MTObjective->WriteData(filename.str());
323 if (Configuration.verbose)
325 GA->PrintUniquePop(uniquepop);
328 boost::posix_time::ptime endtime =
329 boost::posix_time::microsec_clock::local_time();
330 std::cout <<
"Total Runtime: " << endtime - starttime << std::endl;
333 cerr << e.what() << endl;
void SetupMTFitParameters(const tConfObject &Configuration, C1DMTObjective &Objective)
ublas::vector< double > ttranscribed
bool Insert(const tfitvec &fitness, const ttranscribed &popmember)
Caclulate the roughness for anisotropic MT models.
boost::shared_ptr< GeneralObjective > pCGeneralObjective
void SetupAnnealingGA(boost::shared_ptr< GeneralGA > &GA, const MTAnisoGAConf Configuration)
This is a special objective function to fit phase tensor MT data.
Implements binary tournament selection for genetic algorithms.
void f(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
A population that is encoded as a simple binary string.
AnnealingGA implements a genetic algorithm with an annealing style objective function.
virtual void GetData(const std::string filename)
read in data from file, determines format by ending
void h(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
void SetParams(const double mutation, const double crossover)
boost::shared_ptr< PlottableObjective > MTObjective
virtual ttranscribed GetValues(const tpopmember &member)
Re-Implements the function from BinaryTranscribe.
std::vector< gplib::rvec > tparamvector
We provide some typedefs that are used in other parts as well.
This is the standard propagation class that generates a new population from the old one...
The class MTStation is used to store the transfer functions and related information for a MT-site...
ublas::vector< bool > tpopmember
std::vector< std::vector< int > > tparamindv
int main(int argc, char *argv[])
Program to invert MT data for 1D anisotropic structure with a genetic algorithm.
void GetData(const std::string &filename)
void PrintAll(std::ostream &output)
This class stores a single unique copy of each population member that is added to it...
This class implements the Gray code representation of a binary string and the corresponding transcrip...
ublas::vector< double > tfitvec
Implements a genetic algorithm based on the concept of pareto-optimality, best suited for multi-objec...
CLevanisoConf Configuration
ublas::vector< int > tsizev
void SetParams(const double InitT, const int AnnealG, const double AnnealR)
Set the parameters for the annealing process.
The basic exception class for all errors that arise in gplib.