diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/population.hpp	Thu Aug 25 11:05:55 2011 +0100
@@ -0,0 +1,109 @@
+//  Copyright 2011, Ian Hobson.
+//
+//  This file is part of gpsynth.
+//
+//  gpsynth is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  gpsynth is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with gpsynth in the file COPYING. 
+//  If not, see http://www.gnu.org/licenses/.
+
+// Population class, manages a population of synth graphs and their evolution
+
+// TODO - split out evolution behaviour in to a separate class, support
+// multiple populations
+
+#pragma once
+
+#include "converter.hpp"
+#include "evaluator.hpp"
+#include "grammar.hpp"
+#include "statistics.hpp"
+#include "synth_graph.hpp"
+
+#include "boost/function.hpp"
+
+#include <string>
+#include <vector>
+
+typedef boost::function<bool (sg::Graph&)> MutationOperator;
+
+class Population : public EvaluatorListenerInterface {
+  std::vector<sg::Graph> population_;
+  int best_fit_;
+  std::vector<double> fitnesses_;
+  // current generation
+  int generation_;
+  // total generations
+  int max_generations_;
+  int tournament_size_;
+  double fitness_threshold_;
+  double crossover_rate_;
+  double mutation_rate_;
+  bool reproduce_best_;
+  bool keep_temp_folders_;
+  GrammarInterface* grammar_;
+  ConverterInterface* converter_;
+  EvaluatorInterface* evaluator_;
+  // work folder, where all files for current run are placed
+  std::string work_folder_;
+  // folder to write logs to
+  std::string log_folder_;
+  // folder to save best renders
+  std::string render_folder_;
+  // folder to save best dot scripts
+  std::string dot_folder_;
+  // folder to save exported graphs
+  std::string export_folder_;
+  // temp folder for current generation
+  std::string generation_folder_;
+  // available mutation operators
+  std::vector<MutationOperator> mutators_;
+  // selector for genetic operations
+  stats::ProbabilitySelector genetic_operation_selector_;
+  
+  enum GeneticOperation {
+    kCrossover,
+    kMutation,
+    kReproduction
+  };
+  
+public:
+  Population(GrammarInterface* grammar,
+             ConverterInterface* converter,
+             EvaluatorInterface* evaluator);
+  
+  void InitializePopulation(std::size_t size);
+  void SetWorkFolder(const std::string& path);
+  void SetFitnessThreshold(double value) { fitness_threshold_ = value; }
+  void SetTournamentSize(int size) { tournament_size_ = size; }
+  void Evolve(int generations);
+  void ExportToDOT();
+  void SetCrossoverRate(double rate);
+  void SetMutationRate(double rate);
+  void SetReproduceBest(bool value) { reproduce_best_ = value; }
+  void SetKeepTempFolders(bool value) { keep_temp_folders_ = value; }
+  
+private:
+  void EvaluateGeneration();
+  void AnalyzeGeneration();
+  void BreedNewGeneration();
+  int SelectIndividual();
+  void PerformCrossover(sg::Graph& child_1, sg::Graph& child_2);
+  void PrepareNewChild(sg::Graph& child);
+  void GraphRatedNotification(std::size_t graphs_rated);
+  void SetupGeneticOperationSelector();
+  void SaveGenerationBest();
+  bool CheckThreshold() const;
+  void CleanTempFolders() const;
+  void NewGenerationMessage() const;
+  void SetGenerationFolder();
+};