From d884e603cb26b5e2ac6080b8eafd0d21b2c4447c Mon Sep 17 00:00:00 2001 From: "James P. Howard, II" Date: Fri, 19 Aug 2022 19:20:48 -0400 Subject: [PATCH] You know, there is no reason Model has to define the step/run interface The user can do that for themselves --- docs/changelog.rst | 4 ++ examples/boltzmann1d/boltzmann1d.cc | 6 -- examples/boltzmann1d/boltzmann1d.h | 9 +-- examples/boltzmann2d/boltzmann2d.cc | 6 -- examples/boltzmann2d/boltzmann2d.h | 9 +-- include/kami/model.h | 21 ------- test/unit-kami-agent.cc | 8 --- test/unit-kami-model.cc | 6 +- test/unit-kami-sequential.cc | 92 +++++++++++++++++++++++++++++ test/unit-kami-stagedagent.cc | 8 --- 10 files changed, 99 insertions(+), 70 deletions(-) create mode 100644 test/unit-kami-sequential.cc diff --git a/docs/changelog.rst b/docs/changelog.rst index 022a476..a315f65 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 diff --git a/examples/boltzmann1d/boltzmann1d.cc b/examples/boltzmann1d/boltzmann1d.cc index eab5463..c0f1ac7 100644 --- a/examples/boltzmann1d/boltzmann1d.cc +++ b/examples/boltzmann1d/boltzmann1d.cc @@ -163,12 +163,6 @@ BoltzmannWealthModel1D::BoltzmannWealthModel1D(unsigned int number_agents, unsig } } -std::shared_ptr BoltzmannWealthModel1D::run(unsigned int steps) { - for (auto i = 0; i < steps; i++) - step(); - return shared_from_this(); -} - std::shared_ptr BoltzmannWealthModel1D::step() { console->trace("Executing model step {}", ++_step_count); _sched->step(shared_from_this()); diff --git a/examples/boltzmann1d/boltzmann1d.h b/examples/boltzmann1d/boltzmann1d.h index 2df56c6..be38c89 100644 --- a/examples/boltzmann1d/boltzmann1d.h +++ b/examples/boltzmann1d/boltzmann1d.h @@ -96,14 +96,7 @@ public: /** * Execute a single time-step for the model. */ - std::shared_ptr step() override; - - /** - * Execute a number of time-steps for the model. - * - * @param[in] n the number of steps to execute. - */ - std::shared_ptr run(unsigned int n) override; + std::shared_ptr step(); private: unsigned int _step_count; diff --git a/examples/boltzmann2d/boltzmann2d.cc b/examples/boltzmann2d/boltzmann2d.cc index b8967b6..bb91463 100644 --- a/examples/boltzmann2d/boltzmann2d.cc +++ b/examples/boltzmann2d/boltzmann2d.cc @@ -164,12 +164,6 @@ BoltzmannWealthModel2D::BoltzmannWealthModel2D(unsigned int number_agents, unsig } } -std::shared_ptr BoltzmannWealthModel2D::run(unsigned int steps) { - for (auto i = 0; i < steps; i++) - step(); - return shared_from_this(); -} - std::shared_ptr BoltzmannWealthModel2D::step() { console->trace("Executing model step {}", _step_count++); _sched->step(shared_from_this()); diff --git a/examples/boltzmann2d/boltzmann2d.h b/examples/boltzmann2d/boltzmann2d.h index 1bbfcc6..8e52426 100644 --- a/examples/boltzmann2d/boltzmann2d.h +++ b/examples/boltzmann2d/boltzmann2d.h @@ -99,14 +99,7 @@ public: /** * Execute a single time-step for the model. */ - std::shared_ptr step() override; - - /** - * Execute a number of time-steps for the model. - * - * @param[in] n the number of steps to execute. - */ - std::shared_ptr run(unsigned int n) override; + std::shared_ptr step(); private: unsigned int _step_count; diff --git a/include/kami/model.h b/include/kami/model.h index 3b06293..60ce064 100644 --- a/include/kami/model.h +++ b/include/kami/model.h @@ -45,27 +45,6 @@ namespace kami { class LIBKAMI_EXPORT Model : public std::enable_shared_from_this { 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 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 step() = 0; - /** * @brief Get the `Domain` associated with this model * diff --git a/test/unit-kami-agent.cc b/test/unit-kami-agent.cc index 769a47e..c199212 100644 --- a/test/unit-kami-agent.cc +++ b/test/unit-kami-agent.cc @@ -40,14 +40,6 @@ public: }; class TestModel : public Model { -public: - std::shared_ptr step() override { - return shared_from_this(); - } - - std::shared_ptr run(unsigned int) override { - return shared_from_this(); - } }; TEST(Agent, DefaultConstructor) { diff --git a/test/unit-kami-model.cc b/test/unit-kami-model.cc index dda5294..e4d9921 100644 --- a/test/unit-kami-model.cc +++ b/test/unit-kami-model.cc @@ -44,11 +44,7 @@ public: class TestModel : public Model { public: - std::shared_ptr step() override { - return shared_from_this(); - } - - std::shared_ptr run(unsigned int) override { + std::shared_ptr step() { return shared_from_this(); } }; diff --git a/test/unit-kami-sequential.cc b/test/unit-kami-sequential.cc new file mode 100644 index 0000000..08bc7c9 --- /dev/null +++ b/test/unit-kami-sequential.cc @@ -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 +#include +#include + +#include +#include + +using namespace kami; +using namespace std; + +class TestAgent : public Agent { +public: + AgentID step(std::shared_ptr model) override { + return get_agent_id(); + } +}; + +class TestModel : public Model { +public: + std::optional>> step() { + return _sched->step(shared_from_this()); + } +}; + +class SequentialSchedulerTest : public ::testing::Test { +protected: + std::shared_ptr mod = nullptr; + + void SetUp() override { + mod = std::make_shared(); + auto popul_foo = std::make_shared(); + auto sched_foo = std::make_shared(); + + // Domain is not required for this test + static_cast(mod->set_population(popul_foo)); + static_cast(mod->set_scheduler(sched_foo)); + + for (auto i = 0; i < 10; i++) { + auto agent_foo = std::make_shared(); + static_cast(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(); +} + diff --git a/test/unit-kami-stagedagent.cc b/test/unit-kami-stagedagent.cc index 124f1da..654f0f2 100644 --- a/test/unit-kami-stagedagent.cc +++ b/test/unit-kami-stagedagent.cc @@ -44,14 +44,6 @@ public: }; class TestModel : public Model { -public: - std::shared_ptr step() override { - return shared_from_this(); - } - - std::shared_ptr run(unsigned int) override { - return shared_from_this(); - } }; TEST(StagedAgent, DefaultConstructor) {