def covetBoundTline(rf, bound, disbd): x0 = bound[0] x1 = bound[1] try: y0 = rf(bound[0]) except (ValueError, ZeroDivisionError, OverflowError, Warning, TypeError): y0 = rf(bound[0] + bf.getulp(bound[0])) try: y1 = rf(bound[1]) except (ValueError, ZeroDivisionError, OverflowError, Warning, TypeError): y1 = rf(bound[1] - bf.getulp(bound[1])) ulp_x0 = bf.getulp(x0) y1 = float(y1) y0 = float(y0) k = (y1 - y0) / (x1 - x0) delta_val = k * ulp_x0 if bf.getulp(y1) < bf.getulp(y0): s_y = y1 s_x = x1 e_x = x0 e_y = y0 else: s_y = y0 s_x = x0 e_x = x1 e_y = y1 l_fun = lambda x: (x - s_x) * k + s_y if disbd == 2.0: return [(s_x, s_y), delta_val, bf.getMidDistance(x1, x0), 1.0, 1.0, x1, (x1, x0), (e_x, e_y)] glob_fitness_fun = np.frompyfunc(lambda x: bf.fitness_fun(rf, l_fun, x), 1, 1) # ret = differential_evolution(glob_fitness_fun,popsize=15, bounds=[bound]) mid_point = x0 + (x1 - x0) / 2.0 ret = minimize(glob_fitness_fun, [mid_point], bounds=[bound]) mid_b = ret.x[0] if 1.0 / glob_fitness_fun(mid_point) - 1.0 / ret.fun > 0: mid_b = mid_point print "replace" max_err = float(rf(mid_point)) - l_fun(mid_point) estimate_max_error = (mid_point - x0) * (mid_point - x1) curve_k = float(max_err / estimate_max_error) if curve_k == 0.0: curve_k = 1.0 return [(s_x, s_y), delta_val, bf.getMidDistance(x1, x0), 1.0, curve_k, mid_b, (x1, x0), (e_x, e_y)]
def lineAproFun(kb_val, x): i = kb_val[0] j = kb_val[7] if x == j[0]: return j[1] dv = kb_val[1] ulp_x = bf.getulp(x) if kb_val[4] == 0: compen = 0 else: compen = (x - i[0]) * (x - j[0]) * kb_val[4] rs = (x - i[0]) / ulp_x * dv + i[1] + compen return float(rs)
def derivingApproximation(th, bound_l, rf, name, filename, inp): bound_idx = 0 num_line = 0 save_line = [] exr.glob_point_l = [] for bound in bound_l: temp_ploy_fit = '' n = int(bf.getFPNum(bound[0], bound[1])) ori_bound = bound print bound print print "%.12e" % float(bf.getFPNum(bound[0], bound[1])) # To make sure the length of bound less than a value (1e12) limit_of_bound = 1e12 samll_bound_l = [] ulp_x = bf.getulp(bound[0]) # Step2: linear iterative approximation # partition the bound according to the limit_of_bound if n / limit_of_bound > 2.0: temp_bound0 = bound[0] temp_dis = limit_of_bound * ulp_x while (temp_bound0 + temp_dis < bound[1]): if (inp < temp_bound0 + temp_dis) & (inp > temp_bound0): samll_bound_l.append([temp_bound0, inp]) samll_bound_l.append([inp, temp_bound0 + temp_dis]) else: samll_bound_l.append([temp_bound0, temp_bound0 + temp_dis]) temp_bound0 = temp_bound0 + temp_dis samll_bound_l.append([temp_bound0, bound[1]]) print len(samll_bound_l) print bound i = 0 for idx_b in samll_bound_l: i = i + 1 iter_liner_build(th, idx_b, rf, n) else: if (inp < bound[1]) & (inp > bound[0]): iter_liner_build(th, [bound[0], inp], rf, n) iter_liner_build(th, [inp, bound[1]], rf, n) else: iter_liner_build(th, bound, rf, n) if len(exr.glob_point_l) >= 30: temp_ploy_fit = generate_fitting(exr.glob_point_l) covertToC(exr.glob_point_l, n, name, bound_idx, filename, ori_bound, temp_ploy_fit) save_line = save_line + exr.glob_point_l num_line = num_line + len(exr.glob_point_l) bound_idx = bound_idx + 1 exr.glob_point_l = [] return save_line, num_line
def pointToBound(th, rf, pf, point): print "Begin Find the bound around the inputs and under the threshold" # right ward iteration step = 4e2 print "Right forward to find the up bound" ulp_p = bf.getulp(point) p0_err = bf.getUlpError(rf(point), pf(point)) temp_b1 = point for i in range(0, int(4e2)): temp_b1 = temp_b1 + ulp_p * step max_err_mid, temp_b1 = bf.max_errorOnPoint(rf, pf, temp_b1, step) try: times = np.max([np.log10(max_err_mid / th), 2.0]) except AttributeError: times = 1.0 if (max_err_mid < th): temp_b1 = step_back(th, temp_b1, step, rf, pf, -1, ulp_p) bound_up = temp_b1 break step = int(step * times) print "Left forward to find the down bound" step = 4e2 temp_b1 = point for i in range(0, int(4e2)): temp_b1 = temp_b1 - ulp_p * step max_err_mid, temp_b1 = bf.max_errorOnPoint(rf, pf, temp_b1, step) try: times = np.max([np.log10(max_err_mid / th), 2.0]) except AttributeError: times = 1.0 # print step # times = np.max([np.log2(max_err_mid / th), 2.0]) if (max_err_mid < th): # print step / times temp_b1 = step_back(th, temp_b1, step, rf, pf, 1, ulp_p) bound_down = temp_b1 break step = int(step * times) return [bound_down, bound_up]
def covertToC(glob_l, n, name, idx, filename, bound, temp_ploy_fit): print "Cover To C code" orig_stdout = sys.stdout name = 'patch_of_' + name len_glob = len(glob_l) x_0 = bound[0] ulp_x = bf.getulp(glob_l[0][0][0]) temp_n = 0.0 temp_n_max = 0.0 f = open(filename + '/' + name + '.c', 'a') name = name.split("gsl_")[1] sys.stdout = f print 'static double array_x_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[0][0] print '};' print 'static double array_y_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[0][1] print '};' print 'static double array_e_y_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[7][1] print '};' print 'static double array_detla_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[1] print '};' print 'static double array_idx_' + name + '_' + str(idx) + '[' + str( len_glob + 1) + '] = {' print "%.18e," % temp_n for i in glob_l: print "%.18e," % (i[2] + temp_n) temp_n = i[2] + temp_n print '};' # print 'static double array_maxX_' + str(idx) + '[' + str(len_glob) + '] = {' # for i in glob_l: # print "%.15e," % (i[3]+temp_n_max) # temp_n_max = (i[2]+temp_n_max) # print '};' print 'static double array_maxE_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[4] print '};' print "double accuracy_improve_patch_of_gsl_" + name + '_' + str( idx) + "(double x)" print "{" print " long int n = " + str(n) + ";" print " int len_glob = " + str(len_glob) + ";" print " double ulp_x = " + repr(ulp_x) + ";" print " double x_0 = " + repr(x_0) + ";" print " double compen = 0.0;" print " double n_x = ((x-x_0)/ulp_x);" if temp_ploy_fit == '': # print " int idx = floor(n_x*len_glob/n);" print " int idx = floor(len_glob/2);" else: print temp_ploy_fit print " if(idx>=len_glob){" print " idx = len_glob-1;" print " }" print " while((idx>=0)&&(idx<len_glob)){" print " if((n_x>array_idx_" + name + '_' + str( idx) + "[idx])&&(n_x<array_idx_" + name + '_' + str(idx) + "[idx+1])){" print " compen = ulp_x*ulp_x * (n_x-array_idx_" + name + '_' + str( idx) + "[idx+1])*(n_x-array_idx_" + name + '_' + str( idx) + "[idx])*array_maxE_" + name + '_' + str(idx) + "[idx];" print " return (x-array_x_" + name + '_' + str( idx) + "[idx])/ulp_x*array_detla_" + name + '_' + str( idx) + "[idx]+array_y_" + name + '_' + str(idx) + "[idx]+compen;" print " }" print " else if(n_x<array_idx_" + name + '_' + str(idx) + "[idx]){" print " idx = idx - 1;" print " }" print " else if(n_x>array_idx_" + name + '_' + str(idx) + "[idx+1]){" print " idx = idx + 1;" print " }" print " else if(x==array_x_" + name + '_' + str(idx) + "[idx]){" print " return array_y_" + name + '_' + str(idx) + "[idx];" print " }" print " else{" print " return array_e_y_" + name + '_' + str(idx) + "[idx];" print " }" print " }" print "}" sys.stdout = orig_stdout f.close()
def DEMC(rf,pf,inpdm,fnm,limit_n,limit_time): st = time.time() file_name = "../experiments/detecting_results/DEMC/" + fnm if not os.path.exists("../experiments/detecting_results/DEMC/"): os.makedirs("../experiments/detecting_results/DEMC/") count = 0 final_max = 0.0 final_x = 0.0 final_count1 = 0 final_count2 = 0 final_bound = [] record_res_l = [] dom_l = bf.fdistribution_partition(inpdm[0], inpdm[1]) glob_fitness_con = np.frompyfunc(lambda x: bf.fitness_fun1(rf, pf, x), 1, 1) glob_fitness_real = np.frompyfunc(lambda x: bf.fitness_fun(rf, pf, x), 1, 1) try: print "Detecting possible maximum error by DEMC 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 = [] for k in dom_l: temp_max = 0.0 temp_x = 0.0 res = differential_evolution(glob_fitness_con, popsize=15, bounds=[k], polish=False, strategy='best1bin') x = res.x[0] count2 = count2+res.nfev err = 1.0/glob_fitness_real(x) if err > temp_max: temp_max = err temp_x = x temp = [temp_max, temp_x, k] res_l.append(temp) t1 = time.time() - temp_st # print t1 res_l = sorted(res_l, reverse=True) 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]: gen_l = produce_interval(j[1], j[2]) glob_fitness_real_temp = lambda x: bf.fitness_fun(rf, pf, reduce_x(gen_l[0],gen_l[1],x)) # glob_fitness_real_temp = lambda x: bf.fitness_fun(rf, pf, x) x = math.asin((2*j[1]-gen_l[0]-gen_l[1])/(gen_l[1]-gen_l[0])) # x = j[1] res = basinhopping(glob_fitness_real_temp,x,stepsize=bf.getulp(x)*1e10,minimizer_kwargs=minimizer_kwargs,niter_success=10,niter=200) count1 = count1 + res.nfev # x = res.x[0] x = reduce_x(gen_l[0],gen_l[1],res.x[0]) err = 1.0/res.fun temp = [err, x, gen_l] 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 produce_interval(x,k): a = bf.getulp(x)*1e14 return [np.max([x-a,k[0]]),np.min([x+a,k[1]])]