mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-09 14:58:02 -05:00
Started rebuilding the interface around Population requirements
This commit is contained in:
@@ -39,10 +39,11 @@
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/kami.h>
|
||||
#include <kami/model.h>
|
||||
#include <kami/multigrid1d.h>
|
||||
#include <kami/population.h>
|
||||
#include <kami/random.h>
|
||||
|
||||
kami::MultiGrid1D *MoneyAgent1D::_world = nullptr;
|
||||
BoltzmannWealthModel1D *MoneyAgent1D::_model = nullptr;
|
||||
std::shared_ptr<spdlog::logger> console = nullptr;
|
||||
std::shared_ptr<std::mt19937> rng = nullptr;
|
||||
|
||||
@@ -61,18 +62,13 @@ struct fmt::formatter<kami::GridCoord1D> : fmt::formatter<std::string> {
|
||||
};
|
||||
|
||||
void MoneyAgent1D::step() {
|
||||
_step_counter++;
|
||||
this->_step_counter++;
|
||||
|
||||
console->trace("Agent {} is moving", this->get_agent_id());
|
||||
move_agent();
|
||||
console->trace("Agent {} is giving money", this->get_agent_id());
|
||||
if (_agent_wealth > 0) give_money();
|
||||
this->move_agent();
|
||||
if (_agent_wealth > 0) this->give_money();
|
||||
}
|
||||
|
||||
void MoneyAgent1D::set_world(kami::MultiGrid1D *world) { _world = world; }
|
||||
|
||||
void MoneyAgent1D::set_model(BoltzmannWealthModel1D *model) { _model = model; }
|
||||
|
||||
void MoneyAgent1D::move_agent() {
|
||||
console->trace("Entering move_agent");
|
||||
auto agent_id = get_agent_id();
|
||||
@@ -86,14 +82,14 @@ void MoneyAgent1D::move_agent() {
|
||||
}
|
||||
|
||||
void MoneyAgent1D::give_money() {
|
||||
kami::AgentID agent_id = get_agent_id();
|
||||
kami::GridCoord1D location = _world->get_location_by_agent(agent_id);
|
||||
std::vector<kami::AgentID> *cell_mates = _world->get_location_contents(location);
|
||||
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 = _model->get_agent_by_id(other_agent_id);
|
||||
auto other_agent = std::dynamic_pointer_cast<MoneyAgent1D>(_population->get_agent_by_id(other_agent_id));
|
||||
|
||||
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
|
||||
other_agent->_agent_wealth += 1;
|
||||
@@ -101,46 +97,43 @@ void MoneyAgent1D::give_money() {
|
||||
}
|
||||
}
|
||||
|
||||
BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x) {
|
||||
_world = new kami::MultiGrid1D(length_x, true);
|
||||
_sched = new kami::RandomScheduler(this, rng);
|
||||
BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x, unsigned int new_seed) {
|
||||
rng = std::make_shared<std::mt19937>();
|
||||
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>();
|
||||
|
||||
MoneyAgent1D::set_world(domain);
|
||||
MoneyAgent1D::set_population(pop);
|
||||
|
||||
_domain = domain;
|
||||
_sched = sched;
|
||||
_pop = pop;
|
||||
|
||||
console->debug("Scheduler initiated with seed {}", new_seed);
|
||||
|
||||
_step_count = 0;
|
||||
MoneyAgent1D::set_world(_world);
|
||||
MoneyAgent1D::set_model(this);
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, (int)length_x - 1);
|
||||
|
||||
for (unsigned int i = 0; i < number_agents; i++) {
|
||||
auto *new_agent = new MoneyAgent1D();
|
||||
auto new_agent = std::make_shared<MoneyAgent1D>();
|
||||
|
||||
_agent_list.insert(std::pair<kami::AgentID, MoneyAgent1D *>(new_agent->get_agent_id(), new_agent));
|
||||
_sched->add_agent(new_agent->get_agent_id());
|
||||
_world->add_agent(new_agent->get_agent_id(), kami::GridCoord1D(dist(*rng)));
|
||||
console->trace("Initializing agent with AgentID {}", new_agent->get_agent_id());
|
||||
_pop->add_agent(new_agent);
|
||||
domain->add_agent(new_agent->get_agent_id(), kami::GridCoord1D(dist(*rng)));
|
||||
}
|
||||
}
|
||||
|
||||
BoltzmannWealthModel1D::~BoltzmannWealthModel1D() {
|
||||
for (auto & agent_pair : _agent_list)
|
||||
delete agent_pair.second;
|
||||
|
||||
delete _sched;
|
||||
delete _world;
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel1D::step() {
|
||||
_step_count++;
|
||||
_sched->step();
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel1D::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++) step();
|
||||
}
|
||||
|
||||
MoneyAgent1D *BoltzmannWealthModel1D::get_agent_by_id(kami::AgentID _agent_id) const {
|
||||
MoneyAgent1D *_agent_pair = _agent_list.at(_agent_id);
|
||||
|
||||
return _agent_pair;
|
||||
void BoltzmannWealthModel1D::step() {
|
||||
console->trace("Executing model step {}", _step_count++);
|
||||
_sched->step();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -159,15 +152,13 @@ int main(int argc, char **argv) {
|
||||
console = spdlog::stdout_color_st(ident);
|
||||
console->set_level(spdlog::level::from_str(log_level_option));
|
||||
console->info("Compiled with Kami/{}, log level {}", kami::version.to_string(), log_level_option);
|
||||
console->info("Starting Boltzmann Wealth Model with {} agents on a {}-unit grid for {} steps",agent_count, x_size, max_steps);
|
||||
console->info("Starting Boltzmann Wealth Model with {} agents on a {}-unit grid for {} steps", agent_count, x_size, max_steps);
|
||||
|
||||
rng = std::make_shared<std::mt19937>(initial_seed);
|
||||
BoltzmannWealthModel1D model(agent_count, x_size);
|
||||
auto model = std::make_shared<BoltzmannWealthModel1D>(agent_count, x_size, initial_seed);
|
||||
model->get_scheduler()->set_model(model);
|
||||
model->get_population()->set_model(model);
|
||||
|
||||
spdlog::stopwatch sw;
|
||||
for (int i = 0; i < max_steps; i++) {
|
||||
console->trace("Initiating model step {}", i);
|
||||
model.step();
|
||||
}
|
||||
for(int i = 0; i < max_steps; i++) model->step();
|
||||
console->info("Boltzmann Wealth Model simulation complete, requiring {} seconds", sw);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,13 @@
|
||||
* 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:
|
||||
/**
|
||||
* Create the agent
|
||||
@@ -50,16 +57,6 @@ public:
|
||||
*/
|
||||
void step() override;
|
||||
|
||||
/**
|
||||
* Give the agent a reference copy of the domain it is expected to work in
|
||||
*/
|
||||
static void set_world(kami::MultiGrid1D *world);
|
||||
|
||||
/**
|
||||
* Give the agent a reference copy of the model it is expected to work in
|
||||
*/
|
||||
static void set_model(class BoltzmannWealthModel1D *model);
|
||||
|
||||
/**
|
||||
* Move the agent to a random location on the world
|
||||
*/
|
||||
@@ -70,17 +67,22 @@ public:
|
||||
*/
|
||||
void give_money();
|
||||
|
||||
private:
|
||||
static kami::MultiGrid1D *_world;
|
||||
static BoltzmannWealthModel1D *_model;
|
||||
int _step_counter;
|
||||
int _agent_wealth;
|
||||
static void set_world(std::shared_ptr<kami::MultiGrid1D> world) {
|
||||
_world = std::move(world);
|
||||
}
|
||||
|
||||
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 {
|
||||
class BoltzmannWealthModel1D : public kami::Model, std::enable_shared_from_this<BoltzmannWealthModel1D> {
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create an instance of the one-dimensional Boltzmann wealth model.
|
||||
@@ -89,12 +91,7 @@ public:
|
||||
* @param[in] length_x the length of the one-dimensional world the agents
|
||||
* occupy.
|
||||
*/
|
||||
explicit BoltzmannWealthModel1D(unsigned int number_agents = 10, unsigned int length_x = 10);
|
||||
|
||||
/**
|
||||
* Destroy the instance
|
||||
*/
|
||||
~BoltzmannWealthModel1D();
|
||||
explicit BoltzmannWealthModel1D(unsigned int number_agents = 10, unsigned int length_x = 10, unsigned int new_seed = 42);
|
||||
|
||||
/**
|
||||
* Execute a single time-step for the model.
|
||||
@@ -107,19 +104,6 @@ public:
|
||||
* @param[in] n the number of steps to execute.
|
||||
*/
|
||||
void run(unsigned int n) override;
|
||||
|
||||
/**
|
||||
* Get the MoneyAgent instance associated with the given `AgentID`
|
||||
*
|
||||
* @returns an pointer to the `MoneyAgent` that was requested.
|
||||
*/
|
||||
[[nodiscard]] MoneyAgent1D *get_agent_by_id(kami::AgentID agent_id) const override;
|
||||
|
||||
private:
|
||||
std::map<kami::AgentID, MoneyAgent1D *> _agent_list;
|
||||
kami::RandomScheduler *_sched;
|
||||
kami::MultiGrid1D *_world;
|
||||
unsigned int _step_count;
|
||||
};
|
||||
|
||||
#endif // BOLTZMANN1D_H
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
|
||||
#include "boltzmann2d.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
@@ -40,9 +42,9 @@
|
||||
#include <kami/agent.h>
|
||||
#include <kami/kami.h>
|
||||
#include <kami/multigrid2d.h>
|
||||
#include <kami/population.h>
|
||||
#include <kami/random.h>
|
||||
|
||||
kami::MultiGrid2D *MoneyAgent2D::_world = nullptr;
|
||||
BoltzmannWealthModel2D *MoneyAgent2D::_model = nullptr;
|
||||
std::shared_ptr<spdlog::logger> console = nullptr;
|
||||
std::shared_ptr<std::mt19937> rng = nullptr;
|
||||
|
||||
@@ -61,18 +63,13 @@ struct fmt::formatter<kami::GridCoord2D> : fmt::formatter<std::string> {
|
||||
};
|
||||
|
||||
void MoneyAgent2D::step() {
|
||||
_step_counter++;
|
||||
this->_step_counter++;
|
||||
|
||||
console->trace("Agent {} is moving", this->get_agent_id());
|
||||
move_agent();
|
||||
console->trace("Agent {} is giving money", this->get_agent_id());
|
||||
if (_agent_wealth > 0) give_money();
|
||||
this->move_agent();
|
||||
if(_agent_wealth > 0) this->give_money();
|
||||
}
|
||||
|
||||
void MoneyAgent2D::set_world(kami::MultiGrid2D *world) { _world = world; }
|
||||
|
||||
void MoneyAgent2D::set_model(BoltzmannWealthModel2D *model) { _model = model; }
|
||||
|
||||
void MoneyAgent2D::move_agent() {
|
||||
console->trace("Entering move_agent");
|
||||
auto agent_id = get_agent_id();
|
||||
@@ -86,14 +83,14 @@ void MoneyAgent2D::move_agent() {
|
||||
}
|
||||
|
||||
void MoneyAgent2D::give_money() {
|
||||
kami::AgentID agent_id = get_agent_id();
|
||||
kami::GridCoord2D location = _world->get_location_by_agent(agent_id);
|
||||
std::vector<kami::AgentID> *cell_mates = _world->get_location_contents(location);
|
||||
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 = _model->get_agent_by_id(other_agent_id);
|
||||
auto other_agent = std::dynamic_pointer_cast<MoneyAgent2D>(_population->get_agent_by_id(other_agent_id));
|
||||
|
||||
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
|
||||
other_agent->_agent_wealth += 1;
|
||||
@@ -105,47 +102,40 @@ BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsig
|
||||
rng = std::make_shared<std::mt19937>();
|
||||
rng->seed(new_seed);
|
||||
|
||||
_world = new kami::MultiGrid2D(length_x, length_y, true, true);
|
||||
_sched = new kami::RandomScheduler(this, rng);
|
||||
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>();
|
||||
|
||||
MoneyAgent2D::set_world(domain);
|
||||
MoneyAgent2D::set_population(pop);
|
||||
|
||||
_domain = domain;
|
||||
_sched = sched;
|
||||
_pop = pop;
|
||||
|
||||
console->debug("Scheduler initiated with seed {}", new_seed);
|
||||
|
||||
_step_count = 0;
|
||||
MoneyAgent2D::set_world(_world);
|
||||
MoneyAgent2D::set_model(this);
|
||||
|
||||
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 = new MoneyAgent2D();
|
||||
auto new_agent = std::make_shared<MoneyAgent2D>();
|
||||
|
||||
_agent_list.insert(std::pair<kami::AgentID, MoneyAgent2D *>(new_agent->get_agent_id(), new_agent));
|
||||
_sched->add_agent(new_agent->get_agent_id());
|
||||
_world->add_agent(new_agent->get_agent_id(), kami::GridCoord2D(dist_x(*rng), dist_x(*rng)));
|
||||
console->trace("Initializing agent with AgentID {}", new_agent->get_agent_id());
|
||||
_pop->add_agent(new_agent);
|
||||
domain->add_agent(new_agent->get_agent_id(), kami::GridCoord2D(dist_x(*rng), dist_x(*rng)));
|
||||
}
|
||||
}
|
||||
|
||||
BoltzmannWealthModel2D::~BoltzmannWealthModel2D() {
|
||||
for (auto & agent_pair : _agent_list)
|
||||
delete agent_pair.second;
|
||||
|
||||
delete _sched;
|
||||
delete _world;
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel2D::step() {
|
||||
_step_count++;
|
||||
_sched->step();
|
||||
}
|
||||
|
||||
void BoltzmannWealthModel2D::run(unsigned int steps) {
|
||||
for (auto i = 0; i < steps; i++) step();
|
||||
}
|
||||
|
||||
MoneyAgent2D *BoltzmannWealthModel2D::get_agent_by_id(kami::AgentID agent_id) const {
|
||||
MoneyAgent2D *agent = _agent_list.at(agent_id);
|
||||
return agent;
|
||||
void BoltzmannWealthModel2D::step() {
|
||||
console->trace("Executing model step {}", _step_count++);
|
||||
_sched->step();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -167,12 +157,11 @@ int main(int argc, char **argv) {
|
||||
console->info("Compiled with Kami/{}, log level {}", kami::version.to_string(), log_level_option);
|
||||
console->info("Starting Boltzmann Wealth Model with {} agents on a {}x{}-unit grid for {} steps", agent_count, x_size, y_size, max_steps);
|
||||
|
||||
BoltzmannWealthModel2D model(agent_count, x_size, y_size, initial_seed);
|
||||
auto model = std::make_shared<BoltzmannWealthModel2D>(agent_count, x_size, y_size, initial_seed);
|
||||
model->get_scheduler()->set_model(model);
|
||||
model->get_population()->set_model(model);
|
||||
|
||||
spdlog::stopwatch sw;
|
||||
for (int i = 0; i < max_steps; i++) {
|
||||
console->trace("Initiating model step {}", i);
|
||||
model.step();
|
||||
}
|
||||
for(int i = 0; i < max_steps; i++) model->step();
|
||||
console->info("Boltzmann Wealth Model simulation complete, requiring {} seconds", sw);
|
||||
}
|
||||
|
||||
@@ -29,16 +29,25 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/kami.h>
|
||||
#include <kami/multigrid2d.h>
|
||||
#include <kami/population.h>
|
||||
#include <kami/random.h>
|
||||
|
||||
/**
|
||||
* 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:
|
||||
/**
|
||||
* Create the agent
|
||||
@@ -50,16 +59,6 @@ public:
|
||||
*/
|
||||
void step() override;
|
||||
|
||||
/**
|
||||
* Give the agent a reference copy of the domain it is expected to work in
|
||||
*/
|
||||
static void set_world(kami::MultiGrid2D *world);
|
||||
|
||||
/**
|
||||
* Give the agent a reference copy of the model it is expected to work in
|
||||
*/
|
||||
static void set_model(class BoltzmannWealthModel2D *model);
|
||||
|
||||
/**
|
||||
* Move the agent to a random location on the world
|
||||
*/
|
||||
@@ -70,17 +69,22 @@ public:
|
||||
*/
|
||||
void give_money();
|
||||
|
||||
private:
|
||||
static kami::MultiGrid2D *_world;
|
||||
static BoltzmannWealthModel2D *_model;
|
||||
int _step_counter;
|
||||
int _agent_wealth;
|
||||
static void set_world(std::shared_ptr<kami::MultiGrid2D> world) {
|
||||
_world = std::move(world);
|
||||
}
|
||||
|
||||
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 {
|
||||
class BoltzmannWealthModel2D : public kami::Model, std::enable_shared_from_this<BoltzmannWealthModel2D> {
|
||||
private:
|
||||
unsigned int _step_count;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create an instance of the two-dimensional Boltzmann wealth model.
|
||||
@@ -95,11 +99,6 @@ public:
|
||||
*/
|
||||
explicit BoltzmannWealthModel2D(unsigned int number_agents = 10, unsigned int length_x = 10, unsigned int length_y = 10, unsigned int new_seed = 42);
|
||||
|
||||
/**
|
||||
* Destroy the instance
|
||||
*/
|
||||
~BoltzmannWealthModel2D();
|
||||
|
||||
/**
|
||||
* Execute a single time-step for the model.
|
||||
*/
|
||||
@@ -111,19 +110,6 @@ public:
|
||||
* @param[in] n the number of steps to execute.
|
||||
*/
|
||||
void run(unsigned int n) override;
|
||||
|
||||
/**
|
||||
* Get the MoneyAgent2D instance associated with the given `AgentID`
|
||||
*
|
||||
* @returns an pointer to the `MoneyAgent2D` that was requested.
|
||||
*/
|
||||
[[nodiscard]] MoneyAgent2D *get_agent_by_id(kami::AgentID agent_id) const override;
|
||||
|
||||
private:
|
||||
std::map<kami::AgentID, MoneyAgent2D *> _agent_list;
|
||||
kami::RandomScheduler *_sched;
|
||||
kami::MultiGrid2D *_world;
|
||||
unsigned int _step_count;
|
||||
};
|
||||
|
||||
#endif // BOLTZMANN2D_H
|
||||
|
||||
Reference in New Issue
Block a user