亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Python求解帶約束的最優(yōu)化問(wèn)題詳解

 更新時(shí)間:2020年02月11日 10:18:47   作者:深山里的小白羊  
今天小編就為大家分享一篇使用Python求解帶約束的最優(yōu)化問(wèn)題詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

題目:

1. 利用拉格朗日乘子法

#導(dǎo)入sympy包,用于求導(dǎo),方程組求解等等
from sympy import * 
 
#設(shè)置變量
x1 = symbols("x1")
x2 = symbols("x2")
alpha = symbols("alpha")
beta = symbols("beta")
 
#構(gòu)造拉格朗日等式
L = 10 - x1*x1 - x2*x2 + alpha * (x1*x1 - x2) + beta * (x1 + x2)
 
#求導(dǎo),構(gòu)造KKT條件
difyL_x1 = diff(L, x1) #對(duì)變量x1求導(dǎo)
difyL_x2 = diff(L, x2) #對(duì)變量x2求導(dǎo)
difyL_beta = diff(L, beta) #對(duì)乘子beta求導(dǎo)
dualCpt = alpha * (x1 * x1 - x2) #對(duì)偶互補(bǔ)條件
 
#求解KKT等式
aa = solve([difyL_x1, difyL_x2, difyL_beta, dualCpt], [x1, x2, alpha, beta])
 
#打印結(jié)果,還需驗(yàn)證alpha>=0和不等式約束<=0
for i in aa:
 if i[2] >= 0:
 if (i[0]**2 - i[1]) <= 0:
  print(i)

結(jié)果:

(-1, 1, 4, 6)
(0, 0, 0, 0)

2. scipy包里面的minimize函數(shù)求解

from scipy.optimize import minimize
import numpy as np 
 
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt 
 
#目標(biāo)函數(shù):
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 
 
#畫(huà)三維模式圖
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()
 
#畫(huà)等高線圖
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')
 plt.ylabel('y')
 plt.contourf(X, Y, Z1, 8, alpha=0.75, cmap='rainbow')
 plt.contourf(X, Y, Z2, 8, alpha=0.75, cmap='rainbow')
 plt.contourf(X, Y, Z3, 8, alpha=0.75, cmap='rainbow')
 C1 = plt.contour(X, Y, Z1, 8, colors='black')
 C2 = plt.contour(X, Y, Z2, 8, colors='blue')
 C3 = plt.contour(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)) #設(shè)置初始值,初始值的設(shè)置很重要,很容易收斂到另外的極值點(diǎn)中,建議多試幾個(gè)值
 
 #求解#
 res = minimize(func(args), x0, method='SLSQP', constraints=cons)
 #####
 print(res.fun)
 print(res.success)
 print(res.x)
 
 # draw3D()
 drawContour()

結(jié)果:

7.99999990708696
True
[-1.00000002 1.00000002]

以上這篇使用Python求解帶約束的最優(yōu)化問(wèn)題詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python實(shí)現(xiàn)抽獎(jiǎng)小程序

    python實(shí)現(xiàn)抽獎(jiǎng)小程序

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)抽獎(jiǎng)小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 查看python下OpenCV版本的方法

    查看python下OpenCV版本的方法

    今天小編就為大家分享一篇查看python下OpenCV版本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • python?tkinter自定義實(shí)現(xiàn)Expander控件

    python?tkinter自定義實(shí)現(xiàn)Expander控件

    和其他成熟的GUI庫(kù)相比,tkinter的組件并不是太多,但在自定義組件這一點(diǎn)上,并不遜色于其他框架,下面小編就教大家如何自定義一個(gè)Expander控件吧
    2023-08-08
  • Python使用asyncio異步時(shí)的常見(jiàn)問(wèn)題總結(jié)

    Python使用asyncio異步時(shí)的常見(jiàn)問(wèn)題總結(jié)

    這篇文章主要為大家整理了開(kāi)發(fā)人員在?Python?中使用?asyncio?時(shí)提出的常見(jiàn)問(wèn)題以及解決方法,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下
    2023-04-04
  • Python+tkinter使用40行代碼實(shí)現(xiàn)計(jì)算器功能

    Python+tkinter使用40行代碼實(shí)現(xiàn)計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了Python+tkinter使用40行代碼實(shí)現(xiàn)計(jì)算器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python使用Srapy框架爬蟲(chóng)模擬登陸并抓取知乎內(nèi)容

    Python使用Srapy框架爬蟲(chóng)模擬登陸并抓取知乎內(nèi)容

    這里我們來(lái)看如何通過(guò)Python使用Srapy框架爬蟲(chóng)模擬登陸并抓取知乎內(nèi)容的實(shí)例,要實(shí)現(xiàn)持續(xù)的爬取需要利用到cookie的保存,我們首先還是來(lái)回顧一下cookie的相關(guān)知識(shí)點(diǎn):
    2016-07-07
  • python讀取mat文件中的struct問(wèn)題

    python讀取mat文件中的struct問(wèn)題

    這篇文章主要介紹了python讀取mat文件中的struct問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python中的迭代器你了解嗎

    Python中的迭代器你了解嗎

    迭代器是一種特殊的對(duì)象,它實(shí)現(xiàn)了迭代協(xié)議,允許按照一定的順序逐個(gè)訪問(wèn)元素,本文就來(lái)帶大家深入了解一下Python中迭代器的使用,需要的可以參考下
    2023-05-05
  • Python網(wǎng)絡(luò)爬蟲(chóng)之HTTP原理

    Python網(wǎng)絡(luò)爬蟲(chóng)之HTTP原理

    在寫(xiě)爬蟲(chóng)之前,我們還需要了解一些基礎(chǔ)知識(shí),如HTTP原理、網(wǎng)頁(yè)的基礎(chǔ)知識(shí)、爬蟲(chóng)的基本原理、Cookies的基本原理等。本文中,我們就對(duì)這些基礎(chǔ)知識(shí)做一個(gè)簡(jiǎn)單的總結(jié),需要的朋友參考一下
    2023-04-04
  • Python實(shí)現(xiàn)人臉識(shí)別

    Python實(shí)現(xiàn)人臉識(shí)別

    這篇文章主要介紹了Python實(shí)現(xiàn)人臉識(shí)別,首選抓取多張圖片,從中獲取特征數(shù)據(jù)集和平均特征值然后寫(xiě)入?csv?文件?-?計(jì)算特征數(shù)據(jù)集的歐式距離作對(duì)比,下面一起來(lái)看具體得實(shí)現(xiàn)過(guò)程吧
    2022-01-01

最新評(píng)論