Facebook
From Coral Plover, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 194
  1. /*
  2.  
  3. Dla każdej osoby w każdy dzień losować czy uda się do hotelu.
  4. Jeśli tak, to do jakiego.
  5.  
  6. Optymalizacja: Jak odnotować kolizję:
  7.     np. słownik (para osób, liczba)
  8.  
  9. */
  10.  
  11. const numPeople = 1000;
  12. const matchProbability = 1;
  13. const numHotels = 100;
  14. const numDays = 100;
  15.  
  16. const numPeopleInHotel = [];
  17. const people = [];
  18. const hotels = [];
  19. const meetings = {};
  20.  
  21. let currentSimulationDay = 0;
  22.  
  23. class Person {
  24.     constructor(personID) {
  25.         this.ID = personID;
  26.         this.currentHotel = null;
  27.     }
  28.  
  29.     selectHotel() {
  30.         this.currentHotel = Math.floor(Math.random()*numHotels);
  31.         console.log(`[${currentSimulationDay}] ${this.ID} -> ${this.currentHotel}`)
  32.         return this.currentHotel;
  33.     };
  34. }
  35.  
  36. class Hotel {
  37.     constructor(hotelID) {
  38.         this.ID = hotelID;
  39.         this.currentNumPeople = 0;
  40.         this.peopleInside = [];
  41.     }
  42.  
  43.     visitHotel(personID) {
  44.         this.currentNumPeople++;
  45.         this.peopleInside.push(personID);
  46.     }
  47.  
  48.     newDay() {
  49.         this.currentNumPeople = 0;
  50.         this.peopleInside = [];
  51.     }
  52. }
  53.  
  54.  
  55. for (let i=0; i<numPeople; i++) {
  56.     people[i] = new Person(i);
  57. }
  58.  
  59. for (let i=0; i<numHotels; i++) {
  60.     hotels[i] = new Hotel(i);
  61. }
  62.  
  63. while (currentSimulationDay++ < numDays) {
  64.    
  65.     // all people decide to go to hotel or not
  66.     people.forEach(person => {
  67.  
  68.         if (Math.random() < matchProbability) {
  69.             // if the person goes to the hotel, select a random hotel he goes to
  70.             const selectedHotel = person.selectHotel();
  71.             hotels[selectedHotel].visitHotel(person.ID);
  72.         }
  73.  
  74.     });
  75.  
  76.     // console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
  77.     // console.log('Podsumowanie dnia ' + currentSimulationDay);
  78.     hotels.forEach(hotel => {
  79.         // console.log(`${hotel.ID} -> ${hotel.currentNumPeople}.`);
  80.        
  81.         hotel.peopleInside.forEach(personA => {
  82.             hotel.peopleInside.forEach(personB => {
  83.                 if (personA !== personB) {
  84.                     if (!meetings[[personA, personB]]) {
  85.                         meetings[[personA, personB]] = 0;
  86.                     };
  87.                     meetings[[personA, personB]]++;
  88.                 }
  89.             });
  90.         });
  91.  
  92.         hotel.newDay();
  93.     });  
  94. }
  95.  
  96. console.log(meetings);
  97.