为了熟悉全局优化方法,特别是shgo
优化器,scipy.optimize v1.3.0
我尝试在具有给定平均值的约束下最小化向量的方差var(x)
:x = [x1,...,xN]
0 <= xi <= 1
x
import numpy as np
from scipy.optimize import shgo
# Constraint
avg = 0.5 # Given average value of x
cons = {'type': 'eq', 'fun': lambda x: np.mean(x)-avg}
# Minimize the variance of x under the given constraint
res = shgo(lambda x: np.var(x), bounds=6*[(0, 1)], constraints=cons)
Run Code Online (Sandbox Code Playgroud)
该shgo
方法在这个问题上失败了:
>>> res
fun: 0.0
message: 'Failed to find a feasible minimiser point. Lowest sampling point …
Run Code Online (Sandbox Code Playgroud)