Quantifier.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * @file kernel/qss/Quantifier.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 QSS_QUANTIFIER
  26. #define QSS_QUANTIFIER
  27. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  28. #include <artis-star/kernel/qss/Data.hpp>
  29. #include <cmath>
  30. namespace artis::qss {
  31. struct QuantifierParameters {
  32. bool allow_offsets;
  33. bool zero_init_offset;
  34. double quantum;
  35. unsigned int archive_length;
  36. };
  37. template<typename Time>
  38. class Quantifier
  39. : public artis::pdevs::Dynamics<Time, Quantifier<Time>, QuantifierParameters> {
  40. public:
  41. struct input {
  42. enum values {
  43. IN, RESET
  44. };
  45. };
  46. struct output {
  47. enum values {
  48. OUT
  49. };
  50. };
  51. Quantifier(const std::string &name,
  52. const artis::pdevs::Context<Time, Quantifier<Time>, QuantifierParameters> &context)
  53. :
  54. artis::pdevs::Dynamics<Time, Quantifier<Time>, QuantifierParameters>(name, context) {
  55. DECLARE_STATES(int,
  56. ((state::PHASE, &Quantifier<Time>::_phase),
  57. (state::ADAPTIVE_STATE, &Quantifier<Time>::_adaptive_state)));
  58. DECLARE_STATES(unsigned int,
  59. ((state::STEP_NUMBER, &Quantifier<Time>::_step_number)));
  60. DECLARE_STATES(double,
  61. ((state::OFFSET, &Quantifier<Time>::_offset),
  62. (state::UP_THRESHOLD, &Quantifier<Time>::_up_threshold),
  63. (state::DOWN_THRESHOLD, &Quantifier<Time>::_down_threshold)));
  64. this->input_ports({{input::IN, "in"},
  65. {input::RESET, "reset"}});
  66. this->output_port({output::OUT, "out"});
  67. this->observables({{var::UP, "up"},
  68. {var::DOWN, "down"},
  69. {var::VALUE, "value"}});
  70. _adaptive = context.parameters().allow_offsets;
  71. _adaptive_state = _adaptive ? adaptive_state::POSSIBLE : adaptive_state::IMPOSSIBLE;
  72. _zero_init_offset = context.parameters().zero_init_offset;
  73. _step_size = context.parameters().quantum;
  74. assert(_step_size > 0);
  75. _past_length = context.parameters().archive_length;
  76. assert(_past_length > 2);
  77. }
  78. virtual ~Quantifier() {}
  79. virtual void dconf(const typename Time::type &t, const typename Time::type &e,
  80. const common::event::Bag<Time> &bag) {
  81. dint(t);
  82. dext(t, e, bag);
  83. }
  84. virtual void dint(const typename Time::type & /* t */) {
  85. switch (_phase) {
  86. case phase::INIT:
  87. break;
  88. case phase::IDLE:
  89. break;
  90. case phase::RESPONSE:
  91. _phase = phase::IDLE;
  92. break;
  93. }
  94. }
  95. virtual void dext(const typename Time::type &t, const typename Time::type &e,
  96. const common::event::Bag<Time> &bag) {
  97. bool reset = false;
  98. std::for_each(bag.begin(), bag.end(),
  99. [this, t, e, &reset](const common::event::ExternalEvent<Time> &event) {
  100. if (event.on_port(input::IN)) {
  101. IntegratorData data;
  102. double shifting_factor;
  103. double value;
  104. int cnt;
  105. event.data()(data);
  106. value = data.value;
  107. if (_phase == phase::INIT) {
  108. init_step_number_and_offset(value);
  109. update_thresholds();
  110. _phase = phase::RESPONSE;
  111. } else {
  112. cnt = 0;
  113. while (value >= _up_threshold or value <= _down_threshold) {
  114. cnt++;
  115. if (value >= _up_threshold) {
  116. _step_number++;
  117. } else {
  118. _step_number--;
  119. }
  120. switch (_adaptive_state) {
  121. case adaptive_state::IMPOSSIBLE:
  122. update_thresholds();
  123. break;
  124. case adaptive_state::POSSIBLE:
  125. if (value >= _up_threshold) {
  126. store_change(_step_size, t);
  127. } else {
  128. store_change(-_step_size, t);
  129. }
  130. shifting_factor = shift_quanta();
  131. assert(shifting_factor >= 0
  132. and shifting_factor <= 1);
  133. if (shifting_factor != 0 and shifting_factor != 1) {
  134. if (value >= _up_threshold) {
  135. update_thresholds(shifting_factor,
  136. direction::DIRECTION_DOWN);
  137. } else {
  138. update_thresholds(shifting_factor,
  139. direction::DIRECTION_UP);
  140. }
  141. _adaptive_state = adaptive_state::DONE;
  142. } else {
  143. update_thresholds();
  144. }
  145. break;
  146. case adaptive_state::DONE:
  147. init_step_number_and_offset(value);
  148. _adaptive_state = adaptive_state::POSSIBLE;
  149. update_thresholds();
  150. break;
  151. }
  152. }
  153. }
  154. } else if (event.on_port(input::RESET)) {
  155. _offset = 0;
  156. reset = true;
  157. _archive.clear();
  158. }
  159. });
  160. if (reset) {
  161. _phase = phase::INIT;
  162. } else {
  163. _phase = phase::RESPONSE;
  164. }
  165. }
  166. virtual void start(const typename Time::type & /* time */) {
  167. _offset = 0;
  168. _phase = phase::INIT;
  169. }
  170. virtual typename Time::type ta(const typename Time::type & /* time */) {
  171. switch (_phase) {
  172. case phase::INIT:
  173. case phase::IDLE:
  174. return Time::infinity;
  175. case phase::RESPONSE:
  176. return 0.0;
  177. }
  178. return Time::infinity;
  179. }
  180. virtual common::event::Bag<Time> lambda(const typename Time::type & /* time */) const {
  181. common::event::Bag<Time> msgs;
  182. const QuantifierData data = {_up_threshold, _down_threshold};
  183. msgs.push_back(common::event::ExternalEvent<Time>(output::OUT, data));
  184. return msgs;
  185. }
  186. virtual common::event::Value observe(const typename Time::type & /* t */,
  187. unsigned int index) const {
  188. switch (index) {
  189. case var::UP:
  190. return (double) _up_threshold;
  191. case var::DOWN:
  192. return (double) _down_threshold;
  193. case var::VALUE:
  194. return (double) (_up_threshold - _down_threshold);
  195. default:
  196. return common::event::Value();
  197. }
  198. }
  199. private:
  200. void init_step_number_and_offset(double value) {
  201. _step_number = static_cast<long int>(std::floor(value / _step_size));
  202. if (_zero_init_offset) {
  203. _offset = 0;
  204. } else {
  205. _offset = value - static_cast<double>(_step_number) * _step_size;
  206. }
  207. }
  208. bool monotonous(unsigned int range) {
  209. if ((range + 1) > _archive.size()) {
  210. return false;
  211. }
  212. for (size_t i = 0; i < range; i++) {
  213. if (_archive[i].value * _archive[i + 1].value < 0) {
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. bool oscillating(unsigned int range) {
  220. if ((range + 1) > _archive.size()) {
  221. return false;
  222. }
  223. for (size_t i = _archive.size() - range; i < _archive.size() - 1; i++) {
  224. if (_archive[i].value * _archive[i + 1].value > 0) {
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. double shift_quanta() {
  231. double factor = 0;
  232. if (oscillating(_past_length - 1) and
  233. _archive.back().date - _archive.front().date != 0) {
  234. double acc;
  235. double local_estim;
  236. int cnt;
  237. acc = 0;
  238. cnt = 0;
  239. for (size_t i = 0; i < _archive.size() - 2; ++i) {
  240. if (0 != (_archive[i + 2].date - _archive[i].date)) {
  241. if ((_archive.back().value * _archive[i + 1].value) > 0) {
  242. local_estim =
  243. 1 - (_archive[i + 1].date - _archive[i].date) /
  244. (_archive[i + 2].date - _archive[i].date);
  245. } else {
  246. local_estim = (_archive[i + 1].date - _archive[i].date) /
  247. (_archive[i + 2].date - _archive[i].date);
  248. }
  249. acc += local_estim;
  250. cnt++;
  251. }
  252. }
  253. acc = acc / cnt;
  254. factor = acc;
  255. _archive.resize(0);
  256. }
  257. return factor;
  258. }
  259. void store_change(double val, const typename Time::type &time) {
  260. record_t record;
  261. record.date = time;
  262. record.value = val;
  263. _archive.push_back(record);
  264. while (_archive.size() > _past_length) {
  265. _archive.pop_front();
  266. }
  267. }
  268. void update_thresholds() {
  269. auto step_number = static_cast<double>(_step_number);
  270. _up_threshold = _offset + _step_size * (step_number + 1);
  271. _down_threshold = _offset + _step_size * (step_number - 1);
  272. }
  273. void update_thresholds(double factor) {
  274. auto step_number = static_cast<double>(_step_number);
  275. _up_threshold = _offset + _step_size * (step_number + (1 - factor));
  276. _down_threshold = _offset + _step_size * (step_number - (1 - factor));
  277. }
  278. struct direction {
  279. enum values {
  280. DIRECTION_UP, DIRECTION_DOWN
  281. };
  282. };
  283. void update_thresholds(double factor, const typename direction::values &d) {
  284. auto step_number = static_cast<double>(_step_number);
  285. if (d == direction::DIRECTION_UP) {
  286. _up_threshold = _offset + _step_size * (step_number + (1 - factor));
  287. _down_threshold = _offset + _step_size * (step_number - 1);
  288. } else {
  289. _up_threshold = _offset + _step_size * (step_number + 1);
  290. _down_threshold = _offset + _step_size * (step_number - (1 - factor));
  291. }
  292. }
  293. struct state {
  294. enum values {
  295. PHASE, ADAPTIVE_STATE, STEP_NUMBER, OFFSET, UP_THRESHOLD, DOWN_THRESHOLD
  296. };
  297. };
  298. struct var {
  299. enum values {
  300. UP, DOWN, VALUE
  301. };
  302. };
  303. struct phase {
  304. enum values {
  305. INIT, IDLE, RESPONSE
  306. };
  307. };
  308. struct adaptive_state {
  309. enum values {
  310. IMPOSSIBLE, POSSIBLE, DONE
  311. };
  312. };
  313. struct record_t {
  314. double value;
  315. typename Time::type date;
  316. };
  317. // parameters
  318. bool _adaptive;
  319. bool _zero_init_offset;
  320. unsigned int _past_length;
  321. double _step_size;
  322. // state
  323. int _phase;
  324. int _adaptive_state;
  325. unsigned int _step_number; // long int
  326. double _offset;
  327. double _up_threshold;
  328. double _down_threshold;
  329. std::deque<record_t> _archive;
  330. };
  331. }
  332. #endif