首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最陡峭的下降轨迹行为

最陡峭的下降轨迹行为
EN

Stack Overflow用户
提问于 2019-06-10 14:11:20
回答 1查看 333关注 0票数 1

我已经编写了在公式1/2 * (x1^2 +γ* x2^2)给出的二次形式上执行最陡峭下降的代码。在数学上,我将Boyd's Convex Optimization一书中给出的方程作为指导,并希望重现给定的例子。我的问题是,我的最陡峭下降路径的轨迹看起来有点奇怪,与书中显示的轨迹不匹配,尽管它似乎是收敛的。

例如,我的代码输出了什么:

它应该是什么样子:

另一个:

Vs它应该是什么样子:

下面是一个可重现的例子,尽管有点长:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt

# Function value at a given point
def f(x1,x2):
    return np.power(np.e,x1 + 3*x2 - 0.1) + np.power(np.e,x1 - 3*x2 - 0.1) + np.power(np.e,- x1 - 0.1)

# Partial Derivatives
def g(x1, x2):
    g1 = np.power(np.e, x1 + 3 * x2 - 0.1) + np.power(np.e, x1 - 3 * x2 - 0.1) - np.power(np.e, - x1 - 0.1)
    g2 = np.power(3*np.e, x1 + 3 * x2 - 0.1) - np.power(3 * np.e, x1 - 3 * x2 - 0.1)
    return np.asarray([g1[0], g2[0]])

# Descent Parameters
alpha = 0.1
beta = 0.7

# Starting point
x0 = np.array([-1, 1], ndmin = 2, dtype = np.float64 ).T
f0 = f(x0[0], x0[1]) # Initial function value

# Calculate the minimum
xmin = np.array([0,0], ndmin = 2).T # Analytic minimum to the problem
fmin = f(0,0)

nangles = 1000
nopts = 1000
thetas = np.linspace(0, 2 * np.pi, nangles)
cont = np.zeros((5,2,nangles))
Delta = (f0 - fmin)/4

# function that plots the level curves or contour lines
for i in range(nangles):
    ray = np.vstack([np.linspace(0,4,nopts) * np.cos(thetas[i]),
                        np.linspace(0,4,nopts) * np.sin(thetas[i])])

    fray = f(ray[0], ray[1])

    def get_ind(expression):
        ind = np.nonzero(expression)[0].max(0)
        return(ray[:,ind - 1] + ray[:,ind]) / 2

    cont[0,:,i] = get_ind(fray < f0)
    cont[1,:,i] = get_ind(fray < f0 - 3 * Delta)
    cont[2,:,i] = get_ind(fray < f0 - 2 * Delta)
    cont[3,:,i] = get_ind(fray < f0 - Delta)
    cont[4,:,i] = get_ind(fray < f0 + Delta)

def plt_contour():
    fig, ax = plt.subplots()
    ax.plot(cont[0,0,:], cont[0,1,:], '--',
            cont[1,0,:], cont[1,1,:], '--',
            cont[2,0,:], cont[2,1,:], '--',
            cont[3,0,:], cont[3,1,:], '--',
            cont[4,0,:], cont[4,1,:], '--')
    ax.axis('equal')
    ax.axis('off')
    return fig, ax

maxiters = 100
xs = np.zeros((2, maxiters))
fs = np.zeros((1, maxiters))
x = x0.copy()
Pnorm = np.diag([8,2])

for i in range(maxiters):
    print(i)
    fval = f(x[0],x[1])
    xs[:,i:i + 1] = x
    fs[:,i] = fval
    if fval - fmin < 1e-10: #stopping criterion
        break
    # Backtracking Line Search
    gval = g(x[0],x[1])
    v = np.dot(-np.linalg.inv(Pnorm), gval) # I think this is the step that I am not doing right
    s = 1
    for k in range(10):
        xnew = x + (s * v).reshape(2,1)
        fxnew = f(xnew[0], xnew[1])
        if fxnew < fval + s * alpha * gval.T @ v:
            break
        else:
            s = s * beta
    x = x + (s * v).reshape(2,1)

# Plot path
fig, ax = plt_contour()
fig.set_size_inches(5,5)
ax.plot( xs[0,0:i + 1], xs[1, 0:i + 1], 'o')
ax.plot( xs[0,0:i + 1], xs[1, 0:i + 1], '-')
plt.show()

我有一种预感,我做错的地方是最陡峭的下降步骤,也就是变量v,也许我实际上没有在做我认为我正在做的事情。为任何帮助干杯。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-10 14:44:58

g2计算是否正确?3不应该在幂函数之外吗?

代码语言:javascript
复制
g2 = 3*np.power(np.e, x1 + 3 * x2 - 0.1) - 3*np.power(np.e, x1 - 3 * x2 - 0.1)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56521298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档