问题描述 假设某银行有4个窗口对外接待客户,从早晨银行开门(开门9:00am,关门5:00pm)起不断有客户进入银行。由于每个窗口在某个时刻只能接待一个客户,因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进入银行的客户(建议:客户进入时间使用随机函数产生),如果某个窗口的业务员正空闲,则可上前办理业务;反之,若4个窗口均有窗户所占,他便会排在人数最少的队伍后面。 任务要求: 1)编制一个程序以模拟银行的这种业务活动并计算一天中客户在银行逗留的平均时间。 2)建议有如下设置: a)客户到达时间随机产生,一天客户的人数设定为100人。b)银行业务员处理时间随机产生,平均处理时间10分钟。3)将一天的数据(包括业务员和客户)以文件方式输出。
时间: 2023-06-20 12:08:55 浏览: 49
以下是Python代码实现:
```python
import random
class Customer:
def __init__(self, arrival_time):
self.arrival_time = arrival_time
self.service_time = 0
self.wait_time = 0
def set_service_time(self, service_time):
self.service_time = service_time
self.wait_time = self.service_time - self.arrival_time
class Teller:
def __init__(self):
self.busy = False
self.customer = None
self.service_time = 0
def start_service(self, customer, current_time):
self.busy = True
self.customer = customer
self.service_time = current_time
service_time = random.randint(5, 15)
customer.set_service_time(current_time + service_time)
def end_service(self):
self.busy = False
self.customer = None
self.service_time = 0
class Bank:
def __init__(self):
self.tellers = [Teller() for _ in range(4)]
self.customers = []
self.waiting_times = []
self.current_time = 0
def add_customer(self):
customer = Customer(self.current_time)
self.customers.append(customer)
return customer
def get_shortest_queue(self):
queue_lengths = [len([c for c in self.customers if c.wait_time == 0 and t.customer != c]) for t in self.tellers]
return queue_lengths.index(min(queue_lengths))
def simulate(self):
for _ in range(100):
self.add_customer()
while True:
for teller in self.tellers:
if teller.busy and self.current_time == teller.customer.service_time:
teller.end_service()
for teller in self.tellers:
if not teller.busy:
queue_index = self.get_shortest_queue()
queue = [c for c in self.customers if c.wait_time == 0 and self.tellers[queue_index].customer != c]
if queue:
customer = min(queue, key=lambda c: c.arrival_time)
teller.start_service(customer, self.current_time)
for customer in self.customers:
if customer.wait_time == 0:
queue_lengths = [len([c for c in self.customers if c.wait_time == i and self.tellers[j].customer != c]) for j in range(4) for i in range(1, 5)]
shortest_queue = queue_lengths.index(min(queue_lengths))
shortest_queue_teller = shortest_queue // 4
customer.wait_time = self.current_time + shortest_queue % 4 * 5 - customer.arrival_time
self.waiting_times.append(sum([c.wait_time for c in self.customers]) / len(self.customers))
if all([t.busy for t in self.tellers]):
break
self.current_time += 1
with open('bank_data.txt', 'w') as f:
f.write('Customer Arrival Time,Service Start Time,Service End Time,Wait Time\n')
for customer in self.customers:
f.write(f'{customer.arrival_time},{customer.service_time - 10},{customer.service_time},{customer.wait_time}\n')
if __name__ == '__main__':
bank = Bank()
bank.simulate()
print(f'Average waiting time: {bank.waiting_times[-1]}')
```
程序首先定义了客户类、业务员类和银行类。在银行类中,我们使用一个列表来存储所有的客户,一个列表来存储所有的业务员,一个列表来存储顾客的等待时间。`add_customer` 方法用于添加客户,`get_shortest_queue` 方法用于获取最短的队列,`simulate` 方法用于模拟银行的业务活动。
程序中使用了三个循环。第一个循环用于添加100个客户。第二个循环用于处理正在接待客户的业务员。如果业务员正在办理业务并且当前时间等于该业务员的服务结束时间,则结束服务。第三个循环用于处理空闲业务员。如果业务员空闲,则找到最短的队列,并从该队列中选择最早到达银行的客户进行服务。这里使用了一个 lambda 函数来对队列进行排序。
每一次循环后,程序将计算客户的等待时间,并将其加入等待时间列表中。如果所有业务员都忙碌,则退出循环。最后,程序将客户的数据写入文件,并输出客户的平均等待时间。
注意,程序中的时间单位为分钟,等待时间是指客户从进入银行到开始办理业务所花费的时间。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)