scipy.optimize.minimize有多个约束

时间:2018-07-10 17:50:02

标签: python pandas optimization scipy

我正在尝试使用scipy.optimize.minimize执行约束非线性优化问题,尤其是最小化问题。具体来说,我正在尝试执行constrained linear regression,它可以写为:

最小y-(b 1 x 1 + b 2 x 2 +。 。+ b n x n + b n + 1 x n + 1 + .... b < sub> p x p

要遵守 b 1 + b 2 + ... + b n = 1,

b n + 1 + b n + 2 + ... + b p = 1,

0 <= b i <= 1 for i = 1,2,...,p

到目前为止,这是我的代码:

import pandas as pd
import numpy as np
from scipy.optimize import minimize
import warnings

def loss(x):
    loss_function = np.sum(np.square((np.dot(x, m) - y)))
    return loss_function

m = np.vstack([var_1, var_2, var_3,..., var_n, var_n+1,...,var_p])
y = np.array(ret['var_y'])

cons = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1.0})

x0 = np.zeros(m.shape[0])

res = minimize(loss, x0, method='SLSQP', constraints=cons,
                   bounds=[(0, 1) for i in range(m.shape[0])], options={'disp': True})

我的问题是,如何在模型中实现这些约束?

0 个答案:

没有答案