From bf22fa5a789f70189be1b7a90cd9afcb9034202e Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@etu.univ-amu.fr> Date: Fri, 18 Jan 2019 11:01:46 +0100 Subject: [PATCH] GeneticAlgorithm now works with a mean to predict, and only save the best quarter of its population --- neural_network/src/GeneticAlgorithm.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/neural_network/src/GeneticAlgorithm.cpp b/neural_network/src/GeneticAlgorithm.cpp index 7468235..bd145da 100644 --- a/neural_network/src/GeneticAlgorithm.cpp +++ b/neural_network/src/GeneticAlgorithm.cpp @@ -39,7 +39,19 @@ void GeneticAlgorithm::init(int nbInputs, const std::string & topology, int nbOu std::vector<float> GeneticAlgorithm::predict(FeatureModel::FeatureDescription & fd) { - return generation[0]->mlp.predict(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; } float GeneticAlgorithm::update(FeatureModel::FeatureDescription & fd, int gold) @@ -119,10 +131,12 @@ void GeneticAlgorithm::save(const std::string & filename) fprintf(file->getDescriptor(), "%d %d %s\n", nbInputs, nbOutputs, topology.c_str()); delete file; - for (auto & individual : generation) + unsigned int quarter = generation.size() / 4; + + for (unsigned int i = 0; i < quarter; i++) { - individual->mlp.saveStruct(filename); - individual->mlp.saveParameters(filename); + generation[i]->mlp.saveStruct(filename); + generation[i]->mlp.saveParameters(filename); } } -- GitLab