Function:
def Phi(u, X):
return -(1+u[0]*X[0]+u[1]*X[1]+u[2]*X[2]+u[3]*X[3])
And I know that X[0]...X[3]
is in [-0.08,0.08]
and u[0],u[1],u[2],u[3] >= 0
and u[0]+u[1]+u[2]+u[3] = 1
, also I know gradient of my functions. Then I defined constraint:
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
And bounds
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))
But I have " TypeError: minimize() missing 1 required positional argument: 'x0'
". And I havent information about x0
. Is it correct minimization of function with constraints or its impossible to do this?
UPD
Result is
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))
But there is some problem. I have gradient for u0,u1,u2,u3 in numpy array 'grad'. How to use it correctly? If i do jac=grad in parametrs of minimize then result is
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()