models.hpp 14 KB

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