RESPOND 2.4.0
Researching Effective Strategies to Prevent Opioid Death
Loading...
Searching...
No Matches
simulation.hpp
1
2// File: simulation.hpp //
3// Project: respond //
4// Created Date: 2026-02-05 //
5// Author: Matthew Carroll //
6// ----- //
7// Last Modified: 2026-02-12 //
8// Modified By: Matthew Carroll //
9// ----- //
10// Copyright (c) 2026 Syndemics Lab at Boston Medical Center //
12#ifndef RESPOND_SIMULATION_HPP_
13#define RESPOND_SIMULATION_HPP_
14
15#include <map>
16#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include <Eigen/Dense>
22
23#include <respond/history.hpp>
24#include <respond/model.hpp>
25
26namespace respond {
31public:
33 Simulation() : Simulation("console") {}
34
38 Simulation(const std::string &log_name) : _log_name(log_name) {}
39
41 ~Simulation() = default;
42
45 void Run() {
46 for (const auto &model : _models) {
47 model->RunTransitions();
48 }
49 }
50
54 void AddModel(const std::unique_ptr<Model> &model) {
55 // because push_back is a move operation we're taking over ownership of
56 // the unique pointer
57 _models.push_back(model->clone());
58 }
59
62 const std::vector<std::unique_ptr<Model>> &GetModels() const {
63 return _models;
64 }
65
68 std::vector<std::string> GetModelNames() const {
69 std::vector<std::string> ret;
70 for (auto &m : _models) {
71 ret.push_back(m->GetModelName());
72 }
73 return ret;
74 }
75
77 void ClearModels() { _models.clear(); }
78
82 const std::vector<std::map<std::string, std::vector<Eigen::VectorXd>>>
84 std::vector<std::map<std::string, std::vector<Eigen::VectorXd>>> ret;
85 int model_idx = 0;
86 for (const auto &model : _models) {
87 std::map<std::string, std::vector<Eigen::VectorXd>> inner_ret;
88 for (const auto &kv : model->GetHistories()) {
89 inner_ret[kv.first] = kv.second.GetStateAsVector();
90 }
91 ret.push_back(inner_ret);
92 model_idx++;
93 }
94 return ret;
95 }
96
100 const std::vector<std::map<std::string, History>>
102 std::vector<std::map<std::string, History>> ret;
103 for (const auto &model : _models) {
104 ret.push_back(model->GetHistories());
105 }
106 return ret;
107 }
108
111 const std::vector<std::pair<std::string, std::string>>
113 std::vector<std::pair<std::string, std::string>> ret;
114 for (const auto &model : _models) {
115 for (const auto &kv : model->GetHistories()) {
116 std::pair<std::string, std::string> p = {model->GetModelName(),
117 kv.first};
118 ret.push_back(p);
119 }
120 }
121 return ret;
122 }
123
126 std::string GetLogName() const { return _log_name; }
127
131 Simulation(const Simulation &other) : _log_name(other.GetLogName()) {
132 ClearModels();
133 for (const auto &m : other.GetModels()) {
134 _models.push_back(m->clone());
135 }
136 }
137
142 if (this != &other) {
143 ClearModels();
144 _log_name = other.GetLogName();
145 for (const auto &m : other.GetModels()) {
146 _models.push_back(m->clone());
147 }
148 }
149 return *this;
150 }
151
152private:
153 std::string _log_name;
154 std::vector<std::unique_ptr<Model>> _models;
155};
156} // namespace respond
157
158#endif // RESPOND_SIMULATION_HPP_
Manages and executes multiple models in a coordinated simulation. A Simulation aggregates Model insta...
Definition: simulation.hpp:30
std::vector< std::string > GetModelNames() const
Retrieves the names of all models in the simulation.
Definition: simulation.hpp:68
const std::vector< std::map< std::string, std::vector< Eigen::VectorXd > > > GetModelHistories() const
Retrieves the complete state histories for all models.
Definition: simulation.hpp:83
void Run()
Executes one step of the simulation for all models. Calls RunTransitions() on each registered model i...
Definition: simulation.hpp:45
~Simulation()=default
Virtual destructor for polymorphic cleanup.
const std::vector< std::unique_ptr< Model > > & GetModels() const
Retrieves all models in the simulation.
Definition: simulation.hpp:62
Simulation(const std::string &log_name)
Constructs a Simulation with a specified logger.
Definition: simulation.hpp:38
void ClearModels()
Removes all models from the simulation.
Definition: simulation.hpp:77
std::string GetLogName() const
Retrieves the logger name used by this simulation.
Definition: simulation.hpp:126
Simulation(const Simulation &other)
Copy constructor creating an independent deep copy of the simulation. All models are cloned; modifica...
Definition: simulation.hpp:131
const std::vector< std::map< std::string, History > > GetModelSparseHistories() const
Retrieves sparse history objects for all models.
Definition: simulation.hpp:101
Simulation()
Default constructor initializing with "console" logger.
Definition: simulation.hpp:33
Simulation & operator=(const Simulation &other)
Copy assignment operator for deep copying simulation state.
Definition: simulation.hpp:141
void AddModel(const std::unique_ptr< Model > &model)
Adds a model to the simulation. The model is cloned and managed by the simulation.
Definition: simulation.hpp:54
const std::vector< std::pair< std::string, std::string > > GetModelHistoryNames() const
Retrieves pairs of (model name, history name) for all histories.
Definition: simulation.hpp:112