Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tp4.hpp"
- using namespace std::chrono;
- std::mt19937 generator(0);
- std::uniform_int_distribution<int> distribution(5,8);
- std::uniform_int_distribution<int> repartition(1,12);
- std::uniform_int_distribution<int> naissance(3,6);
- std::uniform_int_distribution<int> sexe(0,1);
- std::uniform_real_distribution<float> survivalite(0,100);
- /* Définition de la variable de population */
- std::vector<Rabbit*> Rabbit::population;
- int Rabbit::mort_count = 0;
- Rabbit::Rabbit() : adult(0), age(-1), survival(20), maturity(distribution(generator))
- {
- //std::cout << "Creation d'un lapin" << std::endl;
- }
- void Rabbit::update()
- {
- // Record start time
- /* Your algorithm here */
- age++;
- /* Test survivavilité */
- float mort = survivalite(generator);
- if (mort >= (float)pow((float)survival/100, (float)1/12) * 100)
- {
- dead();
- }
- /* Update de l'age */
- else if (age >= 15 * 12)
- {
- dead();
- }
- else
- {
- /* Update maturity */
- if (!adult && age >= maturity)
- {
- adult = 1;
- survival = 50;
- }
- /* Update anniversaire (porté + survavibilité) */
- else if (age % 12 == 0)
- {
- anniversary();
- if (age >= 11 * 12)
- survival = survival * 0.9;
- }
- }
- }
- void Rabbit::anniversary()
- {
- }
- void Rabbit::dead()
- {
- /*
- int i = 0;
- bool deleted = false;
- while (!deleted)
- {
- if (Rabbit::population[i] == this)
- {
- //Rabbit::population.erase(Rabbit::population.begin() + i);
- Rabbit::population[i] = nullptr;
- deleted = true;
- //Rabbit::mort_count++;
- }
- i++;
- }*/
- delete this;
- }
- Female::Female() : porte(distribution(generator))
- {
- }
- void Female::anniversary()
- {
- porte = distribution(generator);
- int i = 0;
- int mois_tmp;
- mois.clear();
- while (i < porte)
- {
- mois_tmp = repartition(generator);
- std::vector<int>::iterator p;
- p = std::find (mois.begin(), mois.end(), mois_tmp);
- if (p != mois.end())
- {
- }
- else
- {
- mois.push_back(mois_tmp);
- i++;
- }
- }
- }
- void Female::update()
- {
- Rabbit::update();
- if (adult)
- {
- int nb_baby = naissance(generator);
- /* A ameliorer avec un while */
- for (int i = 0; i < mois.size(); i++)
- {
- if (age % 12 == mois[i])
- {
- //std::cout << "Naissance de portee du lapin :" << this << std::endl;
- for (int j = 0; j < nb_baby; j++)
- {
- if (sexe(generator))
- Rabbit::population.push_back(new Female());
- else
- Rabbit::population.push_back(new Rabbit());
- }
- mois.erase(mois.begin() + i);
- }
- }
- }
- }
- int main ()
- {
- for (int j = 0; j < 1000; j++)
- {
- Rabbit::population.push_back(new Female());
- }
- for (int j = 0; j < 1000; j++)
- {
- Rabbit::population.push_back(new Rabbit());
- }
- std::cout << (float)pow((float)20/100, (float)1/12) << std::endl;
- std::cout << "Update de la population :" << std::endl;
- for (int j = 0; j < 12 * 4; j++)
- {
- auto start = std::chrono::high_resolution_clock::now();
- //std::cout << "Taille de la population : " << Rabbit::population.size() << std::endl;
- for (int i = 0; i < Rabbit::population.size(); i++)
- {
- if (Rabbit::population[i])
- Rabbit::population[i]->update();
- }
- std::cout << "Mois : " << j << " / Pop : " << Rabbit::population.size() << std::endl;
- auto finish = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double> elapsed = finish - start;
- /*if (elapsed.count() > 1.000f)
- std::cout << "Elapsed time: " << elapsed.count() << " s\n";*/
- }
- std::cout << "Taille de la population : " << Rabbit::population.size() << std::endl;
- std::cout << "Nombre de lapins morts : " << Rabbit::mort_count << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement