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