From 7d98b2f2d0b12e1d3a464a6fd34841a19905d749 Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@etu.univ-amu.fr> Date: Fri, 18 Jan 2019 11:26:55 +0100 Subject: [PATCH] Genetic Algorithm now only saves the best MLP --- neural_network/include/GeneticAlgorithm.hpp | 4 +- neural_network/src/GeneticAlgorithm.cpp | 64 +++++---------------- 2 files changed, 16 insertions(+), 52 deletions(-) diff --git a/neural_network/include/GeneticAlgorithm.hpp b/neural_network/include/GeneticAlgorithm.hpp index e52b327..0af8e2e 100644 --- a/neural_network/include/GeneticAlgorithm.hpp +++ b/neural_network/include/GeneticAlgorithm.hpp @@ -43,7 +43,9 @@ class GeneticAlgorithm : public NeuralNetwork /// @param nbOutputs The size of the mlp output layer.. Individual(dynet::ParameterCollection & model, int nbInputs, const std::string & topology, int nbOutputs); /// @brief Create a blank individual, so that it will be loaded from a saved model. - Individual(); + /// + /// @param bestId The id of the individual that was saved in the file. + Individual(unsigned int bestId); void becomeChildOf(Individual * other); void becomeChildOf(Individual * mom, Individual * dad); void mutate(float probability); diff --git a/neural_network/src/GeneticAlgorithm.cpp b/neural_network/src/GeneticAlgorithm.cpp index bd145da..9c858c8 100644 --- a/neural_network/src/GeneticAlgorithm.cpp +++ b/neural_network/src/GeneticAlgorithm.cpp @@ -39,19 +39,7 @@ void GeneticAlgorithm::init(int nbInputs, const std::string & topology, int nbOu std::vector<float> GeneticAlgorithm::predict(FeatureModel::FeatureDescription & fd) { - std::vector<float> prediction = generation[0]->mlp.predict(fd); - - for (unsigned int i = 1; i < generation.size(); i++) - { - auto otherPred = generation[i]->mlp.predict(fd); - for (unsigned int j = 0; j < prediction.size(); j++) - prediction[j] += otherPred[j]; - } - - for (unsigned int j = 0; j < prediction.size(); j++) - prediction[j] /= generation.size(); - - return prediction; + return generation[0]->mlp.predict(fd); } float GeneticAlgorithm::update(FeatureModel::FeatureDescription & fd, int gold) @@ -128,16 +116,10 @@ float GeneticAlgorithm::update(FeatureModel::FeatureDescription & fd, int gold) void GeneticAlgorithm::save(const std::string & filename) { File * file = new File(filename, "w"); - fprintf(file->getDescriptor(), "%d %d %s\n", nbInputs, nbOutputs, topology.c_str()); + fprintf(file->getDescriptor(), "%u\n", generation[0]->id); delete file; - - unsigned int quarter = generation.size() / 4; - - for (unsigned int i = 0; i < quarter; i++) - { - generation[i]->mlp.saveStruct(filename); - generation[i]->mlp.saveParameters(filename); - } + generation[0]->mlp.saveStruct(filename); + generation[0]->mlp.saveParameters(filename); } void GeneticAlgorithm::printTopology(FILE * output) @@ -156,37 +138,17 @@ void GeneticAlgorithm::printTopology(FILE * output) void GeneticAlgorithm::load(const std::string & filename) { File * file = new File(filename, "r"); - int i, o; - char buffer[1024]; - if (fscanf(file->getDescriptor(), "%d %d %[^\n]\n", &i, &o, buffer) != 3) + unsigned int bestId; + if (fscanf(file->getDescriptor(), "%u\n", &bestId) != 1) { - fprintf(stderr, "ERROR (%s) : file \'%s\' bad format. Aborting.\n", ERRINFO, filename.c_str()); + fprintf(stderr, "ERROR (%s) : expected best id when reading file \'%s\'. Aborting.\n", ERRINFO, filename.c_str()); exit(1); } - delete file; + generation.emplace_back(new Individual(bestId)); - this->nbInputs = i; - this->nbOutputs = o; - this->topology = buffer; - - auto splited = split(topology, ' '); - if (splited.size() != 2 || !isNum(splited[0])) - { - fprintf(stderr, "ERROR (%s) : wrong topology \'%s\'. Aborting.\n", ERRINFO, topology.c_str()); - exit(1); - } - - int nbElems = std::stoi(splited[0]); - - for (int i = 0; i < nbElems; i++) - generation.emplace_back(new Individual()); - - for (int i = 0; i < nbElems; i++) - { - generation[i]->mlp.loadStruct(model, filename, 0); - generation[i]->mlp.loadParameters(model, filename); - } + generation[0]->mlp.loadStruct(model, filename, 0); + generation[0]->mlp.loadParameters(model, filename); } GeneticAlgorithm::Individual::Individual(dynet::ParameterCollection & model, int nbInputs, const std::string & topology, int nbOutputs) : mlp("MLP_" + std::to_string(idCount)) @@ -199,10 +161,10 @@ GeneticAlgorithm::Individual::Individual(dynet::ParameterCollection & model, int mutate(1.0); } -GeneticAlgorithm::Individual::Individual() - : mlp("MLP_" + std::to_string(idCount)) +GeneticAlgorithm::Individual::Individual(unsigned int bestId) + : mlp("MLP_" + std::to_string(bestId)) { - this->id = idCount++; + this->id = bestId; this->loss = 0.0; this->value = 0.0; } -- GitLab