Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 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.     age++;
  24.    
  25.     /* Test survivavilité */
  26.     float mort = survivalite(generator);
  27.    
  28.     if (mort >= (float)pow((float)survival/100, (float)1/12) * 100)
  29.     {
  30.         dead();
  31.     }
  32.  
  33.      /* Update de l'age */
  34.     else if (age >= 15 * 12)
  35.     {
  36.         dead();
  37.     }
  38.     else
  39.     {
  40.         /* Update maturity */
  41.         if (!adult && age >= maturity)
  42.         {
  43.             //std::cout << "Le lapin : " << this << " passe adulte !" << std::endl;
  44.             adult = 1;
  45.             survival = 50;
  46.         }
  47.  
  48.         /* Update anniversaire (porté + survavibilité) */
  49.         else if (age % 12 == 0)
  50.         {
  51.             /*float mort = survivalite(generator);
  52.             if (mort >= (float)pow((float)20/100, (float)1/12) * 100)
  53.             {
  54.                
  55.             }*/
  56.             anniversary();
  57.             if (age >= 11 * 12)
  58.                 survival = survival * 0.9;
  59.         }
  60.     }
  61. }
  62.  
  63. void Rabbit::anniversary()  
  64. {
  65.    
  66. }
  67.  
  68. void Rabbit::dead()
  69. {
  70.     int i = 0;
  71.     bool deleted = false;
  72.  
  73.     while (!deleted)
  74.     {
  75.         if (Rabbit::population[i] == this)
  76.         {
  77.             Rabbit::population.erase(Rabbit::population.begin() + i);
  78.             deleted = true;
  79.             Rabbit::mort_count++;
  80.         }            
  81.         i++;
  82.     }
  83. }
  84.  
  85. Female::Female() : porte(distribution(generator))
  86. {
  87.     //std::cout << "Je suis une femmelle !" << std::endl;
  88. }
  89.  
  90. void Female::anniversary()
  91. {
  92.  
  93.     porte = distribution(generator);
  94.  
  95.  
  96.     int i = 0;
  97.     int mois_tmp;
  98.  
  99.     mois.clear();
  100.     while (i < porte)
  101.     {
  102.         mois_tmp = repartition(generator);
  103.  
  104.         std::vector<int>::iterator p;
  105.         p = std::find (mois.begin(), mois.end(), mois_tmp);
  106.  
  107.         if (p != mois.end())
  108.         {
  109.  
  110.         }
  111.         else
  112.         {
  113.             mois.push_back(mois_tmp);
  114.             i++;
  115.         }
  116.     }
  117.     /*std::cout << "La portee pour la nouvelle annee sera : " << std::endl;
  118.     for (auto i = mois.begin(); i != mois.end(); ++i)
  119.         std::cout << *i << " / ";
  120.     std::cout << std::endl;*/
  121. }
  122.  
  123. void Female::update()
  124. {
  125.     Rabbit::update();
  126.     if (adult)
  127.     {
  128.         int nb_baby = naissance(generator);
  129.         /* A ameliorer avec un while */
  130.         for (int i = 0; i < mois.size(); i++)
  131.         {
  132.             if (age % 12 == mois[i])
  133.             {
  134.                 //std::cout << "Naissance de portee du lapin :" << this << std::endl;
  135.                 for (int j = 0; j < nb_baby; j++)
  136.                 {
  137.                     if (sexe(generator))
  138.                         Rabbit::population.push_back(new Female());
  139.                     else
  140.                         Rabbit::population.push_back(new Rabbit());
  141.                 }
  142.                 mois.erase(mois.begin() + i);
  143.             }
  144.         }
  145.     }  
  146. }
  147.  
  148. int main ()
  149. {
  150.  
  151.      for (int j = 0; j < 1000; j++)
  152.     {
  153.         Rabbit::population.push_back(new Female());
  154.     }
  155.     for (int j = 0; j < 1000; j++)
  156.     {
  157.         Rabbit::population.push_back(new Rabbit());
  158.     }
  159.  
  160.  
  161.     std::cout << (float)pow((float)20/100, (float)1/12) << std::endl;
  162.  
  163.    
  164.  
  165.     std::cout << "Update de la population :" << std::endl;
  166.  
  167.     for (int j = 0; j < 12 * 4; j++)
  168.     {
  169.         //std::cout << "Taille de la population : " << Rabbit::population.size() << std::endl;
  170.         for (int i = 0; i < Rabbit::population.size(); i++)
  171.         {
  172.             Rabbit::population[i]->update();
  173.         }
  174.         //std::cout << "Mois : " << j << std::endl;
  175.     }
  176.     std::cout << "Taille de la population : " << Rabbit::population.size() << std::endl;
  177.     std::cout << "Nombre de lapins morts : " << Rabbit::mort_count << std::endl;
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement