gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:42:10 32 4
gpt4 key购买 nike

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

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

最佳答案

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

您需要向 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.

关于python - Python 库 cvxpy 中非严格不等式作为约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59400094/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com