#include "master_slave.hpp" #include "message.hpp" #include "agent_worker.hpp" #include "fitness.hpp" #include "ga.hpp" #include "Utils.hpp" /** * @file master_slave.cpp * @brief Handle the start-up of the training part of the algorithm * */ using namespace std; /** * @file master_slave.cpp * @brief Handle the start-up of the training algorithm * */ namespace master_slave{ void run(int argc, char* argv[]){ //Set program options namespace po = boost::program_options; po::options_description desc("Allowed options"); desc.add_options() ("help,h", "print usage message") ("master,m", "launch master") ("slave,s", "launch slave") ("host", po::value<std::string>(), "address of master (used for slave)") ("port,p", po::value<int>(), "host port for slave, port to listen to for master") ("np", po::value<int>(), "number of eval by agent") ("ns", po::value<int>(), "number of eval by slave") ("duration,d", po::value<int>(), "slave duration in minute"); po::variables_map vm; po::store(parse_command_line(argc, argv, desc), vm); //Cas help if (vm.count("help")){ std::cout << desc << std::endl; return; } //Cas host et port std::string host = "localhost"; int port = 54321; if (vm.count("host")) host = vm["host"].as<std::string>(); if (vm.count("port")) port = vm["port"].as<int>(); //Cas master if (vm.count("master")){ //Valeur par défaut int np = 1;//nombre de tests sur un individu, évaluer sur la moyenne int ns = 3;//nombre d'évaluations max d'un individu qu'on fait tourner sur une machine float mutRate = 0.6; float crossRate = 0.6; int tournSize = 2; int popSize = 30; int nbIter = 100; bool fresh = true; //Lecture de la config vector<vector<string>> config = Utils::read_Config(); for(size_t i = 0; i<config.size(); i++){ if(config.at(i).at(0) == "nEvalP"){ np = stoi(config.at(i).at(1)); }else if(config.at(i).at(0) == "nEvalS"){ ns = stoi(config.at(i).at(1)); }else if(config.at(i).at(0) == "mutRate"){ mutRate = stof(config.at(i).at(1)); }else if(config.at(i).at(0) == "crossRate"){ crossRate = stof(config.at(i).at(1)); }else if(config.at(i).at(0) == "tournSize"){ tournSize = stoi(config.at(i).at(1)); }else if(config.at(i).at(0) == "popSize"){ popSize = stoi(config.at(i).at(1)); }else if(config.at(i).at(0) == "nbIter"){ nbIter = stoi(config.at(i).at(1)); }else if(config.at(i).at(0) == "freshPop"){ if(config.at(i).at(1) == "true"){ fresh = true; }else if(config.at(i).at(1) == "false"){ fresh = false; } } } //Application de la config if (vm.count("np")) np = vm["np"].as<int>(); if (vm.count("ns")) ns = vm["ns"].as<int>(); const ga::parameters params{ .mutation_rate = mutRate,//combien de fois on laisse le programme cuisiner .crossover_rate = crossRate,//combien de fois on croise les instructions .tournament_size = tournSize,//taille du pool de sélection des individus (1 à 2% de la population) .population_size = popSize,//taille de la population .nb_iterations = nbIter//nombre de générations }; const agent::parameters agent_params{ .input_size = 1344,//taille de l'écran .output_size = 9,//là c'est spécifique au jeu, c'est le nombre d'actions possibles .nb_registers = 8,// .block_size = 32,//taille du bloc d'instructions .nb_blocks = 10//nombre du bloc du programme }; ga ga(params, agent_params); auto fitness = genetic_algorithms::make_distributed_fitness<double, agent> (port, genetic_algorithms::accumulate::mean_score<double>, np, ns); ga.run(fresh, fitness); return; } //Cas slave if (vm.count("slave")){ int d = 1e6; //Duration par défaut (minute) if (vm.count("duration")) d = vm["duration"].as<int>(); slave<double, agent> slave(host, port, agent_worker, d); slave.run(); return; } //Cas par défaut std::cout << "choose master or slave mode" << std::endl; } }