Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MihailTeofarov: Fever at the Opera Haus
- In February, Gaetano Donizzetti's L'elisir d'amore is staged at the Vienna State Opera. The map
- of the theater is schematized in the in_theatre.dat file: 0s correspond to aisles, 1s to either empty
- seats or seats occupied by people in full health, 2s to people vaccinated against the flu, 3s and 4s
- to sick people (a little or a lot sick, respectively). During the show someone may try to improve
- his or her position: keep in mind that the stage is positioned after the last row of the seating plan
- and that a spectator's position improves if and only if 1. he or she moves closer to the stage and
- 2. moves away from the edges. During the duration of the show, the few people with the flu may
- infect their direct first neighbors (up-down/left-right), and at the end of the show some spectators
- will return home less healthy than before. How many?
- GitHub Copilot: It seems like you're trying to simulate the spread of a disease in a theater. Here's a high-level plan:
- 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.
- 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.
- 3. Create a function to simulate the spread of the disease. This function should iterate over each seat and, if the seat is
- MihailTeofarov: Write a cpp code that:
- 1. reads the theater map: you have 3 different seating configurations (_nv, _v and _sv) with
- which you might observe the effect of the presence and distribution of the vaccinated people
- 2. implements the contagion: select a random seat, if it is occupied by a sick person then
- propose a contagion for each of the 4 nearest neighbors, the probability of infecting a nearby
- person is 10% for mildly sick spectators and 30% for heavily sick spectators (note: 3s
- transform 1s into 3s, while 4s transform both 1s and 3s into 4s, in the infection dynamics all
- 1s are supposed to be healthy people); repeat this dynamics for the duration of the
- performance (1000 time steps)
- 3. implements the swapping: select a random person once every 10 time steps, propose a
- random swap and accept the proposed move only if 1. the candidate seat is marked as 1 (in
- the swapping dynamics 1s can be either free seats or healthy spectators, do not worry about
- any distinction) and 2. it is an improvement
- 4. accumulates a little statistics before drawing conclusions: the opera runs throughout
- February, each night is independent of the previous one because no spectator wants to see the
- same opera twice in one month.
- 5. outputs in one file the number (or — better — the percentage) of 1. empty/healthy seats 2.
- vaccinated people (it should be constant!), 3. mildly sick people and 4. heavily sick people
- for every replica of the show.
- The
- GitHub Copilot: Here is a C++ code that implements the requirements you've described.
- ```cpp
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <random>
- #include <algorithm>
- // Constants
- const int TIME_STEPS = 1000;
- const int SWAP_INTERVAL = 10;
- const int HEALTHY = 1;
- const int VACCINATED = 2;
- const int MILDLY_SICK = 3;
- const int HEAVILY_SICK = 4;
- // Function to simulate contagion
- void simulateContagion(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
- std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
- for (int i = 0; i < TIME_STEPS; i++) {
- int row = distribution(generator);
- int col = distribution(generator);
- if (seatingPlan[row][col] == MILDLY_SICK || seatingPlan[row][col] == HEAVILY_SICK) {
- // Propose contagion for each of the 4 nearest neighbors
- for (int j = std::max(0, row - 1); j <= std::min((int)seatingPlan.size() - 1, row + 1); j++) {
- for (int k = std::max(0, col - 1); k <= std::min((int)seatingPlan[j].size() - 1, col + 1); k++) {
- if (j != row || k != col) {
- if (seatingPlan[j][k] == HEALTHY) {
- seatingPlan[j][k] = seatingPlan[row][col] == MILDLY_SICK ? MILDLY_SICK : HEAVILY_SICK;
- }
- }
- }
- }
- }
- }
- }
- // Function to simulate swapping
- void simulateSwapping(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
- std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
- for (int i = 0; i < TIME_STEPS; i++) {
- if (i % SWAP_INTERVAL == 0) {
- int row1 = distribution(generator);
- int col1 = distribution(generator);
- int row2 = distribution(generator);
- int col2 = distribution(generator);
- if (seatingPlan[row1][col1] != HEALTHY && seatingPlan[row2][col2] == HEALTHY) {
- std::swap(seatingPlan[row1][col1], seatingPlan[row2][col2]);
- }
- }
- }
- }
- // Function to calculate statistics
- void calculateStatistics(const std::vector<std::vector<int>>& seatingPlan, std::ofstream& file) {
- int totalSeats = seatingPlan.size() * seatingPlan[0].size();
- int emptySeats = 0;
- int vaccinated = 0;
- int mildlySick = 0;
- int heavilySick = 0;
- for (const auto& row : seatingPlan) {
- for (int seat : row) {
- switch (seat) {
- case HEALTHY:
- emptySeats++;
- break;
- case VACCINATED:
- vaccinated++;
- break;
- case MILDLY_SICK:
- mildlySick++;
- break;
- case HEAVILY_SICK:
- heavilySick++;
- break;
- }
- }
- }
- file << "Empty/Healthy seats: " << (double)emptySeats / totalSeats * 100 << "%\n";
- file << "Vaccinated people: " << (double)vaccinated / totalSeats * 100 << "%\n";
- file << "Mildly sick people: " << (double)mildlySick / totalSeats * 100 << "%\n";
- file << "Heavily sick people: " << (double)heavilySick / totalSeats * 100 << "%\n";
- }
- int main() {
- std::ifstream file("in_theatre.dat");
- std::ofstream outFile("out_theatre.dat");
- std::vector<std::vector<int>> seatingPlan;
- // Read seating plan from file
- std::string line;
- while (std::getline(file, line)) {
- std::vector<int> row;
- for (char c : line) {
- row.push_back(c - '0');
- }
- seatingPlan.push_back(row);
- }
- // Initialize random number generator
- std::default_random_engine generator;
- // Simulate contagion and swapping
- simulateContagion(seatingPlan, generator);
- simulateSwapping(seatingPlan, generator);
- // Calculate and output statistics
- calculateStatistics(seatingPlan, outFile);
- return 0;
- }
- ```
- 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