import simpy import random import pandas as pd waiting_times = [] def source(env, number, interval, counter): """Gera clientes aleatoriamente""" for i in range(number): c = customer(env, f'Cliente {i}', counter, time_in_bank=30.0) env.process(c) t = random.expovariate(1.0 / interval) yield env.timeout(t) def customer(env, name, counter, time_in_bank): """O cliente chega, é atendido e depois sai.""" arrive = env.now with counter.request() as req: yield req wait = env.now - arrive waiting_times.append(wait) tib = random.expovariate(1.0 / time_in_bank) yield env.timeout(tib) def run_simulation(): env = simpy.Environment() counter = simpy.Resource(env, capacity=5) env.process(source(env, int(6*60/7), 7, counter)) env.run() def print_progress_bar(iteration, total, prefix='', suffix='', length=50, fill='█'): percent = ("{0:.1f}").format(100 * (iteration / float(total))) filledLength = int(length * iteration // total) bar = fill * filledLength + '-' * (length - filledLength) print(f'\r{prefix} |{bar}| {percent}% {suffix}', end='\r') if iteration == total: print() def main(): repetitions = 100000 results = [] for i in range(repetitions): global waiting_times waiting_times = [] run_simulation() total_clients = len(waiting_times) avg_wait = sum(waiting_times) / total_clients if waiting_times else 0 max_wait = max(waiting_times) if waiting_times else 0 avg_queue = sum(waiting_times) / (6*60) if waiting_times else 0 service_level = len([w for w in waiting_times if w > 15]) / total_clients if waiting_times else 0 results.append({ 'Total de Clientes': total_clients, 'Tempo Médio de Espera': avg_wait, 'Tempo Máximo de Espera': max_wait, 'Clientes Médios na Espera': avg_queue, 'Nível de Serviço > 15min': service_level }) print_progress_bar(i + 1, repetitions, prefix='Progress:', suffix='Complete', length=50) save_to_excel(results) def save_to_excel(results): df = pd.DataFrame(results) df.to_excel("resultados.xlsx", index=False) if __name__ == "__main__": main()