python - scipy.optimize.minimize with matrix constraints -
i new scipy.optimize module. using minimize function trying find x minimize multivariate function, takes matrix input homecoming scalar value. have 1 equality constraint , 1 inequality constraint, both taking vector input , homecoming vector values. particularly, here list of constraints:
sum(x) = 1 ; ast + np.log2(x) >= 0 where ast parameter. defined constraint functions below:
for equality constraint: lambda x: sum(x) - 1
for inequality constraint:
def asset_cons(x): #global ast if np.logical_and.reduce( (ast + np.log2(x)) >= 0): homecoming 0.01 else: homecoming -1 then call
cons = ({'type':'eq', 'fun': lambda x: sum(x) - 1}, {'type':'ineq', 'fun': asset_cons}) res = optimize.minize(test_obj, [0.2, 0.8], constraints = cons) but still got error complaining constraint function. allowed homecoming vector value constraint function or have homecoming scalar in order utilize minimize function?
could help me see if way specify constraints has problems?
in principle not wrong @ all. however, bit hard without seeing test_obj , actual error. throw exception (which hints @ programming error) or complain convergence (which hints @ mathematical challenge)?
you have basic thought right; need have function accepting input vector n elements , returning value minimized. boundary conditions should take same input vector , homecoming single scalar output.
to eye there wrong boundary conditions. first 1 (sum(x) - 1) fine, sec 1 mathematically challenging, have defined stepwise function. many optimization algorithms want have continuous functions preferable quite smooth behaviour. (i not know if algorithms used function handle stepwise functions well, guess.
if above holds true, might create things easier by, example:
np.amin(ast + np.log2(x)) the function non-negative if ast + log2(x[n]) >= 0. (it still not extremely smooth, if problem easy improve.) , it'll fit 1 lambda.
if have difficulties in convergence, should seek both cobyla , slsqp, unless know 1 of them improve problem.
python scipy constraints mathematical-optimization minimize
No comments:
Post a Comment