如何解决cvxpy如何制定取决于决策变量符号的表达式
优化问题 public class main{
public static void main(String[] args) throws Exception {
ArrayList<A> arrayListA = JsonConverter.convertFromJson("a.json",new A());
ArrayList<B> arrayListB = JsonConverter.convertFromJson("b.json",new B());
ArrayList<C> arrayListC = JsonConverter.convertFromJson("c.json",new C());
}
}
有两个约束条件列表:
-
Maximize(-obj_Sigma - obj_Cost)
项是决策变量向量obj_Sigma
的四元形式。 -
x
项是一个线性项,它取决于决策变量向量obj_Cost
中条目的符号。
在这个例子中:
- 如果
x
中的条目与x
的符号相同,则将 5 添加到prev_x
- 如果
obj_Cost
中的条目与x
的符号相反,则从prev_x
中减去 5
我的代码如下:
obj_Cost
我知道 import cvxpy as cp
import numpy as np
# Generate a random non-trivial quadratic program.
n = 10
np.random.seed(1)
P = np.random.randn(n,n)
Sigma = P.T @ P
# prevIoUs result
prev_x = np.random.randn(n)
# Define and solve the CVXPY problem.
x = cp.Variable(n)
# objective function first term
obj_Sigma = 1/2*cp.quad_form(x,Sigma)
# objective function 2nd term.
# if x has same sign as the corresponding entry in prev_x,# then add 5.0 to the cost.
# otherwise substract 5 from the cost
# ---- This formulation has problem.
obj_Cost = cp.sum(5*cp.sign(cp.multiply(prev_x,x)))
obj = -obj_Sigma - obj_Cost
prob = cp.Problem(cp.Maximize(obj),[cp.norm(x,1) <= 1.0,cp.abs(x) <= 0.01
])
prob.solve(verbose=True,solver=cp.MOSEK)
表达式有问题。但是我应该如何制定这个成本条款?一般来说,我如何用依赖于决策变量符号的组件来表述问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。