GPLIB++
GeneralPopulation.cpp
Go to the documentation of this file.
1 #include "GeneralPopulation.h"
2 #include "VecMat.h"
3 using namespace std;
4 namespace gplib
5  {
6  GeneralPopulation::GeneralPopulation(const int popsize, const int genesize) :
7  Probabilities(popsize), CrowdingDistances(popsize), Population(popsize,
8  genesize)
9  {
10  OldStored = false;
11  }
12 
14  {
15  }
16 
18  OldPopulation(Old.OldPopulation), Probabilities(Old.Probabilities),
19  CrowdingDistances(Old.CrowdingDistances), Population(Old.Population)
20  {
21  OldStored = Old.OldStored;
22  }
23 
25  const tpopulation &SecondHalf):
26  OldPopulation(FirstHalf.size1() + SecondHalf.size1(), FirstHalf.size2()),
27  Probabilities(FirstHalf.size1() + SecondHalf.size1()),
28  CrowdingDistances(FirstHalf.size1() + SecondHalf.size1()),
29  Population(FirstHalf.size1() + SecondHalf.size1(), FirstHalf.size2())
30  {
31  OldStored = false;
32  ublas::matrix_range<tpopulation> PopFirstHalf(Population, ublas::range(
33  0, FirstHalf.size1()), ublas::range(0, FirstHalf.size2()));
34  ublas::matrix_range<tpopulation> PopSecondHalf(Population,
35  ublas::range(FirstHalf.size1(), FirstHalf.size1()
36  + SecondHalf.size1()), ublas::range(0, FirstHalf.size2()));
37  PopFirstHalf = FirstHalf;
38  PopSecondHalf = SecondHalf;
39  }
40 
42  const GeneralPopulation &source)
43  {
44  if (this != &source)
45  {
46  OldPopulation = source.OldPopulation;
47  Population = source.Population;
48  Probabilities = source.Probabilities;
49  CrowdingDistances = source.CrowdingDistances;
50  }
51  return *this;
52  }
53 
54  void GeneralPopulation::PrintPopulation(ostream &output) const
55  {
56  const int popsize = Population.size1();
57  const int genesize = Population.size2();
58  int i, j;
59  output << popsize << " " << genesize << endl;
60  for (i = 0; i < popsize; ++i)
61  {
62  for (j = 0; j < genesize; ++j)
63  output << Population(i, j) << " ";
64  output << endl;
65  }
66  }
67  void GeneralPopulation::ResizePop(const int popsize, const int genesize)
68  {
69  OldPopulation.resize(popsize, genesize);
70  Population.resize(popsize, genesize);
71  Probabilities.resize(popsize);
72  CrowdingDistances.resize(popsize);
73  }
74 
75  void GeneralPopulation::ReadPopulation(std::istream &input)
76  {
77  unsigned int popsize, genesize, dummy;
78  unsigned int i, j;
79 
80  input >> popsize >> genesize;
81  ResizePop(popsize, genesize);
82  for (i = 0; i < popsize; ++i)
83  {
84  for (j = 0; j < genesize; ++j)
85  {
86  input >> dummy;
87  Population(i, j) = (dummy == 1);
88  }
89  }
90  }
91  void GeneralPopulation::PrintProbabilities(std::ostream &output) const
92  {
93  for (unsigned int i = 0; i < Probabilities.size(); ++i)
94  {
95  output << Probabilities(i) << " ";
96  }
97  output << endl;
98  }
99 
100  void GeneralPopulation::PrintDistances(std::ostream &output) const
101  {
102  for (unsigned int i = 0; i < CrowdingDistances.size(); ++i)
103  {
104  output << CrowdingDistances(i) << " ";
105  }
106  output << endl;
107  }
108  }
The base class for the population of a genetic algorithm, implements storage and access functions...
tpopulation Population
The population of the current iteration.
void PrintProbabilities(std::ostream &output) const
GeneralPopulation(const int popsize, const int genesize)
void PrintDistances(std::ostream &output) const
void ReadPopulation(std::istream &input)
virtual GeneralPopulation & operator=(const GeneralPopulation &source)
void PrintPopulation(std::ostream &output) const
gplib::rmat tpopulation
Definition: gentypes.h:25
void ResizePop(const int popsize, const int genesize)
Change the population size.