def PTB_2D(max_err,point,rf,pf,th): ini_step = 1e4 print "Begin to find the bound" # getting a initial bound ini_bound = generate_bound(point,ini_step) print ini_bound # sampling in ini_bound input_l = produce_n_input(ini_bound, 100) # extract a line function to trace the maximum error glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x) res_l = [] sum_err = 0 for i in input_l: temp_err = 1.0/glob_fitness_real(i) res_l.append(temp_err) sum_err = sum_err+math.log(temp_err) print sum_err temp_th = (th-sum_err/len(res_l))/2.0 + sum_err/len(res_l) print temp_th p = mid_line_finder(temp_th, input_l,res_l) print p # trace the line function to find new bound up_point = find_bound(point, ini_step, p, rf, pf, th, 1) down_point = find_bound(point, ini_step, p, rf, pf, th, -1) new_bound = [[down_point[0], up_point[0]], [down_point[1], up_point[1]]] temp_res = DDEMC_pure(rf, pf, [new_bound],3, 100) new_point = temp_res[1] return new_bound,new_point
def plot_line_err(rf,pf,input_l,p): glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x) X = [] Y = [] Yl = [] Z = [] Z2 = [] U = [] st = time.time() for i in input_l: temp_err = 1.0 / glob_fitness_real(i) # temp_err2 = glob_fitness_real2(i) # print log(temp_err) X.append(i[0]) Y.append(i[1]) Yl.append(p(i[0])) Z.append(float(math.log(temp_err))) U.append(float(rf(i[0], i[1]))) print "run time is " print time.time() - st print max(Z) print Yl[0:10] plot_3D_error(X, Y, Z, Z2, U, Yl)
def DDEMC_pure(rf, pf, inpdm, limit_n, limit_time): st = time.time() count = 0 final_max = 0.0 final_x = [] final_count1 = 0 final_count2 = 0 final_bound = [] record_res_l = [] dom_l = bf.fpartition(inpdm) glob_fitness_con = lambda x: bf.mfitness_fun1(rf, pf, x) glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x) try: signal.alarm(limit_time) while (count < limit_n): temp_st = time.time() count1 = 0 count2 = 0 rand_seed = np.random.randint(1, 1000000) np.random.seed(rand_seed) res_l = [] temp_max = 0.0 for k in dom_l: temp_x = [] res = differential_evolution(glob_fitness_con, popsize=15, bounds=k, polish=False, strategy='best1bin') x = res.x count2 = count2 + res.nfev err = 1.0 / glob_fitness_real(x) if err > temp_max: temp_max = err temp_x = x temp = [temp_max, list(temp_x), k] res_l.append(temp) t1 = time.time() - temp_st # print t1 res_l.sort() res_l.reverse() temp_max = res_l[0][0] temp_x = res_l[0][1] bound = res_l[0][2] res_lr = [] s_len = np.min([len(res_l), 20]) # print res_l[0:s_len] # glob_fitness_real_temp = lambda x: x*x minimizer_kwargs = {"method": "Nelder-Mead"} for j in res_l[0:s_len]: # x = j[1] gen_l = produce_interval(j[1], j[2]) glob_fitness_real_temp = lambda z: bf.mfitness_fun( rf, pf, reduce_x(gen_l, z)) # # glob_fitness_real_temp = lambda x: bf.fitness_fun(rf, pf, x) x = generate_x(gen_l, j[1]) res = basinhopping(glob_fitness_real_temp, x, minimizer_kwargs=minimizer_kwargs, niter_success=10, niter=200) count1 = count1 + res.nfev x = res.x err = 1.0 / res.fun temp = [err, x] res_lr.append(temp) if err > temp_max: temp_max = err temp_x = reduce_x(gen_l, x) bound = j[2] t2 = time.time() - temp_st temp_l = [ temp_max, temp_x, bound, t2, count1, count2, rand_seed, count, t1 ] # print temp_l final_count1 = final_count1 + count1 final_count2 = final_count2 + count2 record_res_l.append(temp_l) count = count + 1 if temp_max > final_max: final_max = temp_max final_x = temp_x final_bound = bound final_time = time.time() - st return [ final_max, final_x, final_bound, final_time, count, final_count1, final_count2 ] except TimeoutError: final_time = time.time() - st return [ final_max, final_x, final_bound, final_time, count, final_count1, final_count2 ]
def DDEMC_root(rf, pf, inpdm, fnm, limit_n, limit_time): st = time.time() file_name = "../experiments/detecting_results/DDEMC4v_1/" + fnm if not os.path.exists("../experiments/detecting_results/DDEMC4v_1/"): os.makedirs("../experiments/detecting_results/DDEMC4v_1/") count = 0 final_max = 0.0 final_x = [] final_count1 = 0 final_count2 = 0 final_bound = [] record_res_l = [] dom_l = bf.fpartition(inpdm) glob_fitness_con = lambda x: bf.mfitness_fun1(rf, pf, x) glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x) try: print "Detecting possible maximum error by DDEMC algorithm" signal.alarm(limit_time) while (count < limit_n): temp_st = time.time() count1 = 0 count2 = 0 rand_seed = bf.rd_seed[count] np.random.seed(rand_seed) res_l = [] pec_count = 0 len_dom = len(dom_l) # print len_dom for k in dom_l: temp_max = 0.0 temp_x = [] # res = differential_evolution(glob_fitness_con, popsize=15, bounds=k, polish=False, strategy='best1bin') mid_p = get_mid(k) fake_pf = lambda x: pf(*x) res = minimize(fake_pf, mid_p, bounds=k) print res x = res.x count2 = count2 + res.nfev err = 1.0 / glob_fitness_real(x) if err > temp_max: temp_max = err temp_x = x temp = [temp_max, list(temp_x), k] res_l.append(temp) pec_count = pec_count + 1 # pec_dom = int(pec_count*100.0/float(len_dom)) # if (pec_dom >= 10) & (math.fmod(pec_dom,10) == 0): # print repr(pec_dom)+"%" t1 = time.time() - temp_st # print t1 res_l.sort() res_l.reverse() temp_max = res_l[0][0] temp_x = res_l[0][1] bound = res_l[0][2] res_lr = [] s_len = np.min([len(res_l), 10]) # print res_l[0:s_len] # glob_fitness_real_temp = lambda x: x*x minimizer_kwargs = {"method": "Nelder-Mead"} for j in res_l[0:s_len]: # x = j[1] gen_l = produce_interval(j[1], j[2]) glob_fitness_real_temp = lambda z: bf.mfitness_fun( rf, pf, reduce_x(gen_l, z)) # # glob_fitness_real_temp = lambda x: bf.fitness_fun(rf, pf, x) x = generate_x(gen_l, j[1]) res = basinhopping(glob_fitness_real_temp, x, minimizer_kwargs=minimizer_kwargs, niter_success=10, niter=200) count1 = count1 + res.nfev x = reduce_x(gen_l, res.x) err = 1.0 / res.fun temp = [err, x] res_lr.append(temp) if err > temp_max: temp_max = err temp_x = x bound = j[2] t2 = time.time() - temp_st temp_l = [ temp_max, temp_x, bound, t2, count1, count2, rand_seed, count, t1 ] # print temp_l final_count1 = final_count1 + count1 final_count2 = final_count2 + count2 record_res_l.append(temp_l) count = count + 1 if temp_max > final_max: final_max = temp_max final_x = temp_x final_bound = bound final_time = time.time() - st bf.output_err(record_res_l, file_name, fnm) return [ final_max, final_x, final_bound, final_time, count, final_count1, final_count2 ] except TimeoutError: final_time = time.time() - st bf.output_err(record_res_l, file_name, fnm) return [ final_max, final_x, final_bound, final_time, count, final_count1, final_count2 ]
def DDEMC_para(rf, pf, count, inpdm, fnm, limit_time, id): st = time.time() file_name = "../experiments/detecting_results/DDEMC" + str(id) + "v/" + fnm final_max = 0.0 final_x = [] final_count1 = 0 final_count2 = 0 final_bound = [] record_res_l = [] dom_l = bf.fpartition(inpdm) glob_fitness_con = lambda x: bf.mfitness_fun1(rf, pf, x) glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x) try: print "Detecting possible maximum error by DDEMC algorithm" signal.alarm(limit_time) temp_st = time.time() count1 = 0 count2 = 0 # rand_seed = np.random.randint(1,1000000) rand_seed = bf.rd_seed[count] np.random.seed(rand_seed) res_l = [] pec_count = 0 len_dom = len(dom_l) # print len_dom for k in dom_l: temp_max = 0.0 temp_x = [] res = differential_evolution(glob_fitness_con, popsize=15, bounds=k, polish=False, strategy='best1bin') x = res.x count2 = count2 + res.nfev err = 1.0 / glob_fitness_real(x) if err > temp_max: temp_max = err temp_x = x temp = [temp_max, list(temp_x), k] res_l.append(temp) pec_count = pec_count + 1 pec_dom = int(pec_count * 100.0 / float(len_dom)) # if (pec_dom >= 10) & (math.fmod(pec_dom,10) == 0): # print repr(pec_dom)+"%" t1 = time.time() - temp_st # print t1 res_l.sort() res_l.reverse() temp_max = res_l[0][0] temp_x = res_l[0][1] bound = res_l[0][2] res_lr = [] s_len = np.min([len(res_l), 10]) # print res_l[0:s_len] # glob_fitness_real_temp = lambda x: x*x distan_two_search_x = [1.0] * len(temp_x) minimizer_kwargs = {"method": "Nelder-Mead"} for j in res_l[0:s_len]: # x = j[1] gen_l = produce_interval(j[1], j[2]) glob_fitness_real_temp = lambda z: bf.mfitness_fun( rf, pf, reduce_x(gen_l, z)) # # glob_fitness_real_temp = lambda x: bf.fitness_fun(rf, pf, x) x = generate_x(gen_l, j[1]) res = basinhopping(glob_fitness_real_temp, x, minimizer_kwargs=minimizer_kwargs, niter_success=10, niter=200) count1 = count1 + res.nfev x = reduce_x(gen_l, res.x) err = 1.0 / res.fun temp = [err, x] res_lr.append(temp) temp_distan_two_search_x = get_point_distance(x, j[1]) if err > temp_max: temp_max = err temp_x = x bound = j[2] distan_two_search_x = temp_distan_two_search_x t2 = time.time() - temp_st # print distan_two_search_x # temp_l = [temp_max, temp_x, bound, t2, count1, count2, rand_seed, count, t2-t1,distan_two_search_x] # print temp_l # final_count1 = final_count1 + count1 # final_count2 = final_count2 + count2 # record_res_l.append(temp_l) # count = count + 1 # distan_two_search_x_final = [1.0]*len(temp_x) # if temp_max > final_max: # final_max = temp_max # final_x = temp_x # final_bound = bound # distan_two_search_x_final = distan_two_search_x # # print distan_two_search_x_final # final_time = time.time() - st # bf.output_err(record_res_l, file_name, fnm) # print distan_two_search_x_final # [final_max, final_x, final_bound, final_time, count, final_count1, final_count2, distan_two_search_x_final] return [ temp_max, temp_x, bound, t2, count1, count2, rand_seed, count, t2 - t1, distan_two_search_x ] except TimeoutError: final_time = time.time() - st # bf.output_err(record_res_l, file_name, fnm) return [ temp_max, temp_x, bound, t2, count1, count2, rand_seed, count, t2 - t1, distan_two_search_x ]