Unfit  3.1.1
Data fitting and optimization software
GeneticAlgorithm.hpp
1 // Unfit: Data fitting and optimization software
2 //
3 // Copyright (C) 2012- Dr Martin Buist & Dr Alberto Corrias
4 // Contacts: martin.buist _at_ nus.edu.sg; alberto _at_ nus.edu.sg
5 //
6 // See the 'Contributors' file for a list of those who have contributed
7 // to this work.
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 #ifndef UNFIT_INCLUDE_GENETICALGORITHM_HPP_
23 #define UNFIT_INCLUDE_GENETICALGORITHM_HPP_
24 
25 #include <random>
26 #include <utility>
27 #include <vector>
28 #include "GenericCostFunction.hpp"
29 #include "GenericOptimizer.hpp"
30 
31 namespace Unfit
32 {
48 {
50  friend class TestGeneticAlgorithm;
51  public:
56 
62  virtual ~GeneticAlgorithm() {}
63 
95  int FindMin(GenericCostFunction &CostFunction,
96  std::vector<double> &coordinates) override;
97 
102  void Reset() override;
103 
104  private:
114  void GeneratePopulation(GenericCostFunction &CostFunction);
115 
122  void InitialiseBounds();
123 
135  void CrossOver(const std::vector<double> &parent_1,
136  const std::vector<double> &parent_2, std::vector<double> &offspring_1,
137  std::vector<double> &offspring_2);
138 
148  void Reproduce(GenericCostFunction &CostFunction);
149 
159  std::pair<unsigned, unsigned> GetMatingPair();
160 
171  void MutateGenes(GenericCostFunction &CostFunction);
172 
179  void CalculateRanks();
180 
191  int ProcessFindMin(GenericCostFunction &CostFunction);
192 
194  unsigned n_keep_;
196  unsigned dimensions_;
198  std::vector<double> ranks_;
200  std::mt19937 generator_;
202  std::vector<std::uniform_real_distribution<double>> distributions_;
203 };
204 
205 } // namespace Unfit
206 
207 #endif
int ProcessFindMin(GenericCostFunction &CostFunction)
Definition: GeneticAlgorithm.cpp:249
void GeneratePopulation(GenericCostFunction &CostFunction)
Definition: GeneticAlgorithm.cpp:157
void Reset() override
Definition: GeneticAlgorithm.cpp:54
unsigned dimensions_
Definition: GeneticAlgorithm.hpp:196
Definition: GenericOptimizer.hpp:41
unsigned n_keep_
Definition: GeneticAlgorithm.hpp:194
Definition: Bounds.hpp:27
int FindMin(GenericCostFunction &CostFunction, std::vector< double > &coordinates) override
A method to find a minimum point of a function using a Genetic Algorithm approach.
Definition: GeneticAlgorithm.cpp:192
void Reproduce(GenericCostFunction &CostFunction)
Definition: GeneticAlgorithm.cpp:175
void MutateGenes(GenericCostFunction &CostFunction)
Definition: GeneticAlgorithm.cpp:77
void CrossOver(const std::vector< double > &parent_1, const std::vector< double > &parent_2, std::vector< double > &offspring_1, std::vector< double > &offspring_2)
Definition: GeneticAlgorithm.cpp:129
void CalculateRanks()
Definition: GeneticAlgorithm.cpp:97
void InitialiseBounds()
Definition: GeneticAlgorithm.cpp:65
std::pair< unsigned, unsigned > GetMatingPair()
Definition: GeneticAlgorithm.cpp:110
Definition: GenericCostFunction.hpp:36
A class to implement the Genetic Algorithm optimization method.
Definition: GeneticAlgorithm.hpp:47
GeneticAlgorithm()
Definition: GeneticAlgorithm.cpp:44
std::mt19937 generator_
Definition: GeneticAlgorithm.hpp:200
std::vector< double > ranks_
Definition: GeneticAlgorithm.hpp:198
Definition: TestGeneticAlgorithm.cpp:39
std::vector< std::uniform_real_distribution< double > > distributions_
Definition: GeneticAlgorithm.hpp:202
virtual ~GeneticAlgorithm()
Definition: GeneticAlgorithm.hpp:62