main.cpp 8.1 KB

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