Is there a way to autogenerate a dictionary list of multiple constraints in scipy.minimize? When I use the following code (where the list constraint is a list of sage multivariate polynomials over the same ring)
cons = [{'type': 'eq', 'fun': lambda s: ((constraint[0])(*s))},
{'type': 'eq', 'fun': lambda s: ((constraint[1])(*s))},
{'type': 'eq', 'fun': lambda s: ((constraint[2])(*s))},
{'type': 'eq', 'fun': lambda s: ((constraint[3])(*s))}]
y0 = [.5 for xx in x]
bnds = tuple([(0.0, 1.0) for xx in x])
ssoln = scipy.optimize.minimize(HH, y0, jac=dHH, method='SLSQP', bounds=bnds, constraints=cons)
print ssoln
My output is
status: 0
success: True
njev: 14
nfev: 22
fun: -2.2669026273652237
x: array([ 0.034829615490635, 0.933405952554424, 0.93340765416238 ,
0.093323548109654, 0.335713397575351, 0.413107862378296])
message: 'Optimization terminated successfully.'
jac: array([-3.321836605297572, 2.640225014918886, 2.640252390205999,
-2.273713195767229, -0.682455873949375, -0.351132324172705, 0. ])
nit: 14
However if I try to create cons by
cons=[]
for ii in range(len(constraint)):
cons.append({'type': 'eq', 'fun': lambda s: ((constraint[ii])(*s))})
minimize fails with
status: 6
success: False
njev: 1
nfev: 1
fun: -4.1588830833596715
x: array([ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5])
message: 'Singular matrix C in LSQ subproblem'
jac: array([ 0., 0., 0., 0., 0., 0., 0.])
nit: 1
My list, constraint, of sage polynomials may change in length, in number of polynomials from problem to problem and I don't want to have to hard code the cons dict list as first given above for each problem. Is there a way to automate?
The following works but I understand it is not best practices to eval strings
str1='{\'type\': \'eq\', \'fun\': lambda s: ((constraint['
str2='])(*s))},'
mystr='['
for ii in range(len(constraint)):
mystr=mystr+str1+str(ii)+str2
mystr=mystr+']'
cons = eval(mystr)