微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CVX解决A/x, 1/x这种形式的问题

CVX解决A/x, 1/x这种形式的问题

尝试使用CVX解决凸优化问题的过程中,有这样一条约束:

a = 10
x = Variable(nonneg=True) # 声明X变量为非负
y = Variable()
cons = [a/x <= y])

因为1/x在x>0是为凸的,也符合CVX规定的凸<=凹。
但是在运行的时候,报错:

cvxpy.error.DCPError: Problem does not follow DCP rules.

说不符合DCP规则。

最后在CVX官网找到了解决办法如下:
This has the effect of automatically constraining the argument of a function to be in the function’s domain. For example, if we form sqrt(x+1) in a CVX specification, where x is a variable, then x will automatically be constrained to be larger than or equal to −1. There is no need to add a separate constraint, x>=-1, to enforce this.

Monotonicity of a function is determined in the extended sense, i.e., including the values of the argument outside its domain. For example, sqrt(x) is determined to be nondecreasing since its value is constant (−∞) for negative values of its argument; then jumps up to 0 for argument zero, and increases for positive values of its argument.

CVX does not consider a function to be convex or concave if it is so only over a portion of its domain, even if the argument is constrained to lie in one of these portions. As an example, consider the function 1/x. This function is convex for x>0, and concave for x<0. But you can never write 1/x in CVX (unless x is constant), even if you have imposed a constraint such as x>=1, which restricts x to lie in the convex portion of function 1/x. You can use the CVX function inv_pos(x), defined as 1/x for x>0 and ∞ otherwise, for the convex portion of 1/x; CVX recognizes this function as convex and nonincreasing. In CVX, you can express the concave portion of 1/x, where x is negative, using -inv_pos(-x), which will be correctly recognized as concave and nonincreasing.
大意是说对于sqrt(x+1)这种,CVX自动生成了约束x>=-1,因为sqrt的定义域>=0,所以不用再去加入约束x>=-1。
但是对于1/x这种,定义域x>0时为凸函数,x<0时为凹函数
CVX不允许写1/x(除非x为常数), 加了x>1也没用。正确的办法如下:
使用inv_pos(x),类似于1/x(x>0)
最后我们来测试下:

a=10
y = Variable()
x = Variable()
print((a/x<=y).is_dcp())
print((a*inv_pos(x) <= y).is_dcp())

输出为:

False
True

可见第二种方法满足了DCP规则!
更多的DCP规则在链接: 点这里!

原文地址:https://www.jb51.cc/wenti/3280913.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐