如何最小化这样的功能

How to minimize function like this

函数:

def Phi(u, X):
    return -(1+u[0]*X[0]+u[1]*X[1]+u[2]*X[2]+u[3]*X[3])

而且我知道 X[0]...X[3][-0.08,0.08]u[0],u[1],u[2],u[3] >= 0u[0]+u[1]+u[2]+u[3] = 1 中,我也知道我函数的梯度。然后我定义了约束:

def constraint1(u):
    return u[0]+u[1]+u[2]+u[3]-1.0
def constraint2(u):
    return u[0]-1.0
def constraint3(u):
    return u[1]-1.0
def constraint4(u):
    return u[2]-1.0
def constraint5(u):
    return u[3]-1.0

和界限

bnds = Bounds ([-0.08, -0.08, -0.08], [0.08, 0.08, 0.08])
cons = [{'type': 'eq', 'fun': constraint1},
               {'type': 'ineq', 'fun': constraint2},
               {'type': 'ineq', 'fun': constraint3},
               {'type': 'ineq', 'fun': constraint4},
               {'type': 'ineq', 'fun': constraint5},]
print(minimize(Phi, method='BFGS', jac=grad, constraints=cons, bounds=bnds))

但是我有“TypeError: minimize() missing 1 required positional argument: 'x0'”。我没有关于 x0 的信息。带约束的函数最小化是正确的还是不可能的?

UPD

结果是

def Phi2(params):
    u0,u1,u2,u3,x0,x1,x2,x3 = params
    return -(1+u0*x0+u1*x1+u2*x2+u3*x3)


x0 = np.asarray([0,0,0,0,0,0,0,0])

def constraint1(params):
    u0,u1,u2,u3,x0,x1,x2,x3 = params
    return u0+u1+u2+u3-1.0


bnds = Bounds ([0,0,0,0,-0.08,-0.08,-0.08,-0.08,], [1,1,1,1,0.08, 0.08, 0.08])

cons = [{'type': 'eq', 'fun': constraint1}]
print(minimize(Phi2,x0, method='BFGS', constraints=cons, bounds=bnds))

但是有一些问题。我在 numpy 数组 'grad' 中有 u0、u1、u2、u3 的梯度。如何正确使用?如果我在最小化参数中做 jac=grad 那么结果是

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

scipy documentation 表示 x0 是一个位置参数,因此它是必需的。

关于 x0 是这样说的:

Initial guess. Array of real elements of size (n,), where ‘n’ is the number of independent variables.

看来你至少要提供你的初步猜测。您是否尝试过提供一个空数组?