style(examples): automatic reformatting

This commit is contained in:
James P. Howard, II
2023-01-22 20:44:55 -05:00
parent 8cf2193f7c
commit e56c257707
10 changed files with 162 additions and 65 deletions

View File

@@ -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";
@@ -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);

View File

@@ -48,15 +48,23 @@ 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());
}
};
@@ -64,12 +72,15 @@ struct fmt::formatter<kami::GridCoord2D> : fmt::formatter<std::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>();
@@ -99,13 +110,19 @@ private:
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>();
@@ -134,11 +151,11 @@ private:
/**
* 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);
@@ -152,7 +169,8 @@ private:
/**
* 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.
@@ -161,8 +179,13 @@ public:
* @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;

View File

@@ -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);

View File

@@ -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();
@@ -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();
@@ -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) {

View File

@@ -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());
}
};
@@ -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);
@@ -170,7 +182,10 @@ 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};
@@ -178,7 +193,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);
@@ -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;

View File

@@ -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
@@ -81,7 +84,8 @@ private:
/**
* The one-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel1D : public kami::Model {
class BoltzmannWealthModel1D
: public kami::Model {
public:
/**
@@ -91,7 +95,11 @@ 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, 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.

View File

@@ -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());
}
};
@@ -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();
@@ -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();
@@ -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);
@@ -171,7 +184,10 @@ 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};
@@ -179,7 +195,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);
@@ -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;

View File

@@ -43,13 +43,16 @@
/**
* A sample agent for a two-dimensional Boltzmann wealth model
*/
class MoneyAgent2D : public kami::Agent {
class MoneyAgent2D
: public kami::Agent {
public:
/**
* Create the agent
*/
MoneyAgent2D() : _step_counter(0), _agent_wealth(1) {}
MoneyAgent2D()
:_step_counter(0), _agent_wealth(1) {
}
/**
* Deconstruct the agent
@@ -64,12 +67,12 @@ public:
/**
* Move the agent to a random location on the world
*/
std::optional<kami::GridCoord2D> move_agent(const std::shared_ptr<kami::Model> &model);
std::optional<kami::GridCoord2D> move_agent(const std::shared_ptr<kami::Model>& model);
/**
* Give money to a random agent
*/
std::optional<kami::AgentID> give_money(const std::shared_ptr<kami::Model> &model);
std::optional<kami::AgentID> give_money(const std::shared_ptr<kami::Model>& model);
private:
int _step_counter;
@@ -79,7 +82,8 @@ private:
/**
* The two-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel2D : public kami::Model {
class BoltzmannWealthModel2D
: public kami::Model {
public:
/**
@@ -93,7 +97,12 @@ public:
* @param[in] new_seed the initial seed used for the random number
* generator.
*/
explicit BoltzmannWealthModel2D(unsigned int number_agents = 10, unsigned int length_x = 10, unsigned int length_y = 10, unsigned int new_seed = 42);
explicit BoltzmannWealthModel2D(
unsigned int number_agents = 10,
unsigned int length_x = 10,
unsigned int length_y = 10,
unsigned int new_seed = 42
);
/**
* Execute a single time-step for the model.

View File

@@ -45,9 +45,13 @@
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());
}
};
@@ -65,7 +69,10 @@ kami::AgentID StarterAgent::step(std::shared_ptr<kami::Model> model) {
return this->get_agent_id();
}
StarterModel::StarterModel(unsigned int number_agents, unsigned int new_seed) {
StarterModel::StarterModel(
unsigned int number_agents,
unsigned int new_seed
) {
rng = std::make_shared<std::mt19937>();
rng->seed(new_seed);
@@ -93,7 +100,10 @@ std::shared_ptr<kami::Model> StarterModel::step() {
return shared_from_this();
}
int main(int argc, char **argv) {
int main(
int argc,
char** argv
) {
std::string ident = "starter";
std::string log_level_option = "info";
CLI::App app{ident};
@@ -101,7 +111,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);

View File

@@ -39,7 +39,8 @@
/**
* A starter agent for a starter model
*/
class StarterAgent : public kami::Agent {
class StarterAgent
: public kami::Agent {
private:
int _step_counter = 0;
@@ -65,7 +66,8 @@ public:
/**
* The one-dimensional Boltzmann wealth model
*/
class StarterModel : public kami::Model {
class StarterModel
: public kami::Model {
private:
unsigned int _step_count;
@@ -77,7 +79,10 @@ public:
* @param[in] length_x the length of the one-dimensional world the agents
* occupy.
*/
explicit StarterModel(unsigned int number_agents = 10, unsigned int new_seed = 42);
explicit StarterModel(
unsigned int number_agents = 10,
unsigned int new_seed = 42
);
/**
* Execute a single time-step for the model.