mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-09 14:58:02 -05:00
Move to exception-based error handling
This commit is contained in:
@@ -71,7 +71,7 @@ public:
|
||||
*/
|
||||
explicit BankAgent(int reserve_percent) : _reserve_percent(reserve_percent) {};
|
||||
|
||||
inline std::optional<std::unique_ptr<nlohmann::json>> collect() override {
|
||||
inline std::unique_ptr<nlohmann::json> collect() override {
|
||||
auto ret = std::make_unique<nlohmann::json>();
|
||||
|
||||
(*ret)["reserves"] = _reserves;
|
||||
@@ -107,7 +107,7 @@ public:
|
||||
explicit PersonAgent(int wallet, std::shared_ptr<BankAgent> &bank) :
|
||||
_wallet(wallet), _bank(bank) {};
|
||||
|
||||
inline std::optional<std::unique_ptr<nlohmann::json>> collect() override {
|
||||
inline std::unique_ptr<nlohmann::json> collect() override {
|
||||
auto ret = std::make_unique<nlohmann::json>();
|
||||
|
||||
(*ret)["savings"] = _savings;
|
||||
@@ -164,8 +164,8 @@ public:
|
||||
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::optional<std::unique_ptr<nlohmann::json>> collect() override {
|
||||
return std::nullopt;
|
||||
inline std::unique_ptr<nlohmann::json> collect() override {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -52,10 +52,9 @@ std::optional<kami::GridCoord2D> PersonAgent::move_agent(std::shared_ptr<kami::R
|
||||
auto agent_id = get_agent_id();
|
||||
|
||||
auto domain = model->get_domain();
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain.value());
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain);
|
||||
|
||||
auto move_list_opt = world->get_neighborhood(agent_id, false, kami::GridNeighborhoodType::Moore);
|
||||
auto move_list = move_list_opt.value();
|
||||
auto move_list = world->get_neighborhood(agent_id, false, kami::GridNeighborhoodType::Moore);
|
||||
std::uniform_int_distribution<int> dist(0, (int) move_list->size() - 1);
|
||||
auto new_location = *std::next(move_list->begin(), dist(*rng));
|
||||
|
||||
@@ -72,14 +71,11 @@ std::optional<kami::AgentID> PersonAgent::do_business(std::shared_ptr<kami::Repo
|
||||
if (!(_savings > 0 | _wallet > 0 | _bank->_available_to_loan > 0))
|
||||
return agent_id;
|
||||
|
||||
auto domain = model->get_domain();
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain.value());
|
||||
|
||||
auto agents = model->get_population();
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents.value());
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(model->get_domain());
|
||||
auto population = model->get_population();
|
||||
|
||||
auto location = world->get_location_by_agent(agent_id);
|
||||
auto cell_mates_opt = world->get_location_contents(location.value());
|
||||
auto cell_mates_opt = world->get_location_contents(location);
|
||||
|
||||
if (!cell_mates_opt)
|
||||
return std::nullopt;
|
||||
@@ -87,7 +83,7 @@ std::optional<kami::AgentID> PersonAgent::do_business(std::shared_ptr<kami::Repo
|
||||
// Note, here we reverse the logic from that used in the Mesa
|
||||
// implementation. We prefer the guard clause to the nested
|
||||
// if statements. See Fowler.
|
||||
auto cell_mates = cell_mates_opt.value();
|
||||
auto cell_mates = cell_mates_opt;
|
||||
if (cell_mates->size() < 2)
|
||||
return std::nullopt;
|
||||
|
||||
@@ -106,7 +102,7 @@ std::optional<kami::AgentID> PersonAgent::do_business(std::shared_ptr<kami::Repo
|
||||
// really, this is just more elegant.
|
||||
auto trade_amount = (int) std::round(coin_flip(*rng)) * 3 + 2;
|
||||
|
||||
auto customer = std::static_pointer_cast<PersonAgent>(population->get_agent_by_id(customer_id).value());
|
||||
auto customer = std::static_pointer_cast<PersonAgent>(population->get_agent_by_id(customer_id));
|
||||
console->debug("Agent {} trading amount {} with agent {}", agent_id, trade_amount, customer_id);
|
||||
customer->_wallet += trade_amount;
|
||||
_wallet -= trade_amount;
|
||||
|
||||
@@ -82,12 +82,12 @@ std::optional<kami::GridCoord1D> MoneyAgent1D::move_agent(std::shared_ptr<kami::
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid1D>(domain.value());
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid1D>(domain);
|
||||
|
||||
auto move_list_opt = world->get_neighborhood(agent_id, false);
|
||||
if (!move_list_opt)
|
||||
return std::nullopt;
|
||||
auto move_list = move_list_opt.value();
|
||||
auto move_list = move_list_opt;
|
||||
std::uniform_int_distribution<int> dist(0, (int) move_list->size() - 1);
|
||||
auto new_location = *std::next(move_list->begin(), dist(*rng));
|
||||
|
||||
@@ -105,26 +105,26 @@ std::optional<kami::AgentID> MoneyAgent1D::give_money(std::shared_ptr<kami::Mode
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid1D>(domain.value());
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid1D>(domain);
|
||||
|
||||
auto agents = model->get_population();
|
||||
if (!agents)
|
||||
throw (std::domain_error("model is missing population"));
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents.value());
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents);
|
||||
|
||||
auto location = world->get_location_by_agent(agent_id);
|
||||
auto cell_mates_opt = world->get_location_contents(location.value());
|
||||
auto cell_mates_opt = world->get_location_contents(location);
|
||||
|
||||
if (!cell_mates_opt)
|
||||
return std::nullopt;
|
||||
|
||||
auto cell_mates = cell_mates_opt.value();
|
||||
auto cell_mates = cell_mates_opt;
|
||||
if (cell_mates->size() < 2)
|
||||
return std::nullopt;
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, (int) cell_mates->size() - 1);
|
||||
auto other_agent_id = *std::next(cell_mates->begin(), dist(*rng));
|
||||
auto other_agent = std::static_pointer_cast<MoneyAgent1D>(population->get_agent_by_id(other_agent_id).value());
|
||||
auto other_agent = std::static_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;
|
||||
|
||||
@@ -82,12 +82,12 @@ std::optional<kami::GridCoord2D> MoneyAgent2D::move_agent(const std::shared_ptr<
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain.value());
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain);
|
||||
|
||||
auto move_list_opt = world->get_neighborhood(agent_id, false, kami::GridNeighborhoodType::VonNeumann);
|
||||
if (!move_list_opt)
|
||||
return std::nullopt;
|
||||
auto move_list = move_list_opt.value();
|
||||
auto move_list = move_list_opt;
|
||||
std::uniform_int_distribution<int> dist(0, (int) move_list->size() - 1);
|
||||
auto new_location = *std::next(move_list->begin(), dist(*rng));
|
||||
|
||||
@@ -105,26 +105,26 @@ std::optional<kami::AgentID> MoneyAgent2D::give_money(const std::shared_ptr<kami
|
||||
auto domain = model->get_domain();
|
||||
if (!domain)
|
||||
throw (std::domain_error("model is missing domain"));
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain.value());
|
||||
auto world = std::static_pointer_cast<kami::MultiGrid2D>(domain);
|
||||
|
||||
auto agents = model->get_population();
|
||||
if (!agents)
|
||||
throw (std::domain_error("model is missing population"));
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents.value());
|
||||
auto population = std::static_pointer_cast<kami::Population>(agents);
|
||||
|
||||
auto location = world->get_location_by_agent(agent_id);
|
||||
auto cell_mates_opt = world->get_location_contents(location.value());
|
||||
auto cell_mates_opt = world->get_location_contents(location);
|
||||
|
||||
if (!cell_mates_opt)
|
||||
return std::nullopt;
|
||||
|
||||
auto cell_mates = cell_mates_opt.value();
|
||||
auto cell_mates = cell_mates_opt;
|
||||
if (cell_mates->size() < 2)
|
||||
return std::nullopt;
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, (int) cell_mates->size() - 1);
|
||||
auto other_agent_id = *std::next(cell_mates->begin(), dist(*rng));
|
||||
auto other_agent = std::static_pointer_cast<MoneyAgent2D>(population->get_agent_by_id(other_agent_id).value());
|
||||
auto other_agent = std::static_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;
|
||||
|
||||
Reference in New Issue
Block a user