[英]How to optimize trace of inverse in CVXPY?
How can I formulate an objective function that minimizes the trace of an inverse matrix using cvxpy?如何使用 cvxpy 制定一个最小化逆矩阵迹的目标函数?
Concretely the following problem:具体有以下问题:
subject to:受:
I have seen the trace_inv objective function in cvx but so far was not able to figure out how to translate this to cvxpy.我已经在 cvx 中看到了 trace_inv 目标函数,但到目前为止还无法弄清楚如何将其转换为 cvxpy。
I figured it out by looking at the source code on git, https://github.com/cvxr/CVX/tree/master/functions .我通过查看 git 上的源代码https://github.com/cvxr/CVX/tree/master/functions弄明白了。
The function matrix_frac()
fits the problem.函数
matrix_frac()
适合这个问题。
Here is a minimal example:这是一个最小的例子:
import cvxpy as cp
n = 20
k = 5
A_list = []
for i in range(n):
A = np.random.uniform(0,10,(6,6))
A_list.append(A.T@A)
# Create two scalar optimization variables.
z = cp.Variable(n)
# Create two constraints.
constraints = [z >= 0,
z <= 1,
np.ones((n,))@z == k]
cost = sum([z[i] * A_list[i] for i in range(n)])
# Form objective.
obj = cp.Minimize(cp.matrix_frac(np.eye(6), cost))
# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve() # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
# compute trace of inverse using z
S = np.zeros(A_list[0].shape)
for i in range(len(z.value)):
S += z.value[i] * A_list[i]
value = np.trace(np.linalg.inv(S))
print("value", value)
print(prob.value == value)
Output:输出:
status: optimal
optimal value 0.02061809722699777
value 0.02061809722699777
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.