Example #1
0
def csv_bar_chart(ar_list: List[ArrivalDistribution],
                  ser_list: List[ConstantRateServer],
                  perform_param: PerformParameter,
                  opt_method: OptMethod,
                  change_metric=ChangeEnum.RATIO_REF_NEW) -> pd.DataFrame:
    """Write the data for a special case in a csv."""
    size = len(ar_list)

    bar_matrix = np.zeros(shape=[size, 4])
    ar_list_copy = ar_list[:]
    ser_list_copy = ser_list[:]

    for i in range(size - 1, 0, -1):
        # i = row list index = number_servers - 1. That's why i goes to 0,
        # not 1
        large_setting = FatCrossPerform(arr_list=ar_list_copy,
                                        ser_list=ser_list_copy,
                                        perform_param=perform_param)

        utilization = format(large_setting.approximate_utilization(), '.3f')

        standard_bound, h_mit_bound = compare_mitigator(setting=large_setting,
                                                        opt_method=opt_method,
                                                        number_l=i)

        standard_bound = float(format(standard_bound, '.4f'))
        h_mit_bound = float(format(h_mit_bound, '.4f'))

        if h_mit_bound >= 1:
            warn(f"h-mitigator bound = {h_mit_bound} is >= 1")

        if change_metric == ChangeEnum.RATIO_REF_NEW:
            # improvement factor
            opt_diff = float(format(standard_bound / h_mit_bound, '.4f'))

        elif change_metric == ChangeEnum.DIFF_REF_NEW:
            # absolute difference
            opt_diff = float(format(standard_bound - h_mit_bound, '.4f'))

        else:
            raise NameError(
                f"metric parameter = {change_metric} is infeasible")

        bar_matrix[i, ] = [standard_bound, h_mit_bound, opt_diff, utilization]

        del ar_list_copy[-1]
        del ser_list_copy[-1]
        # delete one flow and its server to analyze a smaller network

    delay_bounds_df = pd.DataFrame({
        "number_servers": range(1, size + 1),
        "standard_bound": bar_matrix[:, 0],
        "h_mit_bound": bar_matrix[:, 1],
        "improvement": bar_matrix[:, 2],
        "utilization": bar_matrix[:, 3],
    })

    delay_bounds_df = delay_bounds_df[[
        "number_servers", "standard_bound", "h_mit_bound", "improvement",
        "utilization"
    ]]

    delay_bounds_df.to_csv(
        f"bar_chart_{str(size)}_{opt_method.name}_improvement_factor.csv",
        index=True,
        quoting=csv.QUOTE_NONNUMERIC)

    return delay_bounds_df
Example #2
0
def csv_fat_cross_time(arrival_enum: ArrivalEnum,
                       list_number_servers: List[int],
                       perform_param: PerformParameter, opt_method: OptMethod,
                       mc_dist: MonteCarloDist, target_util: float) -> dict:
    """Chooses parameters by Monte Carlo type random choice."""
    total_iterations = 10**5

    time_ratio = {"Number_of_servers": "Ratio"}

    for number_servers in list_number_servers:
        print(f"number of servers = {number_servers}")
        # 1 Parameter for service

        param_array = mc_enum_to_dist(arrival_enum=arrival_enum,
                                      mc_dist=mc_dist,
                                      number_flows=number_servers,
                                      number_servers=number_servers,
                                      total_iterations=total_iterations)

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

        for i in tqdm(range(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.MMOOFluid:
                arrive_list = [
                    MMOOCont(mu=param_array[i, j],
                             lamb=param_array[i, number_servers + j],
                             peak_rate=param_array[i, 2 * number_servers + j])
                    for j in range(number_servers)
                ]

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

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

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

            computation_necessary = True

            # print(res_array[i, ])
            if target_util > 0.0:
                util = fat_cross_setting.approximate_utilization()
                if util < target_util or util > 1:
                    time_array[i, ] = np.nan
                    computation_necessary = False

            if computation_necessary:
                # time_standard, time_lyapunov = compare_time()
                time_array[i, 0], time_array[i, 1] = compare_time(
                    setting=fat_cross_setting,
                    opt_method=opt_method,
                    number_l=number_servers - 1)

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

    filename = (f"time_{perform_param.to_name()}_{arrival_enum.name}"
                f"_{opt_method.name}.csv")

    with open(filename, 'w') as csv_file:
        writer = csv.writer(csv_file)
        for key, value in time_ratio.items():
            writer.writerow([key, value])

    return time_ratio
Example #3
0
                                      server=ConstantRateServer(rate=0.24),
                                      perform_param=OUTPUT_TIME)

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

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

    ARR_LIST = [DM1(lamb=11.0), DM1(lamb=9.0)]
    SER_LIST = [ConstantRateServer(rate=5.0), ConstantRateServer(rate=4.0)]

    SETTING2 = FatCrossPerform(arr_list=ARR_LIST,
                               ser_list=SER_LIST,
                               perform_param=DELAY_PROB)

    print("compare bounds\n")

    print(
        compare_mitigator(setting=SETTING2,
                          opt_method=OptMethod.GRID_SEARCH,
                          print_x=True))

    print(
        compare_mitigator(setting=SETTING2,
                          opt_method=OptMethod.PATTERN_SEARCH,
                          print_x=True))

    print(
Example #4
0
def csv_fat_cross_param_power(name: str, arrival_enum: ArrivalEnum,
                              number_flows: int, number_servers: int,
                              perform_param: PerformParameter,
                              opt_method: OptMethod, mc_dist: MonteCarloDist,
                              compare_metric: ChangeEnum,
                              total_iterations: int,
                              target_util: float) -> dict:
    """Chooses parameters by Monte Carlo type random choice."""
    param_array = mc_enum_to_dist(arrival_enum=arrival_enum,
                                  mc_dist=mc_dist,
                                  number_flows=number_flows,
                                  number_servers=number_servers,
                                  total_iterations=total_iterations)

    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:
            arr_list = [
                DM1(lamb=param_array[i, j]) for j in range(number_flows)
            ]

        elif arrival_enum == ArrivalEnum.DGamma1:
            arr_list = [
                DGamma1(alpha_shape=param_array[i, j],
                        beta_rate=param_array[i, number_flows + j])
                for j in range(number_flows)
            ]

        elif arrival_enum == ArrivalEnum.DWeibull1:
            arr_list = [
                DWeibull1(lamb=param_array[i, j]) for j in range(number_flows)
            ]

        elif arrival_enum == ArrivalEnum.MD1:
            arr_list = [
                MD1(lamb=param_array[i, j], mu=1.0)
                for j in range(number_flows)
            ]

        elif arrival_enum == ArrivalEnum.MMOODisc:
            arr_list = [
                MMOODisc(stay_on=param_array[i, j],
                         stay_off=param_array[i, number_flows + j],
                         peak_rate=param_array[i, 2 * number_flows + j])
                for j in range(number_flows)
            ]

        elif arrival_enum == ArrivalEnum.MMOOFluid:
            arr_list = [
                MMOOCont(mu=param_array[i, j],
                         lamb=param_array[i, number_flows + j],
                         peak_rate=param_array[i, 2 * number_flows + j])
                for j in range(number_flows)
            ]

        elif arrival_enum == ArrivalEnum.Massoulie:
            arr_list = [
                LeakyBucketMassoulie(sigma_single=param_array[i, j],
                                     rho_single=param_array[i,
                                                            number_flows + j],
                                     m=20) for j in range(number_flows)
            ]
            # NOTE: n is fixed

        elif arrival_enum == ArrivalEnum.TBConst:
            arr_list = [
                DetermTokenBucket(sigma_single=param_array[i, j],
                                  rho_single=param_array[i, number_flows + j],
                                  m=1) for j in range(number_flows)
            ]

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

        ser_list = [
            ConstantRateServer(
                rate=param_array[i,
                                 arrival_enum.number_parameters() *
                                 number_flows + j])
            for j in range(number_servers)
        ]

        fat_cross_setting = FatCrossPerform(arr_list=arr_list,
                                            ser_list=ser_list,
                                            perform_param=perform_param)

        computation_necessary = True

        if target_util > 0.0:
            util = fat_cross_setting.approximate_utilization()
            if util < target_util or util > 1:
                res_array[i, ] = np.nan
                computation_necessary = False

        if computation_necessary:
            # standard_bound, h_mit_bound = compare_mitigator()
            res_array[i, 0], res_array[i, 1] = compare_mitigator(
                setting=fat_cross_setting,
                opt_method=opt_method,
                number_l=number_servers - 1)

            if (perform_param.perform_metric == PerformEnum.DELAY_PROB
                    and res_array[i, 1] > 1.0):
                # write as nan if second (in particular both) value(s) are > 1.0
                res_array[i, ] = np.nan

        if np.isnan(res_array[i, 0]) or np.isnan(res_array[i, 1]):
            res_array[i, ] = np.nan

    res_array_no_full_nan = remove_full_nan_rows(full_array=res_array)
    valid_iterations = res_array_no_full_nan.shape[0]

    if valid_iterations < total_iterations * 0.2:
        warn(f"Many nan's: {total_iterations - valid_iterations} nans "
             f"out of {total_iterations}!")

        if valid_iterations < 100:
            raise NotEnoughResults("result is useless")

    res_dict = two_col_array_to_results(arrival_enum=arrival_enum,
                                        param_array=param_array,
                                        res_array=res_array,
                                        number_servers=number_servers,
                                        compare_metric=compare_metric)

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

    filename = name
    filename += f"_results_{perform_param.to_name()}_{arrival_enum.name}_" \
                f"MC{mc_dist.to_name()}_{opt_method.name}_" \
                f"{compare_metric.name}_util_{target_util}"

    with open(filename + ".csv", 'w') as csv_file:
        writer = csv.writer(csv_file)
        for key, value in res_dict.items():
            writer.writerow([key, value])

    return res_dict
    MMOO_1 = MMOOCont(mu=1.0, lamb=2.2, peak_rate=3.4)
    MMOO_2 = MMOOCont(mu=3.6, lamb=1.6, peak_rate=0.4)
    CONST_RATE_1 = ConstantRateServer(rate=2.0)
    CONST_RATE_2 = ConstantRateServer(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)

    OPTI_OLD = Optimize(setting=SETTING, number_param=1, print_x=True)
    print(OPTI_OLD.grid_search(grid_bounds=[(0.1, 4.0)], delta=0.1))
    print(OPTI_OLD.pattern_search(start_list=[0.5], delta=3.0, delta_min=0.01))
    print(Optimize.nelder_mead(self=OPTI_OLD, simplex=SIMPLEX_RAND))
    print(
        Optimize.nelder_mead_old(self=OPTI_OLD,
                                 simplex=SIMPLEX_RAND,
                                 nelder_mead_param=NM_PARAM_SET))
    print(OPTI_OLD.basin_hopping(start_list=[2.0]))
    print(OPTI_OLD.diff_evolution(bound_list=[(0.1, 4.0)]))
    print(OPTI_OLD.bfgs(start_list=[0.4]))

    OPTI_NEW = OptimizeMitigator(setting_h_mit=SETTING,
Example #6
0
    print("\n-------------------------------------------\n")
    print("Fat Cross Performance Bounds:\n")

    DELAY_PROB6 = PerformParameter(perform_metric=PerformEnum.DELAY_PROB,
                                   value=6)

    ARR_LIST1: List[ArrivalDistribution] = [DM1(lamb=1.0), DM1(lamb=4.0)]

    SER_LIST1: List[ConstantRateServer] = [
        ConstantRateServer(rate=4.0),
        ConstantRateServer(rate=0.5)
    ]

    EXAMPLE_1 = FatCrossPerform(arr_list=ARR_LIST1,
                                ser_list=SER_LIST1,
                                perform_param=DELAY_PROB6)

    print("EXAMPLE_1\n")
    print(
        Optimize(setting=EXAMPLE_1, number_param=1,
                 print_x=True).grid_search(grid_bounds=[(0.1, 5.0)],
                                           delta=0.1))

    ARR_LIST2: List[ArrivalDistribution] = [
        MMOOCont(mu=0.5, lamb=0.5, peak_rate=1.5),
        MMOOCont(mu=0.5, lamb=0.5, peak_rate=0.7)
    ]

    SER_LIST2: List[ConstantRateServer] = [
        ConstantRateServer(rate=2.0),
def fat_cross_perform_df(arr_list: List[ArrivalDistribution],
                         ser_list: List[ConstantRateServer],
                         opt_method: OptMethod,
                         perform_param_list: PerformParamList) -> pd.DataFrame:
    """Compute delay standard_bound for T in T_list and write into dataframe.

    Args:
        arr_list: Arrival object list
        ser_list: Service object list
        opt_method: PS or GS
        perform_param_list: list of performance parameter values

    Returns:
        dataframe

    """
    delta_val = 0.05

    one_param_bounds = [(delta_val, 10.0)]

    standard_bound = [0.0] * len(perform_param_list)
    h_mit_bound = [0.0] * len(perform_param_list)

    print_x = False

    fat_cross_setting = FatCrossPerform(
        arr_list=arr_list,
        ser_list=ser_list,
        perform_param=PerformParameter(perform_metric=PerformEnum.DELAY_PROB,
                                       value=0))

    print(f"utilization: {fat_cross_setting.approximate_utilization()}")
    print()

    for i in range(len(perform_param_list)):
        fat_cross_setting = FatCrossPerform(
            arr_list=arr_list,
            ser_list=ser_list,
            perform_param=perform_param_list.get_parameter_at_i(i))

        if opt_method == OptMethod.GRID_SEARCH:
            standard_bound[i] = Optimize(setting=fat_cross_setting,
                                         number_param=1,
                                         print_x=print_x).grid_search(
                                             grid_bounds=one_param_bounds,
                                             delta=delta_val)
            h_mit_bound[i] = OptimizeMitigator(
                setting_h_mit=fat_cross_setting,
                number_param=2,
                print_x=print_x).grid_search(grid_bounds=[(delta_val, 10.0),
                                                          (1 + delta_val, 8.0)
                                                          ],
                                             delta=delta_val)

        elif opt_method == OptMethod.PATTERN_SEARCH:
            standard_bound[i] = Optimize(setting=fat_cross_setting,
                                         number_param=1,
                                         print_x=print_x).pattern_search(
                                             start_list=[0.5],
                                             delta=3.0,
                                             delta_min=0.01)

            h_mit_bound[i] = OptimizeMitigator(setting_h_mit=fat_cross_setting,
                                               number_param=2,
                                               print_x=print_x).pattern_search(
                                                   start_list=[0.5, 2.0],
                                                   delta=3.0,
                                                   delta_min=0.01)

        else:
            raise NotImplementedError(f"Optimization parameter {opt_method} "
                                      f"is infeasible")

    results_df = pd.DataFrame(
        {
            "standard_bound": standard_bound,
            "h_mit_bound": h_mit_bound
        },
        index=perform_param_list.values_list)

    return results_df