models.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/qss/Derivative.hpp>
  30. #include <artis-star/kernel/dtss/Dynamics.hpp>
  31. namespace artis {
  32. namespace tests {
  33. namespace qss {
  34. class Constant : public artis::qss::Derivative<common::DoubleTime, Constant> {
  35. public:
  36. Constant(const std::string &name,
  37. const artis::pdevs::Context<common::DoubleTime,
  38. Constant,
  39. artis::common::NoParameters> &context)
  40. :
  41. artis::qss::Derivative<common::DoubleTime, Constant>(name,
  42. context) {}
  43. ~Constant() override = default;
  44. double compute() const override { return 0.5; }
  45. };
  46. struct ParabolaParameters {
  47. double alpha;
  48. };
  49. class Parabola
  50. : public artis::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters> {
  51. public:
  52. Parabola(const std::string &name,
  53. const artis::pdevs::Context<common::DoubleTime, Parabola, ParabolaParameters> &context)
  54. :
  55. artis::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters>(
  56. name,
  57. context), _alpha(context.parameters().alpha) {
  58. internal("X", &Parabola::_x);
  59. }
  60. ~Parabola() override = default;
  61. double compute() const override { return _alpha * _x; }
  62. private:
  63. double _alpha;
  64. double _x;
  65. };
  66. struct PreyPredatorParameters {
  67. double a;
  68. double b;
  69. double d;
  70. double e;
  71. };
  72. class Predator
  73. : public artis::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters> {
  74. public:
  75. unsigned int IN_X;
  76. Predator(const std::string &name,
  77. const artis::pdevs::Context<common::DoubleTime,
  78. Predator,
  79. PreyPredatorParameters> &context)
  80. :
  81. artis::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters>(
  82. name,
  83. context), _b(context.parameters().b), _d(context.parameters().d),
  84. _e(context.parameters().e) {
  85. internal("Y", &Predator::_y);
  86. IN_X = external("X", &Predator::_x);
  87. }
  88. ~Predator() override = default;
  89. double compute() const override { return _b * _d * _x * _y - _e * _y; }
  90. private:
  91. // parameters
  92. double _b;
  93. double _d;
  94. double _e;
  95. // state
  96. double _x;
  97. double _y;
  98. };
  99. class Prey
  100. : public artis::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters> {
  101. public:
  102. unsigned int IN_Y;
  103. Prey(const std::string &name,
  104. const artis::pdevs::Context<common::DoubleTime, Prey, PreyPredatorParameters> &context)
  105. :
  106. artis::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters>(
  107. name,
  108. context), _a(context.parameters().a), _b(context.parameters().b) {
  109. internal("X", &Prey::_x);
  110. IN_Y = external("Y", &Prey::_y);
  111. }
  112. ~Prey() override = default;
  113. double compute() const override { return _a * _x - _b * _y * _x; }
  114. private:
  115. // parameters
  116. double _a;
  117. double _b;
  118. // state
  119. double _x;
  120. double _y;
  121. };
  122. struct SmartGardenerParameters {
  123. double threshold;
  124. double prey_proportion;
  125. double predator_proportion;
  126. double delay;
  127. };
  128. class SmartGardener
  129. : public artis::pdevs::Dynamics<common::DoubleTime, SmartGardener, SmartGardenerParameters> {
  130. public:
  131. enum inputs {
  132. IN_X, IN_Y
  133. };
  134. enum outputs {
  135. OUT_X, OUT_Y
  136. };
  137. SmartGardener(const std::string &name,
  138. const artis::pdevs::Context<common::DoubleTime,
  139. SmartGardener,
  140. SmartGardenerParameters> &context)
  141. :
  142. artis::pdevs::Dynamics<common::DoubleTime, SmartGardener, SmartGardenerParameters>(
  143. name, context),
  144. _threshold(context.parameters().threshold),
  145. _prey_proportion(context.parameters().prey_proportion),
  146. _predator_proportion(context.parameters().predator_proportion),
  147. _delay(context.parameters().delay) {
  148. input_ports({{IN_X, "in_x"},
  149. {IN_Y, "in_y"}});
  150. output_ports({{OUT_X, "out_x"},
  151. {OUT_Y, "out_y"}});
  152. }
  153. ~SmartGardener() override = default;
  154. void dint(const typename common::DoubleTime::type & /* t */) override {
  155. switch (_phase) {
  156. case INIT:_phase = IDLE;
  157. _sigma = _delay;
  158. break;
  159. case IDLE:
  160. if (_prey_amount > _threshold) {
  161. _phase = PEST;
  162. } else {
  163. _phase = IDLE;
  164. _sigma = _delay;
  165. }
  166. break;
  167. case PEST:_phase = IDLE;
  168. _sigma = _delay;
  169. break;
  170. }
  171. }
  172. void
  173. dext(const typename common::DoubleTime::type & /* t */,
  174. const typename common::DoubleTime::type &e,
  175. const common::Bag<common::DoubleTime> &bag) override {
  176. std::for_each(bag.begin(), bag.end(),
  177. [this](const common::ExternalEvent<common::DoubleTime> &event) {
  178. artis::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
  189. start(const typename common::DoubleTime::type & /* t */) override { _phase = INIT; }
  190. typename common::DoubleTime::type
  191. ta(const typename common::DoubleTime::type & /* t */) const override {
  192. switch (_phase) {
  193. case INIT:return 0.0;
  194. case IDLE:return _sigma;
  195. case PEST:return 0.0;
  196. }
  197. return common::DoubleTime::infinity;
  198. }
  199. common::Bag<common::DoubleTime>
  200. lambda(const typename common::DoubleTime::type & /* t */) const override {
  201. common::Bag<common::DoubleTime> bag;
  202. if (_phase == PEST) {
  203. artis::qss::IntegratorData data = {_prey_amount * _prey_proportion};
  204. bag.push_back(common::ExternalEvent<common::DoubleTime>(OUT_X, data));
  205. data = {_predator_amount * _predator_proportion};
  206. bag.push_back(common::ExternalEvent<common::DoubleTime>(OUT_Y, data));
  207. }
  208. return bag;
  209. }
  210. private:
  211. enum Phase {
  212. INIT, IDLE, PEST
  213. };
  214. // parameters
  215. double _threshold;
  216. double _prey_proportion;
  217. double _predator_proportion;
  218. double _delay;
  219. // state
  220. Phase _phase;
  221. common::DoubleTime::type _sigma;
  222. double _prey_amount;
  223. double _predator_amount;
  224. };
  225. struct DiscretePreyPredatorParameters {
  226. artis::common::DoubleTime::type time_step;
  227. double init_value;
  228. double a;
  229. double b;
  230. double d;
  231. double e;
  232. };
  233. class DiscretePredator
  234. : public artis::dtss::Dynamics<artis::common::DoubleTime,
  235. DiscretePredator,
  236. DiscretePreyPredatorParameters> {
  237. public:
  238. enum inputs {
  239. RESET, IN_X, IN_Y
  240. };
  241. enum outputs {
  242. OUT
  243. };
  244. enum vars {
  245. VALUE
  246. };
  247. DiscretePredator(const std::string &name,
  248. const artis::dtss::Context<artis::common::DoubleTime,
  249. DiscretePredator,
  250. DiscretePreyPredatorParameters> &context)
  251. :
  252. artis::dtss::Dynamics<artis::common::DoubleTime,
  253. DiscretePredator,
  254. DiscretePreyPredatorParameters>(
  255. name, context), _init_value(context.parameters().init_value),
  256. _b(context.parameters().b), _d(context.parameters().d),
  257. _e(context.parameters().e) {
  258. input_ports({{RESET, "reset"},
  259. {IN_X, "in_x"},
  260. {IN_Y, "in_y"}});
  261. output_ports({{OUT, "out"}});
  262. observables({{VALUE, "value"}});
  263. }
  264. ~DiscretePredator() override = default;
  265. void transition(const artis::common::Bag<artis::common::DoubleTime> &bag,
  266. const artis::common::DoubleTime::type & /* t */) override {
  267. std::for_each(bag.begin(), bag.end(),
  268. [this](const artis::common::ExternalEvent<artis::common::DoubleTime> &event) {
  269. if (event.on_port(IN_X)) {
  270. artis::qss::IntegratorData data;
  271. event.data()(data);
  272. _x = data.value;
  273. } else if (event.on_port(RESET)) {
  274. // TODO
  275. }
  276. });
  277. _y += (_b * _d * _x * _y - _e * _y) * time_step();
  278. }
  279. void start(const artis::common::DoubleTime::type & /* t */) override {
  280. _y = _init_value;
  281. }
  282. artis::common::Bag<artis::common::DoubleTime>
  283. lambda(const artis::common::DoubleTime::type & /* t */) const override {
  284. artis::common::Bag<artis::common::DoubleTime> msgs;
  285. artis::qss::IntegratorData data = {_y};
  286. msgs.push_back(
  287. artis::common::ExternalEvent<artis::common::DoubleTime>(OUT, data));
  288. return msgs;
  289. }
  290. artis::common::Value observe(const artis::common::DoubleTime::type & /* t */,
  291. unsigned int index) const override {
  292. if (index == VALUE) {
  293. return (double) _y;
  294. }
  295. return artis::common::Value();
  296. }
  297. private:
  298. // parameters
  299. double _init_value;
  300. double _b;
  301. double _d;
  302. double _e;
  303. // state
  304. double _x;
  305. double _y;
  306. };
  307. }
  308. }
  309. } // namespace artis tests qss
  310. #endif