Ejemplo n.º 1
0
def test_1v_bound(bound, pf, rf, th):
    glob_fitness_fun = np.frompyfunc(lambda x: bf.fitness_fun(rf, pf, x), 1, 1)
    ret = differential_evolution(glob_fitness_fun, popsize=25, bounds=[bound])
    # res = DEMC_pure(rf,pf,bound,3,1000)
    # max_err = res[0]
    max_err = 1.0 / glob_fitness_fun(ret.x[0])
    # max_x = res[1]
    max_x = ret.x[0]
    st = time.time()
    test_num = 100000
    input_l = np.random.uniform(bound[0], bound[1], test_num)
    pf_res = [pf(i) for i in input_l]
    end_time = time.time() - st
    rf_res = [rf(i) for i in input_l]
    err_lst = []
    good_tests = 0
    bad_tests = 0
    for i, j in zip(pf_res, rf_res):
        temp_err = bf.getUlpError(i, j)
        if temp_err <= th:
            good_tests = good_tests + 1
        else:
            bad_tests = bad_tests + 1
        err_lst.append(bf.getUlpError(i, j))
    mean_err = np.mean(err_lst)
    # temp_max = np.max(err_lst)
    # max_err = np.max([temp_max,max_err])
    per_good = good_tests / test_num
    return end_time, max_err, mean_err, max_x, per_good
Ejemplo n.º 2
0
def test_2v_bound(bound,pf,rf,th):
    # glob_fitness_fun = lambda x: bf.mfitness_fun(rf, pf, x)
    # ret = differential_evolution(glob_fitness_fun, popsize=25, bounds=bound)
    print bound
    # res = DDEMC_pure(rf,pf,[bound],3,20000)
    res = [10,10]
    max_err = res[0]
    # max_err = 1.0/glob_fitness_fun(ret.x[0])
    max_x = res[1]
    # max_x = ret.x[0]
    st = time.time()
    step = 1000
    input_l = produce_n_input(bound, step)
    for i in input_l:
        pf(*i)
    end_time = time.time() - st
    step = 10
    input_l = produce_n_input(bound, step)
    pf_res = [pf(*i) for i in input_l]
    rf_res = [rf(*i) for i in input_l]
    err_lst = [bf.getUlpError(i,j) for i,j in zip(pf_res,rf_res)]
    fid = 0
    sid = 0
    for i in err_lst:
        if i > th:
            fid = fid + 1
        else:
            sid = sid + 1
    mean_err = np.mean(err_lst)
    temp_max = np.max(err_lst)
    max_err = np.max([temp_max,max_err])
    srate = sid/float(step*step)
    return end_time,max_err,mean_err,max_x,srate
Ejemplo n.º 3
0
def Test_id_fun(n_var, idx, rd_seed, num, level, limit_time, th, table_name,
                lock):
    print "Random seed is :" + str(rd_seed)
    pwd = os.getcwd()
    detecting_res_file = pwd + "/final_results_" + str(n_var) + "v.xls"
    print detecting_res_file
    detecting_res = read_res_from_file(detecting_res_file, idx, num)
    print "hello"
    bound = detecting_res[-1]
    print "%.15e" % float(bf.getUlpError(bound[0], bound[1]))
    ld_module = importlib.import_module("bench" + str(n_var) + "v")
    # change it to youself system password
    password = "******"
    # refresh()
    pf = ld_module.gfl[idx - 1]
    rf = ld_module.rfl[idx - 1]
    fnm = detecting_res[0]
    inp = detecting_res[2]
    print inp
    print detecting_res
    max_ret = [detecting_res[1], inp]
    bound = detecting_res[-1]
    if np.log2(detecting_res[1]) > 32:
        res = main1v_tay(rf, pf, level, th, rd_seed, fnm, limit_time, num,
                         password, max_ret, bound, idx, lock)
        # res = main1v(rf, pf, level, th,rd_seed, fnm, limit_time, num, password,max_ret,bound,idx,lock)
        return res
Ejemplo n.º 4
0
def generate_bound_3level(n_var, idx, num):
    detecting_res_file = "final_results_" + str(n_var) + "v.xls"
    detecting_res = read_res_from_file1(detecting_res_file, idx)
    ld_module = importlib.import_module("bench" + str(n_var) + "v")
    # change it to youself system password
    password = "******"
    pf = ld_module.gfl[idx - 1]
    rf = ld_module.rfl[idx - 1]
    th_lst = [16, 12]
    th = np.power(2.0, th_lst[num - 1])
    inp = detecting_res[2]
    max_ret = [detecting_res[1], inp]
    print th
    max_x, bound, bound_l = detectHighErrs(max_ret, th, rf, pf)
    print bf.getUlpError(bound[0], bound[1])
    add_bound_back(detecting_res_file, bound, num, idx)
    return 0
Ejemplo n.º 5
0
def plot_err_inp(n_var, id, rd_seed):
    print "Random seed is :" + str(rd_seed)
    detecting_res_file = "final_results_" + str(n_var) + "v.xls"
    detecting_res = read_res_from_file(detecting_res_file, id, 1)
    ld_module = importlib.import_module("bench" + str(n_var) + "v")
    # change it to youself system password
    password = "******"
    # refresh()
    pf = ld_module.gfl[id - 1]
    rf = ld_module.rfl[id - 1]
    fnm = detecting_res[0]
    inp = detecting_res[2]
    # new_bound = generate_bound([inp],3e12)[0]
    new_bound = bf.getPointBound(inp, 5e12)
    # new_bound = [-226.19467105846508, -226.19464054088897]
    print inp
    print new_bound
    print detecting_res
    dr = 0
    print fnm
    glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x)
    X = []
    X1 = []
    X2 = []
    Y = []
    Y1 = []
    Y2 = []
    Z = []
    Z2 = []
    Z3 = []
    # input_l = np.random.uniform(new_bound[0], new_bound[1],3000)
    input_l = produce_n_input(new_bound, 100)
    for i in input_l:
        # temp_res = rf(i)
        # temp_res = np.log2(float(1.0 / glob_fitness_real(i)))
        temp_res = np.log2(bf.getUlpError(rf(*i), pf(*i)))
        X.append(i[0])
        Y.append(i[1])
        Z.append(float(temp_res))
        # Z.append(rf(*i))
        Z2.append(pf(*i))
        # Z.append(rf(i)-line_fun(i))
    print "max_Z"
    print np.max(Z)
    fig = plt.figure()
    ax = fig.add_subplot(121, projection='3d')
    ax.plot(X, Y, Z, '.')
    # ax = fig.add_subplot(111)
    # ax.plot(X, Z, '.')
    ax.legend()
    plt.show()
Ejemplo n.º 6
0
def plot_err_inp_debug2D(n_var, id, rd_seed):
    print "Random seed is :" + str(rd_seed)
    detecting_res_file = "final_results_" + str(n_var) + "v.xls"
    detecting_res = read_res_from_file(detecting_res_file, id, 1)
    ld_module = importlib.import_module("bench" + str(n_var) + "v")
    # change it to youself system password
    password = "******"
    # refresh()
    pf = ld_module.gfl[id - 1]
    rf = ld_module.rfl[id - 1]
    fnm = detecting_res[0]
    inp = detecting_res[2]
    # new_bound = generate_bound([inp],3e12)[0]
    new_bound = bf.getPointBound(inp, 6e12)
    X = []
    Z = []
    Y = np.random.uniform(new_bound[1][0], new_bound[1][1], 5000)
    for i in Y:
        temp_i = [inp[0], i]
        Z.append(np.log2(float(bf.getUlpError(rf(*temp_i), pf(*temp_i)))))
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(Y, Z, '.', color='black')
    ax.set_xlabel('Inputs', fontsize=16)
    ax.plot([new_bound[1][0], new_bound[1][1]], [12, 12], 'r')
    plt.ylim(0, 30)
    plt.xlim(new_bound[1][0], new_bound[1][1])
    ax.annotate(r'$\varepsilon=12$',
                xy=(new_bound[1][0], 12),
                xycoords='data',
                xytext=(2, 2),
                textcoords='offset points',
                fontsize=20)
    # plt.ylabel('Repair time ratios',fontsize=20)
    ax.set_ylabel('ErrBits', fontsize=16)
    plt.legend()
    # plt.savefig("graph/example_before2D.pdf", format="pdf")
    plt.savefig("graph/example_after2D.pdf", format="pdf")
    plt.show()
Ejemplo n.º 7
0
def tay_idx_fun(tay_fun_lst,x,y):
    point = [x,y]
    for tal in tay_fun_lst:
        if point_in_bound(point,tal[3]):
            cof_float = tal[1]
            cof_float_err = tal[0]
            new_point = tal[2]
            return taylor2_horner_eft_ori2(cof_float, cof_float_err, x, y, new_point)

rf = bv2.rfl[24]
# cof = taylor2_cof(rf,point,int(cof_float[-1]))
# mp.dps = 50
tay_fun = lambda x,y: tay_idx_fun(tay_fun_lst,x,y)
# tay_fun2 = lambda x, y: taylor2_fun(cof, x, y, point)
tpoint = [-1.9999999997623754, -79451.10510568558-bf.getulp(-79451.10510568558)]
point = [-1.9999999997623754, -79451.10510568558-bf.getulp(-79451.10510568558)]
bound = tay_fun_lst[0][3]
a = rf(*tpoint)
b = tay_fun(*tpoint)
print "%.18e" % point[0]
print "%.18e" % point[1]
print "a %.18e" %a
print "%.18e" % (a+bf.getulp(a))
print "%.18e" % (b+bf.getulp(a))
print rf(*point)
print bf.getUlpError(a,b)
print np.log2(bf.getUlpError(a,b))
print bf.getUlpError(tpoint[0],bound[0][1])/1e12
print bf.getUlpError(tpoint[1],bound[1][1])/1e12
#-3.560754883951525005e-18
#-3.560754883952511081e-18
Ejemplo n.º 8
0
def plot_err_inp_debug(n_var, id, rd_seed):
    print "Random seed is :" + str(rd_seed)
    detecting_res_file = "final_results_" + str(n_var) + "v.xls"
    detecting_res = read_res_from_file(detecting_res_file, id, 1)
    ld_module = importlib.import_module("bench" + str(n_var) + "v")
    # change it to youself system password
    password = "******"
    # refresh()
    pf = ld_module.gfl[id - 1]
    rf = ld_module.rfl[id - 1]
    fnm = detecting_res[0]
    inp = detecting_res[2]
    # new_bound = generate_bound([inp],3e12)[0]
    new_bound = bf.getPointBound(inp, 5e12)
    # new_bound = [-226.19467105846508, -226.19464054088897]
    print inp
    print new_bound
    print detecting_res
    kb_fun_file = "/home/yixin/PycharmProjects/NPTaylor/experiments/Localizing_results12/" + fnm + "/kb_fun.txt"
    kb_fun_cof = pickle.load(open(kb_fun_file, "rb"))
    kb = kb_fun_cof[0]
    p0 = kb_fun_cof[1]
    dr = kb_fun_cof[2]
    kbn = kb_fun_cof[3]
    print "********"
    print len(kb)
    # ps_len = len(points)/2
    # temp_res = fabs(rf(*point))
    # for i in range(ps_len-10,ps_len+10):
    #     next_res = fabs(rf(*points[i]))
    #     if next_res<temp_res:
    #         point = points[i]
    kb_fun = lambda x: float(ls_fun(p0, dr, x, kbn, kb))
    print fnm
    fnm.strip()
    bounds_file = "/home/yixin/PycharmProjects/NPTaylor/experiments/Localizing_results12/" + fnm + "/bound_th.txt"
    bounds_th = pickle.load(open(bounds_file, "rb"))
    up_p0 = bounds_th[0]
    kb_up = bounds_th[1]
    down_p0 = bounds_th[2]
    kb_down = bounds_th[3]
    dis = np.fabs(float(kb_up[0])) - np.fabs(float(kb_down[0]))
    kb_up_fun = lambda x: up_p0[1 - dr] + float(kb_up[0]) + (x - up_p0[
        dr]) * float(kb_up[1])
    # kb_down_fun = lambda x: -up_p0[1 - dr] + float(kb_up[0]) + (x - up_p0[dr]) * float(kb_up[1])
    kb_down_fun = lambda x: down_p0[1 - dr] + float(kb_down[0]) + (x - down_p0[
        dr]) * float(kb_down[1])
    # kb_up_fun = lambda x: -down_p0[1 - dr] - float(kb_down[0]) + (x - down_p0[dr]) * float(kb_down[1])
    # kb_down_fun = lambda x: down_p0[1 - dr] + float(kb_down[0]) + (x - down_p0[dr]) * float(kb_down[1])
    # kb_down_fun = lambda x: down_p0[1 - dr] + float(kb_down[0]) + (x - down_p0[dr]) * float(kb_down[1])
    # line_fun = get_line_fun(rf,new_bound)
    glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x)
    X = []
    X1 = []
    X2 = []
    Y = []
    Y1 = []
    Y2 = []
    Z = []
    Z2 = []
    Z3 = []
    Z4 = []
    Z5 = []
    iX = np.random.uniform(new_bound[dr][0], new_bound[dr][1], 600)
    iY = [kb_fun(i) for i in iX]
    Xk = []
    Yk = []
    Zk = []
    for i, j in zip(iX, iY):
        temp_i = [0, 0]
        temp_i[dr] = i
        temp_i[1 - dr] = j
        temp_res = np.log2(float(bf.getUlpError(rf(*temp_i), pf(*temp_i))))
        # if temp_res > 60:
        Xk.append(temp_i[0])
        Yk.append(temp_i[1])
        Zk.append(temp_res / 2.4)
    input_l = produce_n_input(new_bound, 50)
    for i in input_l:
        # temp_res = rf(i)
        temp_res = np.log2(float(1.0 / glob_fitness_real(i)))
        # temp_res = np.log2(float(bf.getUlpError(rf(*i),pf(*i))))
        X.append(i[0])
        Y.append(i[1])
        Z.append(float(temp_res))
        # Z.append(rf(*i))
        Z2.append(pf(*i))
        utemp_i = list(i)
        utemp_i[1 - dr] = kb_up_fun(i[dr])
        if point_in_bound(utemp_i, new_bound):
            X1.append(utemp_i[0])
            Y1.append(utemp_i[1])
            Z3.append(12)
        dtemp_i = list(i)
        dtemp_i[1 - dr] = kb_down_fun(i[dr])
        if point_in_bound(dtemp_i, new_bound):
            X2.append(dtemp_i[0])
            Y2.append(dtemp_i[1])
            Z4.append(12)
        Z5.append(12)
        # Z.append(rf(i)-line_fun(i))
    print "max_Z"
    print np.max(Z)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot(X, Y, Z, '.', color='black')
    ax.plot(X1, Y1, Z3, color='blue', linewidth=5)
    ax.plot(X2, Y2, Z4, color='blue', linewidth=5)
    ax.plot(Xk, Yk, Zk, '.', color='r')
    ax.set_xlabel('Inputs', fontsize=16)
    # plt.ylabel('Repair time ratios',fontsize=20)
    ax.set_zlabel('ErrBits', fontsize=16)
    # ax.scatter(X, Y, Z)
    # ax.scatter(X1, Y1, Z3)
    # ax.scatter(X2, Y2, Z4)
    # ax.scatter(Xk, Yk, Zk)
    # X = np.arange(new_bound[0][0], new_bound[0][1], (new_bound[0][1]-new_bound[0][0])/20.0)
    # Y = np.arange(new_bound[1][0], new_bound[1][1], (new_bound[1][1]-new_bound[1][0])/20.0)
    # X, Y = np.meshgrid(X, Y)
    # # R = np.sqrt(X ** 2 + Y ** 2)
    # Z5 = X*0+Y*0+12
    #
    # # Plot the surface.
    # surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
    #                        linewidth=0, antialiased=False)
    # Z5 = np.asarray(Z5)
    # ax.plot_surface(X,Y,Z5)
    # ax = fig.add_subplot(122, projection='3d')
    # ax.plot(X, Y, Z2, '.')
    # ax = fig.add_subplot(111)
    # ax.plot(X, Z, '.')
    # plt.savefig("graph/example_after.pdf", format="pdf")
    # plt.savefig("graph/example_before.pdf", format="pdf")
    ax.legend()
    plt.show()
Ejemplo n.º 9
0
def read_bound(n_var, idx, num):
    detecting_res_file = "final_results_" + str(n_var) + "v.xls"
    detecting_res = read_res_from_file(detecting_res_file, idx, num)
    bound = detecting_res[-1]
    print "%.15e" % float(bf.getUlpError(bound[0], bound[1]))
Ejemplo n.º 10
0
def plot_err_inp_debug2d(n_var, id, rd_seed):
    print "Random seed is :" + str(rd_seed)
    detecting_res_file = "final_results_" + str(n_var) + "v.xls"
    detecting_res = read_res_from_file(detecting_res_file, id, 1)
    ld_module = importlib.import_module("bench" + str(n_var) + "v")
    # change it to youself system password
    password = "******"
    # refresh()
    pf = ld_module.gfl[id - 1]
    rf = ld_module.rfl[id - 1]
    fnm = detecting_res[0]
    inp = detecting_res[2]
    # new_bound = generate_bound([inp],3e12)[0]
    new_bound = bf.getPointBound(inp, 5e12)
    # new_bound = [-226.19467105846508, -226.19464054088897]
    print inp
    print new_bound
    print detecting_res
    dr = 0
    print fnm
    fnm.strip()
    bounds_file = "/home/yixin/PycharmProjects/NPTaylor/experiments/Localizing_results12/" + fnm + "/bound_th.txt"
    bounds_th = pickle.load(open(bounds_file, "rb"))
    up_p0 = bounds_th[0]
    kb_up = bounds_th[1]
    down_p0 = bounds_th[2]
    kb_down = bounds_th[3]
    kb_up_fun = lambda x: up_p0[1 - dr] + float(kb_up[0]) + (x - up_p0[
        dr]) * float(kb_up[1])
    kb_down_fun = lambda x: down_p0[1 - dr] + float(kb_down[0]) + (x - down_p0[
        dr]) * float(kb_down[1])
    # line_fun = get_line_fun(rf,new_bound)
    glob_fitness_real = lambda x: bf.mfitness_fun(rf, pf, x)
    X = []
    X1 = []
    X2 = []
    Y = []
    Y1 = []
    Y2 = []
    Z = []
    Z2 = []
    Z3 = []
    Z4 = []
    Z5 = []
    # input_l = np.random.uniform(new_bound[0], new_bound[1],3000)
    input_l = produce_n_input(new_bound, 100)
    for i in input_l:
        # temp_res = rf(i)
        # temp_res = np.log2(float(1.0 / glob_fitness_real(i)))
        temp_res = np.log2(bf.getUlpError(rf(*i), pf(*i)))
        X.append(i[0])
        Y.append(i[1])
        Z.append(float(temp_res))
        # Z.append(rf(*i))
        Z2.append(pf(*i))
        utemp_i = list(i)
        utemp_i[1 - dr] = kb_up_fun(i[dr])
        if point_in_bound(utemp_i, new_bound):
            X1.append(utemp_i[0])
            Y1.append(utemp_i[1])
            Z3.append(12)
        dtemp_i = list(i)
        dtemp_i[1 - dr] = kb_down_fun(i[dr])
        if point_in_bound(dtemp_i, new_bound):
            X2.append(dtemp_i[0])
            Y2.append(dtemp_i[1])
            Z4.append(12)
        Z5.append(12)
        # Z.append(rf(i)-line_fun(i))
    print "max_Z"
    print np.max(Z)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot(X, Y, Z, '.')
    ax.plot(X1, Y1, Z3, '.')
    ax.plot(X2, Y2, Z4, '.')
    X = np.arange(new_bound[0][0], new_bound[0][1],
                  (new_bound[0][1] - new_bound[0][0]) / 20.0)
    Y = np.arange(new_bound[1][0], new_bound[1][1],
                  (new_bound[1][1] - new_bound[1][0]) / 20.0)
    X, Y = np.meshgrid(X, Y)
    # R = np.sqrt(X ** 2 + Y ** 2)
    Z5 = X * 0 + Y * 0 + 12
    #
    # # Plot the surface.
    # surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
    #                        linewidth=0, antialiased=False)
    # Z5 = np.asarray(Z5)
    ax.plot_surface(X, Y, Z5)
    # ax = fig.add_subplot(122, projection='3d')
    # ax.plot(X, Y, Z2, '.')
    # ax = fig.add_subplot(111)
    # ax.plot(X, Z, '.')
    ax.legend()
    plt.show()