def arrival_list_to_csv(prefix: str, data_frame_creator: Callable, list_arr_list: List[List[ArrivalDistribution]], ser_list: List[ConstantRateServer], server_index: int, perform_param: PerformParameter, opt_method: OptMethod = None, suffix="_adjusting_arrivals") -> pd.DataFrame: filename = prefix + perform_param.to_name() if opt_method is None: data_frame: pd.DataFrame = data_frame_creator( list_arr_list=list_arr_list, ser_list=ser_list, server_index=server_index, perform_param=perform_param) else: data_frame: pd.DataFrame = data_frame_creator( list_arr_list=list_arr_list, ser_list=ser_list, server_index=server_index, opt_method=opt_method, perform_param=perform_param) filename += "_" + list_arr_list[0][0].to_name() data_frame.to_csv(filename + suffix + '.csv', index=True, quoting=csv.QUOTE_NONNUMERIC) return data_frame
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
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