models.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * @file tests/multithreading/simple/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-2022 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_MULTITHREADING_SIMPLE_MODELS_HPP
  26. #define TESTS_MULTITHREADING_SIMPLE_MODELS_HPP
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  29. #include <random>
  30. #include <iostream>
  31. namespace artis {
  32. namespace tests {
  33. namespace multithreading {
  34. namespace simple {
  35. void delay()
  36. {
  37. for (unsigned int i = 0; i < 1000; ++i) {
  38. std::vector<int> v;
  39. for (unsigned int j = 1000; j > 0; --j) {
  40. v.push_back(j);
  41. }
  42. std::sort(v.begin(), v.end());
  43. }
  44. }
  45. struct State
  46. {
  47. enum values
  48. {
  49. STOP, UP, MAX, DOWN
  50. };
  51. };
  52. struct Vehicle
  53. {
  54. unsigned int index;
  55. double v_max;
  56. double acceleration;
  57. State::values state;
  58. artis::common::DoubleTime::type next_time;
  59. };
  60. struct GeneratorParameters
  61. {
  62. double v_max;
  63. double mean;
  64. double stddev;
  65. unsigned long seed;
  66. };
  67. class Generator
  68. : public artis::pdevs::Dynamics<artis::common::DoubleTime, Generator, GeneratorParameters>
  69. {
  70. public:
  71. struct outputs
  72. {
  73. enum values
  74. {
  75. OUT
  76. };
  77. };
  78. Generator(const std::string &name,
  79. const artis::pdevs::Context<artis::common::DoubleTime,
  80. Generator,
  81. GeneratorParameters> &context)
  82. :
  83. artis::pdevs::Dynamics<artis::common::DoubleTime, Generator, GeneratorParameters>(
  84. name, context),
  85. _v_max(context.parameters().v_max),
  86. _distribution(context.parameters().mean, context.parameters().stddev),
  87. _v_max_distribution(0.5, 1.),
  88. _port_distribution(0, 7)
  89. {
  90. _generator.seed(context.parameters().seed);
  91. output_ports({{outputs::OUT, "out_1"}});
  92. output_ports({{outputs::OUT + 1, "out_2"}});
  93. output_ports({{outputs::OUT + 2, "out_3"}});
  94. output_ports({{outputs::OUT + 3, "out_4"}});
  95. output_ports({{outputs::OUT + 4, "out_5"}});
  96. output_ports({{outputs::OUT + 5, "out_6"}});
  97. output_ports({{outputs::OUT + 6, "out_7"}});
  98. output_ports({{outputs::OUT + 7, "out_8"}});
  99. }
  100. ~Generator() override = default;
  101. void dint(const artis::common::DoubleTime::type &t) override
  102. {
  103. // std::cout << get_full_name() << " at " << t << " - dint" << std::endl;
  104. _last_time = t;
  105. _sigma = _distribution(_generator);
  106. _sigma = _sigma <= 0 ? 0.1 : _sigma;
  107. _next_v_max = _v_max * _v_max_distribution(_generator);
  108. _next_port = _port_distribution(_generator);
  109. ++_index;
  110. }
  111. void start(const artis::common::DoubleTime::type &t) override
  112. {
  113. // std::cout << get_full_name() << " at " << t << " - start" << std::endl;
  114. _last_time = t;
  115. _sigma = _distribution(_generator);
  116. _sigma = _sigma <= 0 ? 0.1 : _sigma;
  117. _next_v_max = _v_max * _v_max_distribution(_generator);
  118. _next_port = _port_distribution(_generator);
  119. _index = 1;
  120. }
  121. artis::common::DoubleTime::type
  122. ta(const artis::common::DoubleTime::type & /* t */) const override
  123. { return _sigma; }
  124. artis::common::event::Bag<artis::common::DoubleTime>
  125. lambda(const artis::common::DoubleTime::type &t) const override
  126. {
  127. artis::common::event::Bag<artis::common::DoubleTime> bag;
  128. // std::cout << get_full_name() << " at " << t << " - lambda" << std::endl;
  129. if (t > 0) {
  130. Vehicle vehicle = {_index, _next_v_max, 0.5, State::STOP, t};
  131. bag.push_back(
  132. artis::common::event::ExternalEvent<artis::common::DoubleTime>(
  133. outputs::OUT + _next_port, vehicle));
  134. }
  135. return bag;
  136. }
  137. common::DoubleTime::type
  138. lookahead(const common::DoubleTime::type & /* t */) const override
  139. {
  140. return _last_time + _sigma;
  141. }
  142. private:
  143. // parameters
  144. double _v_max;
  145. // state
  146. artis::common::DoubleTime::type _sigma;
  147. artis::common::DoubleTime::type _last_time;
  148. std::default_random_engine _generator;
  149. std::normal_distribution<double> _distribution;
  150. std::uniform_real_distribution<double> _v_max_distribution;
  151. std::uniform_int_distribution<int> _port_distribution;
  152. double _next_v_max;
  153. int _next_port;
  154. unsigned int _index;
  155. };
  156. class Counter
  157. : public artis::pdevs::Dynamics<artis::common::DoubleTime, Counter>
  158. {
  159. public:
  160. struct inputs
  161. {
  162. enum values
  163. {
  164. IN
  165. };
  166. };
  167. struct vars
  168. {
  169. enum values
  170. {
  171. COUNTER
  172. };
  173. };
  174. Counter(const std::string &name,
  175. const artis::pdevs::Context<artis::common::DoubleTime, Counter> &context)
  176. :
  177. artis::pdevs::Dynamics<artis::common::DoubleTime, Counter>(name,
  178. context)
  179. {
  180. input_port({inputs::IN, "in"});
  181. observable({vars::COUNTER, "counter"});
  182. }
  183. ~Counter() override = default;
  184. void dext(const artis::common::DoubleTime::type & /* t */,
  185. const artis::common::DoubleTime::type & /* e */,
  186. const artis::common::event::Bag<artis::common::DoubleTime> &bag) override
  187. {
  188. // std::cout << get_full_name() << " at " << t << " - dext" << std::endl;
  189. _counter += bag.size();
  190. }
  191. void start(const artis::common::DoubleTime::type & /* t */) override
  192. {
  193. // std::cout << get_full_name() << " at " << t << " - start" << std::endl;
  194. _counter = 0;
  195. }
  196. artis::common::DoubleTime::type
  197. ta(const artis::common::DoubleTime::type & /* t */) const override
  198. {
  199. return artis::common::DoubleTime::infinity;
  200. }
  201. artis::common::event::Value observe(const artis::common::DoubleTime::type & /* t */,
  202. unsigned int index) const override
  203. {
  204. if (index == vars::COUNTER) {
  205. return _counter;
  206. } else {
  207. return artis::common::event::Value();
  208. }
  209. }
  210. common::DoubleTime::type lookahead(const common::DoubleTime::type & /* t */) const override
  211. {
  212. return common::DoubleTime::infinity;
  213. }
  214. private:
  215. unsigned int _counter;
  216. };
  217. class Link :
  218. public artis::pdevs::Dynamics<common::DoubleTime, Link>
  219. {
  220. public :
  221. struct inputs
  222. {
  223. enum values
  224. {
  225. IN
  226. };
  227. };
  228. struct outputs
  229. {
  230. enum values
  231. {
  232. OUT
  233. };
  234. };
  235. Link(const std::string &name,
  236. const artis::pdevs::Context<common::DoubleTime, Link> &context)
  237. : artis::pdevs::Dynamics<common::DoubleTime, Link>(name, context)
  238. {
  239. input_port({inputs::IN, "in"});
  240. output_port({outputs::OUT, "out"});
  241. }
  242. ~ Link() override = default;
  243. void dint(const artis::common::DoubleTime::type &t) override
  244. {
  245. // delay();
  246. // std::cout << get_full_name() << " at " << t << " - dint" << std::endl;
  247. auto it = _vehicles.begin();
  248. while (it != _vehicles.end()) {
  249. if (it->next_time == t and it->state == State::STOP) {
  250. _vehicles.erase(it);
  251. it = _vehicles.begin();
  252. } else {
  253. ++it;
  254. }
  255. }
  256. for (auto &vehicle: _vehicles) {
  257. if (vehicle.next_time == t) {
  258. switch (vehicle.state) {
  259. case State::UP: {
  260. double duration = vehicle.v_max / vehicle.acceleration;
  261. double acceleration_distance = 0.5 * vehicle.acceleration * duration * duration;
  262. vehicle.state = State::MAX;
  263. vehicle.next_time = t + (_length - 2 * acceleration_distance) / vehicle.v_max;
  264. break;
  265. }
  266. case State::MAX: {
  267. vehicle.state = State::DOWN;
  268. vehicle.next_time = t + vehicle.v_max / vehicle.acceleration;
  269. break;
  270. }
  271. case State::DOWN: {
  272. vehicle.state = State::STOP;
  273. vehicle.next_time = t;
  274. break;
  275. }
  276. case State::STOP: {
  277. assert(false);
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. update_sigma(t);
  284. }
  285. void dext(const artis::common::DoubleTime::type &t,
  286. const artis::common::DoubleTime::type & /* e */,
  287. const artis::common::event::Bag<artis::common::DoubleTime> &bag) override
  288. {
  289. // std::cout << get_full_name() << " at " << t << " - dext" << std::endl;
  290. std::for_each(bag.begin(), bag.end(),
  291. [this, t](const common::event::ExternalEvent<common::DoubleTime> &event) {
  292. if (event.on_port(inputs::IN)) {
  293. Vehicle vehicle;
  294. event.data()(vehicle);
  295. vehicle.next_time = t + vehicle.v_max / vehicle.acceleration;
  296. vehicle.state = State::UP;
  297. _vehicles.push_back(vehicle);
  298. }
  299. });
  300. update_sigma(t);
  301. }
  302. artis::common::DoubleTime::type ta(const artis::common::DoubleTime::type & /* t */) const override
  303. { return _sigma; }
  304. artis::common::event::Bag<artis::common::DoubleTime>
  305. lambda(const artis::common::DoubleTime::type &t) const override
  306. {
  307. artis::common::event::Bag<artis::common::DoubleTime> bag;
  308. // std::cout << get_full_name() << " at " << t << " - lambda" << std::endl;
  309. for (auto vehicle: _vehicles) {
  310. if (vehicle.next_time == t and vehicle.state == State::STOP) {
  311. bag.push_back(
  312. artis::common::event::ExternalEvent<artis::common::DoubleTime>(
  313. outputs::OUT, vehicle));
  314. }
  315. }
  316. return bag;
  317. }
  318. void start(const artis::common::DoubleTime::type & /* t */) override
  319. {
  320. // std::cout << get_full_name() << " at " << t << " - start" << std::endl;
  321. _sigma = artis::common::DoubleTime::infinity;
  322. }
  323. common::DoubleTime::type lookahead(const common::DoubleTime::type &t) const override
  324. {
  325. double eot = artis::common::DoubleTime::infinity;
  326. for (auto vehicle: _vehicles) {
  327. double eot_i = artis::common::DoubleTime::infinity;
  328. if (vehicle.next_time == t and vehicle.state == State::STOP) {
  329. eot_i = t;
  330. } else if (vehicle.state == State::DOWN) {
  331. eot_i = vehicle.next_time;
  332. } else if (vehicle.state == State::MAX) {
  333. eot_i = vehicle.next_time + vehicle.v_max / vehicle.acceleration;
  334. } else if (vehicle.state == State::UP) {
  335. double duration = vehicle.v_max / vehicle.acceleration;
  336. double acceleration_distance = 0.5 * vehicle.acceleration * duration * duration;
  337. eot_i = vehicle.next_time
  338. + (_length - 2 * acceleration_distance) / vehicle.v_max
  339. + vehicle.v_max / vehicle.acceleration;
  340. }
  341. if (eot_i < eot) {
  342. eot = eot_i;
  343. }
  344. }
  345. return eot;
  346. }
  347. private:
  348. void update_sigma(const artis::common::DoubleTime::type &t)
  349. {
  350. if (_vehicles.empty()) {
  351. _sigma = artis::common::DoubleTime::infinity;
  352. } else {
  353. _sigma = std::min_element(_vehicles.begin(), _vehicles.end(),
  354. [](const Vehicle &e1, const Vehicle &e2) {
  355. return e1.next_time < e2.next_time;
  356. })->next_time - t;
  357. }
  358. }
  359. const double _length = 500;
  360. std::deque<Vehicle> _vehicles;
  361. artis::common::DoubleTime::type _sigma;
  362. };
  363. }
  364. }
  365. }
  366. } // namespace artis tests multithreading simple
  367. #endif