def start_simulation_sim_time(no_of_jobs_server_1, no_of_jobs_server_2,
                              arrival_rate, job_distribution, sim_time,
                              scheduler):
    list_of_servers = init_servers(2, scheduler, job_distribution)
    policy_i = ShortestQueue()  #Join shortest queue policy
    dispatcher = Dispatcher(
        policy_i, list_of_servers)  #Create a dispatcher with JSQ policy
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(float(arrival_rate))

    init_server_to_state(no_of_jobs_server_1, list_of_servers[0], scheduler,
                         world)
    init_server_to_state(no_of_jobs_server_2, list_of_servers[1], scheduler,
                         world)
    init_first_jobs(world, [dispatcher], p_arrivals)

    world.number_of_arr_dep = 0  #resetting the number of events before we start
    for x in range(1, 11):
        # Now that each dispatcher has an arrival, we can start looping through events
        while world.next_event() <= float(sim_time) * (
                x * 0.1
        ):  # while the virtual time of next event is less than our simulation time..
            world.process_event(
            )  # We take the event and process it (running the function(s))
        print("{}%".format(x * 10))
    #for loop to step between while loops (every 10%)while world.next

    total_no_jobs = world._stats.number_of_jobs
    print(total_no_jobs)
    world._stats.print_stats()
    save_stats(world)
def start_simulation_less_than_n(no_of_jobs_server_1, no_of_jobs_server_2,
                                 arrival_rate, job_distribution, sim_time,
                                 scheduler):
    list_of_servers = init_servers(3, scheduler, job_distribution)
    global jobs_ran
    global final_data
    stopping_n = no_of_jobs_server_2
    # Create dispatcher
    policy_i = ShortestQueue()  #Join shortest queue policy
    dispatcher = Dispatcher(
        policy_i, list_of_servers)  #Create a dispatcher with JSQ policy
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(float(arrival_rate))
    arrival = 0

    init_server_to_state(no_of_jobs_server_1, list_of_servers[0], scheduler,
                         world)
    init_server_to_state(no_of_jobs_server_2, list_of_servers[1], scheduler,
                         world)
    init_server_to_state(no_of_jobs_server_2, list_of_servers[2], scheduler,
                         world)

    initial_arrival = random.expovariate(p_arrivals._rate)
    params = [dispatcher, world]
    world.schedule_event(p_arrivals.generate_arrival, initial_arrival,
                         params)  #Schedule first arrival to start chain
    last_event = 0
    world.number_of_arr_dep = 0  #resetting the number of events before we start
    # Now we need to schedule the initial arrivals to start the chain of events.
    # Now that each dispatcher has an arrival, we can start looping through events
    while world.next_event() <= float(
            sim_time
    ):  # while the virtual time of next event is less than our simulation time..
        if (list_of_servers[0]._total_jobs <= stopping_n
                and list_of_servers[1]._total_jobs <= stopping_n
                and list_of_servers[2]._total_jobs <= stopping_n):
            break
        last_event = world.next_event()
        world.process_event(
        )  # We take the event and process it (running the function(s))
    #for loop to step between while loops (every 10%)while world.next
    #We've reached a stopping state. Record event parameters and print to file
    jobs_ran += world._stats.number_of_jobs  # We stopped, we add the number of jobs ran this time to global variable
    recorded_x = list_of_servers[0]._total_jobs
    recorded_y = list_of_servers[1]._total_jobs
    recorded_T = last_event  #Last event that happened (e.g. departure that caused the total jobs to be < 4)
    recorded_N = world.number_of_arr_dep  #Get the number of events that happened'
    final_data.append((recorded_x, recorded_y, recorded_T, recorded_N))