Python 的库 cvxpy 中作为约束的非严格不等式

Non-strict inequalities as contstraints in Python's library cvxpy

我正在使用 cvxpy 库来解决一些特定的优化问题

import cvxpy as cp
import numpy as np

(...)

prob = cp.Problem(
    cp.Minimize(max(M*theta-b)) <= 45,
    [-48 <= theta, theta <= 48])

(这里M和b是某些numpy矩阵。)

有趣的是,它尖叫着:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-62-0296c965b1ff> in <module>
      1 prob = cp.Problem(
----> 2     cp.Minimize(max(M*theta-b)) <= 45,
      3     [-10 <= theta, theta <= 10])

~\Anaconda3\lib\site-packages\cvxpy\expressions\expression.py in __gt__(self, other)
    595         """Unsupported.
    596         """
--> 597         raise NotImplementedError("Strict inequalities are not allowed.")

NotImplementedError: Strict inequalities are not allowed.

然而,对我来说,他们看起来一点也不严格...

与您的 中的原因相同(尽管这样的事情很难分析)。

您需要明确地询问 cvxpy max function这始终是必需的/推荐的

cp.Minimize(max(M*theta-b))

应该是

cp.Minimize(cp.max(M*theta-b))

你基本上只能使用 cvxpy 的函数,除了 following:

The CVXPY function sum sums all the entries in a single expression. The built-in Python sum should be used to add together a list of expressions.