问题描述
Python:
我正在用两种类型的来电模拟呼叫中心:销售电话和服务电话。
这些调用具有不同的,独立的分布,并进入同一系统。
iat_sales = random.expovariate(1/3)
yield env.timeout(iat_sales)
我想加入:
iat_service = random.triangular(0,6)
yield env.timeout(iat_service)
我如何同时产生每个事件?
这是我想出的解决方案:
def arrival_list():
sales_time = 0 #sim time of sales arrival
service_time = 0 #sim time of service arrival
sales_list=[] #list of sequential sales arrivals [arrival time,'sales']
service_list=[] #list of sequential sales arrivals [arrival time,'service']
arrivals = [] #ordered list of arrivals (sales and service merged) [arrival time,arrival type,iat]
while sales_time < sim_end:
iat_sales = random.expovariate(sales_rate)
sales_time += iat_sales
sales=[sales_time,'sales']
sales_list.append(sales)
while service_time < sim_end:
iat_service = random.triangular(0,6,0) ####
service_time += iat_service
service=[service_time,'service']
service_list.append(service)
arrivals = sales_list + service_list
arrivals.sort()
arrivals[0].append(arrivals[0][0])
for i in range(len(arrivals)-1):
arrivals[i+1].append(arrivals[i+1][0]-arrivals[i][0])
return arrivals
解决方法
作为参考,可以像这样进行简单的实现,其中以1秒的间隔无限期地运行仿真,并且如果其随机值超过某些阈值,则认为调用已到达:
import random
import time
def generate_calls():
return random.expovariate(1/3),random.triangular(10,20,5)
def simulation(count,sales_acceptance,services_acceptance):
# run the simulation indefinitely
while True:
print('Time: {}'.format(count))
sales_call,services_call = generate_calls()
# calls arrive if the values exceed some thresholds
if sales_call > sales_acceptance:
print('Sales call arrived!')
if services_call > services_acceptance:
print('Services call arrived!')
time.sleep(1)
count += 1
simulation(1,2,13)
,
您可以具有三个单独的并行进程。 1-进行销售呼叫的一种方法。 2-进行服务呼叫的一个过程。 3-一种处理呼叫的过程。
import simpy
import random
sim_end = 1000;
def generateSalesCall(env,call_pipe):
while env.now < sim_end:
# put call in the pipe
yield call_pipe.put("sales");
interval = random.expovariate(1/3);
yield env.timeout(interval);
def generateServiceCall(env,call_pipe):
while env.now < sim_end:
# put call in the pipe
yield call_pipe.put("service");
interval = random.triangular(0,6,0);
yield env.timeout(interval);
def handleCalls(env,call_pipe):
while(True):
call = yield call_pipe.get();
if call == "sales":
print(env.now,"sales call");
elif call == "service":
print(env.now,"service call");
env = simpy.Environment();
call_pipe = simpy.Store(env);
env.process(generateSalesCall(env,call_pipe));
env.process(generateServiceCall(env,call_pipe));
env.process(handleCalls(env,call_pipe));
env.run();