def run_mix_racos(): # parameters sample_size = 8 # the instance number of sampling in an iteration budget = 20000 # budget in online style positive_num = 2 # the set size of PosPop rand_probability = 0.99 # the probability of sample in model uncertain_bit = 2 # the dimension size that is sampled randomly repeat = 4 list_budget = [100, 1000, 10000, 50000] # dimension setting dimension_size = 15 float_region = [-100, 100] integer_region = [-100, 100] categorical_region = [0, 2] dimension = Dimension() dimension.set_dimension_size(dimension_size) for i in range(dimension_size): if i % 3 == 0: dimension.set_region(i, float_region, 0) elif i % 3 == 1: dimension.set_region(i, integer_region, 1) else: dimension.set_region(i, categorical_region, 2) # optimization racos = RacosOptimization(dimension) for i in range(repeat): start_t = time.time() racos.mix_opt(tt_func, ss=sample_size, bud=list_budget[i], pn=positive_num, rp=rand_probability, ub=uncertain_bit) end_t = time.time() optimal = racos.get_optimal() hour, minute, second = time_formulate(start_t, end_t) print('total budget is ', list_budget[i], '------------------------------') print('spending time: ', hour, ' hours ', minute, ' minutes ', second, ' seconds') print('optimal value: ', optimal.get_fitness())
def get_dimension(param_input): ''' get dimension params by param input :param param_input: params input :return: dimension and the label coder ''' dimension = Dimension() label_coder = ParamsHelper() dimension.set_dimension_size(len(param_input)) index = 0 for k, (type, obj) in param_input.items(): dimension.set_region( *label_coder.encode(type=type, index=index, key=k, objs=obj)) index = index + 1 return dimension, label_coder
def run_exp_racos_for_synthetic_problem_analysis(): # parameters sample_size = 10 # the instance number of sampling in an iteration budget = 500 # budget in online style positive_num = 2 # the set size of PosPop rand_probability = 0.99 # the probability of sample in model uncertain_bit = 1 # the dimension size that is sampled randomly adv_threshold = 10 # advance sample size opt_repeat = 10 dimension_size = 10 problem_name = 'sphere' problem_num = 200 start_index = 0 bias_region = 0.2 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-1.0, 1.0] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) log_buffer = [] # logging learner_path = './ExpLearner/SyntheticProbsLearner/' + problem_name + '/dimension' + str(dimension_size)\ + '/DirectionalModel/' + 'learner-' + problem_name + '-' + 'dim' + str(dimension_size) + '-'\ + 'bias' + str(bias_region) + '-' problem_path = './ExpLog/SyntheticProbsLog/' + problem_name + '/dimension' + str(dimension_size)\ + '/DirectionalModel/' + 'bias-' + problem_name + '-' + 'dim' + str(dimension_size) + '-'\ + 'bias' + str(bias_region) + '-' func = DistributedFunction(dimension, bias_region=[-0.5, 0.5]) target_bias = [0.1 for _ in range(dimension_size)] func.setBias(target_bias) if problem_name == 'ackley': prob_fct = func.DisAckley else: prob_fct = func.DisSphere relate_error_list = [] for prob_i in range(problem_num): print( start_index + prob_i, '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++') log_buffer.append( str(start_index + prob_i) + '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++') log_buffer.append('+++++++++++++++++++++++++++++++') log_buffer.append('optimization parameters') log_buffer.append('sample size: ' + str(sample_size)) log_buffer.append('budget: ' + str(budget)) log_buffer.append('positive num: ' + str(positive_num)) log_buffer.append('random probability: ' + str(rand_probability)) log_buffer.append('uncertain bits: ' + str(uncertain_bit)) log_buffer.append('advance num: ' + str(adv_threshold)) log_buffer.append('+++++++++++++++++++++++++++++++') log_buffer.append('problem parameters') log_buffer.append('dimension size: ' + str(dimension_size)) log_buffer.append('problem name: ' + problem_name) log_buffer.append('bias_region: ' + str(bias_region)) log_buffer.append('+++++++++++++++++++++++++++++++') problem_file = problem_path + str(start_index + prob_i) + '.txt' problem_str = fo.FileReader(problem_file)[0].split(',') problem_index = int(problem_str[0]) problem_bias = string2list(problem_str[1]) if problem_index != (start_index + prob_i): print('problem error!') exit(0) print('source bias: ', problem_bias) log_buffer.append('source bias: ' + list2string(problem_bias)) reduisal = np.array(target_bias) - np.array(problem_bias) this_distance = reduisal * reduisal.T learner_file = learner_path + str(start_index + prob_i) + '.pkl' log_buffer.append('learner file: ' + learner_file) print('learner file: ', learner_file) net = torch.load(learner_file) net_list = [net] opt_error_list = [] for i in range(opt_repeat): print('optimize ', i, '===================================================') log_buffer.append( 'optimize ' + str(i) + '===================================================') exp_racos = ExpRacosOptimization(dimension, net_list) start_t = time.time() exp_racos.exp_mix_opt(obj_fct=prob_fct, ss=sample_size, bud=budget, pn=positive_num, rp=rand_probability, ub=uncertain_bit, at=adv_threshold) end_t = time.time() print('total budget is ', budget) log_buffer.append('total budget is ' + str(budget)) hour, minute, second = time_formulate(start_t, end_t) print('spending time: ', hour, ':', minute, ':', second) log_buffer.append('spending time: ' + str(hour) + '+' + str(minute) + '+' + str(second)) optimal = exp_racos.get_optimal() opt_error = optimal.get_fitness() optimal_x = optimal.get_features() opt_error_list.append(opt_error) print('validation optimal value: ', opt_error) log_buffer.append('validation optimal value: ' + str(opt_error)) print('optimal x: ', optimal_x) log_buffer.append('optimal nn structure: ' + list2string(optimal_x)) opt_mean = np.mean(np.array(opt_error_list)) relate_error_list.append([this_distance, opt_mean]) opt_std = np.std(np.array(opt_error_list)) print('--------------------------------------------------') print('optimization result: ', opt_mean, '#', opt_std) log_buffer.append('--------------------------------------------------') log_buffer.append('optimization result: ' + str(opt_mean) + '#' + str(opt_std)) result_path = './Results/SyntheticProbs/' + problem_name + '/dimension' + str( dimension_size) + '/' relate_error_file = result_path + 'relate-error-' + problem_name + '-dim' + str(dimension_size) + '-bias'\ + str(bias_region) + '.txt' temp_buffer = [] for i in range(len(relate_error_list)): relate, error = relate_error_list[i] temp_buffer.append(str(relate) + ',' + str(error)) print('relate error logging: ', relate_error_file) log_buffer.append('relate error logging: ' + relate_error_file) fo.FileWriter(relate_error_file, temp_buffer, style='w') optimization_log_file = result_path + 'opt-log-' + problem_name + '-dim' + str(dimension_size) + '-bias'\ + str(bias_region) + '.txt' print('optimization logging: ', optimization_log_file) fo.FileWriter(optimization_log_file, log_buffer, style='w')
MaxIteration = 30 # the number of iterations Budget = 150 # budget in online style PositiveNum = 2 # the set size of PosPop RandProbability = 0.95 # the probability of sample in model UncertainBits = 3 # the dimension size that is sampled randomly # continuous optimization if False: #dimension setting DimSize = 10 regs = [] regs.append(-1) regs.append(1) dim = Dimension() dim.setDimensionSize(DimSize) for i in range(DimSize): dim.setRegion(i, regs, True) racos = RacosOptimizaiton(dim) # call online version RACOS #racos.OnlineTurnOn() #racos.ContinueOpt(Sphere, SampleSize, Budget, PositiveNum, RandProbability, UncertainBits) racos.ContinueOpt(Sphere, SampleSize, MaxIteration, PositiveNum, RandProbability, UncertainBits) print racos.getOptimal().getFeatures() print racos.getOptimal().getFitness()
def synthetic_problems_sample(budget=500, problem_name='sphere', problem_size=5, max_bias=0.5, bias_step=0): sample_size = 10 # the instance number of sampling in an iteration positive_num = 2 # the set size of PosPop rand_probability = 0.99 # the probability of sample in model uncertain_bits = 2 # the dimension size that is sampled randomly start_index = 0 repeat_num = 10 exp_path = path + '/ExpLog/SyntheticProbsLog/' bias = 0 dimension_size = 10 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-1.0, 1.0] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) if bias_step > 0: problem_name += '_group-sample' for prob_i in range(problem_size): if bias_step > 0 and prob_i % (problem_size / max_bias * bias_step) == 0: bias += bias_step else: bias = max_bias # bias log format: 'index,bias_list: dim1 dim2 dim3...' bias_log = [] running_log = [] running_log.append('+++++++++++++++++++++++++++++++++') running_log.append('optimization setting: ') running_log.append('sample_size: ' + str(sample_size)) running_log.append('positive_num: ' + str(positive_num)) running_log.append('rand_probability: ' + str(rand_probability)) running_log.append('uncertain_bits: ' + str(uncertain_bits)) running_log.append('budget: ' + str(budget)) running_log.append('group sample step: ' + str(bias_step)) running_log.append('+++++++++++++++++++++++++++++++++') print(problem_name, ': ', start_index + prob_i, ' ==============================================') running_log.append(problem_name + ': ' + str(start_index + prob_i) + ' ==============================================') # problem setting func = DistributedFunction(dim=dimension, bias_region=[-bias, bias]) if 'ackley' in problem_name: prob = func.DisAckley elif 'sphere' in problem_name: prob = func.DisSphere elif 'rosenbrock' in problem_name: prob = func.DisRosenbrock else: print('Wrong function!') return # bias log bias_log.append(str(prob_i) + ',' + list2string(func.getBias())) print('function: ', problem_name, ', this bias: ', func.getBias()) running_log.append('function: ' + problem_name + ', this bias: ' + list2string(func.getBias())) # optimization setting optimizer = RacosOptimization(dimension) positive_set = [] negative_set = [] new_sample_set = [] label_set = [] for repeat_i in range(repeat_num): print('repeat ', repeat_i, ' ----------------------------------------') running_log.append('repeat ' + str(repeat_i) + ' ----------------------------------------') # optimization process start_t = time.time() optimizer.mix_opt(obj_fct=prob, ss=sample_size, bud=budget, pn=positive_num, rp=rand_probability, ub=uncertain_bits) end_t = time.time() hour, minute, second = time_formulate(start_t, end_t) # optimization results optimal = optimizer.get_optimal() print('optimal v: ', optimal.get_fitness(), ' - ', optimal.get_features()) running_log.append('optimal v: ' + str(optimal.get_fitness()) + ' - ' + list2string(optimal.get_features())) print('spent time: ', hour, ':', minute, ':', second) running_log.append('spent time: ' + str(hour) + ':' + str(minute) + ':' + str(second)) # log samples this_positive, this_negative, this_new, this_label = optimizer.get_log( ) print('sample number: ', len(this_positive), ':', len(this_label)) running_log.append('sample number: ' + str(len(this_positive)) + ':' + str(len(this_label))) positive_set.extend(this_positive) negative_set.extend(this_negative) new_sample_set.extend(this_new) label_set.extend(this_label) print('----------------------------------------------') print('sample finish!') print('all sample number: ', len(positive_set), '-', len(negative_set), '-', len(new_sample_set), \ '-', len(label_set)) running_log.append('----------------------------------------------') running_log.append('all sample number: ' + str(len(positive_set)) + '-' + str(len(negative_set)) + '-' + str(len(new_sample_set)) + '-' + str(len(label_set))) data_log_file = exp_path + str(problem_name) + '/dimension' + str(dimension_size) + '/DataLog/' + \ 'data-' + problem_name + '-' + 'dim' + str(dimension_size) + '-' + 'bias' \ + str(bias) + '-' + str(start_index + prob_i) + '.pkl' bias_log_file = exp_path + str(problem_name) + '/dimension' + str(dimension_size) + '/RecordLog/' + 'bias-' \ + problem_name + '-' + 'dim' + str(dimension_size) + '-' + 'bias' + str(bias) \ + '-' + str(start_index + prob_i) + '.txt' running_log_file = exp_path + str(problem_name) + '/dimension' + str(dimension_size) + '/RecordLog/' + \ 'running-' + problem_name + '-' + 'dim' + str(dimension_size) + '-' + 'bias' \ + str(bias) + '-' + str(start_index + prob_i) + '.txt' print('data logging: ', data_log_file) running_log.append('data log path: ' + data_log_file) save_log(positive_set, negative_set, new_sample_set, label_set, data_log_file) print('bias logging: ', bias_log_file) running_log.append('bias log path: ' + bias_log_file) fo.FileWriter(bias_log_file, bias_log, style='w') print('running logging: ', running_log_file) fo.FileWriter(running_log_file, running_log, style='w') return
from __future__ import division, print_function from smac.facade.func_facade import fmin_smac from ObjectiveFunction import DistributedFunction from Components import Dimension from Tools import RandomOperator import numpy as np dimension_size = 10 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-0.5, 0.5] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) func = DistributedFunction(dimension, bias_region=[-0.5, 0.5]) target_bias = [0.25 for _ in range(dimension_size)] func.setBias(target_bias) ro = RandomOperator() prob_fct = func.DisRosenbrock x0 = [ro.get_uniform_double(-0.5, 0.5) for _ in range(dimension_size)] ans = [] for i in range(10): x, cost, _ = fmin_smac(func=prob_fct, x0=x0, bounds=[[-0.5, 0.5] for _ in range(dimension_size)], maxfun=50, rng=3) ans.append(x) # print("Optimum at {} with cost of {}".format(x, cost)) print(np.mean(ans))
def run(type): dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-1.0, 1.0] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) # problem define func = DistributedFunction(dimension, bias_region=[-bias_region, bias_region]) target_bias = [0.1 for _ in range(dimension_size)] func.setBias(target_bias) if problem_name == 'ackley': prob_fct = func.DisAckley elif problem_name == 'sphere': prob_fct = func.DisSphere elif problem_name == 'rosenbrock': prob_fct = func.DisRosenbrock else: print('Wrong function!') exit() opt_error_list = [] log_buffer.append('+++++++++++++++++++++++++++++++') log_buffer.append('Running: ' + type) log_buffer.append('+++++++++++++++++++++++++++++++') print('+++++++++++++++++++++++++++++++') print('Running: ' + type) print('+++++++++++++++++++++++++++++++') if type == 'ada': # pre=sorted(predictors,key=lambda a:a.dist) expert = Experts(predictors=predictors, eta=eta, bg=budget) for i in range(opt_repeat): print('optimize ', i, '===================================================') log_buffer.append( 'optimize ' + str(i) + '===================================================') start_t = time.time() if type == 'exp': exp_racos = ExpRacosOptimization(dimension, nets) opt_error = exp_racos.exp_mix_opt(obj_fct=prob_fct, ss=sample_size, bud=budget, pn=positive_num, rp=rand_probability, ub=uncertain_bit, at=adv_threshold) elif type == 'ada': exp_racos = ExpAdaRacosOptimization(dimension, expert) opt_error = exp_racos.exp_ada_mix_opt(obj_fct=prob_fct, ss=sample_size, bud=budget, pn=positive_num, rp=rand_probability, ub=uncertain_bit, at=adv_threshold) else: print('Wrong type!') return end_t = time.time() hour, minute, second = time_formulate(start_t, end_t) print('spending time: ', hour, ':', minute, ':', second) log_buffer.append('spending time: ' + str(hour) + '+' + str(minute) + '+' + str(second)) opt_error_list.append(opt_error) print('validation optimal value: ', opt_error) log_buffer.append('validation optimal value: ' + str(opt_error)) opt_mean = np.mean(np.array(opt_error_list), axis=0) opt_std = np.std(np.array(opt_error_list), axis=0) print('--------------------------------------------------') print('optimization result for ' + str(opt_repeat) + ' times average: ', opt_mean, ', standard variance is: ', opt_std) log_buffer.append('--------------------------------------------------') log_buffer.append('optimization result for ' + str(opt_repeat) + ' times average: ' + str(opt_mean) + ', standard variance is: ' + str(opt_std)) return opt_mean
def run_racos(): # parameters sample_size = 10 # the instance number of sampling in an iteration budget = 500 # budget in online style positive_num = 2 # the set size of PosPop rand_probability = 0.99 # the probability of sample in model uncertain_bit = 1 # the dimension size that is sampled randomly bias_region = 0.5 repeat = 10 # dimension setting dimension_size = 10 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-1.0, 1.0] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) func = DistributedFunction(dim=dimension, bias_region=[-bias_region, bias_region]) if problem_name == 'rosenbrock': prob = func.DisRosenbrock else: prob = func.DisSphere # optimization racos = RacosOptimization(dimension) opt_error_list = [] for i in range(repeat): start_t = time.time() racos.mix_opt(prob, ss=sample_size, bud=budget, pn=positive_num, rp=rand_probability, ub=uncertain_bit) end_t = time.time() optimal = racos.get_optimal() hour, minute, second = time_formulate(start_t, end_t) print('total budget is ', budget, '------------------------------') print('spending time: ', hour, ' hours ', minute, ' minutes ', second, ' seconds') print('optimal value: ', optimal.get_fitness()) opt_error = optimal.get_fitness() optimal_x = optimal.get_features() opt_error_list.append(opt_error) print('validation optimal value: ', opt_error) log_buffer.append('validation optimal value: ' + str(opt_error)) print('optimal x: ', optimal_x) log_buffer.append('optimal nn structure: ' + list2string(optimal_x)) opt_mean = np.mean(np.array(opt_error_list)) opt_std = np.std(np.array(opt_error_list)) print('--------------------------------------------------') print('optimization result: ', opt_mean, '#', opt_std) log_buffer.append('--------------------------------------------------') log_buffer.append('optimization result: ' + str(opt_mean) + '#' + str(opt_std)) return opt_mean
def run_for_synthetic_problem(): sample_size = 10 # the instance number of sampling in an iteration budget = 50 # budget in online style positive_num = 2 # the set size of PosPop rand_probability = 0.99 # the probability of sample in model uncertain_bit = 1 # the dimension size that is sampled randomly adv_threshold = 10 # advance sample size opt_repeat = 10 dimension_size = 10 problem_name = 'sphere' bias_region = 0.5 eta = 0.9 step = 100 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-1.0, 1.0] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) log_buffer = [] # problem define func = DistributedFunction(dimension, bias_region=[-0.5, 0.5]) target_bias = [0.2 for _ in range(dimension_size)] func.setBias(target_bias) if problem_name == 'ackley': prob_fct = func.DisAckley else: prob_fct = func.DisSphere log_buffer.append('+++++++++++++++++++++++++++++++') log_buffer.append('optimization parameters') log_buffer.append('sample size: ' + str(sample_size)) log_buffer.append('budget: ' + str(budget)) log_buffer.append('positive num: ' + str(positive_num)) log_buffer.append('random probability: ' + str(rand_probability)) log_buffer.append('uncertain bits: ' + str(uncertain_bit)) log_buffer.append('advance num: ' + str(adv_threshold)) log_buffer.append('+++++++++++++++++++++++++++++++') log_buffer.append('problem parameters') log_buffer.append('dimension size: ' + str(dimension_size)) log_buffer.append('problem name: ' + problem_name) log_buffer.append('bias: ' + list2string(target_bias)) log_buffer.append('+++++++++++++++++++++++++++++++') predictors, load_buffer = get_predicotrs() expert = Experts(predictors=predictors, eta=eta, step=step) log_buffer.extend(load_buffer) opt_error_list = [] for i in range(opt_repeat): print('optimize ', i, '===================================================') log_buffer.append( 'optimize ' + str(i) + '===================================================') exp_racos = ExpAdaRacosOptimization(dimension, expert) start_t = time.time() exp_racos.exp_ada_mix_opt(obj_fct=prob_fct, ss=sample_size, bud=budget, pn=positive_num, rp=rand_probability, ub=uncertain_bit, at=adv_threshold) end_t = time.time() print('total budget is ', budget) log_buffer.append('total budget is ' + str(budget)) hour, minute, second = time_formulate(start_t, end_t) print('spending time: ', hour, ':', minute, ':', second) log_buffer.append('spending time: ' + str(hour) + '+' + str(minute) + '+' + str(second)) optimal = exp_racos.get_optimal() opt_error = optimal.get_fitness() optimal_x = optimal.get_features() opt_error_list.append(opt_error) print('validation optimal value: ', opt_error) log_buffer.append('validation optimal value: ' + str(opt_error)) print('optimal x: ', optimal_x) log_buffer.append('optimal nn structure: ' + list2string(optimal_x)) opt_mean = np.mean(np.array(opt_error_list)) opt_std = np.std(np.array(opt_error_list)) print('--------------------------------------------------') print('optimization result: ', opt_mean, '#', opt_std) log_buffer.append('--------------------------------------------------') log_buffer.append('optimization result: ' + str(opt_mean) + '#' + str(opt_std)) result_path = path + '/Results/Ada/' + problem_name + '/dimension' + str( dimension_size) + '/' optimization_log_file = result_path + 'opt-log-' + problem_name + '-dim' + str(dimension_size) + '-bias' \ + str(bias_region) + '.txt' print('optimization logging: ', optimization_log_file) fo.FileWriter(optimization_log_file, log_buffer, style='w') return
print mean_r, '#', std_r return # continuous optimization if True: # dimension setting repeat = 15 results = [] DimSize = 100 regs = [] regs.append(0.0) regs.append(1.0) dim = Dimension() dim.setDimensionSize(DimSize) for i in range(DimSize): dim.setRegion(i, regs, True) for i in range(repeat): print i, ':--------------------------------------------------------------' racos = RacosOptimization(dim) # call online version RACOS # racos.OnlineTurnOn() # racos.ContinueOpt(Ackley, SampleSize, Budget, PositiveNum, RandProbability, UncertainBits) racos.ContinueOpt(Ackley, SampleSize, MaxIteration, PositiveNum, RandProbability, UncertainBits)
rand_probability = 0.99 # the probability of sample in model uncertain_bits = 2 # the dimension size that is sampled randomly start_index = 0 problem_name = 'sphere' problem_num = 2000 - start_index repeat_num = 10 exp_path = path + '/ExpLog/SyntheticProbsLog/' bias_region = 0.5 dimension_size = 10 dimension = Dimension() dimension.set_dimension_size(dimension_size) dimension.set_regions([[-1.0, 1.0] for _ in range(dimension_size)], [0 for _ in range(dimension_size)]) def run_exp_racos_for_synthetic_problem_analysis(): # parameters positive_num = 2 # the set size of PosPop rand_probability = 0.99 # the probability of sample in model uncertain_bit = 1 # the dimension size that is sampled randomly adv_threshold = 10 # advance sample size opt_repeat = 10 log_buffer = []