Skip to content

Add flexible Intervention<TSeq> API for reusable model interventions#187

Closed
Copilot wants to merge 2 commits intomasterfrom
copilot/add-flexible-api-for-interventions
Closed

Add flexible Intervention<TSeq> API for reusable model interventions#187
Copilot wants to merge 2 commits intomasterfrom
copilot/add-flexible-api-for-interventions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 12, 2026

Complex interventions (quarantine, isolation, contact tracing) are currently baked into individual model classes, making them impossible to reuse across models. This PR introduces an Intervention<TSeq> base class that plugs into the simulation lifecycle like GlobalEvent, but with a richer, structured interface.

New: Intervention<TSeq> base class

include/epiworld/intervention-bones.hpp / intervention-meat.hpp:

Hook Called by Purpose
setup(Model*) Model::reset() Per-run init (resize tracking arrays, etc.)
update(Model*) run_globalevents() Per-day logic with day-filter support
applies_to(Agent*, Model*) default update() loop Eligibility predicate
apply(Agent*, Model*) default update() loop Apply action to agent
should_release(Agent*, Model*) default update() loop Release predicate
release(Agent*, Model*) default update() loop Remove agent from intervention

The default update() iterates all agents and calls these helpers in order. Subclasses may override any subset or override update() entirely for full control. clone_ptr() is virtual for correct polymorphic cloning.

Model integration

  • config.hpp: Intervention<TSeq> forward decl + InterventionPtr alias
  • model-bones.hpp / model-meat.hpp: interventions vector; add_intervention (ref and shared_ptr overloads), get_intervention, rm_intervention
  • Model::reset() calls intervention->setup(this) after initial state distribution
  • Model::run_globalevents() dispatches interventions after existing global events, flushing the event queue between each

Usage

class ContactQuarantine : public Intervention<> {
    std::vector<int> quarantine_since;
public:
    void setup(Model<>* m) override {
        quarantine_since.assign(m->size(), -1); // idempotent on re-run
    }
    bool applies_to(Agent<>* a, Model<>* /*m*/) override {
        return a->get_state() == EXPOSED && quarantine_since[a->get_id()] < 0;
    }
    void apply(Agent<>* a, Model<>* m) override {
        quarantine_since[a->get_id()] = m->today();
        // queue state transition...
    }
    bool should_release(Agent<>* a, Model<>* m) override {
        int d = quarantine_since[a->get_id()];
        return d >= 0 && (m->today() - d) >= 14;
    }
};

ContactQuarantine quar("14-day quarantine");
model.add_intervention(quar); // stores an independent clone

setup() is called on every Model::reset() (including in run_multiple); one-time operations like add_state() must be guarded by the implementor.

Original prompt

This section details on the original issue you should resolve

<issue_title>A Flexible API For Interventions (summary)</issue_title>
<issue_description>We need to have a summary and a proposed plan of action based on the discussion in #161.</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: cpp_statistician
An expert in statistical analysis and modeling developer using C++.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…integration

Co-authored-by: gvegayon <893619+gvegayon@users.noreply.github.com>
Copilot AI changed the title [WIP] Add a flexible API for interventions based on discussion Add flexible Intervention<TSeq> API for reusable model interventions Mar 12, 2026
Copilot AI requested a review from gvegayon March 12, 2026 19:21
@gvegayon
Copy link
Copy Markdown
Member

This PR was supposed to build some specs, but not implement. Closing.

@gvegayon gvegayon closed this Apr 16, 2026
@gvegayon gvegayon deleted the copilot/add-flexible-api-for-interventions branch April 16, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A Flexible API For Interventions (summary)

2 participants