Quantifier.hpp 15 KB

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