mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-08 14:33:53 -05:00
Clean up model returns
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
#define KAMI_MODEL_H
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include <kami/domain.h>
|
||||
#include <kami/kami.h>
|
||||
@@ -66,7 +67,7 @@ namespace kami {
|
||||
/**
|
||||
* @brief Get the `Domain` associated with this model
|
||||
*/
|
||||
std::shared_ptr<Domain> get_domain();
|
||||
std::optional<std::shared_ptr<Domain>> get_domain();
|
||||
|
||||
/**
|
||||
* @brief Add a `Domain` to this scheduler
|
||||
@@ -79,7 +80,7 @@ namespace kami {
|
||||
/**
|
||||
* @brief Get the `Population` associated with this model
|
||||
*/
|
||||
std::shared_ptr<Population> get_population();
|
||||
std::optional<std::shared_ptr<Population>> get_population();
|
||||
|
||||
/**
|
||||
* @brief Add a `Model` to this scheduler
|
||||
@@ -92,7 +93,7 @@ namespace kami {
|
||||
/**
|
||||
* @brief Get the `Scheduler` associated with this model
|
||||
*/
|
||||
std::shared_ptr<Scheduler> get_scheduler();
|
||||
std::optional<std::shared_ptr<Scheduler>> get_scheduler();
|
||||
|
||||
/**
|
||||
* @brief Add a `Model` to this scheduler
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#define KAMI_RANDOM_H
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
@@ -74,7 +75,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully stepped
|
||||
*/
|
||||
std::shared_ptr<std::vector<AgentID>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
|
||||
/**
|
||||
* @brief Set the RNG
|
||||
|
||||
@@ -27,6 +27,10 @@
|
||||
#ifndef KAMI_SCHEDULER_H
|
||||
#define KAMI_SCHEDULER_H
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <kami/model.h>
|
||||
|
||||
namespace kami {
|
||||
@@ -57,7 +61,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully stepped
|
||||
*/
|
||||
virtual std::shared_ptr<std::vector<AgentID>> step(std::shared_ptr<Model> model) = 0;
|
||||
virtual std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model) = 0;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
@@ -72,7 +76,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully stepped
|
||||
*/
|
||||
virtual std::shared_ptr<std::vector<AgentID>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) = 0;
|
||||
virtual std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) = 0;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#ifndef KAMI_SEQUENTIAL_H
|
||||
#define KAMI_SEQUENTIAL_H
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <kami/agent.h>
|
||||
@@ -58,7 +60,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully stepped
|
||||
*/
|
||||
std::shared_ptr<std::vector<AgentID>> step(std::shared_ptr<Model> model) override;
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model) override;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
@@ -73,7 +75,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully stepped
|
||||
*/
|
||||
std::shared_ptr<std::vector<AgentID>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -27,7 +27,8 @@
|
||||
#ifndef KAMI_STAGED_H
|
||||
#define KAMI_STAGED_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <kami/agent.h>
|
||||
@@ -58,7 +59,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully advanced
|
||||
*/
|
||||
std::shared_ptr<std::vector<AgentID>> advance(std::shared_ptr<Model> model);
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> advance(std::shared_ptr<Model> model);
|
||||
|
||||
/**
|
||||
* @brief Advance a single time step.
|
||||
@@ -73,7 +74,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully advanced
|
||||
*/
|
||||
std::shared_ptr<std::vector<AgentID>> advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list);
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list);
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -89,7 +90,7 @@ namespace kami {
|
||||
*
|
||||
* @params returns vector of agents successfully stepped
|
||||
*/
|
||||
std::shared_ptr<std::vector<AgentID>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -17,7 +17,6 @@ create_library(NAME libkami
|
||||
multigrid2d.cc
|
||||
population.cc
|
||||
random.cc
|
||||
scheduler.cc
|
||||
sequential.cc
|
||||
sologrid1d.cc
|
||||
sologrid2d.cc
|
||||
|
||||
@@ -31,7 +31,9 @@
|
||||
|
||||
namespace kami {
|
||||
|
||||
std::shared_ptr<Domain> Model::get_domain() {
|
||||
std::optional<std::shared_ptr<Domain>> Model::get_domain() {
|
||||
if(_domain == nullptr)
|
||||
return std::nullopt;
|
||||
return(_domain);
|
||||
}
|
||||
|
||||
@@ -39,7 +41,9 @@ namespace kami {
|
||||
_domain = std::move(domain);
|
||||
}
|
||||
|
||||
std::shared_ptr<Population> Model::get_population() {
|
||||
std::optional<std::shared_ptr<Population>> Model::get_population() {
|
||||
if(_pop == nullptr)
|
||||
return std::nullopt;
|
||||
return(_pop);
|
||||
}
|
||||
|
||||
@@ -47,7 +51,9 @@ namespace kami {
|
||||
_pop = std::move(population);
|
||||
}
|
||||
|
||||
std::shared_ptr<Scheduler> Model::get_scheduler() {
|
||||
std::optional<std::shared_ptr<Scheduler>> Model::get_scheduler() {
|
||||
if(_sched == nullptr)
|
||||
return std::nullopt;
|
||||
return(_sched);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
@@ -38,7 +39,7 @@ namespace kami {
|
||||
this->set_rng(std::move(rng));
|
||||
}
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> RandomScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> RandomScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
shuffle(agent_list->begin(),agent_list->end(), *_rng);
|
||||
return std::move(this->SequentialScheduler::step(model, agent_list));
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
/*-
|
||||
* 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 {
|
||||
|
||||
} // namespace kami
|
||||
@@ -23,23 +23,38 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <kami/agent.h>
|
||||
#include <kami/sequential.h>
|
||||
|
||||
namespace kami {
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> SequentialScheduler::step(std::shared_ptr<Model> model) {
|
||||
return std::move(this->step(model, model->get_population()->get_agent_list()));
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> SequentialScheduler::step(std::shared_ptr<Model> model) {
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
return std::nullopt;
|
||||
|
||||
return std::move(this->step(model, population.value()->get_agent_list()));
|
||||
}
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> SequentialScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> SequentialScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto return_agent_list = std::make_shared<std::vector<AgentID>>();
|
||||
Scheduler::_step_counter++;
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
return std::nullopt;
|
||||
|
||||
Scheduler::_step_counter++;
|
||||
for(auto agent_id = agent_list->begin(); agent_id < agent_list->end(); agent_id++) {
|
||||
auto agent_opt = model->get_population()->get_agent_by_id(*agent_id);
|
||||
auto agent_opt = population.value()->get_agent_by_id(*agent_id);
|
||||
|
||||
if(agent_opt) {
|
||||
auto agent = agent_opt.value();
|
||||
|
||||
agent->step(model);
|
||||
return_agent_list->push_back(*agent_id);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <kami/agent.h>
|
||||
@@ -32,22 +33,33 @@
|
||||
|
||||
namespace kami {
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> StagedScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> StagedScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
this->SequentialScheduler::step(model, agent_list);
|
||||
return std::move(this->advance(model, agent_list));
|
||||
}
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> StagedScheduler::advance(std::shared_ptr<Model> model) {
|
||||
return std::move(this->advance(model, model->get_population()->get_agent_list()));
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> StagedScheduler::advance(std::shared_ptr<Model> model) {
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
return std::nullopt;
|
||||
|
||||
return std::move(this->advance(model, population.value()->get_agent_list()));
|
||||
}
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> StagedScheduler::advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> StagedScheduler::advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto return_agent_list = std::make_shared<std::vector<AgentID>>();
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
return std::nullopt;
|
||||
|
||||
for(auto agent_id = agent_list->begin(); agent_id < agent_list->end(); agent_id++) {
|
||||
auto agent_opt = model->get_population()->get_agent_by_id(*agent_id);
|
||||
auto agent_opt = population.value()->get_agent_by_id(*agent_id);
|
||||
|
||||
if(agent_opt) {
|
||||
auto agent = std::dynamic_pointer_cast<StagedAgent>(agent_opt.value());
|
||||
|
||||
agent->advance(model);
|
||||
return_agent_list->push_back(*agent_id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user