排队论多服务台损失制排队模型 C++代码
时间: 2023-10-15 18:07:24 浏览: 33
以下是一个排队论多服务台损失制排队模型的C++代码示例:
```cpp
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAX_CUSTOMERS = 100;
const int MAX_SERVERS = 10;
struct Customer {
int arrivalTime;
int serviceTime;
int serverId;
Customer(int _arrivalTime, int _serviceTime) : arrivalTime(_arrivalTime), serviceTime(_serviceTime), serverId(-1) {}
};
struct Server {
int id;
int serviceTime;
int remainingTime;
Server(int _id, int _serviceTime) : id(_id), serviceTime(_serviceTime), remainingTime(0) {}
};
int main() {
// 输入参数
int numServers, maxQueueSize, numCustomers;
int arrivalTimes[MAX_CUSTOMERS], serviceTimes[MAX_CUSTOMERS];
cout << "输入服务台数量、队列最大长度、顾客数量:" << endl;
cin >> numServers >> maxQueueSize >> numCustomers;
cout << "输入每个顾客的到达时间和服务时间:" << endl;
for (int i = 0; i < numCustomers; i++) {
cin >> arrivalTimes[i] >> serviceTimes[i];
}
// 初始化服务台和队列
vector<Server> servers;
queue<Customer> queue;
for (int i = 0; i < numServers; i++) {
servers.emplace_back(i, 0);
}
// 模拟
int currentTime = 0;
int totalWaitTime = 0;
int numLostCustomers = 0;
int numServedCustomers = 0;
int nextCustomerIndex = 0;
while (numServedCustomers + numLostCustomers < numCustomers) {
// 将到达时间 <= 当前时间的顾客加入队列
while (nextCustomerIndex < numCustomers && arrivalTimes[nextCustomerIndex] <= currentTime) {
if (queue.size() < maxQueueSize) {
queue.emplace(arrivalTimes[nextCustomerIndex], serviceTimes[nextCustomerIndex]);
} else {
numLostCustomers++;
}
nextCustomerIndex++;
}
// 将服务时间到达的服务台加入队列
for (Server& server : servers) {
if (server.remainingTime == 0) {
if (!queue.empty()) {
Customer customer = queue.front();
queue.pop();
server.serviceTime = customer.serviceTime;
server.remainingTime = customer.serviceTime;
server.id = customer.serverId;
totalWaitTime += currentTime - customer.arrivalTime;
numServedCustomers++;
}
} else {
server.remainingTime--;
}
}
// 更新时间
currentTime++;
}
// 输出结果
cout << "服务台数量:" << numServers << endl;
cout << "队列最大长度:" << maxQueueSize << endl;
cout << "顾客数量:" << numCustomers << endl;
cout << "丢失的顾客数量:" << numLostCustomers << endl;
cout << "服务的顾客数量:" << numServedCustomers << endl;
cout << "平均等待时间:" << (double)totalWaitTime / numServedCustomers << endl;
return 0;
}
```
该代码实现了一个排队论多服务台损失制排队模型。输入参数包括服务台数量、队列最大长度和顾客数量,以及每个顾客的到达时间和服务时间。程序通过模拟来计算丢失的顾客数量、服务的顾客数量和平均等待时间,并将结果输出到控制台。