mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-08 14:33:53 -05:00
Cleanup and align return values from Model
This commit is contained in:
@@ -25,9 +25,12 @@
|
||||
|
||||
#include "boltzmann1d.h"
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <CLI/App.hpp>
|
||||
#include <CLI/Config.hpp>
|
||||
@@ -39,7 +42,6 @@
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/kami.h>
|
||||
#include <kami/model.h>
|
||||
#include <kami/multigrid1d.h>
|
||||
#include <kami/population.h>
|
||||
#include <kami/random.h>
|
||||
@@ -65,40 +67,61 @@ kami::AgentID MoneyAgent1D::step(std::shared_ptr<kami::Model> model) {
|
||||
this->_step_counter++;
|
||||
|
||||
console->trace("Agent {} is moving", this->get_agent_id());
|
||||
this->move_agent();
|
||||
if (_agent_wealth > 0) this->give_money();
|
||||
this->move_agent(model);
|
||||
if (_agent_wealth > 0)
|
||||
this->give_money(model);
|
||||
|
||||
return this->get_agent_id();
|
||||
}
|
||||
|
||||
void MoneyAgent1D::move_agent() {
|
||||
kami::GridCoord1D MoneyAgent1D::move_agent(std::shared_ptr<kami::Model> model) {
|
||||
console->trace("Entering move_agent");
|
||||
auto agent_id = get_agent_id();
|
||||
auto move_list = _world->get_neighborhood(agent_id, false);
|
||||
std::uniform_int_distribution<int> dist(0, (int)move_list.size() - 1);
|
||||
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid1D>(domain.value());
|
||||
|
||||
auto move_list = world->get_neighborhood(agent_id, false);
|
||||
std::uniform_int_distribution<int> dist(0, (int) move_list.size() - 1);
|
||||
auto new_location = move_list[dist(*rng)];
|
||||
|
||||
console->trace("Moving Agent {} to location {}", agent_id, new_location);
|
||||
_world->move_agent(agent_id, new_location);
|
||||
world->move_agent(agent_id, new_location);
|
||||
console->trace("Exiting move_agent");
|
||||
|
||||
return new_location;
|
||||
}
|
||||
|
||||
kami::AgentID MoneyAgent1D::give_money() {
|
||||
std::optional<kami::AgentID> MoneyAgent1D::give_money(std::shared_ptr<kami::Model> model) {
|
||||
auto agent_id = get_agent_id();
|
||||
auto location = _world->get_location_by_agent(agent_id);
|
||||
auto cell_mates = _world->get_location_contents(location);
|
||||
|
||||
if (cell_mates->size() > 1) {
|
||||
std::uniform_int_distribution<int> dist(0, (int)cell_mates->size() - 1);
|
||||
auto other_agent_id = cell_mates->at(dist(*rng));
|
||||
auto other_agent = std::dynamic_pointer_cast<MoneyAgent1D>(_population->get_agent_by_id(other_agent_id).value());
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid1D>(domain.value());
|
||||
|
||||
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
|
||||
other_agent->_agent_wealth += 1;
|
||||
_agent_wealth -= 1;
|
||||
}
|
||||
auto agents = model->get_population();
|
||||
if (!agents)
|
||||
throw (std::domain_error("model is missing population"));
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents.value());
|
||||
|
||||
return agent_id;
|
||||
auto location = world->get_location_by_agent(agent_id);
|
||||
auto cell_mates = world->get_location_contents(location);
|
||||
|
||||
if (cell_mates->size() < 2)
|
||||
return std::nullopt;
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, (int) cell_mates->size() - 1);
|
||||
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
|
||||
auto other_agent = std::static_pointer_cast<MoneyAgent1D>(population->get_agent_by_id(other_agent_id).value());
|
||||
|
||||
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
|
||||
other_agent->_agent_wealth += 1;
|
||||
_agent_wealth -= 1;
|
||||
|
||||
return other_agent_id;
|
||||
}
|
||||
|
||||
BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x, unsigned int new_seed) {
|
||||
@@ -106,38 +129,38 @@ BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsig
|
||||
rng->seed(new_seed);
|
||||
|
||||
auto domain = std::make_shared<kami::MultiGrid1D>(length_x, true);
|
||||
auto sched = std::make_shared<kami::RandomScheduler>(rng);
|
||||
auto pop = std::make_shared<kami::Population>();
|
||||
auto scheduler = std::make_shared<kami::RandomScheduler>(rng);
|
||||
auto population = std::make_shared<kami::Population>();
|
||||
|
||||
MoneyAgent1D::set_world(domain);
|
||||
MoneyAgent1D::set_population(pop);
|
||||
|
||||
_domain = domain;
|
||||
_sched = sched;
|
||||
_pop = pop;
|
||||
this->set_domain(domain);
|
||||
this->set_scheduler(scheduler);
|
||||
this->set_population(population);
|
||||
|
||||
console->debug("Scheduler initiated with seed {}", new_seed);
|
||||
|
||||
_step_count = 0;
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, (int)length_x - 1);
|
||||
std::uniform_int_distribution<int> dist(0, (int) length_x - 1);
|
||||
|
||||
for (unsigned int i = 0; i < number_agents; i++) {
|
||||
auto new_agent = std::make_shared<MoneyAgent1D>();
|
||||
|
||||
console->trace("Initializing agent with AgentID {}", new_agent->get_agent_id());
|
||||
_pop->add_agent(new_agent);
|
||||
population->add_agent(new_agent);
|
||||
domain->add_agent(new_agent->get_agent_id(), kami::GridCoord1D(dist(*rng)));
|
||||
}
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel1D::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++) step();
|
||||
std::shared_ptr<kami::Model> BoltzmannWealthModel1D::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++)
|
||||
step();
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel1D::step() {
|
||||
std::shared_ptr<kami::Model> BoltzmannWealthModel1D::step() {
|
||||
console->trace("Executing model step {}", ++_step_count);
|
||||
_sched->step(shared_from_this());
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
@@ -29,22 +29,20 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/kami.h>
|
||||
#include <kami/multigrid1d.h>
|
||||
#include <kami/population.h>
|
||||
#include <kami/random.h>
|
||||
|
||||
|
||||
/**
|
||||
* A sample agent for a one-dimensional Boltzmann wealth model
|
||||
*/
|
||||
class MoneyAgent1D : public kami::Agent {
|
||||
private:
|
||||
inline static std::shared_ptr<kami::MultiGrid1D> _world = nullptr;
|
||||
inline static std::shared_ptr<kami::Population> _population = nullptr;
|
||||
|
||||
int _step_counter;
|
||||
int _agent_wealth;
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -60,28 +58,23 @@ public:
|
||||
/**
|
||||
* Move the agent to a random location on the world
|
||||
*/
|
||||
void move_agent();
|
||||
kami::GridCoord1D move_agent(std::shared_ptr<kami::Model> model);
|
||||
|
||||
/**
|
||||
* Give money to a random agent
|
||||
*/
|
||||
kami::AgentID give_money();
|
||||
std::optional<kami::AgentID> give_money(std::shared_ptr<kami::Model> model);
|
||||
|
||||
static void set_world(std::shared_ptr<kami::MultiGrid1D> world) {
|
||||
_world = std::move(world);
|
||||
}
|
||||
private:
|
||||
int _step_counter;
|
||||
int _agent_wealth;
|
||||
|
||||
static void set_population(std::shared_ptr<kami::Population> population) {
|
||||
_population = std::move(population);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The one-dimensional Boltzmann wealth model
|
||||
*/
|
||||
class BoltzmannWealthModel1D : public kami::Model {
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -96,14 +89,18 @@ public:
|
||||
/**
|
||||
* Execute a single time-step for the model.
|
||||
*/
|
||||
void step() override;
|
||||
std::shared_ptr<kami::Model> step() override;
|
||||
|
||||
/**
|
||||
* Execute a number of time-steps for the model.
|
||||
*
|
||||
* @param[in] n the number of steps to execute.
|
||||
*/
|
||||
void run(unsigned int n) override;
|
||||
std::shared_ptr<kami::Model> run(unsigned int n) override;
|
||||
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
};
|
||||
|
||||
#endif // BOLTZMANN1D_H
|
||||
|
||||
@@ -25,9 +25,12 @@
|
||||
|
||||
#include "boltzmann2d.h"
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <CLI/App.hpp>
|
||||
#include <CLI/Config.hpp>
|
||||
@@ -64,38 +67,61 @@ kami::AgentID MoneyAgent2D::step(std::shared_ptr<kami::Model> model) {
|
||||
this->_step_counter++;
|
||||
|
||||
console->trace("Agent {} is moving", this->get_agent_id());
|
||||
this->move_agent();
|
||||
if(_agent_wealth > 0) this->give_money();
|
||||
this->move_agent(model);
|
||||
if (_agent_wealth > 0)
|
||||
this->give_money(model);
|
||||
|
||||
return this->get_agent_id();
|
||||
}
|
||||
|
||||
void MoneyAgent2D::move_agent() {
|
||||
kami::GridCoord2D MoneyAgent2D::move_agent(std::shared_ptr<kami::Model> model) {
|
||||
console->trace("Entering move_agent");
|
||||
auto agent_id = get_agent_id();
|
||||
auto move_list = _world->get_neighborhood(agent_id, kami::GridNeighborhoodType::Moore, false);
|
||||
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain.value());
|
||||
|
||||
auto move_list = world->get_neighborhood(agent_id, kami::GridNeighborhoodType::Moore, false);
|
||||
std::uniform_int_distribution<int> dist(0, (int) move_list.size() - 1);
|
||||
auto new_location = move_list[dist(*rng)];
|
||||
|
||||
console->trace("Moving Agent {} to location {}", agent_id, new_location);
|
||||
_world->move_agent(agent_id, new_location);
|
||||
world->move_agent(agent_id, new_location);
|
||||
console->trace("Exiting move_agent");
|
||||
|
||||
return new_location;
|
||||
}
|
||||
|
||||
void MoneyAgent2D::give_money() {
|
||||
std::optional<kami::AgentID> MoneyAgent2D::give_money(std::shared_ptr<kami::Model> model) {
|
||||
auto agent_id = get_agent_id();
|
||||
auto location = _world->get_location_by_agent(agent_id);
|
||||
auto cell_mates = _world->get_location_contents(location);
|
||||
|
||||
if (cell_mates->size() > 1) {
|
||||
std::uniform_int_distribution<int> dist(0, (int)cell_mates->size() - 1);
|
||||
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
|
||||
auto other_agent = std::dynamic_pointer_cast<MoneyAgent2D>(_population->get_agent_by_id(other_agent_id).value());
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain.value());
|
||||
|
||||
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
|
||||
other_agent->_agent_wealth += 1;
|
||||
_agent_wealth -= 1;
|
||||
}
|
||||
auto agents = model->get_population();
|
||||
if (!agents)
|
||||
throw (std::domain_error("model is missing population"));
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents.value());
|
||||
|
||||
auto location = world->get_location_by_agent(agent_id);
|
||||
auto cell_mates = world->get_location_contents(location);
|
||||
|
||||
if (cell_mates->size() < 2)
|
||||
return std::nullopt;
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, (int) cell_mates->size() - 1);
|
||||
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
|
||||
auto other_agent = std::static_pointer_cast<MoneyAgent2D>(population->get_agent_by_id(other_agent_id).value());
|
||||
|
||||
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
|
||||
other_agent->_agent_wealth += 1;
|
||||
_agent_wealth -= 1;
|
||||
|
||||
return other_agent_id;
|
||||
}
|
||||
|
||||
BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsigned int length_x, unsigned int length_y, unsigned int new_seed) {
|
||||
@@ -103,39 +129,39 @@ BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsig
|
||||
rng->seed(new_seed);
|
||||
|
||||
auto domain = std::make_shared<kami::MultiGrid2D>(length_x, length_y, true, true);
|
||||
auto sched = std::make_shared<kami::RandomScheduler>(rng);
|
||||
auto pop = std::make_shared<kami::Population>();
|
||||
auto scheduler = std::make_shared<kami::RandomScheduler>(rng);
|
||||
auto population = std::make_shared<kami::Population>();
|
||||
|
||||
MoneyAgent2D::set_world(domain);
|
||||
MoneyAgent2D::set_population(pop);
|
||||
|
||||
_domain = domain;
|
||||
_sched = sched;
|
||||
_pop = pop;
|
||||
set_domain(domain);
|
||||
set_scheduler(scheduler);
|
||||
set_population(population);
|
||||
|
||||
console->debug("Scheduler initiated with seed {}", new_seed);
|
||||
|
||||
_step_count = 0;
|
||||
|
||||
std::uniform_int_distribution<int> dist_x(0, (int)length_x - 1);
|
||||
std::uniform_int_distribution<int> dist_y(0, (int)length_y - 1);
|
||||
std::uniform_int_distribution<int> dist_x(0, (int) length_x - 1);
|
||||
std::uniform_int_distribution<int> dist_y(0, (int) length_y - 1);
|
||||
|
||||
for (unsigned int i = 0; i < number_agents; i++) {
|
||||
auto new_agent = std::make_shared<MoneyAgent2D>();
|
||||
|
||||
console->trace("Initializing agent with AgentID {}", new_agent->get_agent_id());
|
||||
_pop->add_agent(new_agent);
|
||||
population->add_agent(new_agent);
|
||||
domain->add_agent(new_agent->get_agent_id(), kami::GridCoord2D(dist_x(*rng), dist_x(*rng)));
|
||||
}
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel2D::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++) step();
|
||||
std::shared_ptr<kami::Model> BoltzmannWealthModel2D::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++)
|
||||
step();
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel2D::step() {
|
||||
std::shared_ptr<kami::Model> BoltzmannWealthModel2D::step() {
|
||||
console->trace("Executing model step {}", _step_count++);
|
||||
_sched->step(shared_from_this());
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
@@ -25,11 +25,14 @@
|
||||
|
||||
#pragma once
|
||||
#ifndef BOLTZMANN2D_H
|
||||
//! @cond SuppressGuard
|
||||
#define BOLTZMANN2D_H
|
||||
//! @endcond
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/kami.h>
|
||||
@@ -41,12 +44,6 @@
|
||||
* A sample agent for a two-dimensional Boltzmann wealth model
|
||||
*/
|
||||
class MoneyAgent2D : public kami::Agent {
|
||||
private:
|
||||
inline static std::shared_ptr<kami::MultiGrid2D> _world = nullptr;
|
||||
inline static std::shared_ptr<kami::Population> _population = nullptr;
|
||||
|
||||
int _step_counter;
|
||||
int _agent_wealth;
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -62,28 +59,23 @@ public:
|
||||
/**
|
||||
* Move the agent to a random location on the world
|
||||
*/
|
||||
void move_agent();
|
||||
kami::GridCoord2D move_agent(std::shared_ptr<kami::Model> model);
|
||||
|
||||
/**
|
||||
* Give money to a random agent
|
||||
*/
|
||||
void give_money();
|
||||
std::optional<kami::AgentID> give_money(std::shared_ptr<kami::Model> model);
|
||||
|
||||
static void set_world(std::shared_ptr<kami::MultiGrid2D> world) {
|
||||
_world = std::move(world);
|
||||
}
|
||||
private:
|
||||
int _step_counter;
|
||||
int _agent_wealth;
|
||||
|
||||
static void set_population(std::shared_ptr<kami::Population> population) {
|
||||
_population = std::move(population);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The two-dimensional Boltzmann wealth model
|
||||
*/
|
||||
class BoltzmannWealthModel2D : public kami::Model {
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -102,14 +94,18 @@ public:
|
||||
/**
|
||||
* Execute a single time-step for the model.
|
||||
*/
|
||||
void step() override;
|
||||
std::shared_ptr<kami::Model> step() override;
|
||||
|
||||
/**
|
||||
* Execute a number of time-steps for the model.
|
||||
*
|
||||
* @param[in] n the number of steps to execute.
|
||||
*/
|
||||
void run(unsigned int n) override;
|
||||
std::shared_ptr<kami::Model> run(unsigned int n) override;
|
||||
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
};
|
||||
|
||||
#endif // BOLTZMANN2D_H
|
||||
|
||||
@@ -88,20 +88,22 @@ StarterModel::StarterModel(unsigned int number_agents, unsigned int new_seed) {
|
||||
}
|
||||
}
|
||||
|
||||
void StarterModel::run(unsigned int steps) {
|
||||
std::shared_ptr<kami::Model> StarterModel::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++) step();
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
void StarterModel::step() {
|
||||
std::shared_ptr<kami::Model> StarterModel::step() {
|
||||
console->trace("Executing model step {}", ++_step_count);
|
||||
_sched->step(shared_from_this());
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::string ident = "starter";
|
||||
std::string log_level_option = "info";
|
||||
CLI::App app{ident};
|
||||
unsigned int agent_count = 100, max_steps = 100, initial_seed = 42;
|
||||
unsigned int agent_count = 100, max_steps = 100, initial_seed = 8675309;
|
||||
|
||||
app.add_option("-c", agent_count, "Set the number of agents")->check(CLI::PositiveNumber);
|
||||
app.add_option("-l", log_level_option, "Set the logging level")->check(CLI::IsMember(SPDLOG_LEVEL_NAMES));
|
||||
@@ -117,6 +119,8 @@ int main(int argc, char **argv) {
|
||||
auto model = std::make_shared<StarterModel>(agent_count, initial_seed);
|
||||
|
||||
spdlog::stopwatch sw;
|
||||
for(int i = 0; i < max_steps; i++) model->step();
|
||||
for (int i = 0; i < max_steps; i++)
|
||||
model->step();
|
||||
|
||||
console->info("Starter Model simulation complete, requiring {} seconds", sw);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
|
||||
#pragma once
|
||||
#ifndef STARTER_H
|
||||
//! @cond SuppressGuard
|
||||
#define STARTER_H
|
||||
//! @endcond
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
@@ -80,14 +82,14 @@ public:
|
||||
/**
|
||||
* Execute a single time-step for the model.
|
||||
*/
|
||||
void step() override;
|
||||
std::shared_ptr<kami::Model> step() override;
|
||||
|
||||
/**
|
||||
* Execute a number of time-steps for the model.
|
||||
*
|
||||
* @param[in] n the number of steps to execute.
|
||||
*/
|
||||
void run(unsigned int n) override;
|
||||
std::shared_ptr<kami::Model> run(unsigned int n) override;
|
||||
};
|
||||
|
||||
#endif // STARTER_H
|
||||
|
||||
@@ -43,10 +43,6 @@ namespace kami {
|
||||
* @brief An abstract for generic models
|
||||
*/
|
||||
class LIBKAMI_EXPORT Model : public std::enable_shared_from_this<Model> {
|
||||
protected:
|
||||
std::shared_ptr<Domain> _domain = nullptr;
|
||||
std::shared_ptr<Population> _pop = nullptr;
|
||||
std::shared_ptr<Scheduler> _sched = nullptr;
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -55,19 +51,25 @@ namespace kami {
|
||||
* @details This function should execute a fixed number of time-steps for the model.
|
||||
*
|
||||
* @param[in] n the number of time steps to execute.
|
||||
*
|
||||
* @returns a shared pointer to this instance of `Model`
|
||||
*/
|
||||
virtual void run(unsigned int n) = 0;
|
||||
virtual std::shared_ptr<Model> run(unsigned int n) = 0;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time-step for the model.
|
||||
*
|
||||
* @details This function should step the model instance. Any activities that the
|
||||
* model should perform as part of its time step should be in this function.
|
||||
*
|
||||
* @returns a shared pointer to this instance of `Model`
|
||||
*/
|
||||
virtual void step() = 0;
|
||||
virtual std::shared_ptr<Model> step() = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the `Domain` associated with this model
|
||||
*
|
||||
* @returns a shared pointer to the `Domain`
|
||||
*/
|
||||
std::optional<std::shared_ptr<Domain>> get_domain();
|
||||
|
||||
@@ -76,11 +78,15 @@ namespace kami {
|
||||
*
|
||||
* @details This method will associate a model with the
|
||||
* scheduler.
|
||||
*
|
||||
* @returns a shared pointer to the `Domain`
|
||||
*/
|
||||
void set_domain(std::shared_ptr<Domain> domain);
|
||||
std::shared_ptr<Domain> set_domain(std::shared_ptr<Domain> domain);
|
||||
|
||||
/**
|
||||
* @brief Get the `Population` associated with this model
|
||||
*
|
||||
* @returns a shared pointer to the `Population`
|
||||
*/
|
||||
std::optional<std::shared_ptr<Population>> get_population();
|
||||
|
||||
@@ -89,11 +95,15 @@ namespace kami {
|
||||
*
|
||||
* @details This method will associate a model with the
|
||||
* scheduler.
|
||||
*
|
||||
* @returns a shared pointer to the `Population`
|
||||
*/
|
||||
void set_population(std::shared_ptr<Population> population);
|
||||
std::shared_ptr<Population> set_population(std::shared_ptr<Population> population);
|
||||
|
||||
/**
|
||||
* @brief Get the `Scheduler` associated with this model
|
||||
*
|
||||
* @returns a shared pointer to the `Scheduler`
|
||||
*/
|
||||
std::optional<std::shared_ptr<Scheduler>> get_scheduler();
|
||||
|
||||
@@ -102,8 +112,16 @@ namespace kami {
|
||||
*
|
||||
* @details This method will associate a model with the
|
||||
* scheduler.
|
||||
*
|
||||
* @returns a shared pointer to the `Scheduler`
|
||||
*/
|
||||
void set_scheduler(std::shared_ptr<Scheduler> scheduler);
|
||||
std::shared_ptr<Scheduler> set_scheduler(std::shared_ptr<Scheduler> scheduler);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Domain> _domain = nullptr;
|
||||
std::shared_ptr<Population> _pop = nullptr;
|
||||
std::shared_ptr<Scheduler> _sched = nullptr;
|
||||
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace kami {
|
||||
* That order should be different for each subsequent call to `step()`,
|
||||
* but is not guaranteed not to repeat.
|
||||
*/
|
||||
class LIBKAMI_EXPORT RandomScheduler : public SequentialScheduler {
|
||||
class LIBKAMI_EXPORT RandomScheduler : public SequentialScheduler, std::enable_shared_from_this<RandomScheduler> {
|
||||
private:
|
||||
std::shared_ptr<std::ranlux24> _rng = nullptr;
|
||||
|
||||
@@ -87,8 +87,10 @@ namespace kami {
|
||||
*
|
||||
* @param rng [in] A uniform random number generator of type `std::mt19937`,
|
||||
* used as the source of randomness.
|
||||
*
|
||||
* @returns a shared pointer to this instance of `RandomScheduler`
|
||||
*/
|
||||
void set_rng(std::shared_ptr<std::ranlux24> rng);
|
||||
std::shared_ptr<RandomScheduler> set_rng(std::shared_ptr<std::ranlux24> rng);
|
||||
|
||||
/**
|
||||
* @brief Get the RNG
|
||||
|
||||
@@ -37,8 +37,9 @@ namespace kami {
|
||||
return(_domain);
|
||||
}
|
||||
|
||||
void Model::set_domain(std::shared_ptr<Domain> domain) {
|
||||
std::shared_ptr<Domain> Model::set_domain(std::shared_ptr<Domain> domain) {
|
||||
_domain = std::move(domain);
|
||||
return _domain;
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<Population>> Model::get_population() {
|
||||
@@ -47,8 +48,9 @@ namespace kami {
|
||||
return(_pop);
|
||||
}
|
||||
|
||||
void Model::set_population(std::shared_ptr<Population> population) {
|
||||
std::shared_ptr<Population> Model::set_population(std::shared_ptr<Population> population) {
|
||||
_pop = std::move(population);
|
||||
return _pop;
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<Scheduler>> Model::get_scheduler() {
|
||||
@@ -57,8 +59,9 @@ namespace kami {
|
||||
return(_sched);
|
||||
}
|
||||
|
||||
void Model::set_scheduler(std::shared_ptr<Scheduler> scheduler) {
|
||||
std::shared_ptr<Scheduler> Model::set_scheduler(std::shared_ptr<Scheduler> scheduler) {
|
||||
_sched = std::move(scheduler);
|
||||
return _sched;
|
||||
}
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
namespace kami {
|
||||
|
||||
RandomScheduler::RandomScheduler(std::shared_ptr<std::ranlux24> rng) {
|
||||
this->set_rng(std::move(rng));
|
||||
this->_rng = std::move(rng);
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> RandomScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
@@ -44,8 +44,9 @@ namespace kami {
|
||||
return std::move(this->SequentialScheduler::step(model, agent_list));
|
||||
}
|
||||
|
||||
void RandomScheduler::set_rng(std::shared_ptr<std::ranlux24> rng) {
|
||||
std::shared_ptr<RandomScheduler> RandomScheduler::set_rng(std::shared_ptr<std::ranlux24> rng) {
|
||||
this->_rng = std::move(rng);
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
std::shared_ptr<std::ranlux24> RandomScheduler::get_rng() { return (this->_rng); }
|
||||
|
||||
@@ -39,9 +39,13 @@ public:
|
||||
|
||||
class TestModel : public kami::Model {
|
||||
public:
|
||||
void step() override {}
|
||||
std::shared_ptr<kami::Model> step() override {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
void run(unsigned int) override {}
|
||||
std::shared_ptr<kami::Model> run(unsigned int) override {
|
||||
return shared_from_this();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Agent, DefaultConstructor) {
|
||||
|
||||
@@ -43,9 +43,13 @@ public:
|
||||
|
||||
class TestModel : public kami::Model {
|
||||
public:
|
||||
void step() override {}
|
||||
std::shared_ptr<kami::Model> step() override {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
void run(unsigned int) override {}
|
||||
std::shared_ptr<kami::Model> run(unsigned int) override {
|
||||
return shared_from_this();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Agent, DefaultConstructor) {
|
||||
|
||||
Reference in New Issue
Block a user