Quantifier.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. DECLARE_STATES(int,
  57. ((STATE, &Quantifier<Time>::_state), (ADAPTIVE_STATE, &Quantifier<Time>::_adaptive_state)));
  58. DECLARE_STATES(unsigned int,
  59. ((STEP_NUMBER, &Quantifier<Time>::_step_number)));
  60. DECLARE_STATES(double,
  61. ((OFFSET, &Quantifier<Time>::_offset), (UP_THRESHOLD, &Quantifier<Time>::_up_threshold), (DOWN_THRESHOLD, &Quantifier<
  62. 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. dint(t);
  81. dext(t, e, bag);
  82. }
  83. virtual void dint(const typename Time::type & /* t */) {
  84. switch (_state) {
  85. case INIT:break;
  86. case IDLE:break;
  87. case RESPONSE:_state = IDLE;
  88. break;
  89. }
  90. }
  91. virtual void dext(const typename Time::type &t, const typename Time::type &e,
  92. const common::Bag<Time> &bag) {
  93. bool reset = false;
  94. std::for_each(bag.begin(), bag.end(),
  95. [this, t, e, &reset](const common::ExternalEvent<Time> &event) {
  96. if (event.on_port(IN)) {
  97. IntegratorData data;
  98. double shifting_factor;
  99. double value;
  100. int cnt;
  101. event.data()(data);
  102. value = data.value;
  103. if (_state == INIT) {
  104. init_step_number_and_offset(value);
  105. update_thresholds();
  106. _state = RESPONSE;
  107. } else {
  108. cnt = 0;
  109. while (value >= _up_threshold or value <= _down_threshold) {
  110. cnt++;
  111. if (value >= _up_threshold) {
  112. _step_number++;
  113. } else {
  114. _step_number--;
  115. }
  116. switch (_adaptive_state) {
  117. case IMPOSSIBLE:update_thresholds();
  118. break;
  119. case POSSIBLE:
  120. if (value >= _up_threshold) {
  121. store_change(_step_size, t);
  122. } else {
  123. store_change(-_step_size, t);
  124. }
  125. shifting_factor = shift_quanta();
  126. assert(shifting_factor >= 0
  127. and shifting_factor <= 1);
  128. if (shifting_factor != 0 and shifting_factor != 1) {
  129. if (value >= _up_threshold) {
  130. update_thresholds(shifting_factor,
  131. DIRECTION_DOWN);
  132. } else {
  133. update_thresholds(shifting_factor,
  134. DIRECTION_UP);
  135. }
  136. _adaptive_state = DONE;
  137. } else {
  138. update_thresholds();
  139. }
  140. break;
  141. case DONE:init_step_number_and_offset(value);
  142. _adaptive_state = POSSIBLE;
  143. update_thresholds();
  144. break;
  145. }
  146. }
  147. }
  148. } else if (event.on_port(RESET)) {
  149. _offset = 0;
  150. reset = true;
  151. _archive.clear();
  152. }
  153. });
  154. if (reset) {
  155. _state = INIT;
  156. } else {
  157. _state = RESPONSE;
  158. }
  159. }
  160. virtual void start(const typename Time::type & /* time */) {
  161. _offset = 0;
  162. _state = INIT;
  163. }
  164. virtual typename Time::type ta(const typename Time::type & /* time */) {
  165. switch (_state) {
  166. case INIT:
  167. case IDLE:return Time::infinity;
  168. case RESPONSE:return 0.0;
  169. }
  170. return Time::infinity;
  171. }
  172. virtual common::Bag<Time> lambda(const typename Time::type & /* time */) const {
  173. common::Bag<Time> msgs;
  174. const QuantifierData data = {_up_threshold, _down_threshold};
  175. msgs.push_back(common::ExternalEvent<Time>(OUT, data));
  176. return msgs;
  177. }
  178. virtual common::Value observe(const typename Time::type & /* t */,
  179. unsigned int index) const {
  180. switch (index) {
  181. case UP:return (double) _up_threshold;
  182. case DOWN:return (double) _down_threshold;
  183. case VALUE:return (double) (_up_threshold - _down_threshold);
  184. default:return common::Value();
  185. }
  186. }
  187. private:
  188. typedef enum {
  189. DIRECTION_UP, DIRECTION_DOWN
  190. } Direction;
  191. void init_step_number_and_offset(double value) {
  192. _step_number = static_cast<long int>(std::floor(value / _step_size));
  193. if (_zero_init_offset) {
  194. _offset = 0;
  195. } else {
  196. _offset = value - static_cast<double>(_step_number) * _step_size;
  197. }
  198. }
  199. bool monotonous(unsigned int range) {
  200. if ((range + 1) > _archive.size()) {
  201. return false;
  202. }
  203. for (size_t i = 0; i < range; i++) {
  204. if (_archive[i].value * _archive[i + 1].value < 0) {
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. bool oscillating(unsigned int range) {
  211. if ((range + 1) > _archive.size()) {
  212. return false;
  213. }
  214. for (size_t i = _archive.size() - range; i < _archive.size() - 1; i++) {
  215. if (_archive[i].value * _archive[i + 1].value > 0) {
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. double shift_quanta() {
  222. double factor = 0;
  223. if (oscillating(_past_length - 1) and
  224. _archive.back().date - _archive.front().date != 0) {
  225. double acc;
  226. double local_estim;
  227. int cnt;
  228. acc = 0;
  229. cnt = 0;
  230. for (size_t i = 0; i < _archive.size() - 2; ++i) {
  231. if (0 != (_archive[i + 2].date - _archive[i].date)) {
  232. if ((_archive.back().value * _archive[i + 1].value) > 0) {
  233. local_estim =
  234. 1 - (_archive[i + 1].date - _archive[i].date) /
  235. (_archive[i + 2].date - _archive[i].date);
  236. } else {
  237. local_estim = (_archive[i + 1].date - _archive[i].date) /
  238. (_archive[i + 2].date - _archive[i].date);
  239. }
  240. acc += local_estim;
  241. cnt++;
  242. }
  243. }
  244. acc = acc / cnt;
  245. factor = acc;
  246. _archive.resize(0);
  247. }
  248. return factor;
  249. }
  250. void store_change(double val, const typename Time::type &time) {
  251. record_t record;
  252. record.date = time;
  253. record.value = val;
  254. _archive.push_back(record);
  255. while (_archive.size() > _past_length) {
  256. _archive.pop_front();
  257. }
  258. }
  259. void update_thresholds() {
  260. auto step_number = static_cast<double>(_step_number);
  261. _up_threshold = _offset + _step_size * (step_number + 1);
  262. _down_threshold = _offset + _step_size * (step_number - 1);
  263. }
  264. void update_thresholds(double factor) {
  265. auto step_number = static_cast<double>(_step_number);
  266. _up_threshold = _offset + _step_size * (step_number + (1 - factor));
  267. _down_threshold = _offset + _step_size * (step_number - (1 - factor));
  268. }
  269. void update_thresholds(double factor, Direction d) {
  270. auto step_number = static_cast<double>(_step_number);
  271. if (d == DIRECTION_UP) {
  272. _up_threshold = _offset + _step_size * (step_number + (1 - factor));
  273. _down_threshold = _offset + _step_size * (step_number - 1);
  274. } else {
  275. _up_threshold = _offset + _step_size * (step_number + 1);
  276. _down_threshold = _offset + _step_size * (step_number - (1 - factor));
  277. }
  278. }
  279. typedef enum vars {
  280. UP, DOWN, VALUE
  281. } Observable;
  282. typedef enum {
  283. INIT, IDLE, RESPONSE
  284. } State;
  285. typedef enum {
  286. IMPOSSIBLE, POSSIBLE, DONE
  287. } AdaptiveState;
  288. struct record_t {
  289. double value;
  290. typename Time::type date;
  291. };
  292. // parameters
  293. bool _adaptive;
  294. bool _zero_init_offset;
  295. unsigned int _past_length;
  296. double _step_size;
  297. // state
  298. int _state;
  299. int _adaptive_state;
  300. unsigned int _step_number; // long int
  301. double _offset;
  302. double _up_threshold;
  303. double _down_threshold;
  304. std::deque<record_t> _archive;
  305. };
  306. }
  307. }
  308. #endif