Refactored agents

This commit is contained in:
James P. Howard, II
2020-12-23 16:47:27 -05:00
parent 4a8947b62d
commit fcc177db7c
7 changed files with 59 additions and 65 deletions

View File

@@ -49,7 +49,7 @@ shared_ptr<spdlog::logger> console;
template <>
struct fmt::formatter<AgentID> : fmt::formatter<string> {
auto format(AgentID agentID, format_context &ctx) {
return format_to(ctx.out(), "{}", agentID.toString());
return format_to(ctx.out(), "{}", agentID.to_string());
}
};
@@ -82,7 +82,7 @@ void MoneyAgent::setModel(class BoltzmannWealthModel *m) {
}
void MoneyAgent::moveAgent() {
auto agentID = this->getAgentID();
auto agentID = this->get_agent_id();
auto moveList = world->getNeighborhood(agentID, false);
auto newLocation = moveList[static_cast<unsigned int>(rand()) % moveList.size()];
@@ -99,7 +99,7 @@ void MoneyAgent::setWealth(int newWealth) {
}
void MoneyAgent::giveMoney() {
AgentID agentID = getAgentID();
AgentID agentID = get_agent_id();
GridCoord1D location = world->getLocationByAgent(agentID);
vector<AgentID> *cellMates = world->getCellContents(location);
@@ -114,7 +114,7 @@ void MoneyAgent::giveMoney() {
}
void MoneyAgent::prinfo(void) const {
AgentID agentID = getAgentID();
AgentID agentID = get_agent_id();
GridCoord1D location = world->getLocationByAgent(agentID);
console->trace("Agent state for agent {}, step {}, wealth {}, location {}", agentID, stepCounter, agentWealth, location);
@@ -133,9 +133,9 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
for (unsigned int i = 0; i < numberAgents; i++) {
MoneyAgent *newAgent = new MoneyAgent();
agentList.insert(pair<AgentID, MoneyAgent *>(newAgent->getAgentID(), newAgent));
sched->addAgent(newAgent->getAgentID());
world->addAgent(newAgent->getAgentID(), GridCoord1D(rand() % static_cast<int>(lengthX)));
agentList.insert(pair<AgentID, MoneyAgent *>(newAgent->get_agent_id(), newAgent));
sched->addAgent(newAgent->get_agent_id());
world->addAgent(newAgent->get_agent_id(), GridCoord1D(rand() % static_cast<int>(lengthX)));
}
}

View File

@@ -51,7 +51,7 @@ shared_ptr<spdlog::logger> console;
template <>
struct fmt::formatter<AgentID> : fmt::formatter<string> {
auto format(AgentID agentID, format_context &ctx) {
return format_to(ctx.out(), "{}", agentID.toString());
return format_to(ctx.out(), "{}", agentID.to_string());
}
};
@@ -84,7 +84,7 @@ void MoneyAgent::setModel(class BoltzmannWealthModel *m) {
}
void MoneyAgent::moveAgent() {
auto agentID = this->getAgentID();
auto agentID = this->get_agent_id();
auto moveList = world->getNeighborhood(agentID, GridNeighborhoodType::Moore, false);
auto newLocation = moveList[static_cast<unsigned int>(rand()) % moveList.size()];
@@ -101,7 +101,7 @@ void MoneyAgent::setWealth(int newWealth) {
}
void MoneyAgent::giveMoney() {
AgentID agentID = getAgentID();
AgentID agentID = get_agent_id();
GridCoord2D location = world->getLocationByAgent(agentID);
vector<AgentID> *cellMates = world->getCellContents(location);
@@ -116,7 +116,7 @@ void MoneyAgent::giveMoney() {
}
void MoneyAgent::prinfo(void) const {
AgentID agentID = getAgentID();
AgentID agentID = get_agent_id();
GridCoord2D location = world->getLocationByAgent(agentID);
console->trace("Agent state for agent {}, step {}, wealth {}, location {}", agentID, stepCounter, agentWealth, location);
@@ -135,9 +135,9 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
for (unsigned int i = 0; i < numberAgents; i++) {
MoneyAgent *newAgent = new MoneyAgent();
agentList.insert(pair<AgentID, MoneyAgent *>(newAgent->getAgentID(), newAgent));
sched->addAgent(newAgent->getAgentID());
world->addAgent(newAgent->getAgentID(),
agentList.insert(pair<AgentID, MoneyAgent *>(newAgent->get_agent_id(), newAgent));
sched->addAgent(newAgent->get_agent_id());
world->addAgent(newAgent->get_agent_id(),
GridCoord2D(rand() % static_cast<int>(lengthX),
rand() % static_cast<int>(lengthY)));
}

View File

@@ -51,7 +51,7 @@ shared_ptr<spdlog::logger> console;
template <>
struct fmt::formatter<AgentID> : fmt::formatter<string> {
auto format(AgentID agentID, format_context &ctx) {
return format_to(ctx.out(), "{}", agentID.toString());
return format_to(ctx.out(), "{}", agentID.to_string());
}
};
@@ -84,7 +84,7 @@ void MoneyAgent::setModel(class BoltzmannWealthModel *m) {
}
void MoneyAgent::moveAgent() {
auto agentID = this->getAgentID();
auto agentID = this->get_agent_id();
auto moveList = world->getNeighborhood(agentID, kami::GridNeighborhoodType::Moore, false);
auto newLocation = moveList[static_cast<unsigned int>(rand()) % moveList.size()];
@@ -101,7 +101,7 @@ void MoneyAgent::setWealth(int newWealth) {
}
void MoneyAgent::giveMoney() {
kami::AgentID agentID = getAgentID();
kami::AgentID agentID = get_agent_id();
kami::GridCoord3D location = world->getLocationByAgent(agentID);
std::vector<kami::AgentID> *cellMates = world->getCellContents(location);
@@ -116,7 +116,7 @@ void MoneyAgent::giveMoney() {
}
void MoneyAgent::prinfo(void) const {
kami::AgentID agentID = getAgentID();
kami::AgentID agentID = get_agent_id();
kami::GridCoord3D location = world->getLocationByAgent(agentID);
console->trace("Agent state for agent {}, step {}, wealth {}, location {}", agentID, stepCounter, agentWealth, location);
@@ -135,9 +135,9 @@ BoltzmannWealthModel::BoltzmannWealthModel(unsigned int numberAgents, unsigned i
for (unsigned int i = 0; i < numberAgents; i++) {
MoneyAgent *newAgent = new MoneyAgent();
agentList.insert(std::pair<kami::AgentID, MoneyAgent *>(newAgent->getAgentID(), newAgent));
sched->addAgent(newAgent->getAgentID());
world->addAgent(newAgent->getAgentID(),
agentList.insert(std::pair<kami::AgentID, MoneyAgent *>(newAgent->get_agent_id(), newAgent));
sched->addAgent(newAgent->get_agent_id());
world->addAgent(newAgent->get_agent_id(),
kami::GridCoord3D(rand() % static_cast<int>(lengthX),
rand() % static_cast<int>(lengthY),
rand() % static_cast<int>(lengthZ)));

View File

@@ -42,10 +42,10 @@ namespace kami {
class LIBKAMI_EXPORT AgentID {
public:
/// \brief Constructs a new unique identifier.
AgentID();
AgentID() : _id(_id_next++){};
/// \brief Convert the identifier to a human readable string.
std::string toString() const;
std::string to_string() const { return std::to_string(this->_id); }
/// \brief Test if two AgentID instances are equal.
friend bool operator==(const AgentID &lhs, const AgentID &rhs);
@@ -63,8 +63,8 @@ class LIBKAMI_EXPORT AgentID {
friend std::ostream &operator<<(std::ostream &lhs, const AgentID &rhs);
private:
static uint64_t idGen;
uint64_t id;
inline static long long _id_next = 1;
long long _id;
};
/// \brief A superclass for Agents
@@ -75,12 +75,13 @@ class LIBKAMI_EXPORT AgentID {
class LIBKAMI_EXPORT Agent {
public:
/// \brief Get the agent's self identification number.
AgentID getAgentID() const;
AgentID get_agent_id() const;
/// \brief Execute a time-step for the agent
///
/// \details This function should step the agent instance. Any activities that the
/// agent should perform as part of its time step should be in this function.
/// \details This function should step the agent instance. Any activities
/// that the agent should perform as part of its time step should be in this
/// function.
virtual void step() = 0;
friend bool operator==(const Agent &lhs, const Agent &rhs);
@@ -88,20 +89,21 @@ class LIBKAMI_EXPORT Agent {
friend bool operator!=(const Agent &lhs, const Agent &rhs);
private:
AgentID agentID;
const AgentID _agent_id;
};
/// \brief A class for agents with and post-`step()` advancement.
///
/// \details Staged agents use a two-phase step to allow agents to take actions without
/// updating the state of the model before all agents have been allowed to
/// update.
/// \details Staged agents use a two-phase step to allow agents to take actions
/// without updating the state of the model before all agents have been allowed
/// to update.
class LIBKAMI_EXPORT StagedAgent : public Agent {
public:
/// \brief Post-step advance the agent
///
/// \details This method should be called after `step()`. Any updates or cleanups
/// to the agent should occur here that must happen after the step is complete.
/// \details This method should be called after `step()`. Any updates or
/// cleanups to the agent should occur here that must happen after the step
/// is complete.
virtual void advance() = 0;
};

View File

@@ -24,22 +24,13 @@
*/
#include <kami/agent.h>
#include <string>
namespace kami {
uint64_t AgentID::idGen = 1;
AgentID::AgentID() {
id = idGen++;
}
std::string AgentID::toString() const {
return std::to_string(id);
}
bool operator==(const AgentID &lhs, const AgentID &rhs) {
return lhs.id == rhs.id;
return lhs._id == rhs._id;
}
bool operator!=(const AgentID &lhs, const AgentID &rhs) {
@@ -47,24 +38,19 @@ bool operator!=(const AgentID &lhs, const AgentID &rhs) {
}
bool operator<(const AgentID &lhs, const AgentID &rhs) {
return lhs.id < rhs.id;
return lhs._id < rhs._id;
}
std::ostream &operator<<(std::ostream &lhs, const AgentID &rhs) {
return lhs << rhs.toString();
}
AgentID Agent::getAgentID() const {
return agentID;
return lhs << rhs.to_string();
}
AgentID Agent::get_agent_id() const { return this->_agent_id; }
bool operator==(const Agent &lhs, const Agent &rhs) {
return lhs.agentID == rhs.agentID;
return lhs._agent_id == rhs._agent_id;
}
bool operator!=(const Agent &lhs, const Agent &rhs) {
return !(lhs == rhs);
}
bool operator!=(const Agent &lhs, const Agent &rhs) { return !(lhs == rhs); }
} // namespace kami

View File

@@ -2,14 +2,14 @@
* TODO FILEHEADER
*/
#include <kami/agent.h>
#include <kami/config.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <kami/agent.h>
#include <kami/config.h>
#include <string>
using namespace kami;
@@ -21,12 +21,15 @@ int main(int argc, char **argv) {
auto console = spdlog::stdout_color_st(ident);
string logLevelOption = "info";
app.add_option("-l", logLevelOption, "Set the logging level")->check(CLI::IsMember(SPDLOG_LEVEL_NAMES));
app.add_option("-l", logLevelOption, "Set the logging level")
->check(CLI::IsMember(SPDLOG_LEVEL_NAMES));
CLI11_PARSE(app, argc, argv);
console->set_level(spdlog::level::from_str(logLevelOption));
console->info("Compiled with Kami/{}, log level {}", KAMI_VERSION_STRING, logLevelOption);
console->info("Compiled with Kami/{}, log level {}", KAMI_VERSION_STRING,
logLevelOption);
Agent testAgent;
console->debug("Successfully created Agent with ID {}", testAgent.getAgentID().toString());
console->debug("Successfully created Agent with ID {}",
testAgent.getAgentID().toString());
}

View File

@@ -2,14 +2,14 @@
* TODO FILEHEADER
*/
#include <kami/agent.h>
#include <kami/config.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <kami/agent.h>
#include <kami/config.h>
#include <string>
using namespace kami;
@@ -21,12 +21,15 @@ int main(int argc, char **argv) {
auto console = spdlog::stdout_color_st(ident);
string logLevelOption = "info";
app.add_option("-l", logLevelOption, "Set the logging level")->check(CLI::IsMember(SPDLOG_LEVEL_NAMES));
app.add_option("-l", logLevelOption, "Set the logging level")
->check(CLI::IsMember(SPDLOG_LEVEL_NAMES));
CLI11_PARSE(app, argc, argv);
console->set_level(spdlog::level::from_str(logLevelOption));
console->info("Compiled with Kami/{}, log level {}", KAMI_VERSION_STRING, logLevelOption);
console->info("Compiled with Kami/{}, log level {}", KAMI_VERSION_STRING,
logLevelOption);
AgentID testAgentID;
console->debug("Successfully created AgentID with ID {}", testAgentID.toString());
console->debug("Successfully created AgentID with ID {}",
testAgentID.to_string());
}