Quantifier.hpp 14 KB

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