models.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /**
  2. * @file tests/pdevs/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_PDEVS_MODELS_HPP
  26. #define TESTS_PDEVS_MODELS_HPP
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  30. #include <chrono>
  31. #include <iostream>
  32. #define DELAY 100
  33. namespace artis {
  34. namespace tests {
  35. namespace pdevs {
  36. struct data {
  37. double x;
  38. double y;
  39. data()
  40. :x(0), y(0) { }
  41. data(double _x, double _y)
  42. :x(_x), y(_y) { }
  43. };
  44. class Inactive : public artis::pdevs::Dynamics<common::DoubleTime, Inactive> {
  45. public:
  46. Inactive(const std::string& name,
  47. const artis::pdevs::Context<common::DoubleTime, Inactive>& context)
  48. :
  49. artis::pdevs::Dynamics<common::DoubleTime, Inactive>(name, context) { }
  50. ~Inactive() override = default;
  51. };
  52. class A : public artis::pdevs::Dynamics<common::DoubleTime, A> {
  53. public:
  54. enum inputs {
  55. IN
  56. };
  57. enum outputs {
  58. OUT
  59. };
  60. A(const std::string& name,
  61. const artis::pdevs::Context<common::DoubleTime, A, artis::common::NoParameters>& context)
  62. :
  63. artis::pdevs::Dynamics<common::DoubleTime, A>(name, context)
  64. {
  65. input_ports({{IN, "in"}});
  66. output_ports({{OUT, "out"}});
  67. }
  68. ~A() override = default;
  69. void dint(const typename common::DoubleTime::type& t) override
  70. {
  71. #ifndef WITH_TRACE
  72. (void)t;
  73. #endif
  74. #ifdef WITH_TRACE
  75. common::Trace<common::DoubleTime>::trace()
  76. << common::TraceElement<common::DoubleTime>(get_name(), t,
  77. common::FormalismType::PDEVS,
  78. common::FunctionType::DELTA_INT,
  79. common::LevelType::USER);
  80. common::Trace<common::DoubleTime>::trace().flush();
  81. #endif
  82. if (_phase == WAIT) {
  83. ++_value.x;
  84. --_value.y;
  85. _phase = SEND;
  86. } else if (_phase == SEND) {
  87. _phase = WAIT;
  88. }
  89. }
  90. void
  91. dext(const typename common::DoubleTime::type& t,
  92. const typename common::DoubleTime::type& /* e */,
  93. const common::Bag<common::DoubleTime>& msgs) override
  94. {
  95. #ifndef WITH_TRACE
  96. (void)t;
  97. (void)msgs;
  98. #endif
  99. #ifdef WITH_TRACE
  100. common::Trace<common::DoubleTime>::trace()
  101. << common::TraceElement<common::DoubleTime>(get_name(), t,
  102. common::FormalismType::PDEVS,
  103. common::FunctionType::DELTA_EXT,
  104. common::LevelType::USER)
  105. << "messages = " << msgs.to_string();
  106. common::Trace<common::DoubleTime>::trace().flush();
  107. #endif
  108. _phase = SEND;
  109. }
  110. void dconf(const typename common::DoubleTime::type& t,
  111. const typename common::DoubleTime::type& /* e */,
  112. const common::Bag<common::DoubleTime>& msgs) override
  113. {
  114. #ifndef WITH_TRACE
  115. (void)t;
  116. (void)msgs;
  117. #endif
  118. #ifdef WITH_TRACE
  119. common::Trace<common::DoubleTime>::trace()
  120. << common::TraceElement<common::DoubleTime>(get_name(), t,
  121. common::FormalismType::PDEVS,
  122. common::FunctionType::DELTA_CONF,
  123. common::LevelType::USER)
  124. << "messages = " << msgs.to_string();
  125. common::Trace<common::DoubleTime>::trace().flush();
  126. #endif
  127. }
  128. void start(const typename common::DoubleTime::type& t) override
  129. {
  130. #ifndef WITH_TRACE
  131. (void)t;
  132. #endif
  133. #ifdef WITH_TRACE
  134. common::Trace<common::DoubleTime>::trace()
  135. << common::TraceElement<common::DoubleTime>(get_name(), t,
  136. common::FormalismType::PDEVS,
  137. common::FunctionType::START,
  138. common::LevelType::USER);
  139. common::Trace<common::DoubleTime>::trace().flush();
  140. #endif
  141. _phase = SEND;
  142. }
  143. typename common::DoubleTime::type
  144. ta(const typename common::DoubleTime::type& t) const override
  145. {
  146. #ifndef WITH_TRACE
  147. (void)t;
  148. #endif
  149. #ifdef WITH_TRACE
  150. common::Trace<common::DoubleTime>::trace()
  151. << common::TraceElement<common::DoubleTime>(get_name(), t,
  152. common::FormalismType::PDEVS,
  153. common::FunctionType::TA,
  154. common::LevelType::USER);
  155. common::Trace<common::DoubleTime>::trace().flush();
  156. #endif
  157. if (_phase == WAIT) {
  158. return 1;
  159. } else {
  160. return 0;
  161. }
  162. }
  163. common::Bag<common::DoubleTime>
  164. lambda(const typename common::DoubleTime::type& t) const override
  165. {
  166. #ifndef WITH_TRACE
  167. (void)t;
  168. #endif
  169. common::Bag<common::DoubleTime> msgs;
  170. if (_phase == SEND) {
  171. msgs.push_back(
  172. artis::common::ExternalEvent<common::DoubleTime>(OUT, _value));
  173. }
  174. #ifdef WITH_TRACE
  175. common::Trace<common::DoubleTime>::trace()
  176. << common::TraceElement<common::DoubleTime>(get_name(), t,
  177. common::FormalismType::PDEVS,
  178. common::FunctionType::LAMBDA,
  179. common::LevelType::USER)
  180. << "messages = " << msgs.to_string();
  181. common::Trace<common::DoubleTime>::trace().flush();
  182. #endif
  183. return msgs;
  184. }
  185. private:
  186. enum Phase {
  187. WAIT, SEND
  188. };
  189. Phase _phase;
  190. data _value;
  191. };
  192. class B : public artis::pdevs::Dynamics<common::DoubleTime, B> {
  193. public:
  194. enum inputs {
  195. IN
  196. };
  197. enum outputs {
  198. OUT
  199. };
  200. B(const std::string& name,
  201. const artis::pdevs::Context<common::DoubleTime, B, artis::common::NoParameters>& context)
  202. :
  203. artis::pdevs::Dynamics<common::DoubleTime, B>(name, context),
  204. _value(0)
  205. {
  206. input_ports({{IN, "in"}});
  207. output_ports({{OUT, "out"}});
  208. }
  209. ~B() override = default;
  210. void dint(const typename common::DoubleTime::type& t) override
  211. {
  212. #ifndef WITH_TRACE
  213. (void)t;
  214. #endif
  215. #ifdef WITH_TRACE
  216. common::Trace<common::DoubleTime>::trace()
  217. << common::TraceElement<common::DoubleTime>(get_name(), t,
  218. common::FormalismType::PDEVS,
  219. common::FunctionType::DELTA_INT,
  220. common::LevelType::USER);
  221. common::Trace<common::DoubleTime>::trace().flush();
  222. #endif
  223. if (_phase == SEND) {
  224. _phase = WAIT;
  225. }
  226. }
  227. void
  228. dext(const typename common::DoubleTime::type& t,
  229. const typename common::DoubleTime::type& /* e */,
  230. const common::Bag<common::DoubleTime>& msgs) override
  231. {
  232. #ifndef WITH_TRACE
  233. (void)t;
  234. (void)msgs;
  235. #endif
  236. #ifdef WITH_TRACE
  237. common::Trace<common::DoubleTime>::trace()
  238. << common::TraceElement<common::DoubleTime>(get_name(), t,
  239. common::FormalismType::PDEVS,
  240. common::FunctionType::DELTA_EXT,
  241. common::LevelType::USER)
  242. << "messages = " << msgs.to_string();
  243. common::Trace<common::DoubleTime>::trace().flush();
  244. #endif
  245. _phase = SEND;
  246. }
  247. void dconf(const typename common::DoubleTime::type& t,
  248. const typename common::DoubleTime::type& e,
  249. const common::Bag<common::DoubleTime>& msgs) override
  250. {
  251. #ifndef WITH_TRACE
  252. (void)t;
  253. (void)msgs;
  254. #endif
  255. dext(t, e, msgs);
  256. #ifdef WITH_TRACE
  257. common::Trace<common::DoubleTime>::trace()
  258. << common::TraceElement<common::DoubleTime>(get_name(), t,
  259. common::FormalismType::PDEVS,
  260. common::FunctionType::DELTA_CONF,
  261. common::LevelType::USER)
  262. << "messages = " << msgs.to_string();
  263. common::Trace<common::DoubleTime>::trace().flush();
  264. #endif
  265. }
  266. void start(const typename common::DoubleTime::type& t) override
  267. {
  268. #ifndef WITH_TRACE
  269. (void)t;
  270. #endif
  271. #ifdef WITH_TRACE
  272. common::Trace<common::DoubleTime>::trace()
  273. << common::TraceElement<common::DoubleTime>(get_name(), t,
  274. common::FormalismType::PDEVS,
  275. common::FunctionType::START,
  276. common::LevelType::USER);
  277. common::Trace<common::DoubleTime>::trace().flush();
  278. #endif
  279. _phase = WAIT;
  280. }
  281. typename common::DoubleTime::type ta(
  282. const typename common::DoubleTime::type& t) const override
  283. {
  284. #ifndef WITH_TRACE
  285. (void)t;
  286. #endif
  287. #ifdef WITH_TRACE
  288. common::Trace<common::DoubleTime>::trace()
  289. << common::TraceElement<common::DoubleTime>(get_name(), t,
  290. common::FormalismType::PDEVS,
  291. common::FunctionType::TA,
  292. common::LevelType::USER);
  293. common::Trace<common::DoubleTime>::trace().flush();
  294. #endif
  295. if (_phase == WAIT) {
  296. return common::DoubleTime::infinity;
  297. } else {
  298. return 0;
  299. }
  300. }
  301. common::Bag<common::DoubleTime> lambda(
  302. const typename common::DoubleTime::type& t) const override
  303. {
  304. #ifndef WITH_TRACE
  305. (void)t;
  306. #endif
  307. common::Bag<common::DoubleTime> msgs;
  308. if (_phase == SEND) {
  309. msgs.push_back(
  310. artis::common::ExternalEvent<common::DoubleTime>(OUT, _value));
  311. }
  312. #ifdef WITH_TRACE
  313. common::Trace<common::DoubleTime>::trace()
  314. << common::TraceElement<common::DoubleTime>(get_name(), t,
  315. common::FormalismType::PDEVS,
  316. common::FunctionType::LAMBDA,
  317. common::LevelType::USER)
  318. << "messages = " << msgs.to_string();
  319. common::Trace<common::DoubleTime>::trace().flush();
  320. #endif
  321. return msgs;
  322. }
  323. private:
  324. enum Phase {
  325. WAIT, SEND
  326. };
  327. Phase _phase;
  328. double _value;
  329. };
  330. class TwoStateModel : public artis::pdevs::Dynamics<common::DoubleTime, TwoStateModel> {
  331. public:
  332. TwoStateModel(const std::string& name,
  333. const artis::pdevs::Context<common::DoubleTime, TwoStateModel, artis::common::NoParameters>& context)
  334. :
  335. artis::pdevs::Dynamics<common::DoubleTime, TwoStateModel>(name, context) { }
  336. ~TwoStateModel() override = default;
  337. void dint(const typename common::DoubleTime::type& t) override
  338. {
  339. if (_phase == S1) {
  340. _phase = S2;
  341. } else if (_phase == S2) {
  342. _phase = S1;
  343. }
  344. _last_time = t;
  345. }
  346. void start(const typename common::DoubleTime::type& t) override
  347. {
  348. _phase = S1;
  349. _last_time = t;
  350. }
  351. typename common::DoubleTime::type
  352. ta(const typename common::DoubleTime::type& /* t */) const override
  353. {
  354. if (_phase == S1) {
  355. return 5;
  356. } else {
  357. return 6;
  358. }
  359. }
  360. common::Bag<common::DoubleTime>
  361. lambda(const typename common::DoubleTime::type& t) const override
  362. {
  363. std::cout << (t - _last_time) << std::endl;
  364. return common::Bag<common::DoubleTime>();
  365. }
  366. private:
  367. enum Phase {
  368. S1, S2
  369. };
  370. Phase _phase;
  371. typename common::DoubleTime::type _last_time;
  372. };
  373. class ThreeStateModel
  374. : public artis::pdevs::Dynamics<common::DoubleTime, ThreeStateModel> {
  375. public:
  376. enum outputs {
  377. OUT
  378. };
  379. enum states {
  380. HEIGHTS, SPEEDS, SCALES, N, INDEX, SIGMA, LAST_TIME
  381. };
  382. ThreeStateModel(const std::string& name,
  383. const artis::pdevs::Context<common::DoubleTime, ThreeStateModel, artis::common::NoParameters>& context)
  384. :
  385. artis::pdevs::Dynamics<common::DoubleTime, ThreeStateModel>(name, context)
  386. {
  387. DECLARE_STATES(std::vector<double>,
  388. ((HEIGHTS, &ThreeStateModel::heights), (SPEEDS, &ThreeStateModel::speeds), (SCALES, &ThreeStateModel::scales)));
  389. DECLARE_STATES(unsigned int,
  390. ((N, &ThreeStateModel::n), (INDEX, &ThreeStateModel::index)));
  391. DECLARE_STATES(typename common::DoubleTime::type,
  392. ((SIGMA, &ThreeStateModel::sigma), (LAST_TIME, &ThreeStateModel::_last_time)));
  393. output_ports({{OUT, "out"}});
  394. }
  395. ~ThreeStateModel() override = default;
  396. void dconf(const typename common::DoubleTime::type& t,
  397. const typename common::DoubleTime::type& e,
  398. const common::Bag<common::DoubleTime>& msgs) override
  399. {
  400. dext(t, e, msgs);
  401. }
  402. void dext(const typename common::DoubleTime::type& /* t */,
  403. const typename common::DoubleTime::type& /* e */,
  404. const common::Bag<common::DoubleTime>& msgs) override
  405. {
  406. for (common::Bag<common::DoubleTime>::const_iterator it = msgs.begin();
  407. it != msgs.end();
  408. ++it) {
  409. ++n;
  410. }
  411. if (sigma == 1) {
  412. if (n > 3) {
  413. ++index;
  414. if (index == scales.size()) {
  415. index = 0;
  416. }
  417. sigma = common::DoubleTime::infinity;
  418. if (scales[index] == 1) {
  419. scales[index] = 2;
  420. } else {
  421. scales[index] = 1;
  422. }
  423. n = 0;
  424. }
  425. } else {
  426. sigma = 1;
  427. n = 0;
  428. }
  429. }
  430. void dint(const typename common::DoubleTime::type& t) override
  431. {
  432. mark_full(t);
  433. if (full_N()) {
  434. raz();
  435. }
  436. compute();
  437. }
  438. void start(const typename common::DoubleTime::type& t) override
  439. {
  440. heights = {0, 0, 0, 0, 0};
  441. speeds = {0.21, 0.3, 0.7, 0.56, 0.14};
  442. scales = {1, 1, 1, 1, 1};
  443. index = 0;
  444. n = 0;
  445. sigma = 0;
  446. _last_time = t;
  447. }
  448. typename common::DoubleTime::type
  449. ta(const typename common::DoubleTime::type& /* t */) const override { return sigma; }
  450. common::Bag<common::DoubleTime>
  451. lambda(const typename common::DoubleTime::type& /* t */) const override
  452. {
  453. common::Bag<common::DoubleTime> msgs;
  454. if (full()) {
  455. msgs.push_back(artis::common::ExternalEvent<common::DoubleTime>(OUT, 0));
  456. }
  457. return msgs;
  458. }
  459. private:
  460. void compute()
  461. {
  462. for (unsigned int i = 0; i < heights.size(); ++i) {
  463. if (heights[i] != -1 and heights[i] < 10) {
  464. heights[i] += speeds[i] * scales[i];
  465. }
  466. }
  467. }
  468. void display() const
  469. {
  470. for (std::vector<double>::const_iterator it = heights.begin();
  471. it != heights.end(); ++it) {
  472. std::cout << *it << " ";
  473. }
  474. std::cout << std::endl;
  475. }
  476. void display_full() const
  477. {
  478. unsigned int i = 1;
  479. for (std::vector<double>::const_iterator it = heights.begin();
  480. it != heights.end(); ++it, ++i) {
  481. if (*it > 10) {
  482. std::cout << "S" << i;
  483. }
  484. }
  485. std::cout << std::endl;
  486. }
  487. bool full() const
  488. {
  489. unsigned int n = 0;
  490. for (std::vector<double>::const_iterator it = heights.begin();
  491. it != heights.end(); ++it) {
  492. if (*it > 10) {
  493. ++n;
  494. }
  495. }
  496. return n > 0;
  497. }
  498. bool full_N() const
  499. {
  500. unsigned int n = 0;
  501. for (std::vector<double>::const_iterator it = heights.begin();
  502. it != heights.end(); ++it) {
  503. if (*it == -1) {
  504. ++n;
  505. }
  506. }
  507. return n >= 2;
  508. }
  509. void mark_full(const typename common::DoubleTime::type& t)
  510. {
  511. for (std::vector<double>::iterator it = heights.begin();
  512. it != heights.end(); ++it) {
  513. if (*it > 10) {
  514. *it = -1;
  515. _last_time = t;
  516. }
  517. }
  518. }
  519. void raz()
  520. {
  521. for (std::vector<double>::iterator it = heights.begin();
  522. it != heights.end(); ++it) {
  523. if (*it == -1) {
  524. *it = 0;
  525. }
  526. }
  527. }
  528. // state
  529. std::vector<double> heights;
  530. std::vector<double> speeds;
  531. std::vector<double> scales;
  532. unsigned int index;
  533. unsigned int n;
  534. typename common::DoubleTime::type sigma;
  535. typename common::DoubleTime::type _last_time;
  536. };
  537. }
  538. }
  539. } // namespace artis tests pdevs
  540. #endif