python - 如何使用带有约束和动态函数的 Scipy minimize

标签 python optimization numpy matrix scipy

我是 scipy.optimize 模块的新手,需要帮助尝试在与矩阵一起使用的公式 V 上使用最小化函数,并且有 2 个约束,但我不确定我是否正确处理了函数的形成或其中之一限制条件。

M 是一个 NxN 矩阵,但例如我会给出 4x4 矩阵

 [[ 0.00123727  0.00011974  0.00067403  0.00060845]
 [ 0.00011974  0.00736665  0.00165674  0.00053581]
 [ 0.00067403  0.00165674  0.00281547  0.00129646]
 [ 0.00060845  0.00053581  0.00129646  0.00153195]]

X 是一个 1xN 矩阵,其中所有数字都是正数,并且加起来必须等于 1,这就是我正在解决的问题

限制:

X[0] + X[1] + X[2] + … X[n] = 1

E = X[0]*R[0] + X[1]*R[1] + X[2]*R[2] + … X[n]*R[n] (E is user input)

我试图最小化的功能是:

V = X[0]**2 * M[0][0]  + 2*X[0]*X[1]*M[0][1] + 2*X[0]*X[2]*M[0][2] + 2 * X[0] * X[3] * M[0][3] * X[1]**2 * M[1][1] + ….etc

到目前为止我的代码是

    cons = ({'type': 'eq', 'fun': lambda x: sum(w) - 1})

    args = n, m, w

    answer = scipy.optimize.minimize(func(*args), w, args=(), method='SLSQP',
                                     bounds=((0, None), (0, None)),
                                     constraints=cons)
def func(n, m, x):
    row = 0
    col = 0
    formula = []
    while row < n:
        while col < n:
            if col == row:
                item = (x[row]**2) * m[row][col]
                formula.append(item)
                col += 1
            else:
                item2 = 2 * x[row] * x[col] * m[row][col]
                formula.append(item2)
                col += 1

        row += 1
        col = 0

    return sum(formula)

我无法理解的是如何表达第二个约束。我也不确定我是否正确处理了公式的创建。非常感谢任何帮助

最佳答案

正如评论中提到的,这可以通过专门的凸求解器来解决。

<小时/>
from __future__ import print_function

import numpy as np
import cvxpy
from scipy.optimize import minimize

# Problem data.
n = 4
M = [
        [0.00123727, 0.00011974, 0.00067403, 0.00060845],
        [0.00011974, 0.00736665, 0.00165674, 0.00053581],
        [0.00067403, 0.00165674, 0.00281547, 0.00129646],
        [0.00060845, 0.00053581, 0.00129646, 0.00153195]]
np.random.seed(1)
R = np.random.randn(n)
E = np.random.randn()

print('R:', R)
print('E:', E)
print()

A = np.matrix([np.ones(n), R])
b = np.matrix([[1], [E]])


def solve_cvxpy():

    # Construct the problem.
    x = cvxpy.Variable(n)
    objective = cvxpy.Minimize(cvxpy.quad_form(x, M))
    constraints = [0 <= x, A*x == b]
    problem = cvxpy.Problem(objective, constraints)

    # Solve the problem.
    result = problem.solve()
    print('cvxpy solution:')
    print(x.value)
    print()


def solve_scipy():
    def simplex_constraint(x):
        return x.sum() - 1
    def dot_constraint(x):
        return x.dot(R) - E
    def objective(x):
        return x.dot(M).dot(x)
    x0 = np.ones(n) / n
    bounds = [(0, np.inf)]*n
    constraints = (
            dict(type='eq', fun=simplex_constraint),
            dict(type='eq', fun=dot_constraint))
    result = minimize(
            objective, x0, method='SLSQP', tol=1e-8,
            bounds=bounds, constraints=constraints)
    print('scipy solution:')
    print(result.x)
    print()

solve_cvxpy()
solve_scipy()
<小时/>
R: [ 1.62434536 -0.61175641 -0.52817175 -1.07296862]
E: 0.865407629325

cvxpy solution:
[[  7.03877116e-01]
 [  8.62848827e-02]
 [  5.54383296e-06]
 [  2.09832457e-01]]

scipy solution:
[  7.03877583e-01   8.62886986e-02  -1.24818775e-17   2.09833718e-01]

关于python - 如何使用带有约束和动态函数的 Scipy minimize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26004679/

相关文章:

python - 为什么我的 MV 无法收敛,而普通 Vars 可以收敛?

python - 索引/切片 numpy 数组的困惑

python - Python Speech_recognition无法检测到麦克风的音频

python - scipy cephes 导入错误

python - 在对列表中查找唯一对

python - 设置 SQLite 数据库进行聚类分析

r - 大数据集上的高效子字符串搜索

arrays - 数组访问总是恒定时间/O(1) 吗?

python - 在 nan 之后提取 numpy 数组中的第一个匹配项

python - 如何在列表中找到最接近的值,其中 x > numpy 数组中每个值的值而不循环