Advertisement
teofarov13

Untitled

Dec 24th, 2023
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1.  
  2.  
  3. MihailTeofarov: Fever at the Opera Haus
  4. In February, Gaetano Donizzetti's L'elisir d'amore is staged at the Vienna State Opera. The map
  5. of the theater is schematized in the in_theatre.dat file: 0s correspond to aisles, 1s to either empty
  6. seats or seats occupied by people in full health, 2s to people vaccinated against the flu, 3s and 4s
  7. to sick people (a little or a lot sick, respectively). During the show someone may try to improve
  8. his or her position: keep in mind that the stage is positioned after the last row of the seating plan
  9. and that a spectator's position improves if and only if 1. he or she moves closer to the stage and
  10. 2. moves away from the edges. During the duration of the show, the few people with the flu may
  11. infect their direct first neighbors (up-down/left-right), and at the end of the show some spectators
  12. will return home less healthy than before. How many?
  13.  
  14. GitHub Copilot: It seems like you're trying to simulate the spread of a disease in a theater. Here's a high-level plan:
  15.  
  16. 1. Load the theater map from the `in_theatre.dat` file. Each number in the file represents a different state of health or type of seat.
  17. 2. Create a function to calculate the distance of each seat from the stage and the edges. This will be used to determine if a spectator's position improves.
  18. 3. Create a function to simulate the spread of the disease. This function should iterate over each seat and, if the seat is
  19.  
  20. MihailTeofarov: Write a cpp code that:
  21. 1. reads the theater map: you have 3 different seating configurations (_nv, _v and _sv) with
  22. which you might observe the effect of the presence and distribution of the vaccinated people
  23. 2. implements the contagion: select a random seat, if it is occupied by a sick person then
  24. propose a contagion for each of the 4 nearest neighbors, the probability of infecting a nearby
  25. person is 10% for mildly sick spectators and 30% for heavily sick spectators (note: 3s
  26. transform 1s into 3s, while 4s transform both 1s and 3s into 4s, in the infection dynamics all
  27. 1s are supposed to be healthy people); repeat this dynamics for the duration of the
  28. performance (1000 time steps)
  29. 3. implements the swapping: select a random person once every 10 time steps, propose a
  30. random swap and accept the proposed move only if 1. the candidate seat is marked as 1 (in
  31. the swapping dynamics 1s can be either free seats or healthy spectators, do not worry about
  32. any distinction) and 2. it is an improvement
  33. 4. accumulates a little statistics before drawing conclusions: the opera runs throughout
  34. February, each night is independent of the previous one because no spectator wants to see the
  35. same opera twice in one month.
  36. 5. outputs in one file the number (or — better — the percentage) of 1. empty/healthy seats 2.
  37. vaccinated people (it should be constant!), 3. mildly sick people and 4. heavily sick people
  38. for every replica of the show.
  39. The
  40.  
  41. GitHub Copilot: Here is a C++ code that implements the requirements you've described.
  42.  
  43. ```cpp
  44. #include <iostream>
  45. #include <fstream>
  46. #include <vector>
  47. #include <random>
  48. #include <algorithm>
  49.  
  50. // Constants
  51. const int TIME_STEPS = 1000;
  52. const int SWAP_INTERVAL = 10;
  53. const int HEALTHY = 1;
  54. const int VACCINATED = 2;
  55. const int MILDLY_SICK = 3;
  56. const int HEAVILY_SICK = 4;
  57.  
  58. // Function to simulate contagion
  59. void simulateContagion(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
  60.     std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
  61.     for (int i = 0; i < TIME_STEPS; i++) {
  62.         int row = distribution(generator);
  63.         int col = distribution(generator);
  64.         if (seatingPlan[row][col] == MILDLY_SICK || seatingPlan[row][col] == HEAVILY_SICK) {
  65.             // Propose contagion for each of the 4 nearest neighbors
  66.             for (int j = std::max(0, row - 1); j <= std::min((int)seatingPlan.size() - 1, row + 1); j++) {
  67.                 for (int k = std::max(0, col - 1); k <= std::min((int)seatingPlan[j].size() - 1, col + 1); k++) {
  68.                     if (j != row || k != col) {
  69.                         if (seatingPlan[j][k] == HEALTHY) {
  70.                             seatingPlan[j][k] = seatingPlan[row][col] == MILDLY_SICK ? MILDLY_SICK : HEAVILY_SICK;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. // Function to simulate swapping
  80. void simulateSwapping(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
  81.     std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
  82.     for (int i = 0; i < TIME_STEPS; i++) {
  83.         if (i % SWAP_INTERVAL == 0) {
  84.             int row1 = distribution(generator);
  85.             int col1 = distribution(generator);
  86.             int row2 = distribution(generator);
  87.             int col2 = distribution(generator);
  88.             if (seatingPlan[row1][col1] != HEALTHY && seatingPlan[row2][col2] == HEALTHY) {
  89.                 std::swap(seatingPlan[row1][col1], seatingPlan[row2][col2]);
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. // Function to calculate statistics
  96. void calculateStatistics(const std::vector<std::vector<int>>& seatingPlan, std::ofstream& file) {
  97.     int totalSeats = seatingPlan.size() * seatingPlan[0].size();
  98.     int emptySeats = 0;
  99.     int vaccinated = 0;
  100.     int mildlySick = 0;
  101.     int heavilySick = 0;
  102.     for (const auto& row : seatingPlan) {
  103.         for (int seat : row) {
  104.             switch (seat) {
  105.                 case HEALTHY:
  106.                     emptySeats++;
  107.                     break;
  108.                 case VACCINATED:
  109.                     vaccinated++;
  110.                     break;
  111.                 case MILDLY_SICK:
  112.                     mildlySick++;
  113.                     break;
  114.                 case HEAVILY_SICK:
  115.                     heavilySick++;
  116.                     break;
  117.             }
  118.         }
  119.     }
  120.     file << "Empty/Healthy seats: " << (double)emptySeats / totalSeats * 100 << "%\n";
  121.     file << "Vaccinated people: " << (double)vaccinated / totalSeats * 100 << "%\n";
  122.     file << "Mildly sick people: " << (double)mildlySick / totalSeats * 100 << "%\n";
  123.     file << "Heavily sick people: " << (double)heavilySick / totalSeats * 100 << "%\n";
  124. }
  125.  
  126. int main() {
  127.     std::ifstream file("in_theatre.dat");
  128.     std::ofstream outFile("out_theatre.dat");
  129.     std::vector<std::vector<int>> seatingPlan;
  130.  
  131.     // Read seating plan from file
  132.     std::string line;
  133.     while (std::getline(file, line)) {
  134.         std::vector<int> row;
  135.         for (char c : line) {
  136.             row.push_back(c - '0');
  137.         }
  138.         seatingPlan.push_back(row);
  139.     }
  140.  
  141.     // Initialize random number generator
  142.     std::default_random_engine generator;
  143.  
  144.     // Simulate contagion and swapping
  145.     simulateContagion(seatingPlan, generator);
  146.     simulateSwapping(seatingPlan, generator);
  147.  
  148.     // Calculate and output statistics
  149.     calculateStatistics(seatingPlan, outFile);
  150.  
  151.     return 0;
  152. }
  153. ```
  154.  
  155. This code reads the seating plan from a file, simulates the contagion and swapping dynamics, and then calculates and outputs the statistics to another file. The contagion and swapping dynamics are simulated using a random number generator. The statistics are calculated by counting the number of each type of seat and then outputting the percentages.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement