diff --git a/neural_network/include/GeneticAlgorithm.hpp b/neural_network/include/GeneticAlgorithm.hpp index e52b327b2625e559f66db7a7b877b4775ffc39d1..0af8e2e103a93a4f041fffbe23aa10e23912d44b 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 bd145da516f79259b2b37ab74c20420cb1285627..9c858c8d548390a44721c4492bda9fa500f8a64f 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; }