models.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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-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_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. _last_time = t;
  104. _sigma = _distribution(_generator);
  105. _sigma = _sigma <= 0 ? 0.1 : _sigma;
  106. _next_v_max = _v_max * _v_max_distribution(_generator);
  107. _next_port = _port_distribution(_generator);
  108. ++_index;
  109. }
  110. void start(const artis::common::DoubleTime::type &t) override
  111. {
  112. _last_time = t;
  113. _sigma = _distribution(_generator);
  114. _sigma = _sigma <= 0 ? 0.1 : _sigma;
  115. _next_v_max = _v_max * _v_max_distribution(_generator);
  116. _next_port = _port_distribution(_generator);
  117. _index = 1;
  118. }
  119. artis::common::DoubleTime::type
  120. ta(const artis::common::DoubleTime::type & /* t */) const override
  121. { return _sigma; }
  122. artis::common::Bag<artis::common::DoubleTime>
  123. lambda(const artis::common::DoubleTime::type &t) const override
  124. {
  125. artis::common::Bag<artis::common::DoubleTime> bag;
  126. if (t > 0) {
  127. Vehicle vehicle = {_index, _next_v_max, 0.5, State::STOP, t};
  128. bag.push_back(
  129. artis::common::ExternalEvent<artis::common::DoubleTime>(
  130. outputs::OUT + _next_port, vehicle));
  131. }
  132. return bag;
  133. }
  134. common::DoubleTime::type
  135. lookahead(const common::DoubleTime::type & /* t */) const override
  136. {
  137. return _last_time + _sigma;
  138. }
  139. private:
  140. // parameters
  141. double _v_max;
  142. // state
  143. artis::common::DoubleTime::type _sigma;
  144. artis::common::DoubleTime::type _last_time;
  145. std::default_random_engine _generator;
  146. std::normal_distribution<double> _distribution;
  147. std::uniform_real_distribution<double> _v_max_distribution;
  148. std::uniform_int_distribution<int> _port_distribution;
  149. double _next_v_max;
  150. int _next_port;
  151. unsigned int _index;
  152. };
  153. class Counter
  154. : public artis::pdevs::Dynamics<artis::common::DoubleTime, Counter>
  155. {
  156. public:
  157. struct inputs
  158. {
  159. enum values
  160. {
  161. IN
  162. };
  163. };
  164. struct vars
  165. {
  166. enum values
  167. {
  168. COUNTER
  169. };
  170. };
  171. Counter(const std::string &name,
  172. const artis::pdevs::Context<artis::common::DoubleTime, Counter> &context)
  173. :
  174. artis::pdevs::Dynamics<artis::common::DoubleTime, Counter>(name,
  175. context)
  176. {
  177. input_port({inputs::IN, "in"});
  178. observable({vars::COUNTER, "counter"});
  179. }
  180. ~Counter() override = default;
  181. void
  182. dext(const artis::common::DoubleTime::type & /* t */,
  183. const artis::common::DoubleTime::type & /* e */,
  184. const artis::common::Bag<artis::common::DoubleTime> &bag) override
  185. {
  186. // std::cout << t << ": " << _counter << std::endl;
  187. _counter += bag.size();
  188. }
  189. void start(const artis::common::DoubleTime::type & /* t */) override
  190. {
  191. _counter = 0;
  192. }
  193. artis::common::DoubleTime::type
  194. ta(const artis::common::DoubleTime::type & /* t */) const override
  195. {
  196. return artis::common::DoubleTime::infinity;
  197. }
  198. artis::common::Value
  199. observe(const artis::common::DoubleTime::type & /* t */,
  200. unsigned int index) const override
  201. {
  202. if (index == vars::COUNTER) {
  203. return _counter;
  204. } else {
  205. return artis::common::Value();
  206. }
  207. }
  208. common::DoubleTime::type
  209. lookahead(const common::DoubleTime::type & /* t */) const override
  210. {
  211. return common::DoubleTime::infinity;
  212. }
  213. private:
  214. unsigned int _counter;
  215. };
  216. class Link :
  217. public artis::pdevs::Dynamics<common::DoubleTime, Link>
  218. {
  219. public :
  220. struct inputs
  221. {
  222. enum values
  223. {
  224. IN
  225. };
  226. };
  227. struct outputs
  228. {
  229. enum values
  230. {
  231. OUT
  232. };
  233. };
  234. Link(const std::string &name,
  235. const artis::pdevs::Context<common::DoubleTime, Link> &context)
  236. : artis::pdevs::Dynamics<common::DoubleTime, Link>(name, context)
  237. {
  238. input_port({inputs::IN, "in"});
  239. output_port({outputs::OUT, "out"});
  240. }
  241. ~ Link() override = default;
  242. void dint(const artis::common::DoubleTime::type &t) override
  243. {
  244. delay();
  245. auto it = _vehicles.begin();
  246. while (it != _vehicles.end()) {
  247. if (it->next_time == t and it->state == State::STOP) {
  248. _vehicles.erase(it);
  249. it = _vehicles.begin();
  250. } else {
  251. ++it;
  252. }
  253. }
  254. for (auto &vehicle: _vehicles) {
  255. if (vehicle.next_time == t) {
  256. switch (vehicle.state) {
  257. case State::UP: {
  258. double duration = vehicle.v_max / vehicle.acceleration;
  259. double acceleration_distance =
  260. 0.5 * vehicle.acceleration * duration * duration;
  261. vehicle.state = State::MAX;
  262. vehicle.next_time = t +
  263. (_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::Bag<artis::common::DoubleTime> &bag) override
  288. {
  289. std::for_each(bag.begin(), bag.end(),
  290. [this, t](const common::ExternalEvent<common::DoubleTime> &event) {
  291. if (event.on_port(inputs::IN)) {
  292. Vehicle vehicle;
  293. event.data()(vehicle);
  294. vehicle.next_time =
  295. 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
  303. ta(const artis::common::DoubleTime::type & /* t */) const override
  304. { return _sigma; }
  305. artis::common::Bag<artis::common::DoubleTime>
  306. lambda(const artis::common::DoubleTime::type &t) const override
  307. {
  308. artis::common::Bag<artis::common::DoubleTime> bag;
  309. for (auto vehicle: _vehicles) {
  310. if (vehicle.next_time == t and vehicle.state == State::STOP) {
  311. bag.push_back(
  312. artis::common::ExternalEvent<artis::common::DoubleTime>(
  313. outputs::OUT, vehicle));
  314. }
  315. }
  316. return bag;
  317. }
  318. void
  319. start(const artis::common::DoubleTime::type & /* t */) override
  320. {}
  321. common::DoubleTime::type
  322. lookahead(const common::DoubleTime::type &t) const override
  323. {
  324. double eot = artis::common::DoubleTime::infinity;
  325. for (auto vehicle: _vehicles) {
  326. double eot_i = artis::common::DoubleTime::infinity;
  327. if (vehicle.next_time == t and vehicle.state == State::STOP) {
  328. eot_i = t;
  329. } else if (vehicle.state == State::DOWN) {
  330. eot_i = vehicle.next_time;
  331. } else if (vehicle.state == State::MAX) {
  332. eot_i = vehicle.next_time + vehicle.v_max / vehicle.acceleration;
  333. } else if (vehicle.state == State::UP) {
  334. double duration = vehicle.v_max / vehicle.acceleration;
  335. double acceleration_distance =
  336. 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