簡體   English   中英

Scipy優化最小化功能

[英]Scipy optimize.minimize function

我嘗試使用scipy.optimize.minimize解決非線性編程任務

max r
x1**2 + y1**2 <= (1-r)**2
(x1-x2)**2 + (y1-y2)**2 >= 4*r**2
0 <= r <= 1

所以我寫了下一個代碼:

r = np.linspace(0, 1, 100)
x1 = np.linspace(0, 1, 100)
y1 = np.linspace(0, 1, 100)
x2 = np.linspace(0, 1, 100)
y2 = np.linspace(0, 1, 100)


fun = lambda r: -r
cons = ({'type': 'ineq',
     'fun': lambda x1, r: [x1[0] ** 2 + x1[1] ** 2 - (1 - r) ** 2],
     'args': (r,)},
    {'type': 'ineq',
     'fun': lambda x2, r: [x2[0] ** 2 + x2[1] ** 2 - (1 - r) ** 2],
     'args': (r,)},
    {'type': 'ineq',
     'fun': lambda x1, x2, r: [(x1[0] - x2[0]) ** 2 + (x1[1] - x2[1]) ** 2 - 4 * r ** 2],
     'args': (x2, r,)})
bnds = ((0, 1), (-1, 1), (-1, 1), (-1, 1), (-1, 1))
x0 = [0, 0, 0, 0, 0]
minimize(fun, x0, bounds=bnds, constraints=cons)

但是我有下一個錯誤

File "C:\Anaconda2\lib\site-packages\scipy\optimize\slsqp.py", line 377, in _minimize_slsqp
c = concatenate((c_eq, c_ieq))
ValueError: all the input arrays must have same number of dimensions

請幫助我找出錯誤並編寫正確的代碼

UPD:感謝@unutbu,我了解如何正確構建它。

fun = lambda x: -x[0]
cons = ({'type': 'ineq',
     'fun': lambda x: -x[1] ** 2 - x[2] ** 2 + (1 - x[0]) ** 2},
    {'type': 'ineq',
     'fun': lambda x: -x[3] ** 2 - x[4] ** 2 + (1 - x[0]) ** 2},
    {'type': 'ineq',
     'fun': lambda x: (x[1] - x[3]) ** 2 + (x[1] - x[4]) ** 2 - 4 * x[0] ** 2})
bnds = ((0, 1), (-1, 1), (-1, 1), (-1, 1), (-1, 1))
x0 = [0.5, 0.3, 0.5, 0.3, 0.5]
answer = minimize(fun, x0, bounds=bnds, constraints=cons)

在最小化任務中,我們必須將約束引入以下形式:

g(x) >= 0

這就是為什么約束看起來像那樣。

您的參數空間似乎是5維的。 參數空間中的一個點將是z = (r, x1, y1, x2, y2) 因此,要最小化的函數-以及約束函數-應該接受點z並返回標量值。

因此,代替

fun = lambda r: -r

采用

def func(z):
    r, x1, y1, x2, y2 = z
    return -r

而不是

lambda x1, r: [x1[0] ** 2 + x1[1] ** 2 - (1 - r) ** 2]

采用

def con1(z):
    r, x1, y1, x2, y2 = z
    return x1**2 + y1**2 - (1-r)**2

等等。


注意,可以通過設置bounds參數而不是定義約束來處理諸如0 <= r <= 1類的簡單約束。 而且如果x1y1x2y2范圍從-1到1,那么您可能還需要更改

x1 = np.linspace(0, 1, 100)
...

x1 = np.linspace(-1, 1, 100)
...

但是,不需要數組rx1y1x2y2來最小化func ,因此您也可以從腳本中完全刪除它們。


import numpy as np
import scipy.optimize as optimize

"""
max r
x1**2 + y1**2 <= (1-r)**2
(x1-x2)**2 + (y1-y2)**2 >= 4*r**2
0 <= r <= 1
"""

def func(z):
    r, x1, y1, x2, y2 = z
    return -r

def con1(z):
    r, x1, y1, x2, y2 = z
    return x1**2 + y1**2 - (1-r)**2

def con2(z):
    r, x1, y1, x2, y2 = z
    return 4*r**2 - (x1-x2)**2 - (y1-y2)**2 

cons = ({'type': 'ineq', 'fun': con1}, {'type': 'ineq', 'fun': con2},)
bnds = ((0, 1), (-1, 1), (-1, 1), (-1, 1), (-1, 1))
guess = [0, 0, 0, 0, 0]
result = optimize.minimize(func, guess, bounds=bnds, constraints=cons)
print(result)

產量

     fun: -1.0
     jac: array([-1.,  0.,  0.,  0.,  0.,  0.])
 message: 'Optimization terminated successfully.'
    nfev: 14
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([ 1.,  0.,  0.,  0.,  0.])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM