main.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. public:
  40. View()
  41. {
  42. selector("Value", {OnlyOneParabolaGraphManager::A, ParabolaGraphManager::S_Integrator,
  43. artis::qss::Integrator<artis::common::DoubleTime>::VALUE});
  44. }
  45. };
  46. artis::common::context::Context<artis::common::DoubleTime> context(0, 5);
  47. artis::common::RootCoordinator<
  48. artis::common::DoubleTime, artis::pdevs::Coordinator<
  49. artis::common::DoubleTime,
  50. OnlyOneParabolaGraphManager,
  51. artis::qss::QSSParameters<ParabolaParameters>>
  52. > rc(context, "root", parameters, artis::common::NoParameters());
  53. rc.attachView("Value", new View());
  54. rc.run(context);
  55. const View::Values& values = rc.observer().view("Value").get("Value");
  56. for (const auto& value: values) {
  57. double v;
  58. value.second(v);
  59. std::cout << value.first << ": " << v << std::endl;
  60. }
  61. }
  62. class PredatorPreyView : public artis::observer::View<artis::common::DoubleTime> {
  63. public:
  64. PredatorPreyView()
  65. {
  66. selector("PredatorView",
  67. {PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::S_Integrator,
  68. artis::qss::Integrator<artis::common::DoubleTime>::VALUE});
  69. selector("PreyView",
  70. {PreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
  71. artis::qss::Integrator<artis::common::DoubleTime>::VALUE});
  72. }
  73. };
  74. void run_predator_prey(artis::common::context::Context<artis::common::DoubleTime>& context,
  75. const PreyPredatorGraphManagerParameters& parameters)
  76. {
  77. artis::common::RootCoordinator<
  78. artis::common::DoubleTime, artis::pdevs::Coordinator<
  79. artis::common::DoubleTime,
  80. PreyPredatorGraphManager,
  81. PreyPredatorGraphManagerParameters>
  82. > rc(context, "root", parameters, artis::common::NoParameters());
  83. rc.attachView("Value1", new PredatorPreyView());
  84. rc.run(context);
  85. rc.save(context);
  86. std::ofstream os("state");
  87. boost::archive::binary_oarchive oa(os);
  88. oa << context;
  89. {
  90. artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
  91. rc.observer().view("Value").get("PredatorView"), context.begin(), 0.1);
  92. while (it.has_next()) {
  93. double v;
  94. (*it).second(v);
  95. std::cout << (*it).first << ";" << v << std::endl;
  96. ++it;
  97. }
  98. }
  99. {
  100. artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
  101. rc.observer().view("Value").get("PreyView"), context.begin(), 0.1);
  102. while (it.has_next()) {
  103. double v;
  104. (*it).second(v);
  105. std::cout << (*it).first << ";" << v << std::endl;
  106. ++it;
  107. }
  108. }
  109. }
  110. void test_predator_prey()
  111. {
  112. PreyPredatorGraphManagerParameters parameters = {{{45.},
  113. {true, true, 0.1, 3},
  114. {0.5, 0.01, 0.01, 0.2}},
  115. {{5000.},
  116. {true, true, 1, 3},
  117. {0.5, 0.01, 0.01, 0.2}}};
  118. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  119. run_predator_prey(context, parameters);
  120. artis::common::context::Context<artis::common::DoubleTime> new_context(context);
  121. new_context.end(200);
  122. run_predator_prey(new_context, parameters);
  123. }
  124. class PredatorPreySmartGardenerView : public artis::observer::View<artis::common::DoubleTime> {
  125. public:
  126. PredatorPreySmartGardenerView()
  127. {
  128. selector("PredatorView",
  129. {PreyPredatorSmartGardenerGraphManager::PREY_PREDATOR,
  130. PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::S_Integrator,
  131. artis::qss::Integrator<artis::common::DoubleTime>::VALUE});
  132. selector("PreyView",
  133. {PreyPredatorSmartGardenerGraphManager::PREY_PREDATOR,
  134. PreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
  135. artis::qss::Integrator<artis::common::DoubleTime>::VALUE});
  136. }
  137. };
  138. void test_predator_prey_smart_gardener()
  139. {
  140. PreyPredatorSmartGardenerGraphManagerParameters parameters = {{{{45.},
  141. {true, true, 0.1, 3},
  142. {0.5, 0.01, 0.01, 0.2}},
  143. {{5000.},
  144. {true, true, 1, 3},
  145. {0.5, 0.01, 0.01, 0.2}}},
  146. {2000, 0.25, 0.75, 5}};
  147. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  148. artis::common::RootCoordinator<
  149. artis::common::DoubleTime, artis::pdevs::Coordinator<
  150. artis::common::DoubleTime,
  151. PreyPredatorSmartGardenerGraphManager,
  152. PreyPredatorSmartGardenerGraphManagerParameters>
  153. > rc(context, "root", parameters, artis::common::NoParameters());
  154. rc.attachView("Value2", new PredatorPreySmartGardenerView());
  155. rc.run(context);
  156. artis::observer::Output<artis::common::DoubleTime> output(rc.observer());
  157. output(0, 100, 0.1);
  158. }
  159. class MixedPredatorPreyView : public artis::observer::View<artis::common::DoubleTime> {
  160. public:
  161. MixedPredatorPreyView()
  162. {
  163. selector("PredatorView",
  164. {MixedPreyPredatorGraphManager::PREDATOR, DiscretePredatorGraphManager::PREDATOR,
  165. DiscretePredator::VALUE});
  166. selector("PreyView",
  167. {MixedPreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
  168. artis::qss::Integrator<artis::common::DoubleTime>::VALUE});
  169. }
  170. };
  171. void test_mixed_predator_prey()
  172. {
  173. MixedPreyPredatorGraphManagerParameters parameters = {{0.0001, 45., 0.5, 0.01, 0.01, 0.2},
  174. {{5000.},
  175. {true, true, 1, 3},
  176. {0.5, 0.01, 0.01, 0.2}}};
  177. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  178. artis::common::RootCoordinator<
  179. artis::common::DoubleTime, artis::pdevs::Coordinator<
  180. artis::common::DoubleTime,
  181. MixedPreyPredatorGraphManager,
  182. MixedPreyPredatorGraphManagerParameters>
  183. > rc(context, "root", parameters, artis::common::NoParameters());
  184. rc.attachView("Value3", new MixedPredatorPreyView());
  185. rc.run(context);
  186. artis::observer::Output<artis::common::DoubleTime> output(rc.observer());
  187. output(0, 100, 0.1);
  188. }
  189. int main()
  190. {
  191. test_parabola();
  192. test_predator_prey();
  193. test_predator_prey_smart_gardener();
  194. test_mixed_predator_prey();
  195. }