Refactoring of the schedulers

This commit is contained in:
James P. Howard, II
2020-12-23 17:31:47 -05:00
parent fcc177db7c
commit 175c1edda8
10 changed files with 94 additions and 86 deletions

View File

@@ -124,7 +124,7 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
world = new MultiGrid1D(lengthX, true);
sched = new RandomScheduler(this, newSeed);
console->debug("Scheduler initiated with seed {}", sched->getSeed());
console->debug("Scheduler initiated with seed {}", sched->get_seed());
stepCount = 0;
MoneyAgent::setWorld(world);
@@ -134,7 +134,7 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
MoneyAgent *newAgent = new MoneyAgent();
agentList.insert(pair<AgentID, MoneyAgent *>(newAgent->get_agent_id(), newAgent));
sched->addAgent(newAgent->get_agent_id());
sched->add_agent(newAgent->get_agent_id());
world->addAgent(newAgent->get_agent_id(), GridCoord1D(rand() % static_cast<int>(lengthX)));
}
}
@@ -165,10 +165,6 @@ void BoltzmannWealthModel::prinfo(void) const {
}
}
int BoltzmannWealthModel::getSeed() const {
return (sched->getSeed());
}
MoneyAgent *BoltzmannWealthModel::getAgentByID(AgentID agentID) const {
MoneyAgent *agentPair = agentList.at(agentID);

View File

@@ -126,7 +126,7 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
world = new MultiGrid2D(lengthX, lengthY, true, true);
sched = new RandomScheduler(this, newSeed);
console->debug("Scheduler initiated with seed {}", sched->getSeed());
console->debug("Scheduler initiated with seed {}", sched->get_seed());
stepCount = 0;
MoneyAgent::setWorld(world);
@@ -136,7 +136,7 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
MoneyAgent *newAgent = new MoneyAgent();
agentList.insert(pair<AgentID, MoneyAgent *>(newAgent->get_agent_id(), newAgent));
sched->addAgent(newAgent->get_agent_id());
sched->add_agent(newAgent->get_agent_id());
world->addAgent(newAgent->get_agent_id(),
GridCoord2D(rand() % static_cast<int>(lengthX),
rand() % static_cast<int>(lengthY)));

View File

@@ -126,7 +126,7 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
world = new kami::MultiGrid3D(lengthX, lengthY, lengthZ, true, true, true);
sched = new kami::RandomScheduler(this, newSeed);
console->debug("Scheduler initiated with seed {}", sched->getSeed());
console->debug("Scheduler initiated with seed {}", sched->get_seed());
stepCount = 0;
MoneyAgent::setWorld(world);
@@ -136,7 +136,7 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
MoneyAgent *newAgent = new MoneyAgent();
agentList.insert(std::pair<kami::AgentID, MoneyAgent *>(newAgent->get_agent_id(), newAgent));
sched->addAgent(newAgent->get_agent_id());
sched->add_agent(newAgent->get_agent_id());
world->addAgent(newAgent->get_agent_id(),
kami::GridCoord3D(rand() % static_cast<int>(lengthX),
rand() % static_cast<int>(lengthY),
@@ -170,10 +170,6 @@ void BoltzmannWealthModel::prinfo(void) const {
}
}
int BoltzmannWealthModel::getSeed() const {
return (sched->getSeed());
}
MoneyAgent *BoltzmannWealthModel::getAgentByID(kami::AgentID agentID) const {
MoneyAgent *agentPair = agentList.at(agentID);

View File

@@ -50,7 +50,7 @@ class LIBKAMI_EXPORT RandomScheduler : public SequentialScheduler {
/// 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.
RandomScheduler(Model *newModel);
RandomScheduler(Model *model);
/// \brief Constructor.
/// \details The Model parameter is used by the scheduler to get
@@ -69,16 +69,16 @@ class LIBKAMI_EXPORT RandomScheduler : public SequentialScheduler {
/// \brief Get the seed used.
/// \details Returns the seed used to initialize the random number
/// generator.
int getSeed(void) const;
int get_seed(void) const;
/// \brief Set the seed used.
/// \details Sets the seed used to initialize the random number generator.
void setSeed(int newSeed);
void set_seed(int seed);
private:
std::random_device rd;
std::mt19937 rng{rd()};
int originalSeed;
std::random_device _rd;
std::mt19937 _rng{_rd()};
int _original_seed;
};
}; // namespace kami

View File

@@ -41,13 +41,13 @@ class LIBKAMI_EXPORT Scheduler {
public:
/// Add an Agent to the Scheduler.
///
/// @param agentID The AgentID of the agent to add.
virtual void addAgent(AgentID agentID) = 0;
/// @param agent_id The AgentID of the agent to add.
virtual void add_agent(AgentID agent_id) = 0;
/// Remove an Agent from the Scheduler.
///
/// @param agentID The AgentID of the agent to remove.
virtual void deleteAgent(AgentID agentID) = 0;
/// @param agent_id The AgentID of the agent to remove.
virtual void delete_agent(AgentID agent_id) = 0;
/// Step the Scheduler.
///

View File

@@ -27,11 +27,12 @@
#ifndef KAMI_SEQUENTIAL_H
#define KAMI_SEQUENTIAL_H
#include <algorithm>
#include <kami/KAMI_EXPORT.h>
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/scheduler.h>
#include <algorithm>
#include <vector>
namespace kami {
@@ -49,17 +50,17 @@ class LIBKAMI_EXPORT SequentialScheduler : public Scheduler {
/// 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(Model *newModel);
SequentialScheduler(Model *model);
/// \brief Add an agent to the scheduler.
/// \details The scheduler maintains a list of all AgentIDs currently
/// assigned. This function adds a new Agent to the list.
void addAgent(AgentID newAgentID);
void add_agent(AgentID agent_id);
/// \brief Remove an agent from the scheduler.
/// \details The scheduler maintains a list of all AgentIDs currently
/// assigned. This function removes an Agent from the list.
void deleteAgent(AgentID oldAgentID);
void delete_agent(AgentID agent_id);
/// \brief Execute a single time step.
/// \details This method will step through the list of Agents in the
@@ -69,9 +70,13 @@ class LIBKAMI_EXPORT SequentialScheduler : public Scheduler {
void step();
protected:
std::vector<AgentID> agentList;
Model *model;
int stepCounter;
/// \brief A vector containing the `AgentID`s of all agents assgined to
/// this scheduler
std::vector<AgentID> _agent_list;
/// \brief A pointer to the `Model` this scehduler belongs to.
Model *_model;
/// \brief Counter to increment on each step.
int _step_counter;
};
}; // namespace kami

View File

@@ -27,11 +27,12 @@
#ifndef KAMI_STAGED_H
#define KAMI_STAGED_H
#include <algorithm>
#include <kami/KAMI_EXPORT.h>
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/scheduler.h>
#include <algorithm>
#include <vector>
namespace kami {
@@ -57,12 +58,12 @@ class LIBKAMI_EXPORT StagedScheduler : public Scheduler {
/// \brief Add an agent to the scheduler.
/// \details The scheduler maintains a list of all AgentIDs currently
/// assigned. This function adds a new Agent to the list.
void addAgent(AgentID);
void add_agent(AgentID agent_id);
/// \brief Remove an agent from the scheduler.
/// \details The scheduler maintains a list of all AgentIDs currently
/// assigned. This function removes an Agent from the list.
void deleteAgent(AgentID);
void delete_agent(AgentID agent_id);
/// \brief Execute a single time step.
/// \details This method will step through the list of Agents in the
@@ -72,9 +73,9 @@ class LIBKAMI_EXPORT StagedScheduler : public Scheduler {
void step();
private:
std::vector<AgentID> agentList;
Model *model;
int stepCounter;
std::vector<AgentID> _agent_list;
Model *_model;
int _step_counter;
};
}; // namespace kami

View File

@@ -29,36 +29,36 @@
#include <kami/scheduler.h>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <iostream>
namespace kami {
RandomScheduler::RandomScheduler(Model *newModel) : SequentialScheduler(newModel) {
auto timeNow = std::chrono::system_clock::now();
auto timeSeconds = std::chrono::time_point_cast<std::chrono::seconds>(timeNow);
auto newSeed = timeSeconds.time_since_epoch().count();
RandomScheduler::RandomScheduler(Model *model) : SequentialScheduler(model) {
using namespace std::chrono;
setSeed(newSeed);
auto time_now = system_clock::now();
auto time_seconds = time_point_cast<seconds>(time_now);
auto seed = time_seconds.time_since_epoch().count();
this->set_seed(seed);
}
RandomScheduler::RandomScheduler(Model *newModel, int newSeed) : SequentialScheduler(newModel) {
setSeed(newSeed);
RandomScheduler::RandomScheduler(Model *model, int seed)
: SequentialScheduler(model) {
this->set_seed(seed);
}
void RandomScheduler::step() {
shuffle(agentList.begin(), agentList.end(), rng);
SequentialScheduler::step();
shuffle(_agent_list.begin(), _agent_list.end(), _rng);
this->SequentialScheduler::step();
}
void RandomScheduler::setSeed(int newSeed) {
originalSeed = newSeed;
rng.seed(originalSeed);
void RandomScheduler::set_seed(int seed) {
this->_rng.seed(this->_original_seed = seed);
}
int RandomScheduler::getSeed(void) const {
return(originalSeed);
}
int RandomScheduler::get_seed(void) const { return (this->_original_seed); }
} // namespace kami

View File

@@ -25,26 +25,28 @@
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/sequential.h>
#include <kami/scheduler.h>
#include <kami/sequential.h>
#include <random>
#include <string>
namespace kami {
SequentialScheduler::SequentialScheduler(Model *newModel) {
stepCounter = 0;
model = newModel;
SequentialScheduler::SequentialScheduler(Model *model) {
_step_counter = 0;
_model = model;
}
void SequentialScheduler::addAgent(AgentID newAgentID) {
agentList.push_back(newAgentID);
void SequentialScheduler::add_agent(AgentID agent_id) {
_agent_list.push_back(agent_id);
}
void SequentialScheduler::deleteAgent(AgentID oldAgentID) {
for (auto agentID = agentList.begin(); agentID < agentList.end(); agentID++) {
if (*agentID == oldAgentID) {
agentList.erase(agentID);
void SequentialScheduler::delete_agent(AgentID agent_id) {
for (auto agent_list_iter = _agent_list.begin();
agent_list_iter < _agent_list.end(); agent_list_iter++) {
if (*agent_list_iter == agent_id) {
_agent_list.erase(agent_list_iter);
return;
}
}
@@ -52,12 +54,12 @@ void SequentialScheduler::deleteAgent(AgentID oldAgentID) {
}
void SequentialScheduler::step() {
stepCounter++;
_step_counter++;
for (auto agentID = agentList.begin(); agentID < agentList.end(); agentID++) {
Agent *agent = model->getAgentByID(*agentID);
if (agent != nullptr)
agent->step();
for (auto agent_list_iter = _agent_list.begin();
agent_list_iter < _agent_list.end(); agent_list_iter++) {
Agent *agent = _model->getAgentByID(*agent_list_iter);
if (agent != nullptr) agent->step();
// ERROR HERE
}
}

View File

@@ -28,26 +28,28 @@
#include <kami/scheduler.h>
#include <kami/sequential.h>
#include <kami/staged.h>
#include <random>
#include <string>
namespace kami {
StagedScheduler::StagedScheduler(Model *newModel) {
stepCounter = 0;
model = newModel;
StagedScheduler::StagedScheduler(Model *model) {
_step_counter = 0;
_model = model;
}
StagedScheduler::~StagedScheduler() {}
void StagedScheduler::addAgent(AgentID newAgentID) {
agentList.push_back(newAgentID);
void StagedScheduler::add_agent(AgentID agent_id) {
_agent_list.push_back(agent_id);
}
void StagedScheduler::deleteAgent(AgentID oldAgentID) {
for (auto agentID = agentList.begin(); agentID < agentList.end(); agentID++) {
if (*agentID == oldAgentID) {
agentList.erase(agentID);
void StagedScheduler::delete_agent(AgentID agent_id) {
for (auto agent_list_iter = _agent_list.begin();
agent_list_iter < _agent_list.end(); agent_list_iter++) {
if (*agent_list_iter == agent_id) {
_agent_list.erase(agent_list_iter);
return;
}
}
@@ -55,20 +57,26 @@ void StagedScheduler::deleteAgent(AgentID oldAgentID) {
}
void StagedScheduler::step() {
stepCounter++;
_step_counter++;
for (auto agentID = agentList.begin(); agentID < agentList.end(); agentID++) {
StagedAgent *agent = dynamic_cast<StagedAgent *>(model->getAgentByID(*agentID));
if (agent != nullptr)
agent->step();
for (auto agent_list_iter = _agent_list.begin();
agent_list_iter < _agent_list.end(); agent_list_iter++) {
StagedAgent *agent =
dynamic_cast<StagedAgent *>(_model->getAgentByID(*agent_list_iter));
if (agent != nullptr) agent->step();
// ERROR HERE
}
for (auto agentID = agentList.begin(); agentID < agentList.end(); agentID++) {
StagedAgent *agent = dynamic_cast<StagedAgent *>(model->getAgentByID(*agentID));
if (agent != nullptr)
agent->advance();
for (auto agent_list_iter = _agent_list.begin();
agent_list_iter < _agent_list.end(); agent_list_iter++) {
StagedAgent *agent =
dynamic_cast<StagedAgent *>(_model->getAgentByID(*agent_list_iter));
if (agent != nullptr) agent->advance();
// ERROR HERE
}
return;
}
} // namespace kami