mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-08 14:33:53 -05:00
Remove internal model references in schedulers
This commit is contained in:
@@ -132,8 +132,8 @@ void BoltzmannWealthModel1D::run(unsigned int steps) {
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel1D::step() {
|
||||
console->trace("Executing model step {}", _step_count++);
|
||||
_sched->step();
|
||||
console->trace("Executing model step {}", ++_step_count);
|
||||
_sched->step(shared_from_this());
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -155,7 +155,6 @@ int main(int argc, char **argv) {
|
||||
console->info("Starting Boltzmann Wealth Model with {} agents on a {}-unit grid for {} steps", agent_count, x_size, max_steps);
|
||||
|
||||
auto model = std::make_shared<BoltzmannWealthModel1D>(agent_count, x_size, initial_seed);
|
||||
model->get_scheduler()->set_model(model);
|
||||
|
||||
spdlog::stopwatch sw;
|
||||
for(int i = 0; i < max_steps; i++) model->step();
|
||||
|
||||
@@ -79,7 +79,7 @@ public:
|
||||
/**
|
||||
* The one-dimensional Boltzmann wealth model
|
||||
*/
|
||||
class BoltzmannWealthModel1D : public kami::Model, std::enable_shared_from_this<BoltzmannWealthModel1D> {
|
||||
class BoltzmannWealthModel1D : public kami::Model {
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ void BoltzmannWealthModel2D::run(unsigned int steps) {
|
||||
|
||||
void BoltzmannWealthModel2D::step() {
|
||||
console->trace("Executing model step {}", _step_count++);
|
||||
_sched->step();
|
||||
_sched->step(shared_from_this());
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -156,7 +156,6 @@ int main(int argc, char **argv) {
|
||||
console->info("Starting Boltzmann Wealth Model with {} agents on a {}x{}-unit grid for {} steps", agent_count, x_size, y_size, max_steps);
|
||||
|
||||
auto model = std::make_shared<BoltzmannWealthModel2D>(agent_count, x_size, y_size, initial_seed);
|
||||
model->get_scheduler()->set_model(model);
|
||||
|
||||
spdlog::stopwatch sw;
|
||||
for(int i = 0; i < max_steps; i++) model->step();
|
||||
|
||||
@@ -81,7 +81,7 @@ public:
|
||||
/**
|
||||
* The two-dimensional Boltzmann wealth model
|
||||
*/
|
||||
class BoltzmannWealthModel2D : public kami::Model, std::enable_shared_from_this<BoltzmannWealthModel2D> {
|
||||
class BoltzmannWealthModel2D : public kami::Model {
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#ifndef KAMI_MODEL_H
|
||||
#define KAMI_MODEL_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <kami/domain.h>
|
||||
#include <kami/kami.h>
|
||||
#include <kami/population.h>
|
||||
@@ -37,7 +39,7 @@ namespace kami {
|
||||
/**
|
||||
* An abstract for generic models
|
||||
*/
|
||||
class LIBKAMI_EXPORT Model {
|
||||
class LIBKAMI_EXPORT Model : public std::enable_shared_from_this<Model> {
|
||||
protected:
|
||||
std::shared_ptr<Domain> _domain = nullptr;
|
||||
std::shared_ptr<Population> _pop = nullptr;
|
||||
|
||||
@@ -48,6 +48,9 @@ namespace kami {
|
||||
* @note First create a Model for the scheduler to live in.
|
||||
*/
|
||||
class LIBKAMI_EXPORT RandomScheduler : public SequentialScheduler {
|
||||
private:
|
||||
std::shared_ptr<std::ranlux24> _rng = nullptr;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor.
|
||||
@@ -70,7 +73,7 @@ namespace kami {
|
||||
*
|
||||
* @param agent_list list of agents to execute the step
|
||||
*/
|
||||
void step(std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
void step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
|
||||
/**
|
||||
* Set the random number generator used to randomize the order of agent
|
||||
@@ -85,10 +88,7 @@ namespace kami {
|
||||
* Get a reference to the random number generator used to randomize
|
||||
* the order of agent stepping.
|
||||
*/
|
||||
[[maybe_unused]] [[maybe_unused]] std::shared_ptr<std::ranlux24> get_rng();
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::ranlux24> _rng = nullptr;
|
||||
std::shared_ptr<std::ranlux24> get_rng();
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -46,29 +46,7 @@ namespace kami {
|
||||
*/
|
||||
int _step_counter = 0;
|
||||
|
||||
/**
|
||||
* A pointer to the `Model` this scheduler schedules
|
||||
*/
|
||||
std::shared_ptr<Model> _model = nullptr;
|
||||
|
||||
public:
|
||||
Scheduler() = default;
|
||||
|
||||
explicit Scheduler(std::shared_ptr<Model> model);
|
||||
|
||||
/**
|
||||
* @brief Get the `Model` associated with this scheduler
|
||||
*/
|
||||
std::shared_ptr<Model> get_model();
|
||||
|
||||
/**
|
||||
* @brief Add a `Model` to this scheduler
|
||||
*
|
||||
* @details This method will associate a model with the
|
||||
* scheduler.
|
||||
*/
|
||||
[[maybe_unused]] void set_model(std::shared_ptr<Model> model);
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
*
|
||||
@@ -77,7 +55,7 @@ namespace kami {
|
||||
* method for every Agent assigned to this scheduler in the order
|
||||
* assigned.
|
||||
*/
|
||||
virtual void step() = 0;
|
||||
virtual void step(std::shared_ptr<Model> model) = 0;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
@@ -89,7 +67,7 @@ namespace kami {
|
||||
*
|
||||
* @param agent_list list of agents to execute the step
|
||||
*/
|
||||
virtual void step(std::shared_ptr<std::vector<AgentID>> agent_list) = 0;
|
||||
virtual void step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) = 0;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -48,16 +48,6 @@ namespace kami {
|
||||
*/
|
||||
class LIBKAMI_EXPORT SequentialScheduler : public Scheduler {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor.
|
||||
*
|
||||
* @details The Model parameter is used by the scheduler to get
|
||||
* access to an Agent. The Model is presumed to maintain a master
|
||||
* list of all Agents in the Model and the Model can be queried for
|
||||
* a reference to any particular Agent at `step()` time.
|
||||
*/
|
||||
SequentialScheduler() = default;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
*
|
||||
@@ -66,7 +56,7 @@ namespace kami {
|
||||
* method for every Agent assigned to this scheduler in the order
|
||||
* assigned.
|
||||
*/
|
||||
void step() override;
|
||||
void step(std::shared_ptr<Model> model);
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
@@ -78,7 +68,7 @@ namespace kami {
|
||||
*
|
||||
* @param agent_list list of agents to execute the step
|
||||
*/
|
||||
void step(std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
virtual void step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list);
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace kami {
|
||||
* method for every StagedAgent assigned to this scheduler in the order
|
||||
* assigned.
|
||||
*/
|
||||
[[maybe_unused]] void advance();
|
||||
void advance(std::shared_ptr<Model> model);
|
||||
|
||||
/**
|
||||
* @brief Advance a single time step.
|
||||
@@ -68,18 +68,9 @@ namespace kami {
|
||||
*
|
||||
* @param agent_list list of agents to execute the step
|
||||
*/
|
||||
void advance(const std::shared_ptr<std::vector<AgentID>>& agent_list);
|
||||
void advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* The Model parameter is used by the scheduler to get access to an Agent.
|
||||
* The Model is presumed to maintain a master list of all Agents in the
|
||||
* Model and the Model can be queried for a reference to any particular
|
||||
* Agent at `step()` time.
|
||||
*/
|
||||
explicit StagedScheduler() = default;
|
||||
|
||||
/**
|
||||
* Execute a single time step.
|
||||
*
|
||||
@@ -88,7 +79,7 @@ namespace kami {
|
||||
* in the same order. Finally, it will execute the `Agent::advance()`
|
||||
* method for each Agent in the same order.
|
||||
*/
|
||||
void step() override;
|
||||
void step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -38,9 +38,9 @@ namespace kami {
|
||||
this->set_rng(std::move(rng));
|
||||
}
|
||||
|
||||
void RandomScheduler::step(std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
void RandomScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
shuffle(agent_list->begin(),agent_list->end(), *_rng);
|
||||
this->SequentialScheduler::step(agent_list);
|
||||
this->SequentialScheduler::step(model, agent_list);
|
||||
}
|
||||
|
||||
void RandomScheduler::set_rng(std::shared_ptr<std::ranlux24> rng) {
|
||||
|
||||
@@ -30,17 +30,5 @@
|
||||
#include <kami/scheduler.h>
|
||||
|
||||
namespace kami {
|
||||
|
||||
Scheduler::Scheduler(std::shared_ptr<Model> model) {
|
||||
this->set_model(std::move(model));
|
||||
}
|
||||
|
||||
[[maybe_unused]] std::shared_ptr<Model> Scheduler::get_model() {
|
||||
return(_model);
|
||||
}
|
||||
|
||||
[[maybe_unused]] void Scheduler::set_model(std::shared_ptr<Model> model) {
|
||||
_model = std::move(model);
|
||||
}
|
||||
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -28,16 +28,16 @@
|
||||
|
||||
namespace kami {
|
||||
|
||||
void SequentialScheduler::step() {
|
||||
this->step(Scheduler::get_model()->get_population()->get_agent_list());
|
||||
void SequentialScheduler::step(std::shared_ptr<Model> model) {
|
||||
this->step(model, model->get_population()->get_agent_list());
|
||||
}
|
||||
|
||||
void SequentialScheduler::step(std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
void SequentialScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
Scheduler::_step_counter++;
|
||||
|
||||
for(auto agent_id = agent_list->begin(); agent_id < agent_list->end(); agent_id++) {
|
||||
auto agent = Scheduler::get_model()->get_population()->get_agent_by_id(*agent_id);
|
||||
if(agent != nullptr) agent->step(_model);
|
||||
auto agent = model->get_population()->get_agent_by_id(*agent_id);
|
||||
if(agent != nullptr) agent->step(model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,20 +32,19 @@
|
||||
|
||||
namespace kami {
|
||||
|
||||
void StagedScheduler::step() {
|
||||
auto agent_list = Scheduler::get_model()->get_population()->get_agent_list();
|
||||
this->SequentialScheduler::step(agent_list);
|
||||
this->advance(agent_list);
|
||||
void StagedScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
this->SequentialScheduler::step(model, agent_list);
|
||||
this->advance(model, agent_list);
|
||||
}
|
||||
|
||||
[[maybe_unused]] void StagedScheduler::advance() {
|
||||
this->advance(Scheduler::get_model()->get_population()->get_agent_list());
|
||||
void StagedScheduler::advance(std::shared_ptr<Model> model) {
|
||||
this->advance(model, model->get_population()->get_agent_list());
|
||||
}
|
||||
|
||||
void StagedScheduler::advance(const std::shared_ptr<std::vector<AgentID>>& agent_list) {
|
||||
void StagedScheduler::advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
for(auto agent_id = agent_list->begin(); agent_id < agent_list->end(); agent_id++) {
|
||||
auto agent = std::dynamic_pointer_cast<StagedAgent>(this->Scheduler::get_model()->get_population()->get_agent_by_id(*agent_id));
|
||||
if(agent != nullptr) agent->advance(_model);
|
||||
auto agent = std::dynamic_pointer_cast<StagedAgent>(model->get_population()->get_agent_by_id(*agent_id));
|
||||
if(agent != nullptr) agent->advance(model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user