Skip to content

Commit

Permalink
style(examples): automatic reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjp committed Jan 23, 2023
1 parent 8cf2193 commit e56c257
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 65 deletions.
7 changes: 5 additions & 2 deletions examples/bankreserves/bankreserves.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ std::shared_ptr<std::mt19937> rng = nullptr;
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EmptyDeclOrStmt"

int main(int argc, char **argv) {
int main(
int argc,
char** argv
) {
std::string ident = "bankreserves";
std::string log_level_option = "info";
std::string output_file_name = ident + ".json";
Expand All @@ -68,7 +71,7 @@ int main(int argc, char **argv) {

// This exercise is really stupid.
auto levels_list = std::make_unique<std::list<std::string>>();
for (auto &level_name: SPDLOG_LEVEL_NAMES)
for (auto& level_name : SPDLOG_LEVEL_NAMES)
levels_list->push_back(std::string(level_name.data(), level_name.size()));

app.add_option("-c", agent_count, "Set the number of agents")->check(CLI::PositiveNumber);
Expand Down
53 changes: 38 additions & 15 deletions examples/bankreserves/bankreserves.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,39 @@ extern std::shared_ptr<spdlog::logger> console;
extern std::shared_ptr<std::mt19937> rng;

template<>
struct fmt::formatter<kami::AgentID> : fmt::formatter<std::string> {
static auto format(kami::AgentID agent_id, format_context &ctx) {
struct fmt::formatter<kami::AgentID>
: fmt::formatter<std::string> {
static auto format(
kami::AgentID agent_id,
format_context& ctx
) {
return format_to(ctx.out(), "{}", agent_id.to_string());
}
};

template<>
struct fmt::formatter<kami::GridCoord2D> : fmt::formatter<std::string> {
static auto format(const kami::GridCoord2D &coord, format_context &ctx) {
struct fmt::formatter<kami::GridCoord2D>
: fmt::formatter<std::string> {
static auto format(
const kami::GridCoord2D& coord,
format_context& ctx
) {
return format_to(ctx.out(), "{}", coord.to_string());
}
};

/**
* A starter agent for a starter model
*/
class BankAgent : public kami::ReporterAgent {
class BankAgent
: public kami::ReporterAgent {
public:
/**
* Constructor
*/
explicit BankAgent(int reserve_percent) : _reserve_percent(reserve_percent) {};
explicit BankAgent(int reserve_percent)
:_reserve_percent(reserve_percent) {
};

inline std::unique_ptr<nlohmann::json> collect() override {
auto ret = std::make_unique<nlohmann::json>();
Expand Down Expand Up @@ -99,13 +110,19 @@ class BankAgent : public kami::ReporterAgent {
friend class PersonAgent;
};

class PersonAgent : public kami::ReporterAgent {
class PersonAgent
: public kami::ReporterAgent {
public:
/**
* Constructor
*/
explicit PersonAgent(int wallet, std::shared_ptr<BankAgent> &bank) :
_wallet(wallet), _bank(bank) {};
explicit PersonAgent(
int wallet,
std::shared_ptr<BankAgent>& bank
)
:
_wallet(wallet), _bank(bank) {
};

inline std::unique_ptr<nlohmann::json> collect() override {
auto ret = std::make_unique<nlohmann::json>();
Expand Down Expand Up @@ -134,11 +151,11 @@ class PersonAgent : public kami::ReporterAgent {
/**
* Move the agent to a random location on the world
*/
std::optional<kami::GridCoord2D> move_agent(std::shared_ptr<kami::ReporterModel> &model);
std::optional<kami::GridCoord2D> move_agent(std::shared_ptr<kami::ReporterModel>& model);

std::optional<kami::AgentID> do_business(std::shared_ptr<kami::ReporterModel> &model);
std::optional<kami::AgentID> do_business(std::shared_ptr<kami::ReporterModel>& model);

std::optional<int> balance_books(std::shared_ptr<kami::ReporterModel> &model);
std::optional<int> balance_books(std::shared_ptr<kami::ReporterModel>& model);

kami::AgentID deposit_to_savings(double amount);

Expand All @@ -152,7 +169,8 @@ class PersonAgent : public kami::ReporterAgent {
/**
* The one-dimensional Boltzmann wealth model
*/
class BankReservesModel : public kami::ReporterModel {
class BankReservesModel
: public kami::ReporterModel {
public:
/**
* Create an instance of the one-dimensional Boltzmann wealth model.
Expand All @@ -161,8 +179,13 @@ class BankReservesModel : public kami::ReporterModel {
* @param[in] length_x the length of the one-dimensional world the agents
* occupy.
*/
explicit BankReservesModel(unsigned int agent_count, unsigned int x_size, unsigned int y_size,
unsigned int initial_seed, unsigned int max_initial_wealth);
explicit BankReservesModel(
unsigned int agent_count,
unsigned int x_size,
unsigned int y_size,
unsigned int initial_seed,
unsigned int max_initial_wealth
);

inline std::unique_ptr<nlohmann::json> collect() override {
return nullptr;
Expand Down
9 changes: 7 additions & 2 deletions examples/bankreserves/brmodel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@
#include <kami/random.h>
#include <kami/reporter.h>

BankReservesModel::BankReservesModel(unsigned int agent_count, unsigned int x_size, unsigned int y_size,
unsigned int initial_seed, unsigned int max_initial_wealth) {
BankReservesModel::BankReservesModel(
unsigned int agent_count,
unsigned int x_size,
unsigned int y_size,
unsigned int initial_seed,
unsigned int max_initial_wealth
) {
rng = std::make_shared<std::mt19937>();
rng->seed(initial_seed);

Expand Down
11 changes: 6 additions & 5 deletions examples/bankreserves/person.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ kami::AgentID PersonAgent::step(std::shared_ptr<kami::ReporterModel> model) {
return get_agent_id();
}

std::optional<kami::GridCoord2D> PersonAgent::move_agent(std::shared_ptr<kami::ReporterModel> &model) {
std::optional<kami::GridCoord2D> PersonAgent::move_agent(std::shared_ptr<kami::ReporterModel>& model) {
console->trace("move_agent() called for agent {}", get_agent_id());
auto agent_id = get_agent_id();

Expand All @@ -64,7 +64,7 @@ std::optional<kami::GridCoord2D> PersonAgent::move_agent(std::shared_ptr<kami::R
return new_location;
}

std::optional<kami::AgentID> PersonAgent::do_business(std::shared_ptr<kami::ReporterModel> &model) {
std::optional<kami::AgentID> PersonAgent::do_business(std::shared_ptr<kami::ReporterModel>& model) {
console->trace("do_business() called for agent {}", get_agent_id());
auto agent_id = get_agent_id();

Expand Down Expand Up @@ -110,9 +110,10 @@ std::optional<kami::AgentID> PersonAgent::do_business(std::shared_ptr<kami::Repo
return customer_id;
}

std::optional<int> PersonAgent::balance_books(std::shared_ptr<kami::ReporterModel> &model) {
console->debug("balance_books() called for agent {} with wallet {}, savings {}, loans {}", get_agent_id(), _wallet,
_savings, _loans);
std::optional<int> PersonAgent::balance_books(std::shared_ptr<kami::ReporterModel>& model) {
console->debug(
"balance_books() called for agent {} with wallet {}, savings {}, loans {}", get_agent_id(), _wallet,
_savings, _loans);

if (_wallet < 0) {
if (_savings >= -_wallet) {
Expand Down
36 changes: 26 additions & 10 deletions examples/boltzmann1d/boltzmann1d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@
std::shared_ptr<spdlog::logger> console = nullptr;
std::shared_ptr<std::mt19937> rng = nullptr;

template <>
struct fmt::formatter<kami::AgentID> : fmt::formatter<std::string> {
static auto format(kami::AgentID agent_id, format_context &ctx) {
template<>
struct fmt::formatter<kami::AgentID>
: fmt::formatter<std::string> {
static auto format(
kami::AgentID agent_id,
format_context& ctx
) {
return format_to(ctx.out(), "{}", agent_id.to_string());
}
};

template<>
struct fmt::formatter<kami::GridCoord1D> : fmt::formatter<std::string> {
static auto format(const kami::GridCoord1D &coord, format_context &ctx) {
struct fmt::formatter<kami::GridCoord1D>
: fmt::formatter<std::string> {
static auto format(
const kami::GridCoord1D& coord,
format_context& ctx
) {
return format_to(ctx.out(), "{}", coord.to_string());
}
};
Expand Down Expand Up @@ -134,7 +142,11 @@ std::optional<kami::AgentID> MoneyAgent1D::give_money(std::shared_ptr<kami::Mode
return other_agent_id;
}

BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x, unsigned int new_seed) {
BoltzmannWealthModel1D::BoltzmannWealthModel1D(
unsigned int number_agents,
unsigned int length_x,
unsigned int new_seed
) {
rng = std::make_shared<std::mt19937>();
rng->seed(new_seed);

Expand Down Expand Up @@ -170,15 +182,18 @@ std::shared_ptr<kami::Model> BoltzmannWealthModel1D::step() {
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EmptyDeclOrStmt"

int main(int argc, char **argv) {
int main(
int argc,
char** argv
) {
std::string ident = "boltzmann1d";
std::string log_level_option = "info";
CLI::App app{ident};
unsigned int x_size = 16, agent_count = x_size, max_steps = 100, initial_seed = 42;

// This exercise is really stupid.
auto levels_list = std::make_unique<std::list<std::string>>();
for (auto &level_name: SPDLOG_LEVEL_NAMES)
for (auto& level_name : SPDLOG_LEVEL_NAMES)
levels_list->push_back(std::string(level_name.data(), level_name.size()));

app.add_option("-c", agent_count, "Set the number of agents")->check(CLI::PositiveNumber);
Expand All @@ -192,8 +207,9 @@ 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);

spdlog::stopwatch sw;

Expand Down
16 changes: 12 additions & 4 deletions examples/boltzmann1d/boltzmann1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
/**
* A sample agent for a one-dimensional Boltzmann wealth model
*/
class MoneyAgent1D : public kami::Agent {
class MoneyAgent1D
: public kami::Agent {

public:
/**
* Create the agent
*/
MoneyAgent1D() : _step_counter(0), _agent_wealth(1) {}
MoneyAgent1D()
:_step_counter(0), _agent_wealth(1) {
}

/**
* Deconstruct the agent
Expand Down Expand Up @@ -81,7 +84,8 @@ class MoneyAgent1D : public kami::Agent {
/**
* The one-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel1D : public kami::Model {
class BoltzmannWealthModel1D
: public kami::Model {

public:
/**
Expand All @@ -91,7 +95,11 @@ class BoltzmannWealthModel1D : public kami::Model {
* @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, unsigned int new_seed = 42);
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.
Expand Down
41 changes: 29 additions & 12 deletions examples/boltzmann2d/boltzmann2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@
std::shared_ptr<spdlog::logger> console = nullptr;
std::shared_ptr<std::mt19937> rng = nullptr;

template <>
struct fmt::formatter<kami::AgentID> : fmt::formatter<std::string> {
static auto format(kami::AgentID agent_id, format_context &ctx) {
template<>
struct fmt::formatter<kami::AgentID>
: fmt::formatter<std::string> {
static auto format(
kami::AgentID agent_id,
format_context& ctx
) {
return format_to(ctx.out(), "{}", agent_id.to_string());
}
};

template<>
struct fmt::formatter<kami::GridCoord2D> : fmt::formatter<std::string> {
static auto format(const kami::GridCoord2D &coord, format_context &ctx) {
struct fmt::formatter<kami::GridCoord2D>
: fmt::formatter<std::string> {
static auto format(
const kami::GridCoord2D& coord,
format_context& ctx
) {
return format_to(ctx.out(), "{}", coord.to_string());
}
};
Expand All @@ -75,7 +83,7 @@ kami::AgentID MoneyAgent2D::step(std::shared_ptr<kami::Model> model) {
return this->get_agent_id();
}

std::optional<kami::GridCoord2D> MoneyAgent2D::move_agent(const std::shared_ptr<kami::Model> &model) {
std::optional<kami::GridCoord2D> MoneyAgent2D::move_agent(const std::shared_ptr<kami::Model>& model) {
console->trace("Entering move_agent");
auto agent_id = get_agent_id();

Expand All @@ -98,7 +106,7 @@ std::optional<kami::GridCoord2D> MoneyAgent2D::move_agent(const std::shared_ptr<
return new_location;
}

std::optional<kami::AgentID> MoneyAgent2D::give_money(const std::shared_ptr<kami::Model> &model) {
std::optional<kami::AgentID> MoneyAgent2D::give_money(const std::shared_ptr<kami::Model>& model) {
console->trace("Entering give_money");
auto agent_id = get_agent_id();

Expand Down Expand Up @@ -134,7 +142,12 @@ std::optional<kami::AgentID> MoneyAgent2D::give_money(const std::shared_ptr<kami
return other_agent_id;
}

BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsigned int length_x, unsigned int length_y, unsigned int new_seed) {
BoltzmannWealthModel2D::BoltzmannWealthModel2D(
unsigned int number_agents,
unsigned int length_x,
unsigned int length_y,
unsigned int new_seed
) {
rng = std::make_shared<std::mt19937>();
rng->seed(new_seed);

Expand Down Expand Up @@ -171,15 +184,18 @@ std::shared_ptr<kami::Model> BoltzmannWealthModel2D::step() {
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EmptyDeclOrStmt"

int main(int argc, char **argv) {
int main(
int argc,
char** argv
) {
std::string ident = "boltzmann2d";
std::string log_level_option = "info";
CLI::App app{ident};
unsigned int x_size = 16, y_size = 16, agent_count = x_size * y_size, max_steps = 100, initial_seed = 42;

// This exercise is really stupid.
auto levels_list = std::make_unique<std::list<std::string>>();
for (auto &level_name: SPDLOG_LEVEL_NAMES)
for (auto& level_name : SPDLOG_LEVEL_NAMES)
levels_list->push_back(std::string(level_name.data(), level_name.size()));

app.add_option("-c", agent_count, "Set the number of agents")->check(CLI::PositiveNumber);
Expand All @@ -194,8 +210,9 @@ 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 {}x{}-unit grid for {} steps", agent_count,
x_size, y_size, max_steps);
console->info(
"Starting Boltzmann Wealth Model with {} agents on a {}x{}-unit grid for {} steps", agent_count,
x_size, y_size, max_steps);

spdlog::stopwatch sw;

Expand Down
Loading

0 comments on commit e56c257

Please sign in to comment.