def random_res(in_l, f1, f2, tlimit, exe_time): start_time = time.time() len_inl = len(in_l) l_var = [] in_var = in_l for i in in_var: tmp_l = depart(i[0], i[1]) l_var.append(tmp_l) l_confs = [] for element in itertools.product(*l_var): l_confs.append(element) len_confs = len(l_confs) print len_inl print len_confs * pow(20, len_inl) * tlimit / (exe_time) n = int( pow((len_confs * pow(20.0, float(len_inl)) * tlimit / (exe_time)), 1.0 / float(len_inl))) print n var_l = [] for k in list(in_l): var_l.append(sorted(np.random.uniform(k[0], k[1], n))) input_l = [] for element in itertools.product(*var_l): input_l.append(element) res_mp = test_mp_fun(f1, input_l) # res_mp = paral_proc_mppl(X,cof,200,1) res_d = test_gsl_fun(f2, input_l) # res_d = paral_proc_pl(X,cof,len(cof)-1,1) re_err_1 = [(np.fabs(fdistan.fdistan(op_f[0], float(op_r[0]))), op_f[1]) for op_f, op_r in zip(res_d, res_mp)] error_l = sorted(re_err_1, reverse=True) end_time = time.time() - start_time return (error_l[0][0], error_l[0][1], end_time)
def test_two_fun(f1, f2, input_l): res_mp = test_mp_fun(f1, input_l) res_d = test_gsl_fun(f2, input_l) re_err_1 = [(np.fabs(fdistan.fdistan(op_f[0], float(op_r[0]))), op_f[1]) for op_f, op_r in zip(res_d, res_mp)] error_l = sorted(re_err_1, reverse=True) return (error_l[0][0], error_l[0][1])
def para_random_res_tmp(in_l, f1, f2, per): start_time = time.time() var_l = [] for k in list(in_l): n = int(per * fdistan.fdistan(k[0], k[1])) print n var_l.append(sorted(np.random.uniform(k[0], k[1], n))) input_l = [] vp = 0 tmp_var_l = [] tmp_input_l = [] for vars in var_l: tmp_var_l.append(vars) if vp == 1: for iters in itertools.product(*tmp_var_l): tmp_input_l.append(iters) tmp_var_l = [] input_l.append(tmp_input_l) vp = 0 else: vp = vp + 1 if tmp_var_l != []: input_l.append(tmp_var_l) p = Pool() lf1 = [] lf2 = [] re_err_1 = [] for inp in range(0, len(input_l[1][0]), 8): one_test_l = [] tmp_test_l = [] for ti in range(0, 8): if inp + ti < len(input_l[1][0]): for tnp in itertools.product(input_l[0], [input_l[1][0][inp + ti]]): tmp_tnp = list(tnp[0]) tmp_tnp.append(tnp[1]) tmp_test_l.append(tmp_tnp) one_test_l.append(tmp_test_l) lf1.extend([f1] * len(one_test_l)) lf2.extend([f2] * len(one_test_l)) tmp_res = p.map(test_two_fun, lf1, lf2, one_test_l) tmp_res = sorted(tmp_res, reverse=True) re_err_1.append(tmp_res[0]) error_l = sorted(re_err_1, reverse=True) end_time = time.time() - start_time return (error_l[0][0], error_l[0][1], end_time)
def pfulp_test_minus(in_l,f1,f2,tlimit,exe_time): #输入区间上下界 in_max = in_l[1] in_min = in_l[0] max_err = 0.0 n = int(tlimit*1000/exe_time) # 得到输入X X = sorted(np.random.uniform(in_min, in_max, n)) # 得到高精度输出 res_mp = test_mp_fun(f1, X) # res_mp = paral_proc_mppl(X,cof,200,1) # 得到double输出 res_d = test_gsl_fun(f2, X) # res_d = paral_proc_pl(X,cof,len(cof)-1,1) # 根据公式,得到相对误差nmp.abs((op_f[0] - op_r[0]) / (nmp.nextafter(float(op_r[0]), float(op_r[0]) + 1) - float(op_r[0]))) re_err_1 = [(fdistan.fdistan(op_f[0], float(op_r[0])), op_f[1]) for op_f, op_r in zip(res_d, res_mp)] error_l = sorted(re_err_1,reverse=True) return (error_l[0][0],error_l[0][1],in_l)
def para_random_res(in_l, f1, f2, per, cpu_n): start_time = time.time() var_l = [] lf1 = [] lf2 = [] p = Pool(cpu_n) for k in list(in_l): n = int(per * fdistan.fdistan(k[0], k[1])) print n var_l.append(sorted(np.random.uniform(k[0], k[1], n))) input_l = [] for element in itertools.product(*var_l): inp = list(element) input_l.append(inp) input_l = chunks(input_l, 8) lf1.extend([f1] * len(input_l)) lf2.extend([f2] * len(input_l)) tmp_res = p.map(test_two_fun, lf1, lf2, input_l) tmp_res = sorted(tmp_res, reverse=True) end_time = time.time() - start_time return (tmp_res[0][0], tmp_res[0][1], end_time)
def para_random_res_tlimit(in_l, f1, f2, limit_n, cpu_n): start_time = time.time() var_l = [] lf1 = [] lf2 = [] p = Pool(cpu_n) n_l = [] ts = 0 i = 0 for k in list(in_l): dis_i = int(fdistan.fdistan(k[0], k[1])) n_l.append(dis_i) ts = dis_i + ts print n_l ts = float(ts) tmp_n = [i / ts for i in n_l] print tmp_n tmp_mul = reduce(mul, tmp_n) print tmp_mul basic_k = pow((limit_n / tmp_mul), 1.0 / len(in_l)) print basic_k j = 0 for k in list(in_l): n = int(tmp_n[j] * basic_k) print n var_l.append(sorted(np.random.uniform(k[0], k[1], n))) j = j + 1 input_l = [] for element in itertools.product(*var_l): inp = list(element) input_l.append(inp) print len(input_l) input_l = chunks(input_l, 8) lf1.extend([f1] * len(input_l)) lf2.extend([f2] * len(input_l)) tmp_res = p.map(test_two_fun, lf1, lf2, input_l) tmp_res = sorted(tmp_res, reverse=True) end_time = time.time() - start_time return (tmp_res[0][0], tmp_res[0][1], end_time)
def random_search(in_var, f1, f2, n): var_l = [] l_var = [] for i in in_var: tmp_l = depart(i[0], i[1]) l_var.append(tmp_l) l_confs = [] for element in itertools.product(*l_var): l_confs.append(element) input_l = [] for k in l_confs: var_l = [] for z in k: var_l.append(sorted(np.random.uniform(z[0], z[1], n))) for element in itertools.product(*var_l): input_l.append(element) res_mp = test_mp_fun(f1, input_l) # res_mp = paral_proc_mppl(X,cof,200,1) res_d = test_gsl_fun(f2, input_l) # res_d = paral_proc_pl(X,cof,len(cof)-1,1) re_err_1 = [(np.fabs(fdistan.fdistan(op_f[0], float(op_r[0]))), op_f[1]) for op_f, op_r in zip(res_d, res_mp)] error_l = sorted(re_err_1, reverse=True) return (error_l[0][0], error_l[0][1], in_var)
def get_distan(f1,f2,x): m_x = float(f1(x)) f_x = f2(x) return np.fabs(fdistan.fdistan(m_x,f_x))
def binary_res(in_l,f1,f2,iter,n): #输入区间上下界 in_max = in_l[1] in_min = in_l[0] max_err = 0.0 #考虑迭代次数iter,每个区间取点数目n for i in range(0,iter): mid = (in_max-in_min)/2.0+in_min #计算区间1的最大误差 #得到输入X X = sorted(np.random.uniform(in_min, mid, n)) #得到高精度输出 res_mp = test_mp_fun(f1,X) #res_mp = paral_proc_mppl(X,cof,200,1) #得到double输出 res_d = test_gsl_fun(f2,X) #res_d = paral_proc_pl(X,cof,len(cof)-1,1) #根据公式,得到相对误差nmp.abs((op_f[0] - op_r[0]) / (nmp.nextafter(float(op_r[0]), float(op_r[0]) + 1) - float(op_r[0]))) re_err_1=[(fdistan.fdistan(op_f[0],float(op_r[0])),op_f[1]) for op_f, op_r in zip(res_d, res_mp)] mxer = sorted(re_err_1, reverse=True)[0][0] #re_err_1 = [(nmp.abs((op_f[0]-op_r[0])/(nmp.nextafter(float(op_r[0]),float(op_r[0])+1)-float(op_r[0]))),op_f[1]) for op_f,op_r in zip(res_d,res_mp)] #计算区间2的最大误差 # 得到输入X X = sorted(np.random.uniform(mid, in_max, n)) # 得到高精度输出 res_mp = test_mp_fun(f1, X) # res_mp = paral_proc_mppl(X,cof,200,1) # 得到double输出 res_d = test_gsl_fun(f2, X) # res_d = paral_proc_pl(X,cof,len(cof)-1,1) # 根据公式,得到相对误差nmp.abs((op_f[0] - op_r[0]) / (nmp.nextafter(float(op_r[0]), float(op_r[0]) + 1) - float(op_r[0]))) re_err_2 = [(fdistan.fdistan(op_f[0],float(op_r[0])),op_f[1]) for op_f, op_r in zip(res_d, res_mp)] #re_err_2 = [(nmp.abs((op_f[0]-op_r[0])/(nmp.nextafter(float(op_r[0]),float(op_r[0])+1)-float(op_r[0]))),op_f[1]) for op_f,op_r in zip(res_d,res_mp)] #分别得到两个区间的最大误差: re_err_3 = sorted(re_err_2,reverse=True) x_1 = 0.0 x_2 = 0.0 erl_1 = 0.0 for j in re_err_1: if j[0] > erl_1: erl_1 = j[0] x_1 = j[1] erl_2 = 0.0 for j in re_err_2: if j[0] > erl_2: erl_2 = j[0] x_2 = j[1] if(erl_1>erl_2): #如果区间1的相对误差最大值更大,那么继续在区间1内搜索 in_min = in_min in_max = mid max_err = erl_1 out_min = in_min out_max = in_max out_x = x_1 else: in_min = mid in_max = in_max max_err = erl_2 out_min = in_min out_max = in_max out_x = x_2 return (max_err,out_x,[out_min,out_max])
def distan_cal(a,b): return np.fabs(fdistan.fdistan(a,b))
def test_sig(f1,f2,x,y): d_x = f2(x,y) m_x = float(f1(x,y)) print fdistan.fdistan(d_x,m_x) print d_x-m_x