I'm trying to minimize a function of three N-sized arrays that contain the minimization parameters that I want to find. For example, suppose the parameters I want to find in order to minimize the function are given by the arrays x = x[0],x[1],...,x[N-1]
, a = a[0],a[1],...,a[N-1]
, and b = b[0],b[1],...,b[N-1]
. Also, in the problem, the minimization boundaries are constrained, with the constraints being given as:
0 <= x[i] and sum(x[i])-1=0 for all i=0,...,N-1
0 <= a[i] <= Pi/2 for all i=0,...,N-1
0 <= b[i] <= Pi/2 for all i=0,...,N-1
After this question, I was able to define these constraints as follows:
import numpy as np
#defining the constraints for minimization
#constraints on x:
Dx_lhs = np.diag(np.ones(N))
def xlhs(x): #left hand side
return Dx_lhs @ x
def xrhs(x): #right hand side
return np.sum(x) -1
con1x = {'type': 'ineq', 'fun': lambda x: xlhs(x)}
con2x = {'type': 'eq', 'fun': lambda x: xrhs(x)}
#constraints on a:
Da_lhs = np.diag(np.ones(N))
Da_rhs = -np.diag(np.ones(N))
def alhs(a):
return Da_lhs @ a
def arhs(a):
return Da_rhs @ a + (np.ones(N))*np.pi/2
con1a = {'type': 'ineq', 'fun': lambda a: alhs(H)}
con2a = {'type': 'ineq', 'fun': lambda a: -1.0*Hrhs(H)}
# Restrições em b:
Db_lhs = np.diag(np.ones(N))
Db_rhs = -np.diag(np.ones(N))
def blhs(b):
return Db_lhs @ b
def brhs(b):
return Db_rhs @ b + (np.ones(N))*np.pi/2
con1b = {'type': 'ineq', 'fun': lambda b: alhs(H)}
con2b = {'type': 'ineq', 'fun': lambda b: -1.0*Hrhs(H)}
Now suppose I have a function like:
def fun(mins): #just an example
x, a, b = mins
for i in range(N):
sbi=0; sai=0
for j in range(i+1):
sbi += 2*x[j]*np.tan(b[j])
sli += 2*x[j]*np.tan(a[j])
B[i]=sbi
A[i]=sai
return (B @ C)
Which is not working since probably the first line of the function isn't the right way to define it (I couldn't figure out how should I declare the arrays containing the variables that I want to minimize). Can anyone help me on how can I fix this and apply the scipy.optimize.minimize
to find the values for x[], a[]
and b[]
that minimizes my function?
P.S.: The function presented is just for illustrative purposes, the minimization solution may be obvious.