You know, there is no reason Model has to define the step/run interface

The user can do that for themselves
This commit is contained in:
James P. Howard, II
2022-08-19 19:20:48 -04:00
parent 78aca204f4
commit d884e603cb
10 changed files with 99 additions and 70 deletions

View File

@@ -1,6 +1,10 @@
Changelog
=========
- :feature `0` Removed step()/run() from Model interface
- :feature:`0` Revised interfaces to the grids
- :feature:`0` Updated all support packages to most current versions
- :release:`0.5.1 <2022.08.11>`
- :support:`0` Completed initial unit tests
- :support:`0` Added background info to READ ME

View File

@@ -163,12 +163,6 @@ BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsig
}
}
std::shared_ptr<kami::Model> BoltzmannWealthModel1D::run(unsigned int steps) {
for (auto i = 0; i < steps; i++)
step();
return shared_from_this();
}
std::shared_ptr<kami::Model> BoltzmannWealthModel1D::step() {
console->trace("Executing model step {}", ++_step_count);
_sched->step(shared_from_this());

View File

@@ -96,14 +96,7 @@ public:
/**
* Execute a single time-step for the model.
*/
std::shared_ptr<kami::Model> step() override;
/**
* Execute a number of time-steps for the model.
*
* @param[in] n the number of steps to execute.
*/
std::shared_ptr<kami::Model> run(unsigned int n) override;
std::shared_ptr<kami::Model> step();
private:
unsigned int _step_count;

View File

@@ -164,12 +164,6 @@ BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsig
}
}
std::shared_ptr<kami::Model> BoltzmannWealthModel2D::run(unsigned int steps) {
for (auto i = 0; i < steps; i++)
step();
return shared_from_this();
}
std::shared_ptr<kami::Model> BoltzmannWealthModel2D::step() {
console->trace("Executing model step {}", _step_count++);
_sched->step(shared_from_this());

View File

@@ -99,14 +99,7 @@ public:
/**
* Execute a single time-step for the model.
*/
std::shared_ptr<kami::Model> step() override;
/**
* Execute a number of time-steps for the model.
*
* @param[in] n the number of steps to execute.
*/
std::shared_ptr<kami::Model> run(unsigned int n) override;
std::shared_ptr<kami::Model> step();
private:
unsigned int _step_count;

View File

@@ -45,27 +45,6 @@ namespace kami {
class LIBKAMI_EXPORT Model : public std::enable_shared_from_this<Model> {
public:
/**
* @brief Execute a fixed number of time-steps for the model.
*
* @details This function should execute a fixed number of time-steps for the model.
*
* @param[in] n the number of time steps to execute.
*
* @returns a shared pointer to this instance of `Model`
*/
virtual std::shared_ptr<Model> run(unsigned int n) = 0;
/**
* @brief Execute a single time-step for the model.
*
* @details This function should step the model instance. Any activities that the
* model should perform as part of its time step should be in this function.
*
* @returns a shared pointer to this instance of `Model`
*/
virtual std::shared_ptr<Model> step() = 0;
/**
* @brief Get the `Domain` associated with this model
*

View File

@@ -40,14 +40,6 @@ public:
};
class TestModel : public Model {
public:
std::shared_ptr<Model> step() override {
return shared_from_this();
}
std::shared_ptr<Model> run(unsigned int) override {
return shared_from_this();
}
};
TEST(Agent, DefaultConstructor) {

View File

@@ -44,11 +44,7 @@ public:
class TestModel : public Model {
public:
std::shared_ptr<Model> step() override {
return shared_from_this();
}
std::shared_ptr<Model> run(unsigned int) override {
std::shared_ptr<Model> step() {
return shared_from_this();
}
};

View File

@@ -0,0 +1,92 @@
/*-
* 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 <kami/agent.h>
#include <kami/population.h>
#include <kami/sequential.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace kami;
using namespace std;
class TestAgent : public Agent {
public:
AgentID step(std::shared_ptr<Model> model) override {
return get_agent_id();
}
};
class TestModel : public Model {
public:
std::optional<std::shared_ptr<std::vector<AgentID>>> step() {
return _sched->step(shared_from_this());
}
};
class SequentialSchedulerTest : public ::testing::Test {
protected:
std::shared_ptr<TestModel> mod = nullptr;
void SetUp() override {
mod = std::make_shared<TestModel>();
auto popul_foo = std::make_shared<Population>();
auto sched_foo = std::make_shared<SequentialScheduler>();
// Domain is not required for this test
static_cast<void>(mod->set_population(popul_foo));
static_cast<void>(mod->set_scheduler(sched_foo));
for (auto i = 0; i < 10; i++) {
auto agent_foo = std::make_shared<TestAgent>();
static_cast<void>(popul_foo->add_agent(agent_foo));
}
}
};
TEST(SequentialScheduler, DefaultConstructor) {
// There is really no way this can go wrong, but
// we add this check anyway in case of future
// changes.
EXPECT_NO_THROW(
const SequentialScheduler sched_foo;
);
}
TEST_F(SequentialSchedulerTest, step) {
auto tval = mod->get_population().value()->get_agent_list();
auto rval = mod->step();
EXPECT_TRUE(rval);
EXPECT_EQ(rval.value()->size(), 10);
EXPECT_EQ(*rval.value(), *tval);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -44,14 +44,6 @@ public:
};
class TestModel : public Model {
public:
std::shared_ptr<Model> step() override {
return shared_from_this();
}
std::shared_ptr<Model> run(unsigned int) override {
return shared_from_this();
}
};
TEST(StagedAgent, DefaultConstructor) {