简体   繁体   English

使用Python进行Lagrange多重BFGS优化

[英]Lagrange multipler BFGS optimization with python

I would like to use the scipy optimization routines, in order to minimize functions while applying some constraints. 我想使用scipy优化例程,以便在施加一些约束的同时最小化功能。 I would like to apply the Lagrange multiplier method, but I think that I missed something. 我想应用拉格朗日乘数法,但我想我错过了一些东西。

My simple example: minimize f(x,y)=x^2+y^2, while keeping the constraint: y=x+4.0 我的简单示例:最小化f(x,y)= x ^ 2 + y ^ 2,同时保持约束:y = x + 4.0

import numpy as np
from scipy.optimize import fmin_bfgs
#X=[x,y,l]

def f(X):
   x=X[0]
   y=X[1]
   return x**2+y**2

def g(X):
   x=X[0]
   y=X[1]
   return y-x-4.000

def L(X):
   l=X[2]
   return f(X)+l*g(X)


def dL(X):
   x=X[0]
   y=X[1]
   l=X[2]
   gx=2.0*x
   gy=2.0*y
   gl=g(X)
   tmp=np.array([gx,gy,gl])
   return tmp


x0=np.array([-2.0,2.0,0.0])



print "f(x0)\t\t g(x0) \t\t L(x0)"
print "%12.8f\t%12.8f\t%12.8f\t"%(f(x0),g(x0),L(x0))
print "dL(x0)"
print dL(x0)


xopt=fmin_bfgs(L,x0,fprime=dL,disp=True)
print xopt

Even if my x0 is on the spot, the optimization diverges, badly. 即使我的x0在现场,优化效果仍然存在很大差异。 Could someone please explain me how one should include the Lagrange multiplier properly and how one should initialize the multiplier? 有人可以向我解释一下如何正确地包含拉格朗日乘数,以及如何初始化乘数吗?

The main idea behind the Lagrangian multiplier is to construct a new objective function with the constrains already embedded. 拉格朗日乘子背后的主要思想是构造一个已经嵌入了约束的新目标函数。 First you solve the equations to find the value of the multiplier and then optimize the new objetive function( http://www.math.vt.edu/people/mcquain/1526_Lag_opt_2012.pdf ). 首先,您求解方程式以找到乘数的值,然后优化新的目标函数( http://www.math.vt.edu/people/mcquain/1526_Lag_opt_2012.pdf )。 However in your example you're trying to find the value of the multiplier by optimization. 但是,在您的示例中,您尝试通过优化来找到乘数的值。 By following the instructions provided in the lik you can find that the lagrangian multiplier for your problem is 4. 按照lik中提供的说明,您可以发现问题的拉格朗日乘数为4。

def f(X):
    x=X[0]
    y=X[1]
    return x**2+y**2

def g(X):
    x=X[0]
    y=X[1]
    return y-x-4.000

def L(X):
    return f(X)-4*g(X)

x0=np.array([0,0])
xopt=fmin_bfgs(L,x0,disp=True)

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM