python实例优化⽬标函数_使⽤Python求解带约束的最优化问
题详解
题⽬:
1. 利⽤拉格朗⽇乘⼦法
#导⼊sympy包,⽤于求导,⽅程组求解等等
from sympy import *
#设置变量
x1 = symbols("x1")
x2 = symbols("x2")
alpha = symbols("alpha")
beta = symbols("beta")
#构造拉格朗⽇等式
L = 10 - x1*x1 - x2*x2 + alpha * (x1*x1 - x2) + beta * (x1 + x2)
#求导,构造KKT条件
difyL_x1 = diff(L, x1) #对变量x1求导
difyL_x2 = diff(L, x2) #对变量x2求导
difyL_beta = diff(L, beta) #对乘⼦beta求导
dualCpt = alpha * (x1 * x1 - x2) #对偶互补条件
linspace函数python#求解KKT等式
aa = solve([difyL_x1, difyL_x2, difyL_beta, dualCpt], [x1, x2, alpha, beta])
#打印结果,还需验证alpha>=0和不等式约束<=0
for i in aa:
if i[2] >= 0:
if (i[0]**2 - i[1]) <= 0:
print(i)
结果:
(-1, 1, 4, 6)
(0, 0, 0, 0)
2. scipy包⾥⾯的minimize函数求解
from scipy.optimize import minimize
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
def func(args):
fun = lambda x: 10 - x[0]**2 - x[1]**2
return fun
#约束条件,包括等式约束和不等式约束
def con(args):
cons = ({'type': 'ineq', 'fun': lambda x: x[1]-x[0]**2},
{'type': 'eq', 'fun': lambda x: x[0]+x[1]})
return cons
#画三维模式图
def draw3D():
fig = plt.figure()
ax = Axes3D(fig)
x_arange = np.arange(-5.0, 5.0)
y_arange = np.arange(-5.0, 5.0)
X, Y = np.meshgrid(x_arange, y_arange)
Z1 = 10 - X**2 - Y**2
Z2 = Y - X**2
Z3 = X + Y
plt.xlabel('x')
plt.ylabel('y')
ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap='rainbow') ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, cmap='rainbow') ax.plot_surface(X, Y, Z3, rstride=1, cstride=1, cmap='rainbow') plt.show()
#画等⾼线图
def drawContour():
x_arange = np.linspace(-3.0, 4.0, 256)
y_arange = np.linspace(-3.0, 4.0, 256)
X, Y = np.meshgrid(x_arange, y_arange)
Z1 = 10 - X**2 - Y**2
Z2 = Y - X**2
Z3 = X + Y
plt.xlabel('x')
C1 = ur(X, Y, Z1, 8, colors='black')
C2 = ur(X, Y, Z2, 8, colors='blue')
C3 = ur(X, Y, Z3, 8, colors='red')
plt.clabel(C1, inline=1, fontsize=10)
plt.clabel(C2, inline=1, fontsize=10)
plt.clabel(C3, inline=1, fontsize=10)
plt.show()
if __name__ == "__main__":
args = ()
args1 = ()
cons = con(args1)
x0 = np.array((1.0, 2.0)) #设置初始值,初始值的设置很重要,很容易收敛到另外的极值点中,建议多试⼏个值
#求解#
res = minimize(func(args), x0, method='SLSQP', constraints=cons)
>
print(res.fun)
print(res.success)
print(res.x)
# draw3D()
drawContour()
结果:
7.99999990708696
True
[-1.00000002 1.00000002]
以上这篇使⽤Python求解带约束的最优化问题详解就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持脚本之家。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论