main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @file pdevs/tests.cpp
  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. #include <tests/qss/graph_manager.hpp>
  26. #include <artis-star/common/RootCoordinator.hpp>
  27. #include <artis-star/common/observer/Iterator.hpp>
  28. #include <artis-star/common/observer/Output.hpp>
  29. #include <fstream>
  30. #include <iostream>
  31. #include <boost/archive/binary_oarchive.hpp>
  32. using namespace artis::tests::qss;
  33. void test_parabola()
  34. {
  35. artis::qss::QSSParameters<ParabolaParameters> parameters = {{0.5},
  36. {true, true, 0.001, 3},
  37. {0.2}};
  38. class View : public artis::observer::View<artis::common::DoubleTime>
  39. {
  40. public:
  41. View()
  42. {
  43. selector("Value",
  44. {OnlyOneParabolaGraphManager::A, ParabolaGraphManager::submodel::S_Integrator,
  45. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  46. }
  47. };
  48. artis::common::context::Context<artis::common::DoubleTime> context(0, 5);
  49. artis::common::RootCoordinator<
  50. artis::common::DoubleTime, artis::pdevs::Coordinator<
  51. artis::common::DoubleTime,
  52. OnlyOneParabolaGraphManager,
  53. artis::qss::QSSParameters<ParabolaParameters>>
  54. > rc(context, "root", parameters, artis::common::NoParameters());
  55. rc.attachView("Value", new View());
  56. rc.run(context);
  57. const View::Values &values = rc.observer().view("Value").get("Value");
  58. for (const auto &value: values) {
  59. double v;
  60. value.second(v);
  61. std::cout << value.first << ": " << v << std::endl;
  62. }
  63. }
  64. class PredatorPreyView : public artis::observer::View<artis::common::DoubleTime>
  65. {
  66. public:
  67. PredatorPreyView()
  68. {
  69. selector("PredatorView",
  70. {PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::submodel::S_Integrator,
  71. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  72. selector("PreyView",
  73. {PreyPredatorGraphManager::PREY, PreyGraphManager::submodel::S_Integrator,
  74. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  75. }
  76. };
  77. void run_predator_prey(artis::common::context::Context<artis::common::DoubleTime> &context,
  78. const PreyPredatorGraphManagerParameters &parameters)
  79. {
  80. artis::common::RootCoordinator<
  81. artis::common::DoubleTime, artis::pdevs::Coordinator<
  82. artis::common::DoubleTime,
  83. PreyPredatorGraphManager,
  84. PreyPredatorGraphManagerParameters>
  85. > rc(context, "root", parameters, artis::common::NoParameters());
  86. rc.attachView("Value1", new PredatorPreyView());
  87. rc.run(context);
  88. artis::observer::Output<artis::common::DoubleTime> output(rc.observer());
  89. output(0, 100, 0.1);
  90. // rc.save(context);
  91. //
  92. // std::ofstream os("state");
  93. // boost::archive::binary_oarchive oa(os);
  94. //
  95. // oa << context;
  96. //
  97. // {
  98. // artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
  99. // rc.observer().view("Value").get("PredatorView"), context.begin(), 0.1);
  100. //
  101. // while (it.has_next()) {
  102. // double v;
  103. //
  104. // (*it).second(v);
  105. // std::cout << (*it).first << ";" << v << std::endl;
  106. // ++it;
  107. // }
  108. // }
  109. // {
  110. // artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
  111. // rc.observer().view("Value").get("PreyView"), context.begin(), 0.1);
  112. //
  113. // while (it.has_next()) {
  114. // double v;
  115. //
  116. // (*it).second(v);
  117. // std::cout << (*it).first << ";" << v << std::endl;
  118. // ++it;
  119. // }
  120. // }
  121. }
  122. void test_predator_prey()
  123. {
  124. PreyPredatorGraphManagerParameters parameters = {{{45.},
  125. {true, true, 0.1, 3},
  126. {0.5, 0.01, 0.01, 0.2}},
  127. {{5000.},
  128. {true, true, 1, 3},
  129. {0.5, 0.01, 0.01, 0.2}}};
  130. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  131. run_predator_prey(context, parameters);
  132. // artis::common::context::Context<artis::common::DoubleTime> new_context(context);
  133. //
  134. // new_context.end(200);
  135. // run_predator_prey(new_context, parameters);
  136. }
  137. class PredatorPreySmartGardenerView : public artis::observer::View<artis::common::DoubleTime>
  138. {
  139. public:
  140. PredatorPreySmartGardenerView()
  141. {
  142. selector("PredatorView",
  143. {PreyPredatorSmartGardenerGraphManager::PREY_PREDATOR,
  144. PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::submodel::S_Integrator,
  145. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  146. selector("PreyView",
  147. {PreyPredatorSmartGardenerGraphManager::PREY_PREDATOR,
  148. PreyPredatorGraphManager::PREY, PreyGraphManager::submodel::S_Integrator,
  149. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  150. }
  151. };
  152. void test_predator_prey_smart_gardener()
  153. {
  154. PreyPredatorSmartGardenerGraphManagerParameters parameters = {{{{45.},
  155. {true, true, 0.1, 3},
  156. {0.5, 0.01, 0.01, 0.2}},
  157. {{5000.},
  158. {true, true, 1, 3},
  159. {0.5, 0.01, 0.01, 0.2}}},
  160. {2000, 0.25, 0.75, 5}};
  161. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  162. artis::common::RootCoordinator<
  163. artis::common::DoubleTime, artis::pdevs::Coordinator<
  164. artis::common::DoubleTime,
  165. PreyPredatorSmartGardenerGraphManager,
  166. PreyPredatorSmartGardenerGraphManagerParameters>
  167. > rc(context, "root", parameters, artis::common::NoParameters());
  168. rc.attachView("Value2", new PredatorPreySmartGardenerView());
  169. rc.run(context);
  170. artis::observer::Output<artis::common::DoubleTime> output(rc.observer());
  171. output(0, 100, 0.1);
  172. }
  173. class MixedPredatorPreyView : public artis::observer::View<artis::common::DoubleTime>
  174. {
  175. public:
  176. MixedPredatorPreyView()
  177. {
  178. selector("PredatorView",
  179. {MixedPreyPredatorGraphManager::PREDATOR, DiscretePredatorGraphManager::PREDATOR,
  180. DiscretePredator::VALUE});
  181. selector("PreyView",
  182. {MixedPreyPredatorGraphManager::PREY, PreyGraphManager::submodel::S_Integrator,
  183. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  184. }
  185. };
  186. void test_mixed_predator_prey()
  187. {
  188. MixedPreyPredatorGraphManagerParameters parameters = {{0.0001, 45., 0.5, 0.01, 0.01, 0.2},
  189. {{5000.},
  190. {true, true, 1, 3},
  191. {0.5, 0.01, 0.01, 0.2}}};
  192. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  193. artis::common::RootCoordinator<
  194. artis::common::DoubleTime, artis::pdevs::Coordinator<
  195. artis::common::DoubleTime,
  196. MixedPreyPredatorGraphManager,
  197. MixedPreyPredatorGraphManagerParameters>
  198. > rc(context, "root", parameters, artis::common::NoParameters());
  199. rc.attachView("Value3", new MixedPredatorPreyView());
  200. rc.run(context);
  201. artis::observer::Output<artis::common::DoubleTime> output(rc.observer());
  202. output(0, 100, 0.1);
  203. }
  204. class MultiPredatorPreyView : public artis::observer::View<artis::common::DoubleTime>
  205. {
  206. public:
  207. MultiPredatorPreyView()
  208. {
  209. selector("PredatorView",
  210. {MultiPreyPredatorGraphManager::submodel::S_Integrator,
  211. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  212. selector("PreyView",
  213. {MultiPreyPredatorGraphManager::submodel::S_Integrator + 1,
  214. artis::qss::Integrator<artis::common::DoubleTime>::var::VALUE});
  215. selector("PredatorDerivativeView",
  216. {MultiPreyPredatorGraphManager::submodel::S_Derivative,
  217. PreyPredator::var::VALUE});
  218. selector("PreyDerivativeView",
  219. {MultiPreyPredatorGraphManager::submodel::S_Derivative,
  220. PreyPredator::var::VALUE + 1});
  221. }
  222. };
  223. void test_multi_predator_prey()
  224. {
  225. artis::qss::MultiQSSParameters<PreyPredatorParameters> parameters = {
  226. {{5000.}, {45.}},
  227. {{true, true, 1, 3}, {true, true, 0.1, 3}},
  228. {0.5, 0.01, 0.01, 0.2}
  229. };
  230. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  231. artis::common::RootCoordinator<
  232. artis::common::DoubleTime, artis::pdevs::Coordinator<
  233. artis::common::DoubleTime,
  234. MultiPreyPredatorGraphManager,
  235. artis::qss::MultiQSSParameters<PreyPredatorParameters>>
  236. > rc(context, "root", parameters, artis::common::NoParameters());
  237. rc.attachView("Value4", new MultiPredatorPreyView());
  238. rc.run(context);
  239. artis::observer::Output<artis::common::DoubleTime> output(rc.observer());
  240. output(0, 100, 0.1);
  241. }
  242. int main()
  243. {
  244. test_parabola();
  245. test_predator_prey();
  246. test_predator_prey_smart_gardener();
  247. test_mixed_predator_prey();
  248. test_multi_predator_prey();
  249. }