I wish to design an elevator simulator. the building has two elevators and 52 floors. the building has 120 residents who come and go to random floors at a rate of about 60 trips per hour. write code in C++ which simulates the elevator and trips and can tell me what the average wait time will for each person who wants to use the elevator.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Designing an elevator simulator can be a complex task, but I'll provide a basic C++ code structure to get you started. This code uses a simple first-come, first-served (FCFS) algorithm for elevator scheduling. To make it more realistic, you can implement more advanced algorithms like the "elevator algorithm" or "scan algorithm." Here's the basic code:
cpp
#include <iostream>
#include <vector>
#include <queue>
#include <random>
#include <chrono>
class Person {
public:
int currentFloor;
int destinationFloor;
std::chrono::time_point<std::chrono::steady_clock> arrivalTime;
Person(int currentFloor, int destinationFloor, std::chrono::time_point<std::chrono::steady_clock> arrivalTime)
: currentFloor(currentFloor), destinationFloor(destinationFloor), arrivalTime(arrivalTime) {}
};
class Elevator {
public:
int currentFloor;
int direction; // -1 for down, 0 for idle, 1 for up
std::queue<Person> passengers;
Elevator() : currentFloor(0), direction(0) {}
};
class Building {
public:
int numFloors;
int numResidents;
int tripsPerHour;
std::vector<Elevator> elevators;
std::queue<Person> waitingPeople;
Building(int numFloors, int numResidents, int tripsPerHour, int numElevators)
: numFloors(numFloors), numResidents(numResidents), tripsPerHour(tripsPerHour), elevators(numElevators) {}
void simulate(int durationSeconds) {
std::default_random_engine generator;
std::uniform_int_distribution<int> floorDistribution(0, numFloors - 1);
std::poisson_distribution<int> tripDistribution(tripsPerHour / 3600.0);
auto startTime = std::chrono::steady_clock::now();
int totalWaitTime = 0;
int numPeople = 0;
while (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - startTime).count() < durationSeconds) {
// Generate new people
int newPeople = tripDistribution(generator);
for (int i = 0; i < newPeople; i++) {
int currentFloor = floorDistribution(generator);
int destinationFloor;
do {
destinationFloor = floorDistribution(generator);
} while (currentFloor == destinationFloor);
waitingPeople.emplace(currentFloor, destinationFloor, std::chrono::steady_clock::now());
}
// Process elevators
for (auto& elevator : elevators) {
if (elevator.direction == 0) {
// Idle elevator, pick a direction based on waiting people
if (!waitingPeople.empty()) {
elevator.direction = (waitingPeople.front().currentFloor > elevator.currentFloor) ? 1 : -1;
}
}
// Move elevator and pick up/drop off passengers
elevator.currentFloor += elevator.direction;
if (elevator.currentFloor < 0 || elevator.currentFloor >= numFloors) {
elevator.direction = -elevator.direction;
elevator.currentFloor += elevator.direction;
}
while (!elevator.passengers.empty() && elevator.passengers.front().destinationFloor == elevator.currentFloor) {
Person person = elevator.passengers.front();
elevator.passengers.pop();
totalWaitTime += std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - person.arrivalTime).count();
numPeople++;
}
while (!waitingPeople.empty() && waitingPeople.front().currentFloor == elevator.currentFloor) {
Person person = waitingPeople.front();
waitingPeople.pop();
elevator.passengers.push(person);
}
}
}
std::cout << "Average wait time: " << totalWaitTime / numPeople << " seconds" << std::endl;
}
};
int main() {
Building building(52, 120, 60, 2);
building.simulate(3600); // Simulate for 1 hour
return 0;
}
This code generates new people at a rate of 60 trips per hour and simulates the elevators for one hour. The average wait time is calculated and printed at the end of the simulation. You can modify the code to implement more advanced algorithms or add more features as needed.