24 #include <boost/bind.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/filesystem.hpp>
27 #include <boost/date_time/posix_time/posix_time.hpp>
37 using namespace gplib;
49 double InitTemperature = Configuration.
inittemp;
52 double avgcombfit = 0;
53 GA->DoIteration(0,
false);
56 avgcombfit = accumulate(GA->GetAvgFit().begin(), GA->GetAvgFit().end(),
58 InitTemperature = avgcombfit * abs(Configuration.
inittemp);
68 ifstream surfinfo(surfinfofile.c_str());
72 while (surfinfo.good())
75 surfinfo >> backazimuth >> inputdata >> avelratio;
86 int main(
int argc,
char *argv[])
88 boost::posix_time::ptime starttime =
89 boost::posix_time::microsec_clock::local_time();
90 string version =
"$Id: mtanisoga.cpp 1572 2007-12-17 18:20:43Z mmoorkamp $";
92 cout <<
"Program " << version << endl;
94 <<
"Genetic algorithm to jointly invert MT and SW data for 1D anisotropy"
96 cout <<
"look at anisogajoint.conf for configuration and setup " << endl;
97 cout <<
"The root name of log-files can be overwritten by using " << endl;
98 cout <<
"1dinvga newrootname " << endl;
104 std::ifstream conffile(
"anisogajoint.conf");
108 conffile.seekg(0, std::ios::beg);
111 conffile.seekg(0, std::ios::beg);
123 outputbase = argv[1];
130 ofstream distancefile;
131 ofstream fitstat((outputbase +
".ftst").c_str());
132 ofstream uniquepop((outputbase +
".unpop").c_str());
133 ofstream rankfile((outputbase +
".rank").c_str());
134 ofstream frontfile((outputbase +
".front").c_str());
135 ofstream bestfile((outputbase +
".best").c_str());
140 misfitfile.open((outputbase +
".msft").c_str());
141 valuefile.open((outputbase +
".vals").c_str());
142 probfile.open((outputbase +
".probs").c_str());
143 distancefile.open((outputbase +
".dist").c_str());
154 cerr <<
"Configuration of genes is not consistent ! ";
157 const int nparam = 7;
158 const int genes = nparam * JointConf.
thickbase.size();
159 const int popsize = GaConf.
popsize;
161 const int paramlength = genes / nparam;
166 misfitfile << popsize <<
" " << maxgen <<
" " << endl;
167 valuefile << genes <<
" " << popsize <<
" " << maxgen << endl;
177 basevalues.begin() + paramlength);
179 stepsizes.begin() + paramlength);
181 genesizes.begin() + paramlength);
184 basevalues.begin() + 2 * paramlength);
186 stepsizes.begin() + 2 * paramlength);
188 genesizes.begin() + 2 * paramlength);
191 basevalues.begin() + 3 * paramlength);
193 stepsizes.begin() + 3 * paramlength);
199 JointConf.
dstrikebase.end(), basevalues.begin() + 4
209 basevalues.begin() + 5 * paramlength);
211 stepsizes.begin() + 5 * paramlength);
213 genesizes.begin() + 5 * paramlength);
216 basevalues.begin() + 6 * paramlength);
218 stepsizes.begin() + 6 * paramlength);
220 genesizes.begin() + 6 * paramlength);
222 const int totalgenesize = accumulate(genesizes.begin(),
226 &GeneralPopulation::GetProbabilities, boost::ref(Population)),
227 boost::bind(&GeneralPopulation::GetCrowdingDistances, boost::ref(
230 double indmutationprob = 1.0 - std::pow(1.0
232 cout <<
"Individual Mutation set to: " << indmutationprob << endl;
239 if (MTConf.
mtfit ==
"ptensor")
243 boost::shared_ptr<PTensor1DMTObjective> PTObjective(
245 PTObjective->SetErrorLevel(MTConf.
phaseerror);
246 MTObjective = PTObjective;
252 boost::shared_ptr<Aniso1DMTObjective> AnisoObjective(
255 MTObjective = AnisoObjective;
260 boost::shared_ptr<MultiAnisoSurfaceWaveObjective>
265 boost::shared_ptr<MTAnisoRoughness> MTRegularization(
272 boost::shared_ptr<SWAnisoRoughness> SWRegularization(
277 SWRegularization->SetDeltaStrikeDiffWeight(
280 vector<pCGeneralObjective> ObjVector;
281 ObjVector.push_back(MTObjective);
282 ObjVector.push_back(MultiSurfaceObjective);
283 ObjVector.push_back(SWRegularization);
284 ObjVector.push_back(MTRegularization);
286 boost::shared_ptr<GeneralGA> GA;
287 if (GaConf.
gatype ==
"pareto")
290 boost::shared_ptr<ParetoGA> Pareto(
new ParetoGA(&Propagator,
291 &Population, &Transcribe, ObjVector));
294 else if (GaConf.
gatype ==
"anneal")
297 boost::shared_ptr<AnnealingGA> Annealing(
new AnnealingGA(
298 &Propagator, &Population, &Transcribe, ObjVector));
303 cerr <<
"Invalid GA type in configuration file." << endl;
304 cerr <<
" Has to be either 'pareto' or 'anneal' !" << endl;
307 GA->SetWeights(JointConf.
weights);
311 const int nmtparam = 4 * JointConf.
resbase.size();
312 std::vector<int> MTIndices, SurfaceWaveIndices;
313 generate_n(back_inserter(MTIndices), 2 * paramlength, IntSequence(0));
314 generate_n(back_inserter(MTIndices), paramlength, IntSequence(5
316 generate_n(back_inserter(MTIndices), paramlength, IntSequence(3
319 ParamIndices.push_back(MTIndices);
321 generate_n(back_inserter(SurfaceWaveIndices), 6 * paramlength,
322 IntSequence(paramlength));
323 ParamIndices.push_back(SurfaceWaveIndices);
324 ParamIndices.push_back(SurfaceWaveIndices);
325 ParamIndices.push_back(MTIndices);
327 GA->SetParameterIndices(ParamIndices);
328 GA->SetElitist(GaConf.
elitist);
333 Population.InitPop();
336 for (
int i = 0; i < maxgen; ++i)
339 GA->DoIteration(i, i == (maxgen - 1));
342 GA->PrintMisfit(misfitfile);
343 GA->PrintTranscribed(valuefile);
344 Population.PrintProbabilities(probfile);
345 Population.PrintDistances(distancefile);
347 GA->PrintBestMisfit(frontfile);
350 GA->PrintFitStat(fitstat);
356 tfitvec bestfit(ObjVector.size());
359 vector<int> BestIndices(GA->GetBestModelIndices());
360 const unsigned int noutmodels = GA->GetNBestmodels();
361 assert(BestIndices.size() == noutmodels);
362 const unsigned int nobjective = ObjVector.size();
364 for (
unsigned int i = 0; i < nobjective; ++i)
365 LocalParameters.at(i).resize(ParamIndices.at(i).size(),
false);
366 ofstream finalmodels((outputbase +
".final").c_str());
367 for (
unsigned int h = 0;
h < noutmodels; ++
h)
370 member = row(Population.GetPopulation(), BestIndices.at(
h));
373 transcribed = Transcribe.
GetValues(member);
374 GA->SetupParams(transcribed, LocalParameters);
376 for (
unsigned int f = 0;
f < nobjective; ++
f)
380 bestfit(
f) = ObjVector.at(
f)->CalcPerformance(
381 LocalParameters.at(
f));
387 if (FinalUnique.
Insert(bestfit, transcribed))
390 copy(bestfit.begin(), bestfit.end(), ostream_iterator<double> (
392 finalmodels <<
" " << bestcount << endl;
393 ostringstream filename;
394 filename <<
"best_" << bestcount;
395 cout <<
"Best: " <<
h <<
" Index : " << BestIndices.at(
h)
397 for (
unsigned int f = 0;
f < nobjective; ++
f)
398 cout << bestfit(
f) <<
" ";
399 cout <<
" Saved as : " << filename.str();
400 if (JointConf.
weights.at(0) > 0)
402 MTObjective->WriteModel(filename.str() +
"_mt.mod");
403 MTObjective->WritePlot(filename.str() +
"_mt.plot");
404 MTObjective->WriteData(filename.str());
407 if (JointConf.
weights.at(1) > 0)
409 MultiSurfaceObjective->WriteModel(filename.str()
411 MultiSurfaceObjective->WritePlot(filename.str()
413 MultiSurfaceObjective->WriteData(filename.str()
426 GA->PrintUniquePop(uniquepop);
429 boost::posix_time::ptime endtime =
430 boost::posix_time::microsec_clock::local_time();
431 std::cout <<
"Total Runtime: " << endtime - starttime << std::endl;
434 cerr << e.what() << endl;
std::vector< double > thickbase
void SetupMTFitParameters(const tConfObject &Configuration, C1DMTObjective &Objective)
ublas::vector< double > ttranscribed
std::vector< double > resbase
bool Insert(const tfitvec &fitness, const ttranscribed &popmember)
Caclulate the roughness for anisotropic MT models.
std::vector< int > avelsizes
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.
std::vector< double > avelbase
double deltastrikediffweight
AnnealingGA implements a genetic algorithm with an annealing style objective function.
void GetData(std::ifstream &instream)
virtual void GetData(const std::string filename)
read in data from file, determines format by ending
Minimize the misfit for several surface wave dispersion curves simultaneously.
std::vector< double > aresstep
std::vector< double > avelstep
void h(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
std::vector< double > strikestep
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< int > dstrikesizes
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
std::vector< double > weights
std::vector< double > aresbase
std::vector< double > dstrikebase
void GetData(const std::string &filename)
std::vector< int > ressizes
void PrintAll(std::ostream &output)
void AddMeasurement(const ParkSurfaceWaveData &Measured, const double back, const double avel)
void GetData(std::ifstream &instream)
std::vector< int > aressizes
void GetData(std::ifstream &instream)
int main(int argc, char *argv[])
Program to invert MT and SW data for 1D anisotropic structure with a genetic algorithm.
std::vector< double > velstep
std::vector< double > resstep
This class stores a single unique copy of each population member that is added to it...
void SetupAnnealingGA(boost::shared_ptr< GeneralGA > &GA, const GAConf Configuration)
This class implements the Gray code representation of a binary string and the corresponding transcrip...
std::vector< double > thickstep
void SetupSurfaceWaveObjective(const string surfinfofile, MultiAnisoSurfaceWaveObjective &SurfObjective)
ublas::vector< double > tfitvec
Implements a genetic algorithm based on the concept of pareto-optimality, best suited for multi-objec...
virtual void ReadAscii(const std::string &filename)
Read a file in general ascii format, i.e lines with period velocity each.
CLevanisoConf Configuration
std::vector< int > velsizes
Calculate the roughness for anisotropic SW models.
boost::shared_ptr< GeneralObjective > pCGeneralObjective
std::vector< int > strikesizes
ublas::vector< int > tsizev
void SetParams(const double InitT, const int AnnealG, const double AnnealR)
Set the parameters for the annealing process.
std::vector< double > velbase
The basic exception class for all errors that arise in gplib.
std::vector< int > thicksizes
std::vector< double > dstrikestep
std::vector< double > strikebase