洗车问题 有两台洗车机,一台性能好(3分钟洗一台小汽车,5分钟清洗一台SUV),一台性能较差(6分钟洗一台小汽车,10分钟清洗一台SUV)。车流量为随机,在上午是每小时20辆车,大小随机,比例1:1;下午是每小时30辆车,大小随机,比例为4:6。请问如何分别实现上下午平均等待时间最短。python代码
时间: 2023-11-24 17:08:06 浏览: 86
汽车自动清洗装置PLC控制.doc
下面是一个用 Python 实现洗车问题的代码,使用了 numpy 库来生成随机数:
```python
import numpy as np
class Queue:
def __init__(self):
self.queue = []
self.waiting_time = []
def push(self, car):
self.queue.append(car)
def pop(self):
return self.queue.pop(0)
def get_size(self):
return len(self.queue)
class Car:
def __init__(self, size):
self.size = size
self.enter_time = 0
self.start_time = 0
self.finish_time = 0
self.wait_time = 0
class CarWashMachine:
def __init__(self, wash_time, suv_wash_time):
self.wash_time = wash_time
self.suv_wash_time = suv_wash_time
self.status = 0
def is_available(self):
return self.status == 0
def wash_car(self, car):
if car.size == 'SUV':
wash_time = self.suv_wash_time
else:
wash_time = self.wash_time
car.start_time = max(car.enter_time, self.finish_time)
car.wait_time = car.start_time - car.enter_time
car.finish_time = car.start_time + wash_time
self.status = 1
def simulate(queue, machine1, machine2, num_cars, suv_ratio):
car_count = 0
suv_count = 0
while car_count < num_cars:
# generate car type
if np.random.random() < suv_ratio:
size = 'SUV'
suv_count += 1
else:
size = 'car'
# generate car object
car = Car(size)
car.enter_time = car_count
queue.push(car)
car_count += 1
# check available machine
if machine1.is_available() and (car.size == 'car' or suv_count % 2 == 0):
machine1.wash_car(queue.pop())
elif machine2.is_available():
machine2.wash_car(queue.pop())
# update machine status
if machine1.status == 1 and machine1.finish_time <= car_count:
machine1.status = 0
if machine2.status == 1 and machine2.finish_time <= car_count:
machine2.status = 0
# update finish time
finish_time = min(machine1.finish_time, machine2.finish_time)
if queue.get_size() > 0 and queue.queue[0].enter_time <= finish_time:
finish_time = queue.queue[0].enter_time
machine1.finish_time = finish_time
machine2.finish_time = finish_time
def calculate_avg_wait_time(cars):
wait_time = [car.wait_time for car in cars]
return sum(wait_time) / len(wait_time)
# morning
print('Morning:')
queue = Queue()
machine1 = CarWashMachine(3, 5)
machine2 = CarWashMachine(6, 10)
simulate(queue, machine1, machine2, 20, 0.5)
print('Machine 1 total wait time:', sum(machine1.waiting_time))
print('Machine 2 total wait time:', sum(machine2.waiting_time))
if calculate_avg_wait_time(queue.waiting_time) < calculate_avg_wait_time(machine1.waiting_time):
print('Choose machine 1')
else:
print('Choose machine 2')
# afternoon
print('Afternoon:')
queue = Queue()
machine1 = CarWashMachine(3, 5)
machine2 = CarWashMachine(6, 10)
simulate(queue, machine1, machine2, 30, 0.6)
print('Machine 1 total wait time:', sum(machine1.waiting_time))
print('Machine 2 total wait time:', sum(machine2.waiting_time))
if calculate_avg_wait_time(queue.waiting_time) < calculate_avg_wait_time(machine1.waiting_time):
print('Choose machine 1')
else:
print('Choose machine 2')
```
上面的代码中,我们定义了 Queue 类来模拟车辆排队的过程,Car 类来表示车辆对象,CarWashMachine 类来表示洗车机对象。在 simulate() 函数中,我们模拟了车流量的到达过程,并根据洗车机的性能将车辆分配到不同的洗车机进行洗车。在模拟过程中,我们记录了每辆车的进入队列时间、开始洗车时间、结束洗车时间和等待时间,并计算每小时的平均等待时间。最后,根据计算结果选择平均等待时间最短的洗车机进行洗车。
注意,上午和下午分别进行了一次模拟,因此可以分别得出上午和下午的平均等待时间并进行比较。
阅读全文