9 #include "ParametricModel.h" 10 #include "ParametricModelPopulation.h" 11 #include "Chi2FitFigureOfMerit.h" 12 #include "GeneticAlgorithm.h" 54 int main(
int argc,
char **argv) {
62 std::cout <<
"Algorithm Configuration:" << std::endl
63 <<
" ==> nmc = " << (int)config.get(
"nmc") << std::endl
64 <<
" ==> acceptThreshold = " << (double)config.get(
"acceptThreshold") << std::endl
65 <<
" ==> mutateRate = " << (double)config.get(
"mutateRate") << std::endl
66 <<
" ==> mutateSize = " << (double)config.get(
"mutateSize") << std::endl
67 <<
" ==> maxGenerations = " << (int)config.get(
"maxGenerations") << std::endl
68 <<
" ==> populationSize = " << (int)config.get(
"populationSize") << std::endl;
73 int nmc = config.get(
"nmc");
74 double mean = config.get(
"mean");
75 double sigma = config.get(
"sigma");
77 double xmin = mean-5*sigma;
78 double xmax = mean+5*sigma;
80 TH1 *hData =
new TH1F(
"hData",
"", nbins, xmin, xmax);
81 double dx = (xmax-xmin)/nbins;
83 hData->GetXaxis()->SetTitle(
"x");
84 hData->GetYaxis()->SetTitle(
"Probability density");
85 for(
int mc=0; mc<nmc; mc++) {
86 hData->Fill(rnd.Gaus(mean, sigma), 1./(nmc*dx));
92 TF1 *likelihoodFit =
new TF1(
"likelihoodFit",
"gaus", xmin, xmax);
93 hData->Fit(likelihoodFit,
"NOQ");
100 std::vector<double> x(1);
101 for(
int bin=1; bin<=hData->GetNbinsX(); bin++) {
102 x[0] = hData->GetBinCenter(bin);
103 fom.
addData(x, hData->GetBinContent(bin), hData->GetBinError(bin));
113 TF1 *f =
new TF1(
"f",
"gaus", xmin, xmax);
114 f->SetParameter(0, 1./(sigma*sqrt(2*TMath::Pi())));
115 f->SetParameter(1, mean);
116 f->SetParameter(2, sigma);
117 f->SetParLimits(0, 0.001, 1);
118 f->SetParLimits(1, -10, 10);
119 f->SetParLimits(2, 0.001, 10);
120 f->SetParName(0,
"Constant");
121 f->SetParName(1,
"Mean");
122 f->SetParName(2,
"Sigma");
125 std::cout <<
"Input parameters:" << std::endl;
126 for(
int i=0; i<f->GetNpar(); i++) {
128 f->GetParLimits(i, pmin, pmax);
129 std::cout <<
" ==> " << f->GetParName(i) <<
" : " << f->GetParameter(i)
130 <<
" - range: [" << pmin <<
", " << pmax <<
"]" 144 gSystem->Exec(
"rm -rf figures");
145 gSystem->Exec(
"mkdir -p figures");
146 gStyle->SetOptStat(0);
147 TGraph *gScore =
new TGraph();
148 TGraph *gRMS =
new TGraph();
149 TCanvas *C_anim =
new TCanvas(
"C_anim",
"C_anim");
150 hData->SetMarkerStyle(20);
151 likelihoodFit->SetLineColor(4);
152 likelihoodFit->SetLineStyle(7);
154 TLegend *L_anim =
new TLegend(0.7, 0.7, 0.9, 0.9);
155 L_anim->AddEntry(hData,
"Data",
"lp");
156 L_anim->AddEntry(likelihoodFit,
"Likelihood fit",
"l");
157 L_anim->AddEntry(f,
"GA fit",
"l");
158 TLatex *lt_anim =
new TLatex();
165 if(config.get(
"runTests")) {
171 likelihoodFit->Draw(
"same");
175 lt_anim->SetText(xmin + (xmax-xmin)/10., hData->GetMaximum()*0.9, TString::Format(
"Generation: %d", alg.
getCurrentGeneration()));
179 C_anim->SaveAs(
"figures/C_anim.gif+25");
181 std::flush(std::cout);
184 if(config.get(
"runTests")) {
185 std::cout << std::endl;
195 <<
" ==> Best score is: " << bestModel->
getScore() << std::endl;
197 std::cout <<
"After GA fit: " << std::endl;
198 for(
int i=0; i<bestFormula->GetNpar(); i++) {
199 std::cout <<
" ==> " << f->GetParName(i) <<
" : " << bestFormula->GetParameter(i) << std::endl;
202 std::cout <<
"After Likelihood fit: " << std::endl;
203 for(
int i=0; i<likelihoodFit->GetNpar(); i++) {
204 std::cout <<
" ==> " << f->GetParName(i) <<
" : " << likelihoodFit->GetParameter(i) << std::endl;
207 TCanvas *C_fit =
new TCanvas(
"C_fit",
"C_fit");
210 likelihoodFit->Draw(
"same");
211 bestFormula->SetLineColor(2);
212 bestFormula->Draw(
"same");
214 lt_anim->SetText(xmin + (xmax-xmin)/10., hData->GetMaximum()*0.9, TString::Format(
"Generation: %d", alg.
getCurrentGeneration()));
216 C_fit->SaveAs(
"figures/C_fit.png");
217 C_fit->SaveAs(
"figures/C_fit.root");
219 if(config.get(
"runTests")) {
220 C_fit->SaveAs(
"figures/C_anim.gif++300");
222 TCanvas *C_Score =
new TCanvas(
"C_Score",
"C_Score");
223 C_Score->cd()->SetLogx();
224 C_Score->cd()->SetLogy();
225 gScore->SetLineColor(4);
226 gScore->SetLineWidth(2);
228 C_Score->SaveAs(
"figures/C_Score.png");
229 C_Score->SaveAs(
"figures/C_Score.root");
231 TCanvas *C_RMS =
new TCanvas(
"C_RMS",
"C_RMS");
232 C_RMS->cd()->SetLogx();
233 C_RMS->cd()->SetLogy();
234 gRMS->SetLineColor(4);
235 gRMS->SetLineWidth(2);
237 C_RMS->SaveAs(
"figures/C_RMS.png");
238 C_RMS->SaveAs(
"figures/C_RMS.root");
257 optparse::OptionParser parser = optparse::OptionParser().description(
"Point Cloud Analysis");
260 parser.add_option(
"-n",
"--nmc").action(
"store").dest(
"nmc").set_default(10000)
261 .help(
"Number of toy MC experiments used to build the dataset.");
264 parser.add_option(
"-m",
"--mean").action(
"store").dest(
"mean").set_default(1.5)
265 .help(
"Mean of the gaussian distribution used to generate the dataset.");
268 parser.add_option(
"-s",
"--sigma").action(
"store").dest(
"sigma").set_default(2.3)
269 .help(
"Width (sigma) of the gaussian distribution used to generate the dataset.");
272 parser.add_option(
"-a",
"--acceptThreshold").action(
"store").dest(
"acceptThreshold").set_default(0.85)
273 .help(
"Score threshold to accept a model as a final answer.");
276 parser.add_option(
"-R",
"--mutateRate").action(
"store").dest(
"mutateRate").set_default(0.01)
277 .help(
"Rate at which models are subjected to mutation.");
280 parser.add_option(
"-S",
"--mutateSize").action(
"store").dest(
"mutateSize").set_default(0.1)
281 .help(
"Relative size of the mutation whenever applied.");
284 parser.add_option(
"-G",
"--maxGenerations").action(
"store").dest(
"maxGenerations").set_default(10000)
285 .help(
"Maximum number of generations before aborting the optimization loop.");
288 parser.add_option(
"-G",
"--populationSize").action(
"store").dest(
"populationSize").set_default(500)
289 .help(
"Size of the population to be evolved.");
292 parser.add_option(
"-t",
"--runTests").action(
"store_true").dest(
"runTests").set_default(
false)
293 .help(
"Run tests alongside the main algorithm.");
295 config = parser.parse_args(argc, argv);
void setMutationSize(double relativeSize)
IModel * getBestFitted(int rank=0)
Implements a population of parametric models.
Copyright (C) 2010 Johannes Weißl jargon@molb.org License: your favourite BSD-style license...
void setMutateRate(double rate)
void setFigureOfMerit(IFigureOfMerit *fom)
void initialize(IPopulation *population)
int main(int argc, char **argv)
Main function.
void setNGenerationsMax(int generationsMax)
void parseCommandLine(Config &config, int argc, char **argv)
Prase command line arguments.
Class imlementing the Genetic Algorithm.
void setPopulationSize(int populationSize)
Class representing a model defined by a parametric function.
void setFormula(TF1 *formula)
int getCurrentGeneration()