diff --git a/include/epiworld/model-bones.hpp b/include/epiworld/model-bones.hpp index 1d9d22a1..7959c608 100644 --- a/include/epiworld/model-bones.hpp +++ b/include/epiworld/model-bones.hpp @@ -366,6 +366,14 @@ class Model { * @param skip How many rows to skip. */ void load_agents_entities_ties(std::string fn, int skip); + + /** + * @brief Associate agents-entities from data + */ + void load_agents_entities_ties( + const std::vector & agents_ids, + const std::vector & entities_ids + ); /** * @name Accessing population of the model diff --git a/include/epiworld/model-meat.hpp b/include/epiworld/model-meat.hpp index 3d6afca1..c6cfecd0 100644 --- a/include/epiworld/model-meat.hpp +++ b/include/epiworld/model-meat.hpp @@ -1393,6 +1393,61 @@ inline void Model::load_agents_entities_ties( } +template +inline void Model::load_agents_entities_ties( + const std::vector< int > & agents_ids, + const std::vector< int > & entities_ids +) { + + if (agents_ids.size() != entities_ids.size()) + throw std::length_error( + std::string("agents_ids (") + + std::to_string(agents_ids.size()) + + std::string(") and entities_ids (") + + std::to_string(entities_ids.size()) + + std::string(") should match.") + ); + + + size_t n_entries = agents_ids.size(); + for (size_t i = 0u; i < n_entries; ++i) + { + + if (agents_id[i] >= this->population.size()) + throw std::length_error( + std::string("agents_ids[") + + std::to_string(i) + + std::string("] = ") + + std::to_string(agents_ids[i]) + + std::string(" is out of range (population size: ") + + std::to_string(this->population.size()) + + std::string(").") + ); + + + if (entities_ids[i] >= this->entities.size()) + throw std::length_error( + std::string("entities_ids[") + + std::to_string(i) + + std::string("] = ") + + std::to_string(entities_ids[i]) + + std::string(" is out of range (entities size: ") + + std::to_string(this->entities.size()) + + std::string(").") + ); + + this->population[agents_ids[i]].add_entity( + this->entities[entities_ids[i]], + nullptr + ); + + } + + return; + + +} + template inline void Model::agents_from_adjlist( std::string fn,