Пример #1
0
def Fletcher_Reeves_conj():
    f = conj_f3
    c,b,A = f()
    x0 = np.matrix('10.;-5.')
    g0 = A * x0 + b 
    v0 = -g0

    #pdb.set_trace() 
    lamb0, f_x0 = golden_section_search(lambda k:f_value(f, x0 + k*v0), [0,2], 0.001) 

    x1 = x0 + lamb0 * v0
    g1 = A*x1 + b 
    v1 = -g1 + np.dot(g1.T, g1)[0,0]/np.dot(g0.T, g0)[0,0] * v0
    lamb1, f_x1 = golden_section_search(lambda k:f_value(f, x1 + k*v1), [0,2], 0.001) 
    x2 = x1 + lamb1 * v1
    return x2, f_x1 
Пример #2
0
def BFGS(f, f_deriv, x0, epsilon):
    x_k = x0
    B_k = np.eye(x0.shape[0])
    D_k = np.linalg.inv(B_k)
    g_k = f_deriv(x0)

    while True:
        def_field = [-100., 100.]
        d = -1. * D_k * g_k

        k, min_fk = golden_section_search(lambda k: f(x_k + k * d), def_field,
                                          epsilon)
        sk = k * d
        x_k1 = x_k + sk
        g_k1 = f_deriv(x_k1)
        yk = g_k1 - g_k

        g_sum = np.sqrt(np.sum((g_k1.T * g_k1)))
        if g_sum < epsilon:
            break

        #下一个点的D
        I = np.eye(x0.shape[0])
        rho = 1. / (yk.T * sk)[0, 0]
        V = I - sk * yk.T * rho
        #D_k1 = (I - rho * sk * yk.T) * D_k * (I - rho* yk*sk.T) + rho * sk * sk.T
        D_k1 = V * D_k * V.T + rho * sk * sk.T  #与上式等价

        D_k = D_k1
        x_k = x_k1
        g_k = g_k1

    return x_k1, f(x_k1)
Пример #3
0
def powell_conj():
    '''
    u1=[-11.14, -24.46]
    u2=[-1.8, -0.28]
    '''
    x0 = np.matrix('20.;20.')
    #v1,v2线性无关
    v = np.matrix('1.,1.;-1.,1.')

    c, b, A = f_powell()
    u = np.matrix('0.,0.;0.,0.')
    lamb = np.matrix('0.;0.')

    id = 0
    total = 0
    while total < 3:
        k, min_fk = golden_section_search(
            lambda k: f_value(f_powell, x0 + k * v[:, 0]), [-100., 100], 0.001)
        x1 = x0 + k * v[:, 0]

        k, min_fk = golden_section_search(
            lambda k: f_value(f_powell, x1 + k * v[:, 1]), [-100., 100], 0.001)
        x2 = x1 + k * v[:, 1]

        #找到u向量
        u[:, id] = x2 - x0
        k, min_fk = golden_section_search(
            lambda k: f_value(f_powell, x2 + k * u[:, id]), [-100., 100],
            0.001)

        x0 = x2 + k * u[:, id]
        v[:, 0] = v[:, 1]
        v[:, 1] = u[:, id]

        id = (id + 1) % len(lamb)
        total += 1

        conj = u[:, 0].T * A * u[:, 1]
        #print "conj: ", conj

    x_star = x0

    return x_star, f_value(f_powell, x_star)
Пример #4
0
def conj_grandient_method_for_f2():
    u1 = np.matrix('1.;0.')
    u2 = np.matrix('0.;1.')
    x0 = np.matrix('0.;0.')

    def_field = [-1,1]
    esplison = 0.005
    c,b, A = f2()
    
    '''
    线性搜索用的一次函数, 参数为k
    f = f(xi + kui)
    '''
    k1 = golden_section_search(lambda k:f_value(f2, x0 + k*u1), def_field, esplison)
    x1 = x0 + k1[0] * x0

    k2 = golden_section_search(lambda k:f_value(f2, x1 + k*u2), def_field, esplison)
    x2 = x0 + k1[0] * u1 + k2[0] * u2 
    
    return x2, f_value(f2, x2)
Пример #5
0
def BFGS_simple(f, f_deriv, x0, epsilon):
    x_k = x0
    B_k = np.eye(x0.shape[0])
    g_k = f_deriv(x0)

    while True:
        def_field = [-100., 100.]
        #D_k = np.linalg.inv(B_k)
        #d = -1. * D_k * g_k
        d = np.linalg.solve(B_k, g_k)

        k, min_fk = golden_section_search(lambda k: f(x_k + k * d), def_field,
                                          epsilon)
        x_k1 = x_k + k * d
        g_k1 = f_deriv(x_k1)

        #pdb.set_trace()
        g_sum = np.sum((g_k1.T * g_k1))
        g_sum = np.sqrt(g_sum)

        if g_sum < epsilon:
            break

        sk = x_k1 - x_k
        yk = g_k1 - g_k

        v = yk
        u = B_k * sk

        alpha = 1. / (v.T * sk)[0, 0]
        beta = -1. / (u.T * sk)[0, 0]

        delta_B = alpha * v * v.T + beta * u * u.T

        #下一个点的D
        B_k = B_k + delta_B
        x_k = x_k1
        g_k = g_k1

    return x_k1, f(x_k1)
Пример #6
0
def DFP(f, f_deriv, x0, epsilon):
    x_k = x0
    D_k = np.eye(x0.shape[0])
    g_k = f_deriv(x0)

    while True:
        def_field = [-100., 100.]
        d = -1. * D_k * g_k
        k, min_fk = golden_section_search(lambda k: f(x_k + k * d), def_field,
                                          epsilon)
        x_k1 = x_k + k * d
        g_k1 = f_deriv(x_k1)

        #pdb.set_trace()
        g_sum = np.sum((g_k1.T * g_k1))
        g_sum = np.sqrt(g_sum)

        if g_sum < epsilon:
            break

        sk = x_k1 - x_k
        yk = g_k1 - g_k

        v = sk
        u = D_k * yk

        alpha = 1. / (v.T * yk)[0, 0]
        beta = -1. / (u.T * yk)[0, 0]

        delta_D = alpha * v * v.T + beta * u * u.T

        #下一个点的D
        D_k = D_k + delta_D
        x_k = x_k1
        g_k = g_k1

    return x_k1, f(x_k1)