Started rebuilding the interface around Population requirements

This commit is contained in:
James P. Howard, II
2022-07-09 13:46:11 -04:00
parent f5ae008df5
commit 9bdde03463
28 changed files with 625 additions and 347 deletions

View File

@@ -28,8 +28,8 @@
cmake_minimum_required(VERSION 3.13) # GENERATOR_IS_MULTI_CONFIG
set(KAMI_VERSION_MAJOR 0)
set(KAMI_VERSION_MINOR 4)
set(KAMI_VERSION_PATCH 2)
set(KAMI_VERSION_MINOR 5)
set(KAMI_VERSION_PATCH 0)
set(KAMI_VERSION_STRING ${KAMI_VERSION_MAJOR}.${KAMI_VERSION_MINOR}.${KAMI_VERSION_PATCH})
################################################################################

View File

@@ -3,7 +3,7 @@ from conans import ConanFile, CMake
class KamiConan(ConanFile):
name = "kami"
version = "0.4.2"
version = "0.5.0"
license = "MIT"
author = "James P. Howard, II <james.howard@jhu.edu>"
url = "https://github.com/jhuapl/kami"

View File

@@ -1,6 +1,7 @@
Changelog
=========
- :feature:`0` Restructured the entire interface
- :bug:`0` Numerous build process cleanups
- :feature:`0` Added support semver versioning via neargye-semver
- :bug:`0` Make library static by default

View File

@@ -39,10 +39,11 @@
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/model.h>
#include <kami/multigrid1d.h>
#include <kami/population.h>
#include <kami/random.h>
kami::MultiGrid1D *MoneyAgent1D::_world = nullptr;
BoltzmannWealthModel1D *MoneyAgent1D::_model = nullptr;
std::shared_ptr<spdlog::logger> console = nullptr;
std::shared_ptr<std::mt19937> rng = nullptr;
@@ -61,18 +62,13 @@ struct fmt::formatter<kami::GridCoord1D> : fmt::formatter<std::string> {
};
void MoneyAgent1D::step() {
_step_counter++;
this->_step_counter++;
console->trace("Agent {} is moving", this->get_agent_id());
move_agent();
console->trace("Agent {} is giving money", this->get_agent_id());
if (_agent_wealth > 0) give_money();
this->move_agent();
if (_agent_wealth > 0) this->give_money();
}
void MoneyAgent1D::set_world(kami::MultiGrid1D *world) { _world = world; }
void MoneyAgent1D::set_model(BoltzmannWealthModel1D *model) { _model = model; }
void MoneyAgent1D::move_agent() {
console->trace("Entering move_agent");
auto agent_id = get_agent_id();
@@ -86,14 +82,14 @@ void MoneyAgent1D::move_agent() {
}
void MoneyAgent1D::give_money() {
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);
auto agent_id = get_agent_id();
auto location = _world->get_location_by_agent(agent_id);
auto cell_mates = _world->get_location_contents(location);
if (cell_mates->size() > 1) {
std::uniform_int_distribution<int> dist(0, (int)cell_mates->size() - 1);
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
auto other_agent = _model->get_agent_by_id(other_agent_id);
auto other_agent = std::dynamic_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;
@@ -101,46 +97,43 @@ void MoneyAgent1D::give_money() {
}
}
BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x) {
_world = new kami::MultiGrid1D(length_x, true);
_sched = new kami::RandomScheduler(this, rng);
BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsigned int length_x, unsigned int new_seed) {
rng = std::make_shared<std::mt19937>();
rng->seed(new_seed);
auto domain = std::make_shared<kami::MultiGrid1D>(length_x, true);
auto sched = std::make_shared<kami::RandomScheduler>(rng);
auto pop = std::make_shared<kami::Population>();
MoneyAgent1D::set_world(domain);
MoneyAgent1D::set_population(pop);
_domain = domain;
_sched = sched;
_pop = pop;
console->debug("Scheduler initiated with seed {}", new_seed);
_step_count = 0;
MoneyAgent1D::set_world(_world);
MoneyAgent1D::set_model(this);
std::uniform_int_distribution<int> dist(0, (int)length_x - 1);
for (unsigned int i = 0; i < number_agents; i++) {
auto *new_agent = new MoneyAgent1D();
auto new_agent = std::make_shared<MoneyAgent1D>();
_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(), kami::GridCoord1D(dist(*rng)));
console->trace("Initializing agent with AgentID {}", new_agent->get_agent_id());
_pop->add_agent(new_agent);
domain->add_agent(new_agent->get_agent_id(), kami::GridCoord1D(dist(*rng)));
}
}
BoltzmannWealthModel1D::~BoltzmannWealthModel1D() {
for (auto & agent_pair : _agent_list)
delete agent_pair.second;
delete _sched;
delete _world;
}
void BoltzmannWealthModel1D::step() {
_step_count++;
_sched->step();
}
void BoltzmannWealthModel1D::run(unsigned int steps) {
for (auto i = 0; i < steps; i++) step();
}
MoneyAgent1D *BoltzmannWealthModel1D::get_agent_by_id(kami::AgentID _agent_id) const {
MoneyAgent1D *_agent_pair = _agent_list.at(_agent_id);
return _agent_pair;
void BoltzmannWealthModel1D::step() {
console->trace("Executing model step {}", _step_count++);
_sched->step();
}
int main(int argc, char **argv) {
@@ -159,15 +152,13 @@ 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);
rng = std::make_shared<std::mt19937>(initial_seed);
BoltzmannWealthModel1D model(agent_count, x_size);
auto model = std::make_shared<BoltzmannWealthModel1D>(agent_count, x_size, initial_seed);
model->get_scheduler()->set_model(model);
model->get_population()->set_model(model);
spdlog::stopwatch sw;
for (int i = 0; i < max_steps; i++) {
console->trace("Initiating model step {}", i);
model.step();
}
for(int i = 0; i < max_steps; i++) model->step();
console->info("Boltzmann Wealth Model simulation complete, requiring {} seconds", sw);
}

View File

@@ -39,6 +39,13 @@
* A sample agent for a one-dimensional Boltzmann wealth model
*/
class MoneyAgent1D : public kami::Agent {
private:
inline static std::shared_ptr<kami::MultiGrid1D> _world = nullptr;
inline static std::shared_ptr<kami::Population> _population = nullptr;
int _step_counter;
int _agent_wealth;
public:
/**
* Create the agent
@@ -50,16 +57,6 @@ public:
*/
void step() override;
/**
* Give the agent a reference copy of the domain it is expected to work in
*/
static void set_world(kami::MultiGrid1D *world);
/**
* Give the agent a reference copy of the model it is expected to work in
*/
static void set_model(class BoltzmannWealthModel1D *model);
/**
* Move the agent to a random location on the world
*/
@@ -70,17 +67,22 @@ public:
*/
void give_money();
private:
static kami::MultiGrid1D *_world;
static BoltzmannWealthModel1D *_model;
int _step_counter;
int _agent_wealth;
static void set_world(std::shared_ptr<kami::MultiGrid1D> world) {
_world = std::move(world);
}
static void set_population(std::shared_ptr<kami::Population> population) {
_population = std::move(population);
}
};
/**
* The one-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel1D : public kami::Model {
class BoltzmannWealthModel1D : public kami::Model, std::enable_shared_from_this<BoltzmannWealthModel1D> {
private:
unsigned int _step_count;
public:
/**
* Create an instance of the one-dimensional Boltzmann wealth model.
@@ -89,12 +91,7 @@ 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);
/**
* Destroy the instance
*/
~BoltzmannWealthModel1D();
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.
@@ -107,19 +104,6 @@ public:
* @param[in] n the number of steps to execute.
*/
void run(unsigned int n) override;
/**
* Get the MoneyAgent instance associated with the given `AgentID`
*
* @returns an pointer to the `MoneyAgent` that was requested.
*/
[[nodiscard]] MoneyAgent1D *get_agent_by_id(kami::AgentID agent_id) const override;
private:
std::map<kami::AgentID, MoneyAgent1D *> _agent_list;
kami::RandomScheduler *_sched;
kami::MultiGrid1D *_world;
unsigned int _step_count;
};
#endif // BOLTZMANN1D_H

View File

@@ -25,6 +25,8 @@
#include "boltzmann2d.h"
#include <stdio.h>
#include <map>
#include <memory>
#include <random>
@@ -40,9 +42,9 @@
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid2d.h>
#include <kami/population.h>
#include <kami/random.h>
kami::MultiGrid2D *MoneyAgent2D::_world = nullptr;
BoltzmannWealthModel2D *MoneyAgent2D::_model = nullptr;
std::shared_ptr<spdlog::logger> console = nullptr;
std::shared_ptr<std::mt19937> rng = nullptr;
@@ -61,18 +63,13 @@ struct fmt::formatter<kami::GridCoord2D> : fmt::formatter<std::string> {
};
void MoneyAgent2D::step() {
_step_counter++;
this->_step_counter++;
console->trace("Agent {} is moving", this->get_agent_id());
move_agent();
console->trace("Agent {} is giving money", this->get_agent_id());
if (_agent_wealth > 0) give_money();
this->move_agent();
if(_agent_wealth > 0) this->give_money();
}
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();
@@ -86,14 +83,14 @@ void MoneyAgent2D::move_agent() {
}
void MoneyAgent2D::give_money() {
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);
auto agent_id = get_agent_id();
auto location = _world->get_location_by_agent(agent_id);
auto cell_mates = _world->get_location_contents(location);
if (cell_mates->size() > 1) {
std::uniform_int_distribution<int> dist(0, (int)cell_mates->size() - 1);
kami::AgentID other_agent_id = cell_mates->at(dist(*rng));
auto other_agent = _model->get_agent_by_id(other_agent_id);
auto other_agent = std::dynamic_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;
@@ -105,47 +102,40 @@ BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsig
rng = std::make_shared<std::mt19937>();
rng->seed(new_seed);
_world = new kami::MultiGrid2D(length_x, length_y, true, true);
_sched = new kami::RandomScheduler(this, rng);
auto domain = std::make_shared<kami::MultiGrid2D>(length_x, length_y, true, true);
auto sched = std::make_shared<kami::RandomScheduler>(rng);
auto pop = std::make_shared<kami::Population>();
MoneyAgent2D::set_world(domain);
MoneyAgent2D::set_population(pop);
_domain = domain;
_sched = sched;
_pop = pop;
console->debug("Scheduler initiated with seed {}", new_seed);
_step_count = 0;
MoneyAgent2D::set_world(_world);
MoneyAgent2D::set_model(this);
std::uniform_int_distribution<int> dist_x(0, (int)length_x - 1);
std::uniform_int_distribution<int> dist_y(0, (int)length_y - 1);
for (unsigned int i = 0; i < number_agents; i++) {
auto *new_agent = new MoneyAgent2D();
auto new_agent = std::make_shared<MoneyAgent2D>();
_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(), kami::GridCoord2D(dist_x(*rng), dist_x(*rng)));
console->trace("Initializing agent with AgentID {}", new_agent->get_agent_id());
_pop->add_agent(new_agent);
domain->add_agent(new_agent->get_agent_id(), kami::GridCoord2D(dist_x(*rng), dist_x(*rng)));
}
}
BoltzmannWealthModel2D::~BoltzmannWealthModel2D() {
for (auto & agent_pair : _agent_list)
delete agent_pair.second;
delete _sched;
delete _world;
}
void BoltzmannWealthModel2D::step() {
_step_count++;
_sched->step();
}
void BoltzmannWealthModel2D::run(unsigned int steps) {
for (auto i = 0; i < steps; i++) step();
}
MoneyAgent2D *BoltzmannWealthModel2D::get_agent_by_id(kami::AgentID agent_id) const {
MoneyAgent2D *agent = _agent_list.at(agent_id);
return agent;
void BoltzmannWealthModel2D::step() {
console->trace("Executing model step {}", _step_count++);
_sched->step();
}
int main(int argc, char **argv) {
@@ -167,12 +157,11 @@ 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 {}x{}-unit grid for {} steps", agent_count, x_size, y_size, max_steps);
BoltzmannWealthModel2D model(agent_count, x_size, y_size, initial_seed);
auto model = std::make_shared<BoltzmannWealthModel2D>(agent_count, x_size, y_size, initial_seed);
model->get_scheduler()->set_model(model);
model->get_population()->set_model(model);
spdlog::stopwatch sw;
for (int i = 0; i < max_steps; i++) {
console->trace("Initiating model step {}", i);
model.step();
}
for(int i = 0; i < max_steps; i++) model->step();
console->info("Boltzmann Wealth Model simulation complete, requiring {} seconds", sw);
}

View File

@@ -29,16 +29,25 @@
#include <iostream>
#include <map>
#include <memory>
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/multigrid2d.h>
#include <kami/population.h>
#include <kami/random.h>
/**
* A sample agent for a two-dimensional Boltzmann wealth model
*/
class MoneyAgent2D : public kami::Agent {
private:
inline static std::shared_ptr<kami::MultiGrid2D> _world = nullptr;
inline static std::shared_ptr<kami::Population> _population = nullptr;
int _step_counter;
int _agent_wealth;
public:
/**
* Create the agent
@@ -50,16 +59,6 @@ public:
*/
void step() override;
/**
* Give the agent a reference copy of the domain it is expected to work in
*/
static void set_world(kami::MultiGrid2D *world);
/**
* Give the agent a reference copy of the model it is expected to work in
*/
static void set_model(class BoltzmannWealthModel2D *model);
/**
* Move the agent to a random location on the world
*/
@@ -70,17 +69,22 @@ public:
*/
void give_money();
private:
static kami::MultiGrid2D *_world;
static BoltzmannWealthModel2D *_model;
int _step_counter;
int _agent_wealth;
static void set_world(std::shared_ptr<kami::MultiGrid2D> world) {
_world = std::move(world);
}
static void set_population(std::shared_ptr<kami::Population> population) {
_population = std::move(population);
}
};
/**
* The two-dimensional Boltzmann wealth model
*/
class BoltzmannWealthModel2D : public kami::Model {
class BoltzmannWealthModel2D : public kami::Model, std::enable_shared_from_this<BoltzmannWealthModel2D> {
private:
unsigned int _step_count;
public:
/**
* Create an instance of the two-dimensional Boltzmann wealth model.
@@ -95,11 +99,6 @@ public:
*/
explicit BoltzmannWealthModel2D(unsigned int number_agents = 10, unsigned int length_x = 10, unsigned int length_y = 10, unsigned int new_seed = 42);
/**
* Destroy the instance
*/
~BoltzmannWealthModel2D();
/**
* Execute a single time-step for the model.
*/
@@ -111,19 +110,6 @@ public:
* @param[in] n the number of steps to execute.
*/
void run(unsigned int n) override;
/**
* Get the MoneyAgent2D instance associated with the given `AgentID`
*
* @returns an pointer to the `MoneyAgent2D` that was requested.
*/
[[nodiscard]] MoneyAgent2D *get_agent_by_id(kami::AgentID agent_id) const override;
private:
std::map<kami::AgentID, MoneyAgent2D *> _agent_list;
kami::RandomScheduler *_sched;
kami::MultiGrid2D *_world;
unsigned int _step_count;
};
#endif // BOLTZMANN2D_H

View File

@@ -28,10 +28,11 @@
#define KAMI_AGENT_H
#include <iostream>
#include <memory>
#include <string>
#include <kami/kami.h>
#include <kami/model.h>
namespace kami {
@@ -45,6 +46,19 @@ namespace kami {
* @see Agent
*/
class LIBKAMI_EXPORT AgentID {
private:
inline static long long _id_next = 1;
/**
* The unique identifier is a `long long`.
*
* The unique identifier is an unsigned integer that increments
* monotonically with each new `AgentID` instantiated. This is
* substantially faster than other potential identifiers, such
* as MD5 hashes or UUID objects.
*/
long long _id;
public:
/**
* Constructs a new unique identifier.
@@ -101,19 +115,6 @@ namespace kami {
* @return the output stream for reuse
*/
friend std::ostream &operator<<(std::ostream &lhs, const AgentID &rhs);
private:
inline static long long _id_next = 1;
/**
* The unique identifier is a `long long`.
*
* The unique identifier is an unsigned integer that increments
* monotonically with each new `AgentID` instantiated. This is
* substantially faster than other potential identifiers, such
* as MD5 hashes or UUID objects.
*/
long long _id;
};
/**
@@ -126,6 +127,12 @@ namespace kami {
* @see `StagedAgent`
*/
class LIBKAMI_EXPORT Agent {
private:
const AgentID _agent_id;
protected:
std::shared_ptr<Model> _model;
public:
/**
* Get the `Agent`'s `AgentID`.
@@ -174,8 +181,18 @@ namespace kami {
*/
friend bool operator!=(const Agent &lhs, const Agent &rhs);
private:
const AgentID _agent_id;
/**
* @brief Get the `Model` associated with this agent
*/
[[maybe_unused]] std::shared_ptr<Model> get_model();
/**
* @brief Add a `Model` to this agent
*
* @details This method will associate a model with the
* agent.
*/
[[maybe_unused]] void set_model(std::shared_ptr<Model> model);
};
/**

View File

@@ -29,7 +29,7 @@
* In the source code, include "exampleConfig.h" (no .in suffix).
* This header file will be generated, and filled with the
* right definition values. Change the namings as you wish,
* but make sure they match up with whats in CMakeLists.txt.
* but make sure they match up with what's in CMakeLists.txt.
*/
#pragma once

View File

@@ -38,7 +38,10 @@ namespace kami {
*
* Implementations of virtual environments are expected to subclass `Domain`.
*/
class LIBKAMI_EXPORT Domain {};
class LIBKAMI_EXPORT Domain {
public:
[[maybe_unused]] virtual bool get_bool() = 0;
};
/**
* Provides a coordinate system for each `Domain`.

View File

@@ -91,7 +91,9 @@ namespace kami {
* rectilinear grid where the cells are equal size and laid out in an ordered
* fashion.
*/
class LIBKAMI_EXPORT GridDomain : public Domain {};
class LIBKAMI_EXPORT GridDomain : public Domain {
bool get_bool() override { return true; }
};
/**
* @brief An abstract for gridded coordinates.

View File

@@ -97,7 +97,7 @@ namespace kami {
explicit Grid1D(unsigned int maximum_x, bool wrap_x = false);
/**
* Deconstructor
* Destructor
*/
virtual ~Grid1D();

View File

@@ -108,7 +108,7 @@ namespace kami {
bool wrap_y = false);
/**
* Deconstructor
* Destructor
*/
virtual ~Grid2D();

View File

@@ -34,9 +34,14 @@
namespace kami {
inline semver::version get_version() {
return version;
}
class Agent;
class AgentID;
class Domain;
class Model;
class Population;
class Scheduler;
[[maybe_unused]] inline semver::version get_version() { return version; }
} // namespace kami

View File

@@ -27,8 +27,10 @@
#ifndef KAMI_MODEL_H
#define KAMI_MODEL_H
#include <kami/agent.h>
#include <kami/domain.h>
#include <kami/kami.h>
#include <kami/population.h>
#include <kami/scheduler.h>
namespace kami {
@@ -36,16 +38,12 @@ namespace kami {
* An abstract for generic models
*/
class LIBKAMI_EXPORT Model {
public:
/**
* Get a reference to an `Agent` by `AgentID`
*
* @param[in] agent_id the `AgentID` to search for.
*
* @return a reference to the desired `Agent` or `nullptr` if not found.
*/
[[nodiscard]] virtual Agent *get_agent_by_id(AgentID agent_id) const = 0;
protected:
std::shared_ptr<Domain> _domain = nullptr;
std::shared_ptr<Population> _pop = nullptr;
std::shared_ptr<Scheduler> _sched = nullptr;
public:
/**
* Execute a fixed number of time-steps for the model.
*
@@ -62,6 +60,45 @@ namespace kami {
* model should perform as part of its time step should be in this function.
*/
virtual void step() = 0;
/**
* @brief Get the `Domain` associated with this model
*/
[[maybe_unused]] std::shared_ptr<Domain> get_domain();
/**
* @brief Add a `Domain` to this scheduler
*
* @details This method will associate a model with the
* scheduler.
*/
[[maybe_unused]] void set_domain(std::shared_ptr<Domain> domain);
/**
* @brief Get the `Population` associated with this model
*/
std::shared_ptr<Population> get_population();
/**
* @brief Add a `Model` to this scheduler
*
* @details This method will associate a model with the
* scheduler.
*/
[[maybe_unused]] void set_population(std::shared_ptr<Population> population);
/**
* @brief Get the `Scheduler` associated with this model
*/
[[maybe_unused]] std::shared_ptr<Scheduler> get_scheduler();
/**
* @brief Add a `Model` to this scheduler
*
* @details This method will associate a model with the
* scheduler.
*/
[[maybe_unused]] void set_scheduler(std::shared_ptr<Scheduler> scheduler);
};
} // namespace kami

93
include/kami/population.h Normal file
View File

@@ -0,0 +1,93 @@
/*-
* Copyright (c) 2022 The Johns Hopkins University Applied Physics
* Laboratory LLC
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#ifndef KAMI_POPULATION_H
#define KAMI_POPULATION_H
#include <map>
#include <kami/agent.h>
#include <kami/kami.h>
namespace kami {
/**
* An abstract for generic models
*/
class LIBKAMI_EXPORT Population {
protected:
std::map<kami::AgentID, std::shared_ptr<Agent>> _agent_map;
std::shared_ptr<Model> _model = nullptr;
public:
Population();
explicit Population(std::shared_ptr<Model> model);
/**
* @brief Get the `Model` associated with this scheduler
*/
std::shared_ptr<Model> get_model();
/**
* @brief Add a `Model` to this scheduler
*
* @details This method will associate a model with the
* scheduler.
*/
[[maybe_unused]] void set_model(std::shared_ptr<Model> model);
/**
* Get a reference to an `Agent` by `AgentID`
*
* @param[in] agent_id the `AgentID` to search for.
*
* @return a reference to the desired `Agent` or `nullptr` if not found.
*/
[[nodiscard]] std::shared_ptr<Agent> get_agent_by_id(AgentID agent_id) const;
/**
* Add an Agent to the Population.
*
* @param agent The Agent to add.
*/
void add_agent(const std::shared_ptr<Agent> &agent);
/**
* Remove an Agent from the Population.
*
* @param agent_id The AgentID of the agent to remove.
*/
[[maybe_unused]] void delete_agent(AgentID agent_id);
/*
* Returns the agent list.
*/
std::shared_ptr<std::vector<AgentID>> get_agent_list();
};
} // namespace kami
#endif // KAMI_POPULATION_H

View File

@@ -27,14 +27,14 @@
#ifndef KAMI_RANDOM_H
#define KAMI_RANDOM_H
#include <kami/kami.h>
#include <kami/sequential.h>
#include <algorithm>
#include <memory>
#include <random>
#include <vector>
#include <kami/kami.h>
#include <kami/scheduler.h>
#include <kami/sequential.h>
namespace kami {
/**
@@ -49,28 +49,28 @@ namespace kami {
*/
class LIBKAMI_EXPORT RandomScheduler : public SequentialScheduler {
public:
/**
* @brief Constructor.
*/
RandomScheduler() = default;
/**
* @brief Constructor.
*
* @details The `model` parameter is used by the scheduler to get
* access to an `Agent`. The `Model` is presumed to maintain a master
* list of all `Agent`s in the `Model` and the `Model` can be queried for
* a reference to any particular `Agent` at `step()` time.
*
* @param model [in] A reference to the model the scheduler is timing.
* @param rng [in] A uniform random number generator of type
* `std::mt19937`, used as the source of randomness.
*/
RandomScheduler(Model *model, std::shared_ptr<std::mt19937> rng);
explicit RandomScheduler(std::shared_ptr<std::mt19937> rng);
/**
* @brief Execute a single time step.
*
* @details This method will randomize the list of Agents in the scheduler's
* internal queue and then execute the `Agent::step()` method for every
* Agent assigned to this scheduler in the randomized order.
* @details This method will randomize the list of Agents provided
* then execute the `Agent::step()` method for every Agent listed.
*
* @param agent_list list of agents to execute the step
*/
void step() override;
void step(std::shared_ptr<std::vector<AgentID>> agent_list) override;
/**
* Set the random number generator used to randomize the order of agent
@@ -85,10 +85,10 @@ namespace kami {
* Get a reference to the random number generator used to randomize
* the order of agent stepping.
*/
[[maybe_unused]] std::shared_ptr<std::mt19937> get_rng();
[[maybe_unused]] [[maybe_unused]] std::shared_ptr<std::mt19937> get_rng();
private:
std::shared_ptr<std::mt19937> _rng;
std::shared_ptr<std::mt19937> _rng = nullptr;
};
} // namespace kami

View File

@@ -27,10 +27,10 @@
#ifndef KAMI_SCHEDULER_H
#define KAMI_SCHEDULER_H
#include <kami/KAMI_EXPORT.h>
#include <kami/agent.h>
#include <kami/model.h>
namespace kami {
class Model;
/**
* Create a Kami scheduler.
@@ -40,27 +40,56 @@ namespace kami {
* the step function for each agent based on the type of scheduling implemented.
*/
class LIBKAMI_EXPORT Scheduler {
protected:
/**
* Counter to increment on each step
*/
int _step_counter = 0;
/**
* A pointer to the `Model` this scheduler schedules
*/
std::shared_ptr<Model> _model = nullptr;
public:
/**
* Add an Agent to the Scheduler.
*
* @param agent_id The AgentID of the agent to add.
*/
virtual void add_agent(AgentID agent_id) = 0;
Scheduler() = default;
explicit Scheduler(std::shared_ptr<Model> model);
/**
* Remove an Agent from the Scheduler.
*
* @param agent_id The AgentID of the agent to remove.
* @brief Get the `Model` associated with this scheduler
*/
[[maybe_unused]] virtual void delete_agent(AgentID agent_id) = 0;
std::shared_ptr<Model> get_model();
/**
* Step the Scheduler.
* @brief Add a `Model` to this scheduler
*
* A generic step function that executes a single time step.
* @details This method will associate a model with the
* scheduler.
*/
[[maybe_unused]] void set_model(std::shared_ptr<Model> model);
/**
* @brief Execute a single time step.
*
* @details This method will step through the list of Agents in the
* `Population` and then execute the `Agent::step()`
* method for every Agent assigned to this scheduler in the order
* assigned.
*/
virtual void step() = 0;
/**
* @brief Execute a single time step.
*
* @details This method will step through the list of Agents
* provided and then execute the `Agent::step()`
* method for every Agent assigned to this scheduler in the order
* assigned.
*
* @param agent_list list of agents to execute the step
*/
virtual void step(std::shared_ptr<std::vector<AgentID>> agent_list) = 0;
};
} // namespace kami

View File

@@ -27,11 +27,10 @@
#ifndef KAMI_SEQUENTIAL_H
#define KAMI_SEQUENTIAL_H
#include <algorithm>
#include <vector>
#include <kami/KAMI_EXPORT.h>
#include <kami/agent.h>
#include <kami/kami.h>
#include <kami/model.h>
#include <kami/scheduler.h>
@@ -57,23 +56,7 @@ namespace kami {
* list of all Agents in the Model and the Model can be queried for
* a reference to any particular Agent at `step()` time.
*/
explicit SequentialScheduler(Model *model);
/**
* @brief Add an agent to the scheduler.
*
* @details The scheduler maintains a list of all AgentIDs currently
* assigned. This function adds a new Agent to the list.
*/
void add_agent(AgentID agent_id) override;
/**
* @brief Remove an agent from the scheduler.
*
* @details The scheduler maintains a list of all AgentIDs currently
* assigned. This function removes an Agent from the list.
*/
void delete_agent(AgentID agent_id) override;
SequentialScheduler() = default;
/**
* @brief Execute a single time step.
@@ -85,22 +68,17 @@ namespace kami {
*/
void step() override;
protected:
/**
* A vector containing the `AgentID`s of all agents assigned to this
* scheduler
* @brief Execute a single time step.
*
* @details This method will step through the list of Agents
* provided and then execute the `Agent::step()`
* method for every Agent assigned to this scheduler in the order
* assigned.
*
* @param agent_list list of agents to execute the step
*/
std::vector<AgentID> _agent_list;
/**
* A pointer to the `Model` this scheduler belongs to
*/
Model *_model;
/**
* Counter to increment on each step
*/
int _step_counter;
void step(std::shared_ptr<std::vector<AgentID>> agent_list) override;
};
} // namespace kami

View File

@@ -27,13 +27,13 @@
#ifndef KAMI_STAGED_H
#define KAMI_STAGED_H
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/scheduler.h>
#include <algorithm>
#include <vector>
#include <kami/agent.h>
#include <kami/scheduler.h>
#include <kami/sequential.h>
namespace kami {
/**
@@ -46,7 +46,30 @@ namespace kami {
*
* @note First create a Model for the scheduler to live in.
*/
class LIBKAMI_EXPORT StagedScheduler : public Scheduler {
class LIBKAMI_EXPORT [[maybe_unused]] StagedScheduler : public SequentialScheduler {
private:
/**
* @brief Advance a single time step.
*
* @details This method will step through the list of StagedAgent in the
* scheduler's internal queue and then execute the `StagedAgent::step()`
* method for every StagedAgent assigned to this scheduler in the order
* assigned.
*/
[[maybe_unused]] void advance();
/**
* @brief Advance a single time step.
*
* @details This method will step through the list of StagedAgent
* provided and then execute the `StagedAgent::advance()`
* method for every StagedAgent assigned to this scheduler in the order
* assigned.
*
* @param agent_list list of agents to execute the step
*/
void advance(const std::shared_ptr<std::vector<AgentID>>& agent_list);
public:
/**
* Constructor.
@@ -55,28 +78,7 @@ namespace kami {
* Model and the Model can be queried for a reference to any particular
* Agent at `step()` time.
*/
explicit StagedScheduler(Model *);
/**
* A deconstructor.
*/
~StagedScheduler() = default;
/**
* Add an agent to the scheduler.
*
* The scheduler maintains a list of all AgentIDs currently assigned. This
* function adds a new Agent to the list.
*/
void add_agent(AgentID agent_id) override;
/**
* Remove an agent from the scheduler.
*
* The scheduler maintains a list of all AgentIDs currently assigned. This
* function removes an Agent from the list.
*/
void delete_agent(AgentID agent_id) override;
explicit StagedScheduler() = default;
/**
* Execute a single time step.
@@ -87,11 +89,6 @@ namespace kami {
* method for each Agent in the same order.
*/
void step() override;
private:
std::vector<AgentID> _agent_list;
Model *_model;
int _step_counter;
};
} // namespace kami

View File

@@ -12,9 +12,12 @@ create_library(NAME libkami
domain.cc
grid1d.cc
grid2d.cc
model.cc
multigrid1d.cc
multigrid2d.cc
population.cc
random.cc
scheduler.cc
sequential.cc
sologrid1d.cc
sologrid2d.cc

View File

@@ -54,4 +54,12 @@ namespace kami {
bool operator!=(const Agent &lhs, const Agent &rhs) { return !(lhs == rhs); }
[[maybe_unused]] std::shared_ptr<Model> Agent::get_model() {
return(_model);
}
[[maybe_unused]] void Agent::set_model(std::shared_ptr<Model> model) {
_model = std::move(model);
}
} // namespace kami

58
src/libkami/model.cc Normal file
View File

@@ -0,0 +1,58 @@
/*-
* Copyright (c) 2022 The Johns Hopkins University Applied Physics
* Laboratory LLC
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <memory>
#include <utility>
#include <kami/model.h>
#include <kami/scheduler.h>
namespace kami {
[[maybe_unused]] [[maybe_unused]] std::shared_ptr<Domain> Model::get_domain() {
return(_domain);
}
[[maybe_unused]] void Model::set_domain(std::shared_ptr<Domain> domain) {
_domain = std::move(domain);
}
[[maybe_unused]] std::shared_ptr<Population> Model::get_population() {
return(_pop);
}
[[maybe_unused]] void Model::set_population(std::shared_ptr<Population> population) {
_pop = std::move(population);
}
[[maybe_unused]] [[maybe_unused]] std::shared_ptr<Scheduler> Model::get_scheduler() {
return(_sched);
}
[[maybe_unused]] void Model::set_scheduler(std::shared_ptr<Scheduler> scheduler) {
_sched = std::move(scheduler);
}
} // namespace kami

75
src/libkami/population.cc Normal file
View File

@@ -0,0 +1,75 @@
/*-
* Copyright (c) 2020 The Johns Hopkins University Applied Physics
* Laboratory LLC
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <utility>
#include <vector>
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/population.h>
namespace kami {
Population::Population() = default;
Population::Population(std::shared_ptr<Model> model) {
this->set_model(std::move(model));
}
void Population::add_agent(const std::shared_ptr<Agent> &agent) {
agent->set_model(_model);
_agent_map.insert(std::pair<AgentID, std::shared_ptr<Agent>>(agent->get_agent_id(), agent));
}
[[maybe_unused]] void Population::delete_agent(const AgentID agent_id) {
auto agent_it = _agent_map.find(agent_id);
if(agent_it != _agent_map.end()) _agent_map.erase(agent_it);
}
std::shared_ptr<Agent> Population::get_agent_by_id(const AgentID agent_id) const {
auto agent_it = _agent_map.find(agent_id);
if(agent_it != _agent_map.end()) return(agent_it->second);
return nullptr;
}
std::shared_ptr<std::vector<AgentID>> Population::get_agent_list() {
auto key_selector = [](auto pair){ return pair.first; };
auto agent_ids = std::make_shared<std::vector<AgentID>>(_agent_map.size());
transform(_agent_map.begin(), _agent_map.end(), agent_ids->begin(), key_selector);
return std::move(agent_ids);
}
[[maybe_unused]] std::shared_ptr<Model> Population::get_model() {
return(_model);
}
[[maybe_unused]] void Population::set_model(std::shared_ptr<Model> model) {
_model = std::move(model);
}
} // namespace kami

View File

@@ -23,29 +23,30 @@
* SOFTWARE.
*/
#include <algorithm>
#include <memory>
#include <random>
#include <string>
#include <utility>
#include <vector>
#include <kami/model.h>
#include <kami/random.h>
#include <kami/sequential.h>
namespace kami {
RandomScheduler::RandomScheduler(Model *model, std::shared_ptr<std::mt19937> rng)
: SequentialScheduler(model) {
RandomScheduler::RandomScheduler(std::shared_ptr<std::mt19937> rng) {
this->set_rng(std::move(rng));
}
void RandomScheduler::step() {
shuffle(_agent_list.begin(), _agent_list.end(), *_rng);
this->SequentialScheduler::step();
void RandomScheduler::step(std::shared_ptr<std::vector<AgentID>> agent_list) {
shuffle(agent_list->begin(),agent_list->end(), *_rng);
this->SequentialScheduler::step(agent_list);
}
void RandomScheduler::set_rng(std::shared_ptr<std::mt19937> rng) {
this->_rng = std::move(rng);
}
[[maybe_unused]] std::shared_ptr<std::mt19937> RandomScheduler::get_rng() { return (_rng); }
[[maybe_unused]] std::shared_ptr<std::mt19937> RandomScheduler::get_rng() { return (this->_rng); }
} // namespace kami

46
src/libkami/scheduler.cc Normal file
View File

@@ -0,0 +1,46 @@
/*-
* Copyright (c) 2022 The Johns Hopkins University Applied Physics
* Laboratory LLC
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <memory>
#include <utility>
#include <kami/model.h>
#include <kami/scheduler.h>
namespace kami {
Scheduler::Scheduler(std::shared_ptr<Model> model) {
this->set_model(std::move(model));
}
[[maybe_unused]] std::shared_ptr<Model> Scheduler::get_model() {
return(_model);
}
[[maybe_unused]] void Scheduler::set_model(std::shared_ptr<Model> model) {
_model = std::move(model);
}
} // namespace kami

View File

@@ -23,36 +23,21 @@
* SOFTWARE.
*/
#include <string>
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/sequential.h>
namespace kami {
SequentialScheduler::SequentialScheduler(Model *model) {
_step_counter = 0;
_model = model;
}
void SequentialScheduler::add_agent(AgentID agent_id) {
_agent_list.push_back(agent_id);
}
void SequentialScheduler::delete_agent(AgentID agent_id) {
for (auto agent_list_iter = _agent_list.begin(); agent_list_iter < _agent_list.end(); agent_list_iter++)
if (*agent_list_iter == agent_id) _agent_list.erase(agent_list_iter);
// ERROR HERE
}
void SequentialScheduler::step() {
_step_counter++;
this->step(Scheduler::get_model()->get_population()->get_agent_list());
}
for (auto agent_list_iter = _agent_list.begin();
agent_list_iter < _agent_list.end(); agent_list_iter++) {
Agent *agent = _model->get_agent_by_id(*agent_list_iter);
if (agent != nullptr) agent->step();
void SequentialScheduler::step(std::shared_ptr<std::vector<AgentID>> agent_list) {
Scheduler::_step_counter++;
for(auto agent_id = agent_list->begin(); agent_id < agent_list->end(); agent_id++) {
auto agent = Scheduler::get_model()->get_population()->get_agent_by_id(*agent_id);
if(agent != nullptr) agent->step();
}
}

View File

@@ -23,39 +23,29 @@
* SOFTWARE.
*/
#include <string>
#include <memory>
#include <vector>
#include <kami/agent.h>
#include <kami/model.h>
#include <kami/sequential.h>
#include <kami/staged.h>
namespace kami {
StagedScheduler::StagedScheduler(Model *model) {
_step_counter = 0;
_model = model;
}
void StagedScheduler::add_agent(AgentID agent_id) {
_agent_list.push_back(agent_id);
}
void StagedScheduler::delete_agent(AgentID agent_id) {
for (auto agent_list_iter = _agent_list.begin(); agent_list_iter < _agent_list.end(); agent_list_iter++)
if (*agent_list_iter == agent_id) _agent_list.erase(agent_list_iter);
}
void StagedScheduler::step() {
_step_counter++;
auto agent_list = Scheduler::get_model()->get_population()->get_agent_list();
this->SequentialScheduler::step(agent_list);
this->advance(agent_list);
}
for (auto agent_list_iter = _agent_list.begin(); agent_list_iter < _agent_list.end(); agent_list_iter++) {
auto *agent = dynamic_cast<StagedAgent *>(_model->get_agent_by_id(*agent_list_iter));
if (agent != nullptr) agent->step();
}
[[maybe_unused]] void StagedScheduler::advance() {
this->advance(Scheduler::get_model()->get_population()->get_agent_list());
}
for (auto agent_list_iter = _agent_list.begin(); agent_list_iter < _agent_list.end(); agent_list_iter++) {
auto *agent = dynamic_cast<StagedAgent *>(_model->get_agent_by_id(*agent_list_iter));
if (agent != nullptr) agent->advance();
void StagedScheduler::advance(const std::shared_ptr<std::vector<AgentID>>& agent_list) {
for(auto agent_id = agent_list->begin(); agent_id < agent_list->end(); agent_id++) {
auto agent = std::dynamic_pointer_cast<StagedAgent>(this->Scheduler::get_model()->get_population()->get_agent_by_id(*agent_id));
if(agent != nullptr) agent->advance();
}
}