I have this objective function (in python) :
actions= [...] # some array
Na= len(actions)
# maximize p0 * qr(s,a0,b0) + ... + pn * qr(s,an,bn)
def objective(x):
p = x[:Na] # p is a probability distribution
b = x[Na:2 * Na] # b is an array of positive unbounded scalars
q = np.array([qr(s, actions[a], b[a]) for a in range(0, Na)]) # s is an array
rez = - np.dot(p, q) # np stands for numpy library
return rez
qr
and qc
are regression trees, these are functions mapping arrays to scalars.
I have these constraints :
# p0 * qc(s,a0,b0) + ... + pn * qc(s,an,bn) < beta
def constraint(x):
p = x[:Na]
b = x[Na:2 * Na]
q = np.array([qc(s, actions[a], b[a]) for a in range(0, Na)])
rez = beta - np.dot(p, q) # beta is a scalar
return rez
# elements of p should sum to 1
def constraint_proba_sum_1(x):
p = x[:Na]
rez = 0
for i in range(0, Na):
rez += p[i]
rez = 1 - rez
return rez
How I minimize :
constraints = ({'type': 'ineq', 'fun': constraint},
{'type': 'eq', 'fun': constraint_proba_sum_1})
res = opt.minimize(fun=objective, x0=np.array([0.5, 0.5, 10, 10]), constraints=constraints,
bounds=[(0, 1), (0, 1), (0, None), (0, None)])
The problem is opt.minimize
uses nan
arrays as inputs sometimes during its minimization process "slsqp". Thus the qr
tree raises errors.Why would it evaluate such arrays, in what circumstances ?
I do realize this issue is the same as this post Scipy optimizations methods select nan for input parameter but it is not resolved and it looks like function dependent.
EDIT : It appears that if I remove the constraint constraint_proba_sum_1(x), I dont have NaN value as input anymore.
EDIT 2 : I tryed another API, pyOPT with SLSQP optimization and I have the same issue.