defmg1_queue(arrival_rate, service_mean, service_variance):
"""
M/G/1 queue using Pollaczek-Khinchin formula
- General service time distribution
"""
lambda_ = arrival_rate
Es = service_mean
Var_s = service_variance
# Second moment of service time
Es2 = Var_s + Es**2
rho = lambda_ * Es
if rho >= 1:
return {"error": "System unstable (rho >= 1)"}
# Pollaczek-Khinchin formula
Lq = (lambda_**2 * Es2) / (2 * (1 - rho))
L = Lq + rho
Wq = Lq / lambda_
W = Wq + Es
return {
"model": "M/G/1",
"arrival_rate": lambda_,
"service_mean": Es,
"service_variance": Var_s,
"utilization": rho,
"L": L,
"Lq": Lq,
"W": W,
"Wq": Wq,
"stable": rho < 1
}
4. Erlang C for Call Center Staffing
deferlang_c_staffing(arrival_rate, service_rate, target_service_level,
target_wait_time):
"""
Determine minimum servers for service level target
"""
lambda_ = arrival_rate
mu = service_rate
# Minimum servers for stability
min_servers = int(np.ceil(lambda_ / mu))
for c inrange(min_servers, min_servers + 100):
result = mmc_queue(lambda_, mu, c)
if result.get('error'):
continue# Service level: P(wait <= target)# SL = 1 - C * exp(-(c*mu - lambda) * target_wait)
C = result['P_wait']
exp_term = np.exp(-(c * mu - lambda_) * target_wait_time)
service_level = 1 - C * exp_term
if service_level >= target_service_level:
return {
"recommended_servers": c,
"achieved_service_level": service_level,
"target_service_level": target_service_level,
"P_wait": C,
"utilization": result['utilization'],
"avg_wait": result['Wq']
}
return {"error": "Could not achieve target service level"}
5. Finite Population (M/M/c/K/K)
deffinite_population_queue(arrival_rate, service_rate, num_servers,
population_size):
"""
Finite population queue (machine repair model)
"""
lambda_ = arrival_rate # Per-customer arrival rate
mu = service_rate
c = num_servers
K = population_size
# State probabilities using recursion
P = np.zeros(K + 1)
P[0] = 1# Temporaryfor n inrange(1, K + 1):
if n <= c:
P[n] = P[n-1] * (K - n + 1) * lambda_ / (n * mu)
else:
P[n] = P[n-1] * (K - n + 1) * lambda_ / (c * mu)
# Normalize
P = P / P.sum()
# Performance measures
L = sum(n * P[n] for n inrange(K + 1))
Lq = sum((n - c) * P[n] for n inrange(c + 1, K + 1))
# Effective arrival rate
lambda_eff = sum((K - n) * lambda_ * P[n] for n inrange(K))
W = L / lambda_eff if lambda_eff > 0else0
Wq = Lq / lambda_eff if lambda_eff > 0else0return {
"model": "M/M/c/K/K",
"servers": c,
"population": K,
"L": L,
"Lq": Lq,
"W": W,
"Wq": Wq,
"effective_arrival_rate": lambda_eff,
"state_probabilities": P.tolist()
}
6. Network of Queues (Jackson Network)
defjackson_network(arrival_rates, service_rates, routing_matrix):
"""
Open Jackson network analysis
arrival_rates: external arrivals to each node
service_rates: service rate at each node
routing_matrix: probability of routing from i to j
"""
n_nodes = len(service_rates)
# Solve for effective arrival rates# lambda_i = gamma_i + sum_j(lambda_j * r_ji)
R = np.array(routing_matrix)
gamma = np.array(arrival_rates)
# lambda = gamma + lambda * R => lambda = gamma * (I - R)^-1
I = np.eye(n_nodes)
lambdas = np.linalg.solve((I - R.T), gamma)
# Analyze each queue as M/M/1
results = []
for i inrange(n_nodes):
result = mm1_queue(lambdas[i], service_rates[i])
result['node'] = i
result['effective_arrival_rate'] = lambdas[i]
results.append(result)
# Network totals
L_total = sum(r['L'] for r in results if'L'in r)
return {
"model": "Jackson_Network",
"effective_arrival_rates": lambdas.tolist(),
"node_results": results,
"total_L": L_total
}
Process Integration
This skill integrates with the following processes: