models.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @file tests/qss/models.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef TESTS_QSS_MODELS_HPP
  26. #define TESTS_QSS_MODELS_HPP
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/pdevs/qss/Derivative.hpp>
  30. namespace artis {
  31. namespace tests {
  32. namespace qss {
  33. class Constant : public artis::pdevs::qss::Derivative<common::DoubleTime, Constant> {
  34. public:
  35. Constant(const std::string& name,
  36. const artis::pdevs::Context<common::DoubleTime, Constant, artis::common::NoParameters>& context)
  37. :
  38. artis::pdevs::qss::Derivative<common::DoubleTime, Constant>(name,
  39. context) { }
  40. ~Constant() override = default;
  41. double compute() const override { return 0.5; }
  42. };
  43. struct ParabolaParameters {
  44. double alpha;
  45. };
  46. class Parabola
  47. : public artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters> {
  48. public:
  49. Parabola(const std::string& name,
  50. const artis::pdevs::Context<common::DoubleTime, Parabola, ParabolaParameters>& context)
  51. :
  52. artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters>(
  53. name,
  54. context), _alpha(context.parameters().alpha)
  55. {
  56. internal("X", &Parabola::_x);
  57. }
  58. ~Parabola() override = default;
  59. double compute() const override { return _alpha * _x; }
  60. private:
  61. double _alpha;
  62. double _x;
  63. };
  64. struct PreyPredatorParameters {
  65. double a;
  66. double b;
  67. double d;
  68. double e;
  69. };
  70. class Predator
  71. : public artis::pdevs::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters> {
  72. public:
  73. unsigned int IN_X;
  74. Predator(const std::string& name,
  75. const artis::pdevs::Context<common::DoubleTime, Predator, PreyPredatorParameters>& context)
  76. :
  77. artis::pdevs::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters>(
  78. name,
  79. context), _b(context.parameters().b), _d(context.parameters().d),
  80. _e(context.parameters().e)
  81. {
  82. internal("Y", &Predator::_y);
  83. IN_X = external("X", &Predator::_x);
  84. }
  85. ~Predator() override = default;
  86. double compute() const override { return _b * _d * _x * _y - _e * _y; }
  87. private:
  88. // parameters
  89. double _b;
  90. double _d;
  91. double _e;
  92. // state
  93. double _x;
  94. double _y;
  95. };
  96. class Prey
  97. : public artis::pdevs::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters> {
  98. public:
  99. unsigned int IN_Y;
  100. Prey(const std::string& name,
  101. const artis::pdevs::Context<common::DoubleTime, Prey, PreyPredatorParameters>& context)
  102. :
  103. artis::pdevs::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters>(
  104. name,
  105. context), _a(context.parameters().a), _b(context.parameters().b)
  106. {
  107. internal("X", &Prey::_x);
  108. IN_Y = external("Y", &Prey::_y);
  109. }
  110. ~Prey() override = default;
  111. double compute() const override { return _a * _x - _b * _y * _x; }
  112. private:
  113. // parameters
  114. double _a;
  115. double _b;
  116. // state
  117. double _x;
  118. double _y;
  119. };
  120. struct SmartGardenerParameters {
  121. double threshold;
  122. double prey_proportion;
  123. double predator_proportion;
  124. double delay;
  125. };
  126. class SmartGardener
  127. : public artis::pdevs::Dynamics<common::DoubleTime, SmartGardener, SmartGardenerParameters> {
  128. public:
  129. enum inputs {
  130. IN_X, IN_Y
  131. };
  132. enum outputs {
  133. OUT_X, OUT_Y
  134. };
  135. SmartGardener(const std::string& name,
  136. const artis::pdevs::Context<common::DoubleTime, SmartGardener, SmartGardenerParameters>& context)
  137. :
  138. artis::pdevs::Dynamics<common::DoubleTime, SmartGardener, SmartGardenerParameters>(
  139. name, context),
  140. _threshold(context.parameters().threshold),
  141. _prey_proportion(context.parameters().prey_proportion),
  142. _predator_proportion(context.parameters().predator_proportion),
  143. _delay(context.parameters().delay)
  144. {
  145. input_ports({{IN_X, "in_x"},
  146. {IN_Y, "in_y"}});
  147. output_ports({{OUT_X, "out_x"},
  148. {OUT_Y, "out_y"}});
  149. }
  150. ~SmartGardener() override = default;
  151. void dint(typename common::DoubleTime::type /* t */) override
  152. {
  153. switch (_phase) {
  154. case INIT:
  155. _phase = IDLE;
  156. _sigma = _delay;
  157. break;
  158. case IDLE:
  159. if (_prey_amount > _threshold) {
  160. _phase = PEST;
  161. } else {
  162. _phase = IDLE;
  163. _sigma = _delay;
  164. }
  165. break;
  166. case PEST:
  167. _phase = IDLE;
  168. _sigma = _delay;
  169. break;
  170. }
  171. }
  172. void
  173. dext(typename common::DoubleTime::type /* t */, typename common::DoubleTime::type e,
  174. const common::Bag<common::DoubleTime>& bag) override
  175. {
  176. std::for_each(bag.begin(), bag.end(),
  177. [this](const common::ExternalEvent<common::DoubleTime>& event) {
  178. artis::pdevs::qss::IntegratorData data;
  179. event.data()(data);
  180. if (event.on_port(IN_X)) {
  181. _prey_amount = data.value;
  182. } else if (event.on_port(IN_Y)) {
  183. _predator_amount = data.value;
  184. }
  185. });
  186. _sigma -= e;
  187. }
  188. void start(typename common::DoubleTime::type /* t */) override { _phase = INIT; }
  189. typename common::DoubleTime::type
  190. ta(typename common::DoubleTime::type /* t */) const override
  191. {
  192. switch (_phase) {
  193. case INIT:
  194. return 0.0;
  195. case IDLE:
  196. return _sigma;
  197. case PEST:
  198. return 0.0;
  199. }
  200. return common::DoubleTime::infinity;
  201. }
  202. common::Bag<common::DoubleTime>
  203. lambda(typename common::DoubleTime::type /* t */) const override
  204. {
  205. common::Bag<common::DoubleTime> bag;
  206. if (_phase == PEST) {
  207. artis::pdevs::qss::IntegratorData data = {_prey_amount * _prey_proportion};
  208. bag.push_back(common::ExternalEvent<common::DoubleTime>(OUT_X, data));
  209. data = {_predator_amount * _predator_proportion};
  210. bag.push_back(common::ExternalEvent<common::DoubleTime>(OUT_Y, data));
  211. }
  212. return bag;
  213. }
  214. private:
  215. enum Phase {
  216. INIT, IDLE, PEST
  217. };
  218. // parameters
  219. double _threshold;
  220. double _prey_proportion;
  221. double _predator_proportion;
  222. double _delay;
  223. // state
  224. Phase _phase;
  225. common::DoubleTime::type _sigma;
  226. double _prey_amount;
  227. double _predator_amount;
  228. };
  229. }
  230. }
  231. } // namespace artis tests qss
  232. #endif