Пример #1
0
def grid_param_simple_exp(delay: int, opt_method: OptMethod, metric: str,
                          lamb1_range, lamb2_range, rate1_range,
                          rate2_range) -> dict:
    """Choose parameters along a grid."""
    total_iterations = len(lamb1_range) * len(lamb2_range) * len(
        rate1_range) * len(rate2_range)

    param_array = np.empty([total_iterations, 4])
    res_array = np.empty([total_iterations, 2])

    i = 0
    for lamb1 in lamb1_range:
        for lamb2 in lamb2_range:
            for rate1 in rate1_range:
                for rate2 in rate2_range:
                    delay_prob = PerformParameter(
                        perform_metric=PerformEnum.DELAY_PROB, value=delay)

                    setting = FatCrossPerform(
                        arr_list=[DM1(lamb=lamb1),
                                  DM1(lamb=lamb2)],
                        ser_list=[
                            ConstantRate(rate=rate1),
                            ConstantRate(rate=rate2)
                        ],
                        perform_param=delay_prob)
                    param_array[i, 0] = lamb1
                    param_array[i, 1] = lamb2
                    param_array[i, 2] = rate1
                    param_array[i, 3] = rate2

                    # bound, new_bound
                    res_array[i, 0], res_array[i, 1] = compute_improvement(
                        setting=setting, opt_method=opt_method, number_l=1)

                    # This might be a very dangerous condition
                    if (res_array[i, 1] >= 1 or res_array[i, 0] == nan
                            or res_array[i, 1] == nan):
                        res_array[i, ] = nan

                    if i % floor(total_iterations / 10) == 0:
                        print("iteration {0} of {1}".format(
                            i, total_iterations))

                    i += 1

    return two_col_array_to_results(arrival_enum=ArrivalEnum.DM1,
                                    metric=metric,
                                    param_array=param_array,
                                    res_array=res_array,
                                    number_servers=2)
Пример #2
0
def csv_fat_cross_perform(foi_arrival: ArrivalDistribution,
                          cross_arrival: ArrivalDistribution,
                          foi_service: ConstantRate,
                          cross_service: ConstantRate, number_servers: int,
                          perform_param_list: PerformParamList,
                          opt_method: OptMethod) -> pd.DataFrame:
    """Write dataframe results into a csv file.

    Args:
        foi_arrival: flow of interest's arrival distribution
        cross_arrival: Distribution of cross arrivals
        foi_service: service of the server at the foi
        cross_service: service of remaining servers
        number_servers: number of servers in fat tree
        perform_param_list: list of performance parameter values
        opt_method: optimization method

    Returns:
        csv file

    """
    if number_servers == 2:
        filename = "simple_setting_{0}".format(perform_param_list.to_name())
    else:
        filename = "fat_cross_{0}".format(perform_param_list.to_name())

    arr_list: List[ArrivalDistribution] = [foi_arrival]
    ser_list: List[ConstantRate] = [foi_service]

    for _i in range(number_servers - 1):
        arr_list.append(cross_arrival)
        ser_list.append(cross_service)

    data_frame = fat_cross_df(arr_list=arr_list,
                              ser_list=ser_list,
                              opt_method=opt_method,
                              perform_param_list=perform_param_list)

    filename += "_" + foi_arrival.to_name() + "_" + foi_arrival.to_value(
        number=1, show_n=False) + "_" + foi_service.to_value(
            number=1) + "_" + cross_arrival.to_value(
                number=2,
                show_n=False) + "_" + cross_service.to_value(number=2)

    data_frame.to_csv(filename + '.csv',
                      index=True,
                      quoting=csv.QUOTE_NONNUMERIC)

    return data_frame
Пример #3
0
def mc_time_single(arrival_enum: ArrivalEnum, perform_param: PerformParameter,
                   opt_method: OptMethod, mc_dist: MonteCarloDist) -> dict:
    """Chooses parameters by Monte Carlo type random choice"""
    total_iterations = 10**4

    time_ratio = {"Number_of_servers": "Ratio"}

    # 1 Parameter for service
    size_array = [total_iterations, arrival_enum.number_parameters() + 1]
    # [rows, columns]

    param_array = mc_enum_to_dist(mc_dist=mc_dist, size=size_array)

    time_array = np.empty([total_iterations, 2])

    for i in range(total_iterations):
        if arrival_enum == ArrivalEnum.DM1:
            setting = SingleServerPerform(
                arr=DM1(lamb=param_array[i, 0]),
                const_rate=ConstantRate(rate=param_array[i, 1]),
                perform_param=perform_param)

        elif arrival_enum == ArrivalEnum.MMOO:
            setting = SingleServerPerform(
                arr=MMOOFluid(mu=param_array[i, 0],
                              lamb=param_array[i, 1],
                              burst=param_array[i, 2]),
                const_rate=ConstantRate(rate=param_array[i, 3]),
                perform_param=perform_param)

        else:
            raise NameError(
                f"Arrival parameter {arrival_enum.name} is infeasible")

        # time_standard, time_lyapunov = compute_overhead()
        time_array[i,
                   0], time_array[i,
                                  1] = compute_overhead(setting=setting,
                                                        opt_method=opt_method,
                                                        number_l=1)

        if i % floor(total_iterations / 10) == 0:
            print(f"iteration {i} of {total_iterations}")

    return time_array_to_results(arrival_enum=arrival_enum,
                                 time_array=time_array,
                                 number_servers=1,
                                 time_ratio=time_ratio)
Пример #4
0
def single_hop_comparison(
        aggregation: int, sigma_single: float, rho_single: float,
        service_rate: float, perform_param: PerformParameter,
        opt_method: OptMethod) -> (float, float, float, float, float):

    print_x = False
    constant_rate_server = ConstantRate(service_rate)

    tb_const = TokenBucketConstant(sigma_single=sigma_single,
                                   rho_single=rho_single,
                                   n=aggregation)

    dnc_fifo_single: float = fifo_delay(token_bucket_constant=tb_const,
                                        constant_rate=constant_rate_server)

    const_single = SingleServerPerform(arr=tb_const,
                                       const_rate=constant_rate_server,
                                       perform_param=perform_param)

    leaky_mass_1 = SingleServerPerform(arr=LeakyBucketMassOne(
        sigma_single=sigma_single, rho_single=rho_single, n=aggregation),
                                       const_rate=constant_rate_server,
                                       perform_param=perform_param)

    leaky_mass_2 = SingleServerPerform(arr=LeakyBucketMassTwo(
        sigma_single=sigma_single, rho_single=rho_single, n=aggregation),
                                       const_rate=constant_rate_server,
                                       perform_param=perform_param)

    exact_mass_2 = SingleServerPerform(arr=LeakyBucketMassTwoExact(
        sigma_single=sigma_single, rho_single=rho_single, n=aggregation),
                                       const_rate=constant_rate_server,
                                       perform_param=perform_param)

    bound_list = [(0.05, 15.0)]
    delta = 0.05

    if opt_method == OptMethod.GRID_SEARCH:
        const_opt = Optimize(setting=const_single,
                             print_x=print_x).grid_search(
                                 bound_list=bound_list, delta=delta)

        leaky_mass_1_opt = Optimize(setting=leaky_mass_1,
                                    print_x=print_x).grid_search(
                                        bound_list=bound_list, delta=delta)

        leaky_mass_2_opt = Optimize(setting=leaky_mass_2,
                                    print_x=print_x).grid_search(
                                        bound_list=bound_list, delta=delta)

        exact_mass_2_opt = Optimize(setting=exact_mass_2,
                                    print_x=print_x).grid_search(
                                        bound_list=bound_list, delta=delta)

    else:
        raise NameError("Optimization parameter {0} is infeasible".format(
            opt_method.name))

    return (dnc_fifo_single, const_opt, leaky_mass_1_opt, leaky_mass_2_opt,
            exact_mass_2_opt)
def csv_single_perform(arrival: ArrivalDistribution, service: ConstantRate,
                       perform_param_list: PerformParamList,
                       opt_method: OptMethod) -> pd.DataFrame:
    """Writes dataframe results into a csv file

    Args:
        arrival: flow of interest's arrival distribution
        service: service of the server at the foi
        perform_param_list: list of performance parameter values
        opt_method: optimization method

    Returns:
        csv file
    """

    filename = "single_{0}".format(perform_param_list.to_name())

    data_frame = single_server_df(arr1=arrival,
                                  ser1=service,
                                  opt_method=opt_method,
                                  perform_param_list=perform_param_list)

    filename += "_" + arrival.to_name() + "_" + arrival.to_value(
        number=1, show_n=False) + "_" + service.to_value(number=1)

    data_frame.to_csv(filename + '.csv',
                      index=True,
                      quoting=csv.QUOTE_NONNUMERIC)

    return data_frame
def csv_tandem_compare_perform(
        foi_arrival: ArrivalDistribution, cross_arrival: ArrivalDistribution,
        foi_arrival2: ArrivalDistribution, cross_arrival2: ArrivalDistribution,
        rate: float, number_servers: int, perform_param_list: PerformParamList,
        opt_method: OptMethod, nc_analysis: NCAnalysis) -> pd.DataFrame:
    """Write dataframe results into a csv file.

    Args:
        foi_arrival: flow of interest's arrival distribution
        foi_arrival2: competitor's flow of interest's arrival distribution
        cross_arrival: distribution of cross arrivals
        cross_arrival2: competitor's distribution of cross arrivals
        rate: service rate of servers
        number_servers: number of servers in tandem
        perform_param_list: list of performance parameter values
        opt_method: optimization method
        nc_analysis: Network Calculus analysis type

    Returns:
        csv file

    """
    bounds = [0.0] * len(perform_param_list.values_list)
    bounds2 = [0.0] * len(perform_param_list.values_list)

    filename = "tandem_{0}".format(perform_param_list.to_name())

    arr_list: List[ArrivalDistribution] = [foi_arrival]
    arr_list2: List[ArrivalDistribution] = [foi_arrival2]
    ser_list: List[ConstantRate] = []

    for _i in range(number_servers):
        arr_list.append(cross_arrival)
        arr_list2.append(cross_arrival2)
        ser_list.append(ConstantRate(rate=rate))

    for _i in range(len(perform_param_list.values_list)):
        bounds[_i], bounds2[_i] = tandem_compare(
            arr_list=arr_list,
            arr_list2=arr_list2,
            ser_list=ser_list,
            opt_method=opt_method,
            perform_param=perform_param_list.get_parameter_at_i(_i),
            nc_analysis=nc_analysis)

    results_df = pd.DataFrame({
        "bounds": bounds,
        "bounds2": bounds2
    },
                              index=perform_param_list.values_list)
    results_df = results_df[["bounds", "bounds2"]]

    filename += "_" + str(number_servers) + "servers_" + foi_arrival.to_value(
    ) + "_rate=" + str(rate)

    results_df.to_csv(
        filename + '.csv', index=True, quoting=csv.QUOTE_NONNUMERIC)

    return results_df
def single_hop_contour(sigma_single: float,
                       rho_single: float,
                       utilization: float,
                       perform_param: PerformParameter,
                       pure_snc=False) -> int:
    print_x = False
    show_warn = False

    bound_list = [(0.05, 15.0)]
    delta = 0.05

    aggregation = 1

    # util = n * rho / service => service = n * rho / util

    constant_rate_server = ConstantRate(aggregation * rho_single / utilization)

    tb_const = TokenBucketConstant(sigma_single=sigma_single,
                                   rho_single=rho_single,
                                   n=aggregation)

    if pure_snc:
        competitor = Optimize(
            setting=SingleServerPerform(arr=tb_const,
                                        const_rate=constant_rate_server,
                                        perform_param=perform_param),
            print_x=print_x,
            show_warn=show_warn).grid_search(bound_list=bound_list,
                                             delta=delta)

    else:
        competitor = fifo_delay(token_bucket_constant=tb_const,
                                constant_rate=constant_rate_server)

    leaky_mass_1_opt = Optimize(setting=SingleServerPerform(
        arr=LeakyBucketMassOne(sigma_single=sigma_single,
                               rho_single=rho_single,
                               n=aggregation),
        const_rate=constant_rate_server,
        perform_param=perform_param),
                                print_x=print_x,
                                show_warn=show_warn).grid_search(
                                    bound_list=bound_list, delta=delta)

    while competitor < leaky_mass_1_opt:
        aggregation += 1

        constant_rate_server = ConstantRate(aggregation * rho_single /
                                            utilization)

        tb_const = TokenBucketConstant(sigma_single=sigma_single,
                                       rho_single=rho_single,
                                       n=aggregation)

        if pure_snc:
            competitor = Optimize(
                setting=SingleServerPerform(arr=tb_const,
                                            const_rate=constant_rate_server,
                                            perform_param=perform_param),
                print_x=print_x,
                show_warn=show_warn).grid_search(bound_list=bound_list,
                                                 delta=delta)

        else:
            competitor = fifo_delay(token_bucket_constant=tb_const,
                                    constant_rate=constant_rate_server)

        leaky_mass_1_opt = Optimize(setting=SingleServerPerform(
            arr=LeakyBucketMassOne(sigma_single=sigma_single,
                                   rho_single=rho_single,
                                   n=aggregation),
            const_rate=constant_rate_server,
            perform_param=perform_param),
                                    print_x=print_x,
                                    show_warn=show_warn).grid_search(
                                        bound_list=bound_list, delta=delta)

    # print("(dnc_fifo_single, const_opt, leaky_mass_1_opt)")
    # print(dnc_fifo_single, const_opt, leaky_mass_1_opt)

    return aggregation
Пример #8
0
from nc_arrivals.markov_modulated import MMOODisc, MMOOFluid
from nc_operations.perform_enum import PerformEnum
from nc_service.constant_rate_server import ConstantRate
from optimization.optimize import Optimize
from single_server.single_server_perform import SingleServerPerform
from utils.perform_parameter import PerformParameter

if __name__ == '__main__':
    DELAY_PROB8 = PerformParameter(perform_metric=PerformEnum.DELAY_PROB,
                                   value=8)

    SINGLE_SERVER = SingleServerPerform(arr=MMOOFluid(mu=0.5,
                                                      lamb=0.5,
                                                      burst=2.0),
                                        const_rate=ConstantRate(rate=1.5),
                                        perform_param=DELAY_PROB8)

    print(
        Optimize(SINGLE_SERVER, print_x=True,
                 show_warn=True).grid_search_old(bound_list=[(0.1, 5.0)],
                                                 delta=0.01))

    print(
        Optimize(SINGLE_SERVER, print_x=True,
                 show_warn=True).grid_search(bound_list=[(0.1, 5.0)],
                                             delta=0.01))

    SINGLE_SERVER2 = SingleServerPerform(arr=MMOODisc(stay_on=0.5,
                                                      stay_off=0.5,
                                                      burst=2.0),
Пример #9
0
def csv_single_param_exp_lower(start_time: int,
                               perform_param: PerformParameter,
                               mc_dist: MonteCarloDist,
                               sample=False) -> dict:
    total_iterations = 10**2
    valid_iterations = total_iterations
    metric = "relative"
    sample_size = 10**3

    delta = 0.05

    size_array = [total_iterations, 2]
    # [rows, columns]

    if mc_dist.mc_enum == MCEnum.UNIFORM:
        param_array = np.random.uniform(
            low=0, high=mc_dist.param_list[0], size=size_array)
    elif mc_dist.mc_enum == MCEnum.EXPONENTIAL:
        param_array = np.random.exponential(
            scale=mc_dist.param_list[0], size=size_array)
    else:
        raise NameError("Distribution parameter {0} is infeasible".format(
            mc_dist.mc_enum))

    res_array = np.empty([total_iterations, 3])
    if sample:
        res_array_sample = np.empty([total_iterations, 3])

    for i in tqdm(range(total_iterations)):
        setting = SingleServerPerform(
            arr=DM1(lamb=param_array[i, 0]),
            const_rate=ConstantRate(rate=param_array[i, 1]),
            perform_param=perform_param)

        theta_bounds = [(0.1, 4.0)]
        bound_array = theta_bounds[:]

        res_array[i, 0] = Optimize(setting=setting).grid_search(
            bound_list=bound_array, delta=delta)

        bound_array_power = theta_bounds[:]
        bound_array_power.append((0.9, 4.0))

        res_array[i, 1] = OptimizeNew(setting_new=setting).grid_search(
            bound_list=bound_array_power, delta=delta)

        if perform_param.perform_metric == PerformEnum.DELAY_PROB:
            res_array[i, 2] = delay_prob_lower_exp_dm1_opt(
                t=start_time,
                delay=perform_param.value,
                lamb=param_array[i, 0],
                rate=param_array[i, 1])

            if sample:
                res_array_sample[i, 0] = res_array[i, 0]
                res_array_sample[i, 1] = res_array[i, 1]
                res_array_sample[i, 2] = delay_prob_sample_exp_dm1_opt(
                    t=start_time,
                    delay=perform_param.value,
                    lamb=param_array[i, 0],
                    rate=param_array[i, 1],
                    sample_size=sample_size)

            if res_array[i, 0] > 1.0:
                res_array[i, ] = nan
                if sample:
                    res_array_sample[i, ] = nan

        elif perform_param.perform_metric == PerformEnum.OUTPUT:
            res_array[i, 2] = output_lower_exp_dm1_opt(
                s=start_time,
                delta_time=perform_param.value,
                lamb=param_array[i, 0],
                rate=param_array[i, 1])

        else:
            raise NameError("{0} is an infeasible performance metric".format(
                perform_param.perform_metric))

        if (res_array[i, 1] == inf or res_array[i, 2] == inf
                or res_array[i, 0] == nan or res_array[i, 1] == nan
                or res_array[i, 2] == nan):
            res_array[i, ] = nan
            res_array_sample[i, ] = nan
            valid_iterations -= 1

    # print("exponential results", res_array[:, 2])

    res_dict = three_col_array_to_results(
        arrival_enum=ArrivalEnum.DM1,
        res_array=res_array,
        valid_iterations=valid_iterations,
        metric=metric)

    res_dict.update({
        "iterations": total_iterations,
        "delta_time": perform_param.value,
        "optimization": "grid_search",
        "metric": "relative",
        "MCDistribution": mc_dist.to_name(),
        "MCParam": mc_dist.param_to_string()
    })

    res_dict_sample = three_col_array_to_results(
        arrival_enum=ArrivalEnum.DM1,
        res_array=res_array_sample,
        valid_iterations=valid_iterations,
        metric=metric)

    res_dict_sample.update({
        "iterations": total_iterations,
        "delta_time": perform_param.value,
        "optimization": "grid_search",
        "metric": "relative",
        "MCDistribution": mc_dist.to_name(),
        "MCParam": mc_dist.param_to_string()
    })

    with open(
            "lower_single_{0}_DM1_results_MC{1}_power_exp.csv".format(
                perform_param.to_name(), mc_dist.to_name()), 'w') as csv_file:
        writer = csv.writer(fileobj=csv_file)
        for key, value in res_dict.items():
            writer.writerow([key, value])
    if sample:
        with open(
                "sample_single_{0}_DM1_results_MC{1}_power_exp.csv".format(
                    perform_param.to_name(), mc_dist.to_name()),
                'w') as csv_file:
            writer = csv.writer(fileobj=csv_file)
            for key, value in res_dict_sample.items():
                writer.writerow([key, value])

    return res_dict
Пример #10
0
    except (FloatingPointError, OverflowError):
        return inf

    if print_x:
        print("grid search optimal parameter: theta={0}, p={1}".format(
            grid_res[0].tolist()[0], grid_res[0].tolist()[1]))

    return grid_res[1]


if __name__ == '__main__':
    DELAY = 5
    A_1 = DM1(lamb=1.0, n=1)
    A_2 = DM1(lamb=1.0, n=1)
    A_3 = DM1(lamb=1.0, n=1)
    S_1 = ConstantRate(rate=6.0)
    S_3 = ConstantRate(rate=6.0)
    for i in range(7):
        S_2 = TokenBucketConstant(sigma_single=0.0, rho_single=i, n=1)
        print("rate", i)
        print(
            "new bound",
            new_delay_opt(delay=DELAY,
                          a_1=A_1,
                          a_2=A_2,
                          s_1=S_1,
                          s_2=S_2,
                          s_3=S_3,
                          print_x=False))

        S_2 = ConstantRate(rate=i)
Пример #11
0
                show_n=False) + "_" + cross_service.to_value(number=2)

    data_frame.to_csv(filename + '.csv',
                      index=True,
                      quoting=csv.QUOTE_NONNUMERIC)

    return data_frame


if __name__ == '__main__':
    DELAY_PROB_LIST = PerformParamList(perform_metric=PerformEnum.DELAY_PROB,
                                       values_list=range(4, 11))

    DM1_FOI1 = DM1(lamb=0.2)
    DM1_CROSS1 = DM1(lamb=8.0)
    RATE_FOI1 = ConstantRate(rate=8.0)
    RATE_CROSS1 = ConstantRate(rate=0.2)

    print(
        csv_fat_cross_perform(foi_arrival=DM1_FOI1,
                              cross_arrival=DM1_CROSS1,
                              foi_service=RATE_FOI1,
                              cross_service=RATE_CROSS1,
                              number_servers=2,
                              perform_param_list=DELAY_PROB_LIST,
                              opt_method=OptMethod.GRID_SEARCH))

    DM1_FOI2 = DM1(lamb=0.4)
    DM1_CROSS2 = DM1(lamb=3.5)
    RATE_FOI2 = ConstantRate(rate=4.5)
    RATE_CROSS2 = ConstantRate(rate=0.4)
    return delay_bounds_df


if __name__ == '__main__':
    DELAY_PROB4 = PerformParameter(perform_metric=PerformEnum.DELAY_PROB,
                                   value=4)
    print(
        csv_bar_chart(ar_list=[
            DM1(lamb=0.5),
            DM1(lamb=8.0),
            DM1(lamb=8.0),
            DM1(lamb=8.0),
            DM1(lamb=8.0),
            DM1(lamb=8.0),
            DM1(lamb=8.0),
            DM1(lamb=8.0)
        ],
                      ser_list=[
                          ConstantRate(rate=4.5),
                          ConstantRate(rate=2.0),
                          ConstantRate(rate=2.0),
                          ConstantRate(rate=2.0),
                          ConstantRate(rate=2.0),
                          ConstantRate(rate=2.0),
                          ConstantRate(rate=2.0),
                          ConstantRate(rate=2.0)
                      ],
                      perform_param=DELAY_PROB4,
                      opt_method=OptMethod.PATTERN_SEARCH,
                      metric="relative"))
Пример #13
0
    print("list_of_bounds: ")
    return list_of_bounds


if __name__ == '__main__':
    from nc_operations.perform_enum import PerformEnum
    from nc_service.constant_rate_server import ConstantRate
    from nc_arrivals.qt import DM1
    from single_server.single_server_perform import SingleServerPerform
    from fat_tree.fat_cross_perform import FatCrossPerform
    from utils.perform_parameter import PerformParameter

    OUTPUT_TIME = PerformParameter(perform_metric=PerformEnum.OUTPUT, value=4)

    EXP_ARRIVAL = DM1(lamb=4.4)
    CONST_RATE = ConstantRate(rate=0.24)

    SETTING1 = SingleServerPerform(arr=EXP_ARRIVAL,
                                   const_rate=CONST_RATE,
                                   perform_param=OUTPUT_TIME)
    OPT_METHODS = [
        OptMethod.GRID_SEARCH, OptMethod.GS_OLD, OptMethod.PATTERN_SEARCH,
        OptMethod.BASIN_HOPPING, OptMethod.SIMULATED_ANNEALING,
        OptMethod.DIFFERENTIAL_EVOLUTION
    ]

    # print(
    #     compare_optimization(
    #         setting=SETTING1,
    #         opt_methods=OPT_METHODS,
    #         number_l=1))
Пример #14
0
def mc_time_fat_cross(arrival_enum: ArrivalEnum,
                      list_number_servers: List[int],
                      perform_param: PerformParameter, opt_method: OptMethod,
                      mc_dist: MonteCarloDist) -> dict:
    """Chooses parameters by Monte Carlo type random choice."""
    total_iterations = 10**4

    time_ratio = {"Number_of_servers": "Ratio"}

    for num_serv in tqdm(list_number_servers):

        # 1 Parameter for service

        size_array = [
            total_iterations, (arrival_enum.number_parameters() + 1) * num_serv
        ]
        # [rows, columns]

        param_array = mc_enum_to_dist(mc_dist=mc_dist, size=size_array)

        time_array = np.empty([total_iterations, 2])

        for i in range(total_iterations):
            if arrival_enum == ArrivalEnum.DM1:
                arrive_list = [
                    DM1(lamb=param_array[i, j]) for j in range(num_serv)
                ]
            elif arrival_enum == ArrivalEnum.MMOO:
                arrive_list = [
                    MMOOFluid(
                        mu=param_array[i, j],
                        lamb=param_array[i, num_serv + j],
                        burst=param_array[i, 2 * num_serv + j])
                    for j in range(num_serv)
                ]

            else:
                raise NameError("Arrival parameter {0} is infeasible".format(
                    arrival_enum.name))

            service_list = [
                ConstantRate(rate=param_array[
                    i, arrival_enum.number_parameters() * num_serv + j])
                for j in range(num_serv)
            ]

            setting = FatCrossPerform(
                arr_list=arrive_list,
                ser_list=service_list,
                perform_param=perform_param)

            # time_standard, time_lyapunov = compute_overhead()
            time_array[i, 0], time_array[i, 1] = compute_overhead(
                setting=setting, opt_method=opt_method, number_l=num_serv - 1)

            if i % floor(total_iterations / 10) == 0:
                print("iteration {0} of {1}".format(i, total_iterations))

        print(
            time_array_to_results(
                arrival_enum=arrival_enum,
                time_array=time_array,
                number_servers=num_serv,
                time_ratio=time_ratio))

    with open(
        (f"time_{perform_param.to_name()}_{arrival_enum.name}_{opt_method.name}.csv"
         ), 'w') as csv_file:
        writer = csv.writer(csv_file)
        for key, value in time_ratio.items():
            writer.writerow([key, value])

    return time_ratio
Пример #15
0
            f"Optimization parameter {opt_method.name} is infeasible")

    return time_standard, time_lyapunov


if __name__ == '__main__':
    from fat_tree.fat_cross_perform import FatCrossPerform
    from utils.perform_parameter import PerformParameter
    from nc_service.constant_rate_server import ConstantRate
    from nc_arrivals.qt import DM1
    from single_server.single_server_perform import SingleServerPerform

    OUTPUT_TIME = PerformParameter(perform_metric=PerformEnum.OUTPUT, value=4)

    EXP_ARRIVAL = DM1(lamb=4.4)
    CONST_RATE = ConstantRate(rate=0.24)

    SETTING1 = SingleServerPerform(arr=EXP_ARRIVAL,
                                   const_rate=CONST_RATE,
                                   perform_param=OUTPUT_TIME)

    # print(
    #     compute_improvement(
    #         setting=SETTING1, opt_method=OptMethod.GRID_SEARCH,
    #         print_x=True))

    DELAY_PROB = PerformParameter(perform_metric=PerformEnum.DELAY_PROB,
                                  value=4)

    EXP_ARRIVAL1 = DM1(lamb=11.0)
    EXP_ARRIVAL2 = DM1(lamb=9.0)
def csv_fat_cross_param_power(arrival_enum: ArrivalEnum, number_servers: int,
                              perform_param: PerformParameter,
                              opt_method: OptMethod,
                              mc_dist: MonteCarloDist) -> dict:
    """Chooses parameters by Monte Carlo type random choice."""
    total_iterations = 10**4
    valid_iterations = total_iterations
    metric = "relative"

    size_array = [
        total_iterations,
        (arrival_enum.number_parameters() + 1) * number_servers
        # const_rate has 1 parameter
    ]
    # [rows, columns]

    param_array = mc_enum_to_dist(mc_dist=mc_dist, size=size_array)

    res_array = np.empty([total_iterations, 2])

    # print(res_array)

    for i in tqdm(range(total_iterations), total=total_iterations):
        if arrival_enum == ArrivalEnum.DM1:
            arrive_list = [
                DM1(lamb=param_array[i, j]) for j in range(number_servers)
            ]

        elif arrival_enum == ArrivalEnum.MD1:
            arrive_list = [
                MD1(lamb=param_array[i, j],
                    mu=1 / (param_array[i,
                                        arrival_enum.number_parameters() *
                                        number_servers + j]))
                for j in range(number_servers)
            ]

        elif arrival_enum == ArrivalEnum.MMOO:
            arrive_list = [
                MMOOFluid(mu=param_array[i, j],
                          lamb=param_array[i, number_servers + j],
                          burst=param_array[i, 2 * number_servers + j])
                for j in range(number_servers)
            ]

        elif arrival_enum == ArrivalEnum.EBB:
            arrive_list = [
                EBB(factor_m=param_array[i, j],
                    decay=param_array[i, number_servers + j],
                    rho_single=param_array[i, 2 * number_servers + j])
                for j in range(number_servers)
            ]

        elif arrival_enum == ArrivalEnum.MassOne:
            arrive_list = [
                LeakyBucketMassOne(sigma_single=param_array[i, j],
                                   rho_single=param_array[i,
                                                          number_servers + j],
                                   n=20) for j in range(number_servers)
            ]
            # TODO: note that n is fixed

        elif arrival_enum == ArrivalEnum.TBConst:
            arrive_list = [
                TokenBucketConstant(sigma_single=param_array[i, j],
                                    rho_single=param_array[i,
                                                           number_servers + j],
                                    n=1) for j in range(number_servers)
            ]

        else:
            raise NameError("Arrival parameter {0} is infeasible".format(
                arrival_enum.name))

        if arrival_enum == ArrivalEnum.MD1 or arrival_enum == ArrivalEnum.MM1:
            service_list = [
                ConstantRate(rate=1.0) for j in range(number_servers)
            ]
        else:
            service_list = [
                ConstantRate(
                    rate=param_array[i,
                                     arrival_enum.number_parameters() *
                                     number_servers + j])
                for j in range(number_servers)
            ]

        setting = FatCrossPerform(arr_list=arrive_list,
                                  ser_list=service_list,
                                  perform_param=perform_param)

        # print(res_array[i, ])

        # standard_bound, new_bound = compute_improvement()
        res_array[i, 0], res_array[i, 1] = compute_improvement(
            setting=setting,
            opt_method=opt_method,
            number_l=number_servers - 1)

        if perform_param.perform_metric == PerformEnum.DELAY_PROB:
            if res_array[i, 1] > 1.0:
                res_array[i, ] = nan

        if res_array[i, 0] == nan or res_array[i, 1] == nan:
            res_array[i, ] = nan
            valid_iterations -= 1

    res_dict = two_col_array_to_results(arrival_enum=arrival_enum,
                                        param_array=param_array,
                                        res_array=res_array,
                                        number_servers=number_servers,
                                        valid_iterations=valid_iterations,
                                        metric=metric)

    res_dict.update({
        "iterations": total_iterations,
        "T": perform_param.value,
        "optimization": opt_method.name,
        "metric": metric,
        "MCDistribution": mc_dist.to_name(),
        "MCParam": mc_dist.param_to_string(),
        "number_servers": number_servers
    })

    with open(
            "sim_{0}_{1}_results_MC{2}_{3}_{4}.csv".format(
                perform_param.to_name(), arrival_enum.name, mc_dist.to_name(),
                opt_method.name, metric), 'w') as csv_file:
        writer = csv.writer(csv_file)
        for key, value in res_dict.items():
            writer.writerow([key, value])

    return res_dict
Пример #17
0
from nc_arrivals.qt import DM1
from nc_operations.perform_enum import PerformEnum
from nc_service.constant_rate_server import ConstantRate
from optimization.optimize import Optimize
from optimization.optimize_new import OptimizeNew
from single_server.single_server_perform import SingleServerPerform
from utils.perform_parameter import PerformParameter

if __name__ == '__main__':
    # Single server output calculation
    print("Single Server Performance Bounds:\n")

    OUTPUT_TIME6 = PerformParameter(perform_metric=PerformEnum.OUTPUT, value=6)

    SINGLE_SERVER = SingleServerPerform(arr=DM1(lamb=1.0),
                                        const_rate=ConstantRate(rate=10.0),
                                        perform_param=OUTPUT_TIME6)

    print(SINGLE_SERVER.bound(param_list=[0.1]))

    print(SINGLE_SERVER.new_bound(param_l_list=[0.1, 2.7]))

    print(
        Optimize(SINGLE_SERVER, print_x=True,
                 show_warn=True).grid_search(bound_list=[(0.1, 5.0)],
                                             delta=0.1))
    print(
        OptimizeNew(SINGLE_SERVER, print_x=True,
                    show_warn=True).grid_search(bound_list=[(0.1, 5.0),
                                                            (0.9, 8.0)],
                                                delta=0.1))
Пример #18
0
"""Computed some examples from the project"""

from fat_tree.fat_cross_perform import FatCrossPerform
from nc_arrivals.qt import DM1
from nc_operations.perform_enum import PerformEnum
from nc_service.constant_rate_server import ConstantRate
from optimization.optimize import Optimize
from utils.perform_parameter import PerformParameter

if __name__ == '__main__':
    PROB_VALUES = [0.5, 0.4, 0.3, 0.2, 0.1, 0.05, 0.01]

    for p in PROB_VALUES:
        DELAY_TIME = PerformParameter(perform_metric=PerformEnum.DELAY,
                                      value=p)

        EXAMPLE = FatCrossPerform(arr_list=[DM1(lamb=1.0)],
                                  ser_list=[ConstantRate(rate=2.0)],
                                  perform_param=DELAY_TIME)

        print(
            Optimize(setting=EXAMPLE, print_x=False,
                     show_warn=True).grid_search(bound_list=[(0.01, 1.1)],
                                                 delta=0.01))
def csv_tandem_compare_servers(
        foi_arrival: ArrivalDistribution, cross_arrival: ArrivalDistribution,
        foi_arrival2: ArrivalDistribution, cross_arrival2: ArrivalDistribution,
        rate: float, max_servers: int, perform_param: PerformParameter,
        opt_method: OptMethod, nc_analysis: NCAnalysis) -> pd.DataFrame:
    """Write dataframe results into a csv file.

    Args:
        foi_arrival: flow of interest's arrival distribution
        foi_arrival2: competitor's flow of interest's arrival distribution
        cross_arrival: distribution of cross arrivals
        cross_arrival2: competitor's distribution of cross arrivals
        rate: service rate of servers
        max_servers: max number of servers in tandem
        perform_param: performance parameter values
        opt_method: optimization method
        nc_analysis: Network Calculus analysis type

    Returns:
        csv file

    """
    bounds = [0.0] * max_servers
    bounds2 = [0.0] * max_servers

    filename = "tandem_{0}".format(perform_param.to_name_value())

    arr_list: List[ArrivalDistribution] = [foi_arrival]
    arr_list2: List[ArrivalDistribution] = [foi_arrival2]
    ser_list: List[ConstantRate] = []

    for _i in range(max_servers):
        print("current_number_servers {0}".format(_i + 1))
        start = timer()
        arr_list.append(cross_arrival)
        arr_list2.append(cross_arrival2)
        ser_list.append(ConstantRate(rate=rate))

        bounds[_i], bounds2[_i] = tandem_compare(
            arr_list=arr_list,
            arr_list2=arr_list2,
            ser_list=ser_list,
            opt_method=opt_method,
            perform_param=perform_param,
            nc_analysis=nc_analysis)
        end = timer()
        print("duration: {0}".format(end - start))

    filename += "_max" + str(max_servers) + "servers_" + foi_arrival.to_value(
    ) + "_rate=" + str(rate)

    results_df = pd.DataFrame({
        "bounds": bounds,
        "bounds2": bounds2
    },
                              index=range(1, max_servers + 1))
    results_df = results_df[["bounds", "bounds2"]]

    results_df.to_csv(
        filename + '.csv', index=True, quoting=csv.QUOTE_NONNUMERIC)

    return results_df
            except (ParameterOutOfBounds, OverflowError):
                return inf


if __name__ == '__main__':
    from fat_tree.fat_cross_perform import FatCrossPerform
    from utils.perform_parameter import PerformParameter
    from nc_operations.perform_enum import PerformEnum
    from nc_service.constant_rate_server import ConstantRate
    from nc_arrivals.markov_modulated import MMOOFluid

    DELAY_4 = PerformParameter(perform_metric=PerformEnum.DELAY, value=0.0001)

    MMOO_1 = MMOOFluid(mu=1.0, lamb=2.2, burst=3.4)
    MMOO_2 = MMOOFluid(mu=3.6, lamb=1.6, burst=0.4)
    CONST_RATE_1 = ConstantRate(rate=2.0)
    CONST_RATE_2 = ConstantRate(rate=0.3)

    SIMPLEX_START = np.array([[0.1], [0.3]])
    # SIMPLEX_START = np.array([[100], [200]])
    SIMPLEX_START_NEW = np.array([[0.1, 2.0], [0.3, 1.2], [0.4, 1.1]])
    SIMPLEX_RAND = InitialSimplex(parameters_to_optimize=1).uniform_dist(
        max_theta=0.6, max_l=2.0)

    NM_PARAM_SET = NelderMeadParameters()

    SETTING = FatCrossPerform(
        arr_list=[MMOO_1, MMOO_2],
        ser_list=[CONST_RATE_1, CONST_RATE_2],
        perform_param=DELAY_4)
Пример #21
0
    DELAY_PROB_VAL = 10**(-5)
    DELAY_PROB6 = PerformParameter(perform_metric=PerformEnum.DELAY,
                                   value=DELAY_PROB_VAL)

    NUMBER_AGGREGATIONS = 4

    RHO_SINGLE = 1.0
    SIGMA_SINGLE = 8.0
    SERVICE_RATE = 6.5

    BOUND_LIST = [(0.05, 20.0)]
    DELTA = 0.05
    PRINT_X = True

    CR_SERVER = ConstantRate(SERVICE_RATE)

    TB_CONST = TokenBucketConstant(sigma_single=SIGMA_SINGLE,
                                   rho_single=RHO_SINGLE,
                                   n=NUMBER_AGGREGATIONS)

    CONST_SINGLE = SingleServerPerform(arr=TB_CONST,
                                       const_rate=CR_SERVER,
                                       perform_param=DELAY5)

    LEAKY_MASS_1 = SingleServerPerform(arr=LeakyBucketMassOne(
        sigma_single=SIGMA_SINGLE,
        rho_single=RHO_SINGLE,
        n=NUMBER_AGGREGATIONS),
                                       const_rate=CR_SERVER,
                                       perform_param=DELAY5)
                                theta=param_l_list[0],
                                delta_time=self.perform_param.value,
                                l_power=param_l_list[1])

        else:
            raise NameError(f"{self.perform_param.perform_metric} is an"
                            f"infeasible performance metric")

    def to_string(self) -> str:
        return self.to_name() + "_" + self.arr.to_value(
        ) + "_" + self.ser.to_value() + self.perform_param.to_name_value()


if __name__ == '__main__':
    EXP_ARRIVAL1 = DM1(lamb=1.0)
    CONST_RATE16 = ConstantRate(rate=1.6)
    OUTPUT_4 = PerformParameter(perform_metric=PerformEnum.OUTPUT, value=4)
    EX_OUTPUT = SingleServerPerform(arr=EXP_ARRIVAL1,
                                    const_rate=CONST_RATE16,
                                    perform_param=OUTPUT_4)
    print(EX_OUTPUT.bound(param_list=[0.5]))
    print(EX_OUTPUT.new_bound(param_l_list=[0.5, 1.2]))

    DELAY_PROB_4 = PerformParameter(perform_metric=PerformEnum.DELAY_PROB,
                                    value=4)
    EX_DELAY_PROB = SingleServerPerform(arr=EXP_ARRIVAL1,
                                        const_rate=CONST_RATE16,
                                        perform_param=DELAY_PROB_4)
    print(EX_DELAY_PROB.bound(param_list=[0.5]))
    print(EX_DELAY_PROB.new_bound(param_l_list=[0.5, 1.2]))
    filename += "_" + arrival.to_name() + "_" + arrival.to_value(
        number=1, show_n=False) + "_" + service.to_value(number=1)

    data_frame.to_csv(filename + '.csv',
                      index=True,
                      quoting=csv.QUOTE_NONNUMERIC)

    return data_frame


if __name__ == '__main__':
    OUTPUT_LIST = PerformParamList(perform_metric=PerformEnum.OUTPUT,
                                   values_list=range(4, 15))

    DM1_FOI = DM1(lamb=3.8, n=1)
    CONST_RATE1 = ConstantRate(rate=3.0)

    print(
        csv_single_perform(arrival=DM1_FOI,
                           service=CONST_RATE1,
                           perform_param_list=OUTPUT_LIST,
                           opt_method=OptMethod.GRID_SEARCH))

    MMOO_FOI = MMOOFluid(mu=8.0, lamb=12.0, burst=3.0, n=1)
    CONST_RATE2 = ConstantRate(rate=1.5)

    print(
        csv_single_perform(arrival=MMOO_FOI,
                           service=CONST_RATE2,
                           perform_param_list=OUTPUT_LIST,
                           opt_method=OptMethod.GRID_SEARCH))