Skip to content

Commit

Permalink
Adding function to distribute entities based on data
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Apr 23, 2024
1 parent 6594ed6 commit 93be22d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/epiworld/model-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> & agents_ids,
const std::vector<int> & entities_ids
);

/**
* @name Accessing population of the model
Expand Down
55 changes: 55 additions & 0 deletions include/epiworld/model-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,61 @@ inline void Model<TSeq>::load_agents_entities_ties(

}

template<typename TSeq>
inline void Model<TSeq>::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<typename TSeq>
inline void Model<TSeq>::agents_from_adjlist(
std::string fn,
Expand Down

0 comments on commit 93be22d

Please sign in to comment.