def q_heterogeneous():
    sim_time = 1000000  #Job required to get aprox good V2 matrix
    list_of_servers = init_servers(2, [1, 1], [3, 1])
    list_of_dispatchers = init_dispatchers(
        1, 3, list_of_servers,
        [[12, 4], [], [], sim_time
         ])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(
        3
    )  # Poisson arrivals with arrival rate 3: Load 75% because we have 4 consumptions per time unit.
    init_first_jobs(world, list_of_dispatchers, p_arrivals)
    #Main loop

    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (
                x * 0.01
        ):  # 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))
        #world._stats.write_to_file_jobs()
        print("{}%".format(x))

    v2 = list_of_dispatchers[0]._policy.v2
    with open('./data_results/Spring_paper_figures/v2matrix_500k_jobs.txt',
              'a') as f:
        for key in v2:
            f.write(str(key) + ': ' + str(v2[key]) + '\n')
        f.write('\n')
    world._stats.print_stats()
def q_testing_id(id):
    sim_time = 2000000
    list_of_servers = init_servers(2, 1, 1)  # 2 servers using FIFO and mui = 1
    list_of_dispatchers = init_dispatchers(
        1, 3, list_of_servers,
        [[4, 4], [], [], sim_time
         ])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(1)  # Poisson arrivals with 1 arrival rate.
    init_first_jobs(world, list_of_dispatchers, p_arrivals)
    #Main loop

    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (
                x * 0.01
        ):  # 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))
        #world._stats.write_to_file_jobs()
        print("thread id: {}, finised {}%".format(id, x))
        if (x % 5 == 0):
            print_matrices_q(list_of_dispatchers, x)
            v2 = list_of_dispatchers[0]._policy.v2
            with open('./TDlearn/2Mjobs_V2_per5_RND_' + str(id) + '.txt',
                      'a') as f:
                for key in v2:
                    f.write(str(key) + ': ' + str(v2[key]) + '\n')
                f.write('\n')
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(no_of_servers, server_scheduler, no_of_dispatchers,
                     dispatcher_policy, arrival_rate, job_distribution,
                     sim_time):
    list_of_servers = init_servers(no_of_servers, server_scheduler,
                                   job_distribution)
    list_of_dispatchers = init_dispatchers(no_of_dispatchers,
                                           dispatcher_policy, list_of_servers,
                                           [])
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(float(arrival_rate) / len(list_of_dispatchers))
    init_first_jobs(world, list_of_dispatchers, p_arrivals)
    #Main loop
    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (
                x * 0.01
        ):  # 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))
        world._stats.write_to_file_jobs()
        print("{}%".format(x))

    total_no_jobs = world._stats.number_of_jobs
    print(total_no_jobs)
    world._stats.print_stats()
    save_stats(world)  #Ask user if he wants to save stats to file
def start_simulation_state(no_of_jobs_server_1, no_of_jobs_server_2,
                           arrival_rate, job_distribution, given_x, given_y,
                           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))

    def reset_world(first_arrival):
        nonlocal list_of_servers, dispatcher, statistics, world  # if we want to modify variable from closure, must place as nonlocal
        list_of_servers.clear()  #clear the 2 servers
        list_of_servers = init_servers(2, scheduler, job_distribution)
        dispatcher = Dispatcher(
            policy_i,
            list_of_servers)  # resetting the dispatcher with new servers
        statistics = Statistics()
        world = Global(statistics)
        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)
        params = [dispatcher, world]
        world.schedule_event(p_arrivals.generate_arrival, first_arrival,
                             params)  #Schedule first arrival to start chain

    reset_world(0)  # Call function to setup our world

    # Now we need to schedule the initial arrivals to start the chain of events.
    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..
            if list_of_servers[0]._total_jobs > int(
                    given_x) or list_of_servers[1]._total_jobs > int(given_y):
                next_arrival_new_world = world.next_event(
                )  #Get the time for the first event for new world
                world._stats.write_to_file_intermediate_stats('given_end_of_' +
                                                              given_x +
                                                              '_and_' +
                                                              given_y)
                reset_world(
                    next_arrival_new_world
                )  # This function should reset statistics, world event queue, and server states
            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))
def q_testing():
    sim_time = 100000
    list_of_servers = init_servers(
        2, [1, 1], [1, 1])  # 2 servers, both using FIFO and mui = 1
    list_of_dispatchers = init_dispatchers(
        1, 3, list_of_servers,
        [[4, 4], [], [], sim_time
         ])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(1)  # Poisson arrivals with 1 arrival rate.
    init_first_jobs(world, list_of_dispatchers, p_arrivals)
    #Main loop

    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (
                x * 0.01
        ):  # 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))
        #world._stats.write_to_file_jobs()
        print("{}%".format(x))

    total_no_jobs = world._stats.number_of_jobs
    print(total_no_jobs)
    print('The Q: ' + str(list_of_dispatchers[0]._policy.Q))
    v = list_of_dispatchers[0]._policy.v
    v2 = list_of_dispatchers[0]._policy.v2
    print('mean cost rate r: ' + str(list_of_dispatchers[0]._policy.r))
    print('The v:')
    i = 0
    for key in v:
        i += 1
        print(str(key) + ': ' + str(v[key]) + '  ')
        if not i % 5:
            print('\n')
    print('The v2:')
    i = 0
    for key in v2:
        i += 1
        print(str(key) + ': ' + str(v2[key]) + '\n')
        if not i % 5:
            print('\n')
    world._stats.print_stats()
    save_stats(world)  #Ask user if he wants to save stats to file
def esa_3_server(arr_rate, job_through, no_servers, file_name):
    list_of_servers = init_servers(no_servers, 1, 1)
    list_of_dispatchers = init_dispatchers(1, 2, list_of_servers, [])
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(float(arr_rate))
    init_first_jobs(world, list_of_dispatchers, p_arrivals)
    #Main loop
    while world.next_event(
    ):  # while the virtual time of next event is less than our simulation time..
        if world._stats.number_of_jobs > job_through:
            data = world._stats.get_mean_sd_sojourn()  #format of 'mean,sd'
            with open(
                    './Simulation_results/esa_' + str(no_servers) +
                    '_subfile_' + str(file_name) + '_server_test.txt',
                    'a') as myfile:
                myfile.write(data + "\n")
            break
        world.process_event(
        )  # We take the event and process it (running the function(s))
def q_testing_id(id):
    sim_time = 2000000
    list_of_servers = simutil.init_servers(2, [1, 1], [3, 1])  # 2 servers using FIFO and mui = 3, and 1
    list_of_dispatchers = simutil.init_dispatchers(1, [3], [list_of_servers], [[4, 4], [], [], sim_time])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    job_sizes = Expo(2)  # Lambda = 2,  rho will be lambda / (3+1) = 0.5
    p_arrivals = PoissonArrival(1, job_sizes)  # Poisson arrivals with 1 arrival rate.
    simutil.init_first_jobs(world, list_of_dispatchers, p_arrivals)
    # Main loop

    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (x * 0.01):  # 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))
        # world._stats.write_to_file_jobs()
        print("thread id: {}, finised {}%".format(id, x))
        if(x % 5 == 0):
            v2 = list_of_dispatchers[0]._policy.v2
            with open('../data_results/TD_learn_server_utilization/2Mjobs_TD_alpha_decay_rho_' + str(id) + '.txt', 'a') as f:
                for key in v2:
                    f.write(str(key) + ': ' + str(v2[key]) + '\n')
                f.write('\n')
def td_learn_server_utilization(rho, round_nr):
    sim_time = 5000000
    list_of_servers = simutil.init_servers(2, [1, 1], [3, 1])  # 2 servers using FIFO and mui = 3, and 1
    list_of_dispatchers = simutil.init_dispatchers(1, [3], [list_of_servers], [[12, 4], [], [], sim_time])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    # arrival_rate = 2  # get the arrival rate by using given rho
    job_sizes = Expo(1)  # Lambda = 2,  rho will be lambda / (3+1) = 0.5
    p_arrivals = PoissonArrival(3.92, job_sizes)  # Poisson arrivals with 1 arrival rate.
    simutil.init_first_jobs(world, list_of_dispatchers, p_arrivals)
    # Main loop

    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (x * 0.01):  # 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))
        # world._stats.write_to_file_jobs()
        print("round: {}, rho: {}, finised {}%".format(round_nr, rho, x))

    v2 = list_of_dispatchers[0]._policy.v2
    with open('../data_results/TD_learn_server_utilization/500kjobs_SED_rho_' + str(rho) + '-3.txt', 'a') as f:
        for key in v2:
            f.write(str(key) + ': ' + str(v2[key]) + '\n')
        f.write('\n')
def q_testing_frac_1per():
    sim_time = 20000
    list_of_servers = init_servers(2, 1, 1)  # 2 servers using FIFO and mui = 1
    list_of_dispatchers = init_dispatchers(
        1, 3, list_of_servers,
        [[4, 4], [], [], sim_time
         ])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    p_arrivals = PoissonArrival(1)  # Poisson arrivals with 1 arrival rate.
    init_first_jobs(world, list_of_dispatchers, p_arrivals)
    #Main loop
    all_v2 = []
    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (
                x * 0.01
        ):  # 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))
        all_v2.append(list_of_dispatchers[0]._policy.v2.copy()
                      )  #Want only the copy at this time

    return all_v2
Esempio n. 12
0
def TD_testing(st,
               servers,
               dispatchers,
               arrival_rate,
               job_size_rate,
               job_distribution,
               qlearn,
               arrivals_seed=None,
               policyseed=None,
               file_name=None,
               td_matrix=None):
    sim_time = st
    list_of_servers = simutil.init_servers(
        len(servers[0]), servers[0],
        servers[1])  # List of servers for 1 dispatcher
    servers_for_dispatchers = []
    servers_for_dispatchers.append(list_of_servers)
    list_of_dispatchers = simutil.init_dispatchers(
        dispatchers[0],
        dispatchers[1],
        servers_for_dispatchers,
        qlearn,
        policyseed,
        td_matrix_and_backup=[
            td_matrix, SED(policyseed)
        ])  # 1 dispatcher, Qlearn, 5x5 box, nothing excluded.
    statistics = Statistics()
    world = Global(statistics)
    if job_distribution == 1:
        job_sizes = Expo(job_size_rate)
    if file_name is not None:
        print("file name used")
        f = open(file_name, 'r')
        data = f.read().splitlines()
        sim_time = int(data[-1])
        print(sim_time)
        data2 = [
            tuple(float(x) for x in item.split(',')) for item in data[:-1]
        ]
        p_arrivals = PoissonArrival(
            arrival_rate, job_sizes, arrivals_seed,
            file=data2)  # Poisson arrivals with arrival rate 1.
    else:
        p_arrivals = PoissonArrival(
            arrival_rate, job_sizes,
            arrivals_seed)  # Poisson arrivals with arrival rate 1.

    simutil.init_first_jobs(world, list_of_dispatchers, p_arrivals)
    # Main loop
    start = timer()
    for x in range(1, 101):
        while world.next_event() <= float(sim_time) * (
                x * 0.01
        ):  # 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))
    end = timer()
    print("Total time: {}".format(end - start))
    if dispatchers[1] == 3:
        print('The state counter: ' +
              str(list_of_dispatchers[0]._policy.state_counter))
    total_no_jobs = world._stats.number_of_jobs
    print(total_no_jobs)
    # simutil.print_step_matrix(list_of_dispatchers[0])
    world._stats.print_stats()
    world._stats.save_run(sim_time)
    simutil.save_stats(world)  # Ask user if he wants to save stats to file