Skip to content

Commit

Permalink
Draft of entities model
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Apr 11, 2024
1 parent 9a09073 commit 67ce649
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 17 deletions.
16 changes: 10 additions & 6 deletions epiworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6082,9 +6082,9 @@ class Model {
* AgentsSample<TSeq>::AgentsSample(Model<TSeq>) these vectors are allocated.
*/
///@{
std::vector< Agent<TSeq> * > sampled_population;
std::shared_ptr< std::vector< Agent<TSeq> * > > sampled_population;
size_t sampled_population_n = 0u;
std::vector< size_t > population_left;
std::shared_ptr< std::vector< size_t > > population_left;
size_t population_left_n = 0u;
///@}

Expand Down Expand Up @@ -14621,10 +14621,10 @@ class AgentsSample {

size_t sample_size = 0u;

std::vector< Agent<TSeq>* > * agents = nullptr; ///< Pointer to sample of agents
std::shared_ptr<std::vector< Agent<TSeq>* > > agents = nullptr; ///< Pointer to sample of agents
size_t * agents_n = nullptr; ///< Size of sample of agents

std::vector< size_t > * agents_left = nullptr; ///< Pointer to agents left (iota)
std::shared_ptr<std::vector< size_t > > agents_left = nullptr; ///< Pointer to agents left (iota)
size_t * agents_left_n = nullptr; ///< Size of agents left

Model<TSeq> * model = nullptr; ///< Extracts runif() and (if the case) population.
Expand All @@ -14651,13 +14651,17 @@ class AgentsSample {
);

AgentsSample(
Model<TSeq> * model, Entity<TSeq> & entity_, size_t n,
Model<TSeq> * model,
Entity<TSeq> & entity_,
size_t n,
std::vector< size_t > states_ = {},
bool truncate = false
);

AgentsSample(
Model<TSeq> * model, Agent<TSeq> & agent_, size_t n,
Model<TSeq> * model,
Agent<TSeq> & agent_,
size_t n,
std::vector< size_t > states_ = {},
bool truncate = false
);
Expand Down
2 changes: 2 additions & 0 deletions examples/11-entities/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main.o: main.cpp
g++ -std=c++17 -g main.cpp -o main.o
26 changes: 26 additions & 0 deletions examples/11-entities/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define EPI_DEBUG
#include "../../include/epiworld/epiworld.hpp"

using namespace epiworld;

int main() {

epimodels::ModelSEIREntitiesConn model(
"Flu", // std::string vname,
10000, // epiworld_fast_uint n,
0.01,// epiworld_double prevalence,
4.0,// epiworld_double contact_rate,
0.1,// epiworld_double transmission_rate,
4.0,// epiworld_double avg_incubation_days,
1.0/7.0,// epiworld_double recovery_rate,
{.1, .1, .8},// std::vector< epiworld_double > entities,
{"A", "B", "C"}// std::vector< std::string > entities_names
);

// Running and checking the results
model.run(50, 123);
model.print();

return 0;

}
4 changes: 2 additions & 2 deletions include/epiworld/agent-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class Agent {
std::vector< ToolPtr<TSeq> > tools;
epiworld_fast_uint n_tools = 0u;

std::vector< Agent<TSeq> * > sampled_agents;
std::vector< Agent<TSeq> * > sampled_agents = {};
size_t sampled_agents_n = 0u;
std::vector< size_t > sampled_agents_left;
std::vector< size_t > sampled_agents_left = {};
size_t sampled_agents_left_n = 0u;
int date_last_build_sample = -99;

Expand Down
25 changes: 16 additions & 9 deletions include/epiworld/agentssample-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class AgentsSample {

size_t sample_size = 0u;

std::vector< Agent<TSeq>* > * agents = nullptr; ///< Pointer to sample of agents
std::vector< Agent<TSeq>* >* agents = nullptr; ///< Pointer to sample of agents
size_t * agents_n = nullptr; ///< Size of sample of agents

std::vector< size_t > * agents_left = nullptr; ///< Pointer to agents left (iota)
std::vector< size_t >* agents_left = nullptr; ///< Pointer to agents left (iota)
size_t * agents_left_n = nullptr; ///< Size of agents left

Model<TSeq> * model = nullptr; ///< Extracts runif() and (if the case) population.
Expand All @@ -49,7 +49,7 @@ class AgentsSample {
public:

// Not available (for now)
AgentsSample() = delete; ///< Default constructor
AgentsSample() = delete; ///< Default constructor
AgentsSample(const AgentsSample<TSeq> & a) = delete; ///< Copy constructor
AgentsSample(AgentsSample<TSeq> && a) = delete; ///< Move constructor

Expand All @@ -60,13 +60,17 @@ class AgentsSample {
);

AgentsSample(
Model<TSeq> * model, Entity<TSeq> & entity_, size_t n,
Model<TSeq> * model,
Entity<TSeq> & entity_,
size_t n,
std::vector< size_t > states_ = {},
bool truncate = false
);

AgentsSample(
Model<TSeq> * model, Agent<TSeq> & agent_, size_t n,
Model<TSeq> * model,
Agent<TSeq> & agent_,
size_t n,
std::vector< size_t > states_ = {},
bool truncate = false
);
Expand Down Expand Up @@ -228,19 +232,22 @@ inline AgentsSample<TSeq>::AgentsSample(
agents->resize(n);

size_t i_obs = 0u;
for (size_t i = 0u; i < agents_in_entities; ++i)
for (size_t i = 0u; i < sample_size; ++i)
{

// Sampling a single agent from the set of entities
int jth = std::floor(model->runif() * agents_in_entities);
for (size_t e = 0u; e < cum_agents_count.size(); ++e)
{

// Are we in the limit?
if (jth <= cum_agents_count[e])
{
size_t agent_idx = 0u;
if (e == 0) // From the first group
agent_idx = entities_a[e][jth];
agent_idx = entities_a[e][jth]->get_id();
else
agent_idx = entities_a[e][jth - cum_agents_count[e - 1]];
agent_idx = entities_a[e][jth - cum_agents_count[e - 1]]->get_id();

// Getting the state
size_t state = model->population[agent_idx].get_state();
Expand All @@ -252,7 +259,7 @@ inline AgentsSample<TSeq>::AgentsSample(
continue;
}

agents->operator[](i_obs++) = agent_idx;
agents->operator[](i_obs++) = &(model->population[agent_idx]);

break;
}
Expand Down
1 change: 1 addition & 0 deletions include/epiworld/model-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,7 @@ inline void Model<TSeq>::reset() {
// Re distributing tools and virus
dist_virus();
dist_tools();
dist_entities();

// Distributing initial state, if specified
initial_states_fun(this);
Expand Down
1 change: 1 addition & 0 deletions include/epiworld/models/models.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace epimodels {
#include "seirdconnected.hpp"
#include "sirlogit.hpp"
#include "diffnet.hpp"
#include "seirentitiesconnected.hpp"


}
Expand Down
Loading

0 comments on commit 67ce649

Please sign in to comment.