cvxpy约束矢量条件值

发布于 2025-01-27 02:15:52 字数 747 浏览 3 评论 0原文

首先,我是Python的CVXPY模块的新手。

我对Python的CVXPY库中的约束有疑问。

我正在创建一个包含一些规格的向量权重。 我的问题是创建约束列表时。我的规格是:

  1. 重量不能大于0.2
  2. 重量总和不能大于1.0的
  3. 重量不能小于0的
  4. 所有权重大于0.07不能超过0.5 0.5

的问题。问题是最后一个。我不知道如何在约束定义中过滤体重内部的重量内部。 我认为这不是做到这一点的正确方法。但是我找不到可以在CVXPY网页中帮助/理解的东西。

import cvxpy as cv
w = cv.Variable(mu.shape[1])
constraint=[
            cv.sum(w)==1,                 #all the values have to sum 1
            cv.max(w)<=0.2,               #each value cant be bigger than 0.2
            cv.min(w)>=0.0,               #each value cant be less than 0
            # w[w>0.07].sum()<0.5         #MY PROBLEM, I dont know how to express this in cvxpy
           ]

预先感谢您的宝贵时间。

First of all I'm a newbie with the cvxpy module in python.

I have a doubt about the constraint in the CVXPY library in python.

I'm creating a vector weights that contains some specifications.
My problem is when I create the constraint list. My specifications are:

  1. weight cant be greater than 0.2
  2. weight sum cant be greater than 1.0
  3. weight cant be less than 0
  4. all the weights bigger than 0.07 cant sum more than 0.5

The problem is the last one. I dont know how to filter values inside weight inside is own in the constraint definition.
I think that is not the correct way to do it. But I cant find something that can help/understand in the CVXPY webpage.

import cvxpy as cv
w = cv.Variable(mu.shape[1])
constraint=[
            cv.sum(w)==1,                 #all the values have to sum 1
            cv.max(w)<=0.2,               #each value cant be bigger than 0.2
            cv.min(w)>=0.0,               #each value cant be less than 0
            # w[w>0.07].sum()<0.5         #MY PROBLEM, I dont know how to express this in cvxpy
           ]

Thank you in advance for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2025-02-03 02:15:52

尝试

cv.sum([x for x in w if x > 0.07]) < 0.5

try

cv.sum([x for x in w if x > 0.07]) < 0.5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文