2017-11-18 241 views
1

我正在使用Strong Wolfe條件在Matlab中進行線搜索算法。我的代碼爲Strong Wolfe如下:Strong Wolfe算法

while i<= iterationLimit 
    if (func(x + alpha1*p)) > func(x) + c1*alpha1*(grad(x)'*p) || 
     ((func(x + alpha1*p)) >= (func(x + prevAlpha*p)) && i>1) 
      alphastar = alpha_strongWolfe_zoom(func, grad, x, p, prevAlpha,alpha1, c1, c2); 
       break; 
    end 

    if abs(((grad(x + alpha1*p))'*p)) <= -c2*(grad(x)'*p) 
      alphastar = alpha1; 
       break; 
    end 

    if ((grad(x + alpha1*p))'*p) >= 0 
     alphastar = alpha_strongWolfe_zoom(func, grad, x, p, alpha1, prevAlpha, c1, c2); 
       break; 
    end 

    prevAlpha = alpha1; 
    alpha1  = gamma*alpha1; 
    i   = i+1; 

    end 

但是,我想我的代碼可能不是真的有效。我在想,這可能是因爲我使用的是完整的公式由

func(x + alpha1*p) 

給出一個問題,但我不知道,因爲我想不出另一種方式來做到這一點。 您能否指出使用此代碼的其他低效率?說到Matlab,我仍然是一個初學者。

謝謝你的幫助。

+1

[這裏](https://de.mathworks.com/company/newsletters/articles/accelerating-matlab-algorithms-and-applications.html)是一篇文章,介紹了使MATLAB代碼更高效的可能方法。 – mikkola

回答

0

我修改你的代碼一點點地提高性能:

while (i <= iterationLimit) 
    func_1 = func(x + alpha1*p); 
    gp = grad(x)' * p; 

    if ((func_1 > (func(x) + c1*alpha1*gp)) || ((func_1 >= func(x + prevAlpha*p)) && (i > 1))) 
     alphastar = alpha_strongWolfe_zoom(func, grad, x, p, prevAlpha, alpha1, c1, c2); 
     break; 
    end 

    grad_1 = grad(x + alpha1*p)' * p; 

    if (abs(grad_1) <= -c2*gp) 
     alphastar = alpha1; 
     break; 
    end 

    if (grad_1 >= 0) 
     alphastar = alpha_strongWolfe_zoom(func, grad, x, p, alpha1, prevAlpha, c1, c2); 
     break; 
    end 

    prevAlpha = alpha1; 
    alpha1 = gamma*alpha1; 
    i   = i+1; 
end 

的性能肯定不會再一飛沖天,但你的緩存中間計算是優化YOUT代碼一個很好的起點。

最好的優化只能通過儘可能多地向量化你的計算來實現,但是這隻能通過對代碼和算法/數學知識的深入瞭解才能實現。

相關問題