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