comparison src/population.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:add35537fdbb
1 // Copyright 2011, Ian Hobson.
2 //
3 // This file is part of gpsynth.
4 //
5 // gpsynth is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // gpsynth is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with gpsynth in the file COPYING.
17 // If not, see http://www.gnu.org/licenses/.
18
19 // Population class, manages a population of synth graphs and their evolution
20
21 // TODO - split out evolution behaviour in to a separate class, support
22 // multiple populations
23
24 #pragma once
25
26 #include "converter.hpp"
27 #include "evaluator.hpp"
28 #include "grammar.hpp"
29 #include "statistics.hpp"
30 #include "synth_graph.hpp"
31
32 #include "boost/function.hpp"
33
34 #include <string>
35 #include <vector>
36
37 typedef boost::function<bool (sg::Graph&)> MutationOperator;
38
39 class Population : public EvaluatorListenerInterface {
40 std::vector<sg::Graph> population_;
41 int best_fit_;
42 std::vector<double> fitnesses_;
43 // current generation
44 int generation_;
45 // total generations
46 int max_generations_;
47 int tournament_size_;
48 double fitness_threshold_;
49 double crossover_rate_;
50 double mutation_rate_;
51 bool reproduce_best_;
52 bool keep_temp_folders_;
53 GrammarInterface* grammar_;
54 ConverterInterface* converter_;
55 EvaluatorInterface* evaluator_;
56 // work folder, where all files for current run are placed
57 std::string work_folder_;
58 // folder to write logs to
59 std::string log_folder_;
60 // folder to save best renders
61 std::string render_folder_;
62 // folder to save best dot scripts
63 std::string dot_folder_;
64 // folder to save exported graphs
65 std::string export_folder_;
66 // temp folder for current generation
67 std::string generation_folder_;
68 // available mutation operators
69 std::vector<MutationOperator> mutators_;
70 // selector for genetic operations
71 stats::ProbabilitySelector genetic_operation_selector_;
72
73 enum GeneticOperation {
74 kCrossover,
75 kMutation,
76 kReproduction
77 };
78
79 public:
80 Population(GrammarInterface* grammar,
81 ConverterInterface* converter,
82 EvaluatorInterface* evaluator);
83
84 void InitializePopulation(std::size_t size);
85 void SetWorkFolder(const std::string& path);
86 void SetFitnessThreshold(double value) { fitness_threshold_ = value; }
87 void SetTournamentSize(int size) { tournament_size_ = size; }
88 void Evolve(int generations);
89 void ExportToDOT();
90 void SetCrossoverRate(double rate);
91 void SetMutationRate(double rate);
92 void SetReproduceBest(bool value) { reproduce_best_ = value; }
93 void SetKeepTempFolders(bool value) { keep_temp_folders_ = value; }
94
95 private:
96 void EvaluateGeneration();
97 void AnalyzeGeneration();
98 void BreedNewGeneration();
99 int SelectIndividual();
100 void PerformCrossover(sg::Graph& child_1, sg::Graph& child_2);
101 void PrepareNewChild(sg::Graph& child);
102 void GraphRatedNotification(std::size_t graphs_rated);
103 void SetupGeneticOperationSelector();
104 void SaveGenerationBest();
105 bool CheckThreshold() const;
106 void CleanTempFolders() const;
107 void NewGenerationMessage() const;
108 void SetGenerationFolder();
109 };