Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include "tp4.hpp"
  2.  
  3. using namespace std::chrono;
  4.  
  5. std::mt19937 generator(0);
  6. std::uniform_int_distribution<int> distribution(5,8);
  7. std::uniform_int_distribution<int> repartition(1,12);
  8. std::uniform_int_distribution<int> naissance(3,6);
  9. std::uniform_int_distribution<int> sexe(0,1);
  10. std::uniform_real_distribution<float> survivalite(0,100);
  11.  
  12. /* Définition de la variable de population */
  13. std::vector<Rabbit*> Rabbit::population;
  14. int Rabbit::mort_count = 0;
  15.  
  16. Rabbit::Rabbit() : adult(0), age(-1), survival(20), maturity(distribution(generator))
  17. {  
  18.     //std::cout << "Creation d'un lapin" << std::endl;
  19. }
  20.  
  21. void Rabbit::update()
  22. {
  23.  
  24.     // Record start time
  25.    
  26.  
  27.  
  28.     /* Your algorithm here */
  29.  
  30.     age++;
  31.    
  32.     /* Test survivavilité */
  33.     float mort = survivalite(generator);
  34.  
  35.  
  36.     if (mort >= (float)pow((float)survival/100, (float)1/12) * 100)
  37.     {
  38.         dead();
  39.     }
  40.  
  41.      /* Update de l'age */
  42.     else if (age >= 15 * 12)
  43.     {
  44.         dead();
  45.     }
  46.    
  47.     else
  48.     {
  49.         /* Update maturity */
  50.         if (!adult && age >= maturity)
  51.         {
  52.             adult = 1;
  53.             survival = 50;
  54.         }
  55.  
  56.         /* Update anniversaire (porté + survavibilité) */
  57.         else if (age % 12 == 0)
  58.         {
  59.             anniversary();
  60.             if (age >= 11 * 12)
  61.                 survival = survival * 0.9;
  62.         }
  63.     }
  64. }
  65.  
  66. void Rabbit::anniversary()  
  67. {
  68.    
  69. }
  70.  
  71. void Rabbit::dead()
  72. {
  73.     /*
  74.     int i = 0;
  75.     bool deleted = false;
  76.  
  77.     while (!deleted)
  78.     {
  79.         if (Rabbit::population[i] == this)
  80.         {
  81.             //Rabbit::population.erase(Rabbit::population.begin() + i);
  82.             Rabbit::population[i] = nullptr;
  83.             deleted = true;
  84.             //Rabbit::mort_count++;
  85.         }            
  86.         i++;
  87.     }*/
  88.  
  89.     delete this;
  90. }
  91.  
  92. Female::Female() : porte(distribution(generator))
  93. {
  94.  
  95. }
  96.  
  97. void Female::anniversary()
  98. {
  99.  
  100.     porte = distribution(generator);
  101.  
  102.  
  103.     int i = 0;
  104.     int mois_tmp;
  105.  
  106.     mois.clear();
  107.     while (i < porte)
  108.     {
  109.         mois_tmp = repartition(generator);
  110.  
  111.         std::vector<int>::iterator p;
  112.         p = std::find (mois.begin(), mois.end(), mois_tmp);
  113.  
  114.         if (p != mois.end())
  115.         {
  116.  
  117.         }
  118.         else
  119.         {
  120.             mois.push_back(mois_tmp);
  121.             i++;
  122.         }
  123.     }
  124. }
  125.  
  126. void Female::update()
  127. {
  128.     Rabbit::update();
  129.     if (adult)
  130.     {
  131.         int nb_baby = naissance(generator);
  132.         /* A ameliorer avec un while */
  133.         for (int i = 0; i < mois.size(); i++)
  134.         {
  135.             if (age % 12 == mois[i])
  136.             {
  137.                 //std::cout << "Naissance de portee du lapin :" << this << std::endl;
  138.                 for (int j = 0; j < nb_baby; j++)
  139.                 {
  140.                     if (sexe(generator))
  141.                         Rabbit::population.push_back(new Female());
  142.                     else
  143.                         Rabbit::population.push_back(new Rabbit());
  144.                 }
  145.                 mois.erase(mois.begin() + i);
  146.             }
  147.         }
  148.     }  
  149. }
  150.  
  151. int main ()
  152. {
  153.  
  154.      for (int j = 0; j < 1000; j++)
  155.     {
  156.         Rabbit::population.push_back(new Female());
  157.     }
  158.     for (int j = 0; j < 1000; j++)
  159.     {
  160.         Rabbit::population.push_back(new Rabbit());
  161.     }
  162.  
  163.  
  164.     std::cout << (float)pow((float)20/100, (float)1/12) << std::endl;
  165.  
  166.    
  167.  
  168.     std::cout << "Update de la population :" << std::endl;
  169.  
  170.     for (int j = 0; j < 12 * 4; j++)
  171.     {
  172.  
  173.  
  174.         auto start = std::chrono::high_resolution_clock::now();
  175.  
  176.  
  177.         //std::cout << "Taille de la population : " << Rabbit::population.size() << std::endl;
  178.         for (int i = 0; i < Rabbit::population.size(); i++)
  179.         {
  180.             if (Rabbit::population[i])
  181.                 Rabbit::population[i]->update();
  182.         }
  183.         std::cout << "Mois : " << j << " / Pop : " << Rabbit::population.size() << std::endl;
  184.  
  185.         auto finish = std::chrono::high_resolution_clock::now();
  186.         std::chrono::duration<double> elapsed = finish - start;
  187.         /*if (elapsed.count() > 1.000f)
  188.             std::cout << "Elapsed time: " << elapsed.count() << " s\n";*/
  189.  
  190.  
  191.     }
  192.     std::cout << "Taille de la population : " << Rabbit::population.size() << std::endl;
  193.     std::cout << "Nombre de lapins morts : " << Rabbit::mort_count << std::endl;
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement