scipy.minimize ограниченная задача оптимизации с несколькими переменными - PullRequest
1 голос
/ 03 августа 2020

В настоящее время я пытаюсь реализовать следующую задачу оптимизации в python (чтобы решить ее с помощью scipy.optimize.minimize).

enter image description here

Please note that alpha is given,T is the number of generated random values (i.e. via Monte Carlo simulation, also given), z is an array of artificial variables (ignore the last constraint). The function f(x,y) is equal to -y.T * x (y is an array of nT random values). Variable val is a pandas data frame with all the observed data. Variable r is the realization of the random events (randomly generated using MonteCarlo technique - fitting a distribution, pandas nT).

Unfortunately I am facing different problems while truing to solve it. Can anyone be so kind to help me to code it correctly?

EDIT: following the modified code with correct init and constraints. I am not able to figured out how to write correctly the bounds (for x[0] bounds should be (0, None), for x 1 и x [ 2] границы должны быть (Нет, Нет). Может ли кто-нибудь быть так любезен, чтобы предложить мне правильный путь?

def objective(x, alpha, t):
    #
    return x[1] + (1 / (1 - alpha) * t) * np.sum(x[2])

def problem(val, t = 10, alpha = 0.9):
    #
    y = []
    
    for simbolo in val.columns:
        loc, scale = sts.gumbel_l.fit(val[simbolo])
        y.append(sts.gumbel_l.rvs(loc, scale, t))
        
    init = np.array(([1 / len(val.columns)] * len(val.columns), [1] * t, [0] * t))
    constraints = [
        {"type": "ineq", "fun": lambda x: x[2]},
        {"type": "ineq", "fun": lambda x: np.dot(x[0].T, np.asarray(y)) + x[1] + x[2]}
        ]
    bounds = ((0, None),) * len(val.columns)
    args = (alpha, t)
        
    res = opt.minimize(
        objective,
        x0 = init,
        args = args,
        bounds = bounds, 
        constraints = constraints
    )
    
    return res['x']

Я попытался написать следующие границы:

bounds = (((0, None),) * len(var.columns), ((None, None),) * len(var.columns), ((None, None),) * len(var.columns))

, я получил следующую ошибку:

 File "main.py", line 250, in <module>
    problem(r)
  File "port.py", line 152, in vanilla_cvar
    res = opt.minimize(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/optimize/_minimize.py", line 625, in minimize
    return _minimize_slsqp(fun, x0, args, jac, bounds,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/optimize/slsqp.py", line 315, in _minimize_slsqp
    new_bounds = old_bound_to_new(bounds)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/optimize/_constraints.py", line 316, in old_bound_to_new
    lb, ub = zip(*bounds)
ValueError: too many values to unpack (expected 2)
...