【问题标题】:Non-strict inequalities as contstraints in Python's library cvxpy非严格不等式作为 Python 库 cvxpy 中的约束
【发布时间】:2019-12-18 21:17:34
【问题描述】:

我正在使用 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.

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

【问题讨论】:

    标签: python numpy cvxpy


    【解决方案1】:

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

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

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

    应该是

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

    您基本上只能使用 cvxpy 中的函数,following 除外:

    CVXPY 函数对单个表达式中的所有条目求和。内置的 Python sum 应该用于将表达式列表相加。

    【讨论】:

    • 没错!但是仍然出现一些有趣的错误---&gt; 50 return result.astype(numpy.float64) 51 52 # Return an identity matrix. TypeError: float() argument must be a string or a number, not 'Inequality'
    • 如果没有可重现的示例(您的示例缺少代码),我将无能为力。我唯一非常疯狂的猜测:你用过np.matrix吗?如果,不要!在 2019 年(现在可能是几年),np.matrix 已被弃用/不推荐see note。使用普通的 2d numpy-array。
    • 谢谢。你介意看看吗? stackoverflow.com/questions/59400478/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多