Skip to content

Commit 1426694

Browse files
authored
Updating to epiworld 0.12.0 (#166)
* Updating to epiworld 0.12.0 * Fixing issue in how to get agents * Simplify entity_get_agents function * Correct way to set the agents id * Applying suggested fixes by copilot (updates to epiworld C++) * Bringing last set of changes from epiworld C++
1 parent 2a6b6af commit 1426694

18 files changed

+146
-457
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: epiworldR
22
Type: Package
33
Title: Fast Agent-Based Epi Models
4-
Version: 0.11.3.0
4+
Version: 0.12.0.0
55
Depends: R (>= 4.1.0)
66
Authors@R: c(
77
person(given="George", family="Vega Yon", role=c("aut", "cre"),

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ help:
1111
@echo " debug Build and run a debug Docker container"
1212
@echo " docs Generate documentation with roxygen2"
1313
@echo " local-update Update epiworld.hpp from local epiworld include folder"
14+
@echo " update Update epiworld.hpp from the latest version on GitHub"
1415
@echo " local-update-diagrams Update diagrams from local epiworld docs_src folder"
1516
@echo " website Build the pkgdown website"
1617
@echo " clean Clean compiled files and reset DESCRIPTION and NAMESPACE"

NEWS.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
# epiworldR 0.11.3.0
1+
# epiworldR 0.12.0.0
22

3-
* Updates to `epiworld` version 0.11.3, including a patch to the `ModelSEIRMixingQuarantine` model. It was underestimating the outbreak size. This is a similar issue detected in the collection of the measles models.
3+
## Bug fixes
4+
5+
* Updates to `epiworld` version 0.12.0, including a patch to the `ModelSEIRMixingQuarantine` model. It was underestimating the outbreak size. This is a similar issue detected in the collection of the measles models.
46

57
* The `ModelSEIRMixingQuarantine` gained a couple of extra checks for validating the inputs.
68

9+
## Internal changes
10+
711
* Simplified build configuration to comply with CRAN policies: removed custom
812
`configure` script, using R's built-in OpenMP settings
913
(`$(SHLIB_OPENMP_CXXFLAGS)`) and specifying C++17 via `CXX_STD` in Makevars
1014
and `SystemRequirements` in DESCRIPTION.
1115

16+
* Changing how `Entities` are managed in the system, moved away from using pointers and instead use `reference_wrapper`.
17+
1218

1319
# epiworldR 0.11.2.0
1420

inst/include/epiworld/agent-bones.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class Agent {
7070
friend class Tools<TSeq>;
7171
friend class Tools_const<TSeq>;
7272
friend class Queue<TSeq>;
73-
friend class Entities<TSeq>;
7473
friend class AgentsSample<TSeq>;
7574
friend void default_add_virus<TSeq>(Event<TSeq> & a, Model<TSeq> * m);
7675
friend void default_add_tool<TSeq>(Event<TSeq> & a, Model<TSeq> * m);
@@ -87,9 +86,7 @@ class Agent {
8786
std::vector< size_t > * neighbors_locations = nullptr;
8887
size_t n_neighbors = 0u;
8988

90-
std::vector< size_t > entities;
91-
std::vector< size_t > entities_locations;
92-
size_t n_entities = 0u;
89+
std::vector< std::reference_wrapper<Entity<TSeq>> > entities;
9390

9491
unsigned int state = 0u;
9592
unsigned int state_prev = 0u; ///< For accounting, if need to undo a change.
@@ -102,6 +99,8 @@ class Agent {
10299
std::vector< ToolPtr<TSeq> > tools;
103100
unsigned int n_tools = 0u;
104101

102+
void reset(); ///< Resets the agent to the initial state (no virus, no tools, no entities, state 0.)
103+
105104
public:
106105

107106
Agent();
@@ -247,8 +246,6 @@ class Agent {
247246

248247
const unsigned int & get_state() const;
249248

250-
void reset();
251-
252249
bool has_tool(epiworld_fast_uint t) const;
253250
bool has_tool(std::string name) const;
254251
bool has_tool(const Tool<TSeq> & t) const;
@@ -282,8 +279,8 @@ class Agent {
282279
double operator[](size_t j) const;
283280
///@}
284281

285-
Entities<TSeq> get_entities();
286-
const Entities_const<TSeq> get_entities() const;
282+
std::vector< std::reference_wrapper<Entity<TSeq>> > get_entities();
283+
const std::vector< std::reference_wrapper<Entity<TSeq>> > get_entities() const;
287284
const Entity<TSeq> & get_entity(size_t i) const;
288285
Entity<TSeq> & get_entity(size_t i);
289286
size_t get_n_entities() const;

inst/include/epiworld/agent-events-meat.hpp

Lines changed: 34 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -226,114 +226,59 @@ inline void default_add_entity(Event<TSeq> & a, Model<TSeq> *)
226226

227227
if (p->get_n_entities() > e->size()) // Slower search through the agent
228228
{
229-
for (size_t i = 0u; i < e->size(); ++i)
230-
if(static_cast<int>(e->operator[](i)) == p->get_id())
229+
for (const Agent<TSeq> & agent: e->get_agents())
230+
if(agent.get_id() == p->get_id())
231231
throw std::logic_error("An entity cannot be reassigned to an agent.");
232232
}
233233
else // Slower search through the entity
234234
{
235-
for (size_t i = 0u; i < p->get_n_entities(); ++i)
236-
if(p->get_entity(i).get_id() == e->get_id())
235+
for (const Entity<TSeq> & entity: p->get_entities())
236+
if(entity.get_id() == e->get_id())
237237
throw std::logic_error("An entity cannot be reassigned to an agent.");
238238
}
239239

240240
// It means that agent and entity were not associated.
241241
}
242242

243-
// Adding the entity to the agent
244-
if (++p->n_entities <= p->entities.size())
245-
{
246-
247-
p->entities[p->n_entities - 1] = e->get_id();
248-
p->entities_locations[p->n_entities - 1] = e->n_agents;
249-
250-
} else
251-
{
252-
p->entities.push_back(e->get_id());
253-
p->entities_locations.push_back(e->n_agents);
254-
}
255-
256-
// Adding the agent to the entity
257-
// Adding the entity to the agent
258-
if (++e->n_agents <= e->agents.size())
259-
{
260-
261-
e->agents[e->n_agents - 1] = p->get_id();
262-
// Adjusted by '-1' since the list of entities in the agent just grew.
263-
e->agents_location[e->n_agents - 1] = p->n_entities - 1;
264-
265-
} else
266-
{
267-
e->agents.push_back(p->get_id());
268-
e->agents_location.push_back(p->n_entities - 1);
269-
}
270-
271-
// Today was the last modification
272-
// e->date_last_add_or_remove = m->today();
243+
// Adding the to agent and the entity
244+
p->entities.push_back(std::ref(*e));
245+
e->agents.push_back(std::ref(*p));
273246

274247
}
275248

276249
template<typename TSeq>
277-
inline void default_rm_entity(Event<TSeq> & a, Model<TSeq> * m)
250+
inline void default_rm_entity(Event<TSeq> & a, Model<TSeq> *)
278251
{
279252

280-
Agent<TSeq> * p = a.agent;
281-
Entity<TSeq> * e = a.entity;
282-
size_t idx_agent_in_entity = a.idx_agent;
283-
size_t idx_entity_in_agent = a.idx_object;
284-
285-
if (--p->n_entities > 0)
286-
{
287-
288-
// When we move the end entity to the new location, the
289-
// moved entity needs to reflect the change, i.e., where the
290-
// entity will now be located in the agent
291-
size_t agent_location_in_last_entity =
292-
p->entities_locations[p->n_entities];
293-
294-
Entity<TSeq> * last_entity =
295-
&m->get_entity(p->entities[p->n_entities]); ///< Last entity of the agent
296-
297-
// The end entity will be located where the removed was
298-
last_entity->agents_location[agent_location_in_last_entity] =
299-
idx_entity_in_agent;
300-
301-
// We now make the swap
302-
std::swap(
303-
p->entities[p->n_entities],
304-
p->entities[idx_entity_in_agent]
305-
);
306-
307-
}
308-
309-
if (--e->n_agents > 0)
310-
{
311-
312-
// When we move the end agent to the new location, the
313-
// moved agent needs to reflect the change, i.e., where the
314-
// agent will now be located in the entity
315-
size_t entity_location_in_last_agent = e->agents_location[e->n_agents];
316-
317-
Agent<TSeq> * last_agent =
318-
&m->get_agents()[e->agents[e->n_agents]]; ///< Last agent of the entity
319-
320-
// The end entity will be located where the removed was
321-
last_agent->entities_locations[entity_location_in_last_agent] =
322-
idx_agent_in_entity;
323-
324-
// We now make the swap
325-
std::swap(
326-
e->agents[e->n_agents],
327-
e->agents[idx_agent_in_entity]
328-
);
329-
330-
}
253+
Agent<TSeq> & p = *a.agent;
254+
Entity<TSeq> & e = *a.entity;
255+
256+
// Remove entity from agent's entity list
257+
p.entities.erase(
258+
std::remove_if(
259+
p.entities.begin(),
260+
p.entities.end(),
261+
[&e](const std::reference_wrapper<Entity<TSeq>> & entity_ref) {
262+
return entity_ref.get().get_id() == e.get_id();
263+
}
264+
),
265+
p.entities.end()
266+
);
331267

332-
// Setting the date of the last removal
333-
// e->date_last_add_or_remove = m->today();
268+
// Remove agent from entity's agent list
269+
e.agents.erase(
270+
std::remove_if(
271+
e.agents.begin(),
272+
e.agents.end(),
273+
[&p](const std::reference_wrapper<Agent<TSeq>> & agent_ref) {
274+
return agent_ref.get().get_id() == p.get_id();
275+
}
276+
),
277+
e.agents.end()
278+
);
334279

335280
return;
336281

337282
};
338283

339-
#endif
284+
#endif

0 commit comments

Comments
 (0)