Skip to content

Commit

Permalink
Tools are also passed in a single way
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Jun 6, 2024
1 parent 917d21d commit 161fe2f
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 302 deletions.
264 changes: 125 additions & 139 deletions epiworld.hpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/00-hello-world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ int main()
virus.set_prob_death(.01);
model.add_virus(virus);

epiworld::Tool<int> tool("vaccine");
model.add_tool(tool, .5);
epiworld::Tool<int> tool("vaccine", .5, true);
model.add_tool(tool);

// Generating a random pop
model.agents_smallworld(10000, 20, false, .01);
Expand Down
4 changes: 2 additions & 2 deletions examples/03-simple-sir/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main()
covid19.set_state(1,2,2);

// Creating a tool
epiworld::Tool<> vax("vaccine");
epiworld::Tool<> vax("vaccine", .5, true);
vax.set_susceptibility_reduction(.95);

// Creating a model
Expand All @@ -23,7 +23,7 @@ int main()
// Adding the tool and virus
model.add_virus(covid19);

model.add_tool(vax, .5);
model.add_tool(vax);

// Generating a random pop
model.agents_from_adjlist(
Expand Down
16 changes: 8 additions & 8 deletions examples/04-advanced-usage/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,34 @@ int main() {
covid19.set_state(1,2,3);

// Creating tools ---------------------------------------------------------
epiworld::Tool<DAT> vaccine("Vaccine");
epiworld::Tool<DAT> vaccine("Vaccine", 0.5, true);
vaccine.set_susceptibility_reduction(&model("vax efficacy"));
vaccine.set_recovery_enhancer(0.4);
vaccine.set_death_reduction(&model("vax death"));
vaccine.set_transmission_reduction(0.5);

epiworld::Tool<DAT> mask("Face masks");
epiworld::Tool<DAT> mask("Face masks", 0.5, true);
mask.set_susceptibility_reduction(0.8);
mask.set_transmission_reduction(0.05);

epiworld::Tool<DAT> immune("Immune system");
epiworld::Tool<DAT> immune("Immune system", 1.0, true);
immune.set_susceptibility_reduction(&model("imm efficacy"));
immune.set_recovery_enhancer(&model("imm recovery"));
immune.set_death_reduction(&model("imm death"));
immune.set_transmission_reduction(&model("imm trans"));
DAT seq0(base_seq.size(), false);
immune.set_sequence(seq0);

epiworld::Tool<DAT> post_immunity("Post Immune");
epiworld::Tool<DAT> post_immunity("Post Immune", 0, true);
post_immunity.set_susceptibility_reduction(1.0);

// Adding the virus and the tools to the model ----------------------------
model.add_virus(covid19);

model.add_tool(immune, 1.0);
model.add_tool(vaccine, 0.5);
model.add_tool(mask, 0.5);
model.add_tool_n(post_immunity, 0);
model.add_tool(immune);
model.add_tool(vaccine);
model.add_tool(mask);
model.add_tool(post_immunity);

// Initializing and printing information about the model ------------------
model.queuing_off(); // Not working with rewiring just yet.
Expand Down
8 changes: 4 additions & 4 deletions examples/05-user-data/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ int main()
v.set_prob_infecting(&model("infectiousness"));

// Setting up tool ---------------------------------------------------------
epiworld::Tool<> is("immune system");
epiworld::Tool<> is("immune system", 1.0, true);
is.set_susceptibility_reduction(.3);
is.set_death_reduction(.9);
is.set_recovery_enhancer(&model("recovery"));

epiworld::Tool<> postImm("post immunity");
epiworld::Tool<> postImm("post immunity", 0, false);
postImm.set_susceptibility_reduction(1.0);

model.add_tool(is, 1.0);
model.add_tool_n(postImm, 0u);
model.add_tool(is);
model.add_tool(postImm);
model.add_virus(v);
model.run(112, 30);
model.print();
Expand Down
4 changes: 2 additions & 2 deletions examples/07-surveillance/07-surveillance.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ int main()
covid19.set_infectiousness(.8);

// Creating a tool
epiworld::Tool<> vax("vaccine");
epiworld::Tool<> vax("vaccine", .5, true);
vax.set_contagion_reduction(.95);

// Creating a model
epiworld::Model<> model;

// Adding the tool and virus
model.add_virus(covid19);
model.add_tool(vax, .5);
model.add_tool(vax);

// Generating a random pop
model.population_from_adjlist(
Expand Down
8 changes: 1 addition & 7 deletions include/epiworld/model-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ class Model {
bool directed = false;

std::vector< VirusPtr<TSeq> > viruses = {};

std::vector< ToolPtr<TSeq> > tools = {};
std::vector< epiworld_double > prevalence_tool = {};
std::vector< bool > prevalence_tool_as_proportion = {};
std::vector< ToolToAgentFun<TSeq> > tools_dist_funs = {};

std::vector< Entity<TSeq> > entities = {};
std::vector< Entity<TSeq> > entities_backup = {};
Expand Down Expand Up @@ -336,9 +332,7 @@ class Model {
*/
///@{
void add_virus(Virus<TSeq> & v);
void add_tool(Tool<TSeq> & t, epiworld_double preval);
void add_tool_n(Tool<TSeq> & t, epiworld_fast_uint preval);
void add_tool_fun(Tool<TSeq> & t, ToolToAgentFun<TSeq> fun);
void add_tool(Tool<TSeq> & t);
void add_entity(Entity<TSeq> e);
void rm_virus(size_t virus_pos);
void rm_tool(size_t tool_pos);
Expand Down
13 changes: 7 additions & 6 deletions include/epiworld/model-meat-print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ inline const Model<TSeq> & Model<TSeq>::print(bool lite) const
size_t n_tools_model = tools.size();
for (size_t i = 0u; i < tools.size(); ++i)
{
const auto & tool = tools[i];

if ((n_tools_model > 10) && (i >= 10))
{
Expand All @@ -218,13 +219,13 @@ inline const Model<TSeq> & Model<TSeq>::print(bool lite) const

if (i < n_tools_model)
{
if (prevalence_tool_as_proportion[i])
if (tool->get_prevalence_as_proportion())
{

printf_epiworld(
" - %s (baseline prevalence: %.2f%%)\n",
tools[i]->get_name().c_str(),
prevalence_tool[i] * 100.0
tool->get_name().c_str(),
tool->get_prevalence() * 100.0
);

}
Expand All @@ -233,8 +234,8 @@ inline const Model<TSeq> & Model<TSeq>::print(bool lite) const

printf_epiworld(
" - %s (baseline prevalence: %i seeds)\n",
tools[i]->get_name().c_str(),
static_cast<int>(prevalence_tool[i])
tool->get_name().c_str(),
static_cast<int>(tool->get_prevalence())
);

}
Expand All @@ -243,7 +244,7 @@ inline const Model<TSeq> & Model<TSeq>::print(bool lite) const

printf_epiworld(
" - %s (originated in the model...)\n",
tools[i]->get_name().c_str()
tool->get_name().c_str()
);

}
Expand Down
122 changes: 4 additions & 118 deletions include/epiworld/model-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,6 @@ inline Model<TSeq>::Model(const Model<TSeq> & model) :
directed(model.directed),
viruses(model.viruses),
tools(model.tools),
prevalence_tool(model.prevalence_tool),
prevalence_tool_as_proportion(model.prevalence_tool_as_proportion),
tools_dist_funs(model.tools_dist_funs),
entities(model.entities),
entities_backup(model.entities_backup),
rewire_fun(model.rewire_fun),
Expand Down Expand Up @@ -451,9 +448,6 @@ inline Model<TSeq>::Model(Model<TSeq> && model) :
viruses(std::move(model.viruses)),
// Tools
tools(std::move(model.tools)),
prevalence_tool(std::move(model.prevalence_tool)),
prevalence_tool_as_proportion(std::move(model.prevalence_tool_as_proportion)),
tools_dist_funs(std::move(model.tools_dist_funs)),
// Entities
entities(std::move(model.entities)),
entities_backup(std::move(model.entities_backup)),
Expand Down Expand Up @@ -524,9 +518,6 @@ inline Model<TSeq> & Model<TSeq>::operator=(const Model<TSeq> & m)
viruses = m.viruses;

tools = m.tools;
prevalence_tool = m.prevalence_tool;
prevalence_tool_as_proportion = m.prevalence_tool_as_proportion;
tools_dist_funs = m.tools_dist_funs;

entities = m.entities;
entities_backup = m.entities_backup;
Expand Down Expand Up @@ -766,55 +757,10 @@ template<typename TSeq>
inline void Model<TSeq>::dist_tools()
{

// Starting first infection
int n = size();
std::vector< size_t > idx(n);
for (epiworld_fast_uint t = 0; t < tools.size(); ++t)
for (auto & tool: tools)
{

if (tools_dist_funs[t])
{

tools_dist_funs[t](*tools[t], this);

} else {

// Picking how many
int nsampled;
if (prevalence_tool_as_proportion[t])
{
nsampled = static_cast<int>(std::floor(prevalence_tool[t] * size()));
}
else
{
nsampled = static_cast<int>(prevalence_tool[t]);
}

if (nsampled > static_cast<int>(size()))
throw std::range_error("There are only " + std::to_string(size()) +
" individuals in the population. Cannot add the tool to " + std::to_string(nsampled));

ToolPtr<TSeq> tool = tools[t];

int n_left = n;
std::iota(idx.begin(), idx.end(), 0);
while (nsampled > 0)
{
int loc = static_cast<epiworld_fast_uint>(floor(runif() * n_left--));

population[idx[loc]].add_tool(
tool,
const_cast< Model<TSeq> * >(this),
tool->state_init, tool->queue_init
);

nsampled--;

std::swap(idx[loc], idx[n_left]);

}

}
tool->distribute(this);

// Apply the events
events_run();
Expand Down Expand Up @@ -999,51 +945,17 @@ inline void Model<TSeq>::add_virus(
}

template<typename TSeq>
inline void Model<TSeq>::add_tool(Tool<TSeq> & t, epiworld_double preval)
inline void Model<TSeq>::add_tool(Tool<TSeq> & t)
{

if (preval > 1.0)
throw std::range_error("Prevalence of tool cannot be above 1.0");

if (preval < 0.0)
throw std::range_error("Prevalence of tool cannot be negative");


db.record_tool(t);

// Adding the tool to the model (and database.)
tools.push_back(std::make_shared< Tool<TSeq> >(t));
prevalence_tool.push_back(preval);
prevalence_tool_as_proportion.push_back(true);
tools_dist_funs.push_back(nullptr);

}

template<typename TSeq>
inline void Model<TSeq>::add_tool_n(Tool<TSeq> & t, epiworld_fast_uint preval)
{

db.record_tool(t);

tools.push_back(std::make_shared<Tool<TSeq> >(t));
prevalence_tool.push_back(preval);
prevalence_tool_as_proportion.push_back(false);
tools_dist_funs.push_back(nullptr);

}

template<typename TSeq>
inline void Model<TSeq>::add_tool_fun(Tool<TSeq> & t, ToolToAgentFun<TSeq> fun)
{

db.record_tool(t);

tools.push_back(std::make_shared<Tool<TSeq> >(t));
prevalence_tool.push_back(0.0);
prevalence_tool_as_proportion.push_back(false);
tools_dist_funs.push_back(fun);
}


template<typename TSeq>
inline void Model<TSeq>::add_entity(Entity<TSeq> e)
{
Expand Down Expand Up @@ -1109,29 +1021,15 @@ inline void Model<TSeq>::rm_tool(size_t tool_pos)

// Flipping with the last one
std::swap(tools[tool_pos], tools[tools.size() - 1]);
std::swap(tools_dist_funs[tool_pos], tools_dist_funs[tools.size() - 1]);
std::swap(prevalence_tool[tool_pos], prevalence_tool[tools.size() - 1]);

/* There's an error on windows:
https://github.com/UofUEpiBio/epiworldR/actions/runs/4801482395/jobs/8543744180#step:6:84
More clear here:
https://stackoverflow.com/questions/58660207/why-doesnt-stdswap-work-on-vectorbool-elements-under-clang-win
*/
std::vector<bool>::swap(
prevalence_tool_as_proportion[tool_pos],
prevalence_tool_as_proportion[tools.size() - 1]
);

// auto old = prevalence_tool_as_proportion[tool_pos];
// prevalence_tool_as_proportion[tool_pos] = prevalence_tool_as_proportion[tools.size() - 1];
// prevalence_tool_as_proportion[tools.size() - 1] = old;


tools.pop_back();
tools_dist_funs.pop_back();
prevalence_tool.pop_back();
prevalence_tool_as_proportion.pop_back();

return;

Expand Down Expand Up @@ -2614,18 +2512,6 @@ inline bool Model<TSeq>::operator==(const Model<TSeq> & other) const
)

}

VECT_MATCH(
prevalence_tool,
other.prevalence_tool,
"tools prevalence don't match"
)

VECT_MATCH(
prevalence_tool_as_proportion,
other.prevalence_tool_as_proportion,
"tools as prop don't match"
)

VECT_MATCH(
entities,
Expand Down
4 changes: 2 additions & 2 deletions include/epiworld/models/surveillance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ inline ModelSURV<TSeq>::ModelSURV(
model.add_globalevent(surveillance_program, "Surveilance program", -1);

// Vaccine tool -----------------------------------------------------------
epiworld::Tool<TSeq> vax("Vaccine");
epiworld::Tool<TSeq> vax("Vaccine", prop_vaccinated, true);
vax.set_susceptibility_reduction(&model("Vax efficacy"));
vax.set_transmission_reduction(&model("Vax redux transmission"));

model.add_tool(vax, prop_vaccinated);
model.add_tool(vax);

model.set_name("Surveillance");

Expand Down
Loading

0 comments on commit 161fe2f

Please sign in to comment.