diff --git a/neural_network/src/GeneticAlgorithm.cpp b/neural_network/src/GeneticAlgorithm.cpp index 74682359ff2c243b92883263edbf7e6869bec0ce..bd145da516f79259b2b37ab74c20420cb1285627 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); } }