Quantifier.hpp 14 KB

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