diff --git a/src/aevol_create.cpp b/src/aevol_create.cpp index 744de72bf672233cc9a5a115b2cea773b6b0c7c3..1655f3cfef42903cfc77b57c379ca3dc1ab21e84 100644 --- a/src/aevol_create.cpp +++ b/src/aevol_create.cpp @@ -41,6 +41,7 @@ const char* DEFAULT_PARAM_FILE_NAME = "param.in"; #include <getopt.h> #include <fstream> +#include <list> #include <sstream> #include <string> @@ -73,7 +74,7 @@ int main(int argc, char* argv[]) { exp_manager = new ExpManager(); // Initialize the simulation from the parameter file - std::string chromosome; + std::list<std::string> chromosomes; if (chromosome_file_name != nullptr) { std::ifstream chromosome_file(chromosome_file_name); @@ -81,19 +82,21 @@ int main(int argc, char* argv[]) { Utils::ExitWithUsrMsg(std::string("failed to open source chromosome file ") + chromosome_file_name); } + std::string chromosome; std::getline(chromosome_file, chromosome); if (not chromosome_file or chromosome.length() == 0) { Utils::ExitWithUsrMsg(std::string("failed to read from chromosome file ") + chromosome_file_name); } + chromosomes.push_back(std::move(chromosome)); - std::cout << "Loading chromosome from text file " << chromosome_file_name << " (" << chromosome.length() + std::cout << "Loading chromosome from text file " << chromosome_file_name << " (" << chromosomes.front().length() << " base pairs)" << std::endl; delete [] chromosome_file_name; } if(param_file_name != nullptr) { - if (chromosome.length() != 0) { - ParamLoader::load(param_values, exp_manager, true, chromosome.c_str(), chromosome.length()); + if (chromosomes.size() != 0) { + ParamLoader::load(param_values, exp_manager, chromosomes, true); } else { ParamLoader::load(param_values, exp_manager, true); } diff --git a/src/libaevol/io/parameters/ParamLoader.cpp b/src/libaevol/io/parameters/ParamLoader.cpp index 5e69822f0a0d5f791f7985af4d2aa9533a9e5441..f75cfee647a42e5170c6505f3a2a8cc348f0c815 100644 --- a/src/libaevol/io/parameters/ParamLoader.cpp +++ b/src/libaevol/io/parameters/ParamLoader.cpp @@ -45,9 +45,14 @@ namespace aevol { void ParamLoader::load(const ParamValues& param_values, ExpManager* exp_m, - bool verbose, - const char* chromosome, - int32_t lchromosome) { + bool verbose) { + load(param_values, exp_m, std::list<std::string>(), verbose); +} + +void ParamLoader::load(const ParamValues& param_values, + ExpManager* exp_m, + const std::list<std::string>& chromosomes, + bool verbose) { // Initialize master prng // Will be used to create the initial genome(s) and to generate seeds for the other prngs auto prng = std::make_shared<JumpingMT>(param_values.seed_); @@ -247,8 +252,7 @@ void ParamLoader::load(const ParamValues& param_values, Individual * indiv = NULL; int32_t id_new_indiv = 0; - if (chromosome != NULL) - { + if (not chromosomes.empty()) { printf("Option -c is used: chromosome will be loaded from a text file\n"); #ifndef __REGUL Individual * indiv = new Individual(exp_m, @@ -276,9 +280,11 @@ void ParamLoader::load(const ParamValues& param_values, #endif // Make a copy of the provided chromosome and transfer ownership to the genetic unit we create with it - char* chromosome_copy = new char[lchromosome + 1]; - strncpy(chromosome_copy, chromosome, lchromosome + 1); - indiv->add_GU(chromosome_copy, lchromosome); + assert(chromosomes.size() == 1); + auto& chromosome = chromosomes.front(); + char* chromosome_copy = new char[chromosome.length() + 1]; + strncpy(chromosome_copy, chromosome.c_str(), chromosome.length() + 1); + indiv->add_GU(chromosome_copy, chromosome.length()); chromosome_copy = nullptr; indiv->genetic_unit_nonconst(0).set_min_gu_length(param_values.chromosome_minimal_length_); diff --git a/src/libaevol/io/parameters/ParamLoader.h b/src/libaevol/io/parameters/ParamLoader.h index 3e0c9c30a57a7aa78589cceae9e71072cd812922..51cbac6b53a1024dda78097081fd3a801f2c58a5 100644 --- a/src/libaevol/io/parameters/ParamLoader.h +++ b/src/libaevol/io/parameters/ParamLoader.h @@ -31,6 +31,8 @@ // Includes // ================================================================= #include <cinttypes> +#include <list> +#include <string> #include "legacy/ExpManager.h" #include "ParamValues.h" @@ -58,9 +60,11 @@ class ParamLoader { // ========================================================================= static void load(const ParamValues& param_values, ExpManager* exp_m, - bool verbose = false, - const char* chromosome = nullptr, - int32_t lchromosome = 0); + bool verbose = false); + static void load(const ParamValues& param_values, + ExpManager* exp_m, + const std::list<std::string>& chromosomes, + bool verbose = false); protected: };