91e605d7e28035d15d073860c1c054e8c5d9ddcf
[genetic.git] / src / generator / generation.h
1 #ifndef __GENERATOR_GENERATION_H
2 #define __GENERATOR_GENERATION_H
3
4 #include <vector>
5 #include <cstdlib>
6
7 #include "../gene.h"
8 #include "../chromosome.h"
9 #include "../generation.h"
10
11 using namespace std;
12
13 namespace genetic {
14     namespace generator {
15         /**
16          * Generator for generating entire Generation of Chromosomes (individuals)
17          */
18         template < typename _Chromosome >
19         class Generation {
20         public:
21             /**
22              * Type representing Chromosome Gene
23              */
24             typedef typename _Chromosome::GeneType GeneType;
25         protected:
26             /**
27              * Size of the generation to generate
28              */
29             unsigned int generationSize;
30
31             /**
32              * Size of the chromosome to generate
33              */
34             unsigned int chromosomeSize;
35
36         public:
37             /**
38              * Constructor. Initializes required variables and constants
39              *
40              * @param generationSize Indicates size of the generation
41              * @param chromosomeSize Indicates size of the chromosome
42              */
43             Generation(unsigned int generationSize, unsigned int chromosomeSize) {
44                 this->generationSize = generationSize;
45                 this->chromosomeSize = chromosomeSize;
46
47                 time_t t;
48                 srand((unsigned)time(&t));
49             }
50
51             /**
52              * Breeds new generation of chromosomes.
53              *
54              * @return ::genetic::Generation Generation of Chromosomes
55              */
56             ::genetic::Generation<_Chromosome> breed() {
57                 vector<_Chromosome> chromosomes;
58
59                 for (unsigned int i = 0; i < generationSize; i++) {
60                     vector<GeneType> genes;
61                     for (unsigned int j = 0; j < chromosomeSize; j++) {
62                         GeneType gene(rand() % 2);
63                         genes.push_back(gene);
64                     }
65                     chromosomes.push_back(genes);
66                 }
67
68                 return ::genetic::Generation<_Chromosome>(chromosomes);
69             }
70         };
71     }
72 }
73
74 #endif /* __GENERATOR_GENERATION_H */