GeneticAlgoithm
Implementation of the genetic algorithm
testParentSelection.C
Go to the documentation of this file.
1 /**
2  * @file
3  * @defgroup testParentSelection Macro testParentSelection
4  *
5  * @brief Test Parent rank-based selection.
6  *
7  * @b Objective: test the algorithm to select parents based on the ranking.
8  * The probability to be selected should show a linear dependence on the rank.
9  *
10  * To run this macro using ROOT:
11  * > root -l testParentSelection.C+
12  *
13  * <b>Output example:</b>
14  *
15  * <table class="image" align="center">
16  * <caption align="bottom" id="Fig1"> Parent selection probability as function of parent's rank. </caption>
17  * <tr><td><img src="../../figures/C_ParentProb.png" alt="Test Parent Selection."/></td></tr>
18  * </table>
19  *
20  * @{
21  */
22 
23 #include <iostream>
24 
25 #include <TRandom3.h>
26 #include <TH1.h>
27 #include <TCanvas.h>
28 #include <TStyle.h>
29 #include <TF1.h>
30 #include <TLegend.h>
31 
32 /**
33  * @brief Implements the parent selection criteria.
34  */
35 void selectParents(TRandom3 *random, int &p1, int &p2, int N)
36 {
37 
38  int f1 = 0;
39  int f2 = 0;
40  do {
41  f1 = random->Integer(N);
42  p1 = random->Integer(N);
43  }while(p1 > f1);
44  do{
45  f2 = random->Integer(N);
46  p2 = random->Integer(N);
47  }
48  while(p1 == p2 || p2 > f2);
49 }
50 
51 /**
52  * @brief Tests the parent selection criteria.
53  *
54  * Run a toy simulation and measure the selection probability as function of the rank.
55  */
57 
58  std::cout << "Hello" << std::endl;
59 
60  // Our population size
61  int populationSize = 500;
62 
63  // Initialize a pseudo-random number generator
64  TRandom3 *rnd = new TRandom3(1234);
65 
66  // Initialize a histogram to store the probability density function
67  int nbins = 100;
68  TH1 *hProb = new TH1F("hProb", "", nbins, 0, populationSize);
69 
70  // Number of experiments
71  int nmc = 100000;
72 
73  // Run toy simulation
74  for(int mc=0; mc<nmc; mc++) {
75  int p1, p2;
76  selectParents(rnd, p1, p2, populationSize);
77  hProb->Fill(p1);
78  hProb->Fill(p2);
79  }
80 
81  // Scale the histogram to convert it into a PDF
82  hProb->Scale(1./(2.*nmc*populationSize/nbins));
83 
84  // Plot it.
85  gStyle->SetOptStat(0); // Don't show stat box.
86  TString cname = "C_ParentProb";
87  TCanvas *C = new TCanvas(cname, cname);
88  C->cd()->SetGrid();
89  hProb->SetLineColor(1);
90  hProb->SetMarkerStyle(20);
91  hProb->SetMarkerSize(0.8);
92  hProb->SetLineWidth(2);
93  hProb->GetXaxis()->SetTitle("Rank");
94  hProb->GetYaxis()->SetTitle("Probability density");
95  hProb->GetXaxis()->SetTitleSize(0.05);
96  hProb->GetYaxis()->SetTitleSize(0.05);
97  hProb->GetXaxis()->SetTitleOffset(0.9);
98  hProb->GetYaxis()->SetTitleOffset(0.9);
99  hProb->Draw();
100 
101  // Fit it with a linear function
102  TF1 *fLinear = new TF1("fLinear", "pol1", 0, populationSize);
103  hProb->Fit(fLinear, "NOQ");
104  fLinear->SetLineColor(2);
105  fLinear->Draw("same");
106 
107  // Add a legend
108  TLegend *L = new TLegend(0.6,0.7,0.89,0.89);
109  L->AddEntry(hProb, "Measured PDF", "lp");
110  L->AddEntry(fLinear, "Linear fit:", "l");
111  L->AddEntry("", TString::Format("y = %.2g x + %.2g", fLinear->GetParameter(1), fLinear->GetParameter(0)), "");
112  L->Draw();
113 
114  // Save the plot
115  C->SaveAs(cname+".png");
116  C->SaveAs(cname+".root");
117 }
void testParentSelection()
Tests the parent selection criteria.
void selectParents(TRandom3 *random, int &p1, int &p2, int N)
Implements the parent selection criteria.