Rearrange header files and remove using namespaces to comply with Google recommendations

This commit is contained in:
James P. Howard, II
2022-07-01 17:50:48 -04:00
parent cf0b75c5fb
commit f5ae008df5
4 changed files with 75 additions and 87 deletions

View File

@@ -23,42 +23,39 @@
* SOFTWARE.
*/
#include "boltzmann1d.h"
#include <map>
#include <memory>
#include <random>
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid1d.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <spdlog/stopwatch.h>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include "boltzmann1d.h"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <spdlog/stopwatch.h>
using namespace kami;
using namespace std;
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid1d.h>
MultiGrid1D *MoneyAgent1D::_world = nullptr;
kami::MultiGrid1D *MoneyAgent1D::_world = nullptr;
BoltzmannWealthModel1D *MoneyAgent1D::_model = nullptr;
shared_ptr<spdlog::logger> console = nullptr;
shared_ptr<mt19937> rng = nullptr;
std::shared_ptr<spdlog::logger> console = nullptr;
std::shared_ptr<std::mt19937> rng = nullptr;
template <>
struct fmt::formatter<AgentID> : fmt::formatter<string> {
[[maybe_unused]] static auto format(AgentID agent_id, format_context &ctx) {
struct fmt::formatter<kami::AgentID> : fmt::formatter<std::string> {
[[maybe_unused]] static auto format(kami::AgentID agent_id, format_context &ctx) {
return format_to(ctx.out(), "{}", agent_id.to_string());
}
};
template <>
struct fmt::formatter<GridCoord1D> : fmt::formatter<string> {
[[maybe_unused]] static auto format(const GridCoord1D& coord, format_context &ctx) {
struct fmt::formatter<kami::GridCoord1D> : fmt::formatter<std::string> {
[[maybe_unused]] static auto format(const kami::GridCoord1D& coord, format_context &ctx) {
return format_to(ctx.out(), "{}", coord.to_string());
}
};
@@ -72,7 +69,7 @@ void MoneyAgent1D::step() {
if (_agent_wealth > 0) give_money();
}
void MoneyAgent1D::set_world(MultiGrid1D *world) { _world = world; }
void MoneyAgent1D::set_world(kami::MultiGrid1D *world) { _world = world; }
void MoneyAgent1D::set_model(BoltzmannWealthModel1D *model) { _model = model; }
@@ -89,13 +86,13 @@ void MoneyAgent1D::move_agent() {
}
void MoneyAgent1D::give_money() {
AgentID agent_id = get_agent_id();
GridCoord1D location = _world->get_location_by_agent(agent_id);
vector<AgentID> *cell_mates = _world->get_location_contents(location);
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);
if (cell_mates->size() > 1) {
std::uniform_int_distribution<int> dist(0, (int)cell_mates->size() - 1);
AgentID other_agent_id = cell_mates->at(dist(*rng));
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
auto other_agent = _model->get_agent_by_id(other_agent_id);
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
@@ -105,8 +102,8 @@ void MoneyAgent1D::give_money() {
}
BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x) {
_world = new MultiGrid1D(length_x, true);
_sched = new RandomScheduler(this, rng);
_world = new kami::MultiGrid1D(length_x, true);
_sched = new kami::RandomScheduler(this, rng);
_step_count = 0;
MoneyAgent1D::set_world(_world);
@@ -117,9 +114,9 @@ BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsig
for (unsigned int i = 0; i < number_agents; i++) {
auto *new_agent = new MoneyAgent1D();
_agent_list.insert(pair<AgentID, MoneyAgent1D *>(new_agent->get_agent_id(), new_agent));
_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(), GridCoord1D(dist(*rng)));
_world->add_agent(new_agent->get_agent_id(), kami::GridCoord1D(dist(*rng)));
}
}
@@ -140,16 +137,16 @@ void BoltzmannWealthModel1D::run(unsigned int steps) {
for (auto i = 0; i < steps; i++) step();
}
MoneyAgent1D *BoltzmannWealthModel1D::get_agent_by_id(AgentID _agent_id) const {
MoneyAgent1D *BoltzmannWealthModel1D::get_agent_by_id(kami::AgentID _agent_id) const {
MoneyAgent1D *_agent_pair = _agent_list.at(_agent_id);
return _agent_pair;
}
int main(int argc, char **argv) {
string ident = "boltzmann1d";
std::string ident = "boltzmann1d";
std::string log_level_option = "info";
CLI::App app{ident};
string log_level_option = "info";
unsigned int x_size = 16, agent_count = x_size, max_steps = 100, initial_seed = 42;
app.add_option("-c", agent_count, "Set the number of agents")->check(CLI::PositiveNumber);
@@ -164,7 +161,7 @@ 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 {}-unit grid for {} steps",agent_count, x_size, max_steps);
rng = make_shared<mt19937>(initial_seed);
rng = std::make_shared<std::mt19937>(initial_seed);
BoltzmannWealthModel1D model(agent_count, x_size);
spdlog::stopwatch sw;

View File

@@ -35,13 +35,10 @@
#include <kami/multigrid1d.h>
#include <kami/random.h>
using namespace kami;
using namespace std;
/**
* A sample agent for a one-dimensional Boltzmann wealth model
*/
class MoneyAgent1D : public Agent {
class MoneyAgent1D : public kami::Agent {
public:
/**
* Create the agent
@@ -56,7 +53,7 @@ public:
/**
* Give the agent a reference copy of the domain it is expected to work in
*/
static void set_world(MultiGrid1D *world);
static void set_world(kami::MultiGrid1D *world);
/**
* Give the agent a reference copy of the model it is expected to work in
@@ -74,7 +71,7 @@ public:
void give_money();
private:
static MultiGrid1D *_world;
static kami::MultiGrid1D *_world;
static BoltzmannWealthModel1D *_model;
int _step_counter;
int _agent_wealth;
@@ -83,7 +80,7 @@ private:
/**
* The one-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel1D : public Model {
class BoltzmannWealthModel1D : public kami::Model {
public:
/**
* Create an instance of the one-dimensional Boltzmann wealth model.
@@ -116,12 +113,12 @@ public:
*
* @returns an pointer to the `MoneyAgent` that was requested.
*/
[[nodiscard]] MoneyAgent1D *get_agent_by_id(AgentID agent_id) const override;
[[nodiscard]] MoneyAgent1D *get_agent_by_id(kami::AgentID agent_id) const override;
private:
map<AgentID, MoneyAgent1D *> _agent_list;
RandomScheduler *_sched;
MultiGrid1D *_world;
std::map<kami::AgentID, MoneyAgent1D *> _agent_list;
kami::RandomScheduler *_sched;
kami::MultiGrid1D *_world;
unsigned int _step_count;
};

View File

@@ -23,42 +23,39 @@
* SOFTWARE.
*/
#include "boltzmann2d.h"
#include <map>
#include <memory>
#include <random>
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid2d.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <spdlog/stopwatch.h>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include "boltzmann2d.h"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <spdlog/stopwatch.h>
using namespace kami;
using namespace std;
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid2d.h>
MultiGrid2D *MoneyAgent2D::_world = nullptr;
kami::MultiGrid2D *MoneyAgent2D::_world = nullptr;
BoltzmannWealthModel2D *MoneyAgent2D::_model = nullptr;
shared_ptr<spdlog::logger> console = nullptr;
shared_ptr<mt19937> rng = nullptr;
std::shared_ptr<spdlog::logger> console = nullptr;
std::shared_ptr<std::mt19937> rng = nullptr;
template <>
struct fmt::formatter<AgentID> : fmt::formatter<string> {
[[maybe_unused]] static auto format(AgentID agent_id, format_context &ctx) {
struct fmt::formatter<kami::AgentID> : fmt::formatter<std::string> {
[[maybe_unused]] static auto format(kami::AgentID agent_id, format_context &ctx) {
return format_to(ctx.out(), "{}", agent_id.to_string());
}
};
template <>
struct fmt::formatter<GridCoord2D> : fmt::formatter<string> {
[[maybe_unused]] static auto format(const GridCoord2D& coord, format_context &ctx) {
struct fmt::formatter<kami::GridCoord2D> : fmt::formatter<std::string> {
[[maybe_unused]] static auto format(const kami::GridCoord2D& coord, format_context &ctx) {
return format_to(ctx.out(), "{}", coord.to_string());
}
};
@@ -72,14 +69,14 @@ void MoneyAgent2D::step() {
if (_agent_wealth > 0) give_money();
}
void MoneyAgent2D::set_world(MultiGrid2D *world) { _world = world; }
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();
auto move_list = _world->get_neighborhood(agent_id, GridNeighborhoodType::Moore, false);
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)];
@@ -89,13 +86,13 @@ void MoneyAgent2D::move_agent() {
}
void MoneyAgent2D::give_money() {
AgentID agent_id = get_agent_id();
GridCoord2D location = _world->get_location_by_agent(agent_id);
vector<AgentID> *cell_mates = _world->get_location_contents(location);
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);
if (cell_mates->size() > 1) {
std::uniform_int_distribution<int> dist(0, (int)cell_mates->size() - 1);
AgentID other_agent_id = cell_mates->at(dist(*rng));
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
auto other_agent = _model->get_agent_by_id(other_agent_id);
console->trace("Agent {} giving unit of wealth to agent {}", agent_id, other_agent_id);
@@ -105,11 +102,11 @@ void MoneyAgent2D::give_money() {
}
BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsigned int length_x, unsigned int length_y, unsigned int new_seed) {
rng = make_shared<mt19937>();
rng = std::make_shared<std::mt19937>();
rng->seed(new_seed);
_world = new MultiGrid2D(length_x, length_y, true, true);
_sched = new RandomScheduler(this, rng);
_world = new kami::MultiGrid2D(length_x, length_y, true, true);
_sched = new kami::RandomScheduler(this, rng);
console->debug("Scheduler initiated with seed {}", new_seed);
@@ -123,9 +120,9 @@ BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsig
for (unsigned int i = 0; i < number_agents; i++) {
auto *new_agent = new MoneyAgent2D();
_agent_list.insert(pair<AgentID, MoneyAgent2D *>(new_agent->get_agent_id(), new_agent));
_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(), GridCoord2D(dist_x(*rng), dist_x(*rng)));
_world->add_agent(new_agent->get_agent_id(), kami::GridCoord2D(dist_x(*rng), dist_x(*rng)));
}
}
@@ -146,15 +143,15 @@ void BoltzmannWealthModel2D::run(unsigned int steps) {
for (auto i = 0; i < steps; i++) step();
}
MoneyAgent2D *BoltzmannWealthModel2D::get_agent_by_id(AgentID agent_id) const {
MoneyAgent2D *BoltzmannWealthModel2D::get_agent_by_id(kami::AgentID agent_id) const {
MoneyAgent2D *agent = _agent_list.at(agent_id);
return agent;
}
int main(int argc, char **argv) {
std::string ident = "boltzmann2d";
std::string log_level_option = "info";
CLI::App app{ident};
string log_level_option = "info";
unsigned int x_size = 16, y_size = 16, agent_count = x_size * y_size, max_steps = 100, initial_seed = 42;
app.add_option("-c", agent_count, "Set the number of agents")->check(CLI::PositiveNumber);

View File

@@ -27,21 +27,18 @@
#ifndef BOLTZMANN2D_H
#define BOLTZMANN2D_H
#include <iostream>
#include <map>
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid2d.h>
#include <kami/random.h>
#include <iostream>
#include <map>
using namespace kami;
using namespace std;
/**
* A sample agent for a two-dimensional Boltzmann wealth model
*/
class MoneyAgent2D : public Agent {
class MoneyAgent2D : public kami::Agent {
public:
/**
* Create the agent
@@ -56,7 +53,7 @@ public:
/**
* Give the agent a reference copy of the domain it is expected to work in
*/
static void set_world(MultiGrid2D *world);
static void set_world(kami::MultiGrid2D *world);
/**
* Give the agent a reference copy of the model it is expected to work in
@@ -74,7 +71,7 @@ public:
void give_money();
private:
static MultiGrid2D *_world;
static kami::MultiGrid2D *_world;
static BoltzmannWealthModel2D *_model;
int _step_counter;
int _agent_wealth;
@@ -83,7 +80,7 @@ private:
/**
* The two-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel2D : public Model {
class BoltzmannWealthModel2D : public kami::Model {
public:
/**
* Create an instance of the two-dimensional Boltzmann wealth model.
@@ -120,12 +117,12 @@ public:
*
* @returns an pointer to the `MoneyAgent2D` that was requested.
*/
[[nodiscard]] MoneyAgent2D *get_agent_by_id(AgentID agent_id) const override;
[[nodiscard]] MoneyAgent2D *get_agent_by_id(kami::AgentID agent_id) const override;
private:
map<AgentID, MoneyAgent2D *> _agent_list;
RandomScheduler *_sched;
MultiGrid2D *_world;
std::map<kami::AgentID, MoneyAgent2D *> _agent_list;
kami::RandomScheduler *_sched;
kami::MultiGrid2D *_world;
unsigned int _step_count;
};