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

具有求和约束的线性方程的 CVXPY 最小二乘最小化

如何解决具有求和约束的线性方程的 CVXPY 最小二乘最小化

我正在尝试解决一个超定线性系统,其中解向量应为 1 且 0

A = np.array([[7.958e+01 1.440e+00 6.971e+01 1.040e+00 3.733e+01 1.570e+01],[5.800e+00 3.200e-01 7.200e-01 1.020e+00 1.109e+01 4.980e+00],[1.460e+00 3.400e-01 6.600e-01 6.466e+01 4.050e+00 2.560e+00],[4.340e+00 5.256e+01 1.252e+01 1.427e+01 1.865e+01 4.231e+01],[9.200e-01 2.070e+00 3.610e+00 3.540e+00 5.100e+00 2.380e+00],[0.000e+00 6.000e-01 9.000e-02 2.100e-01 5.700e-01 1.280e+00],[1.450e+00 9.600e-02 2.800e-01 1.500e-01 1.478e+00 4.620e-01],[4.860e-01 2.700e-02 0.000e+00 3.000e-01 7.880e-01 2.330e-01]])

b = np.array([24.6875,6.577,2.2169,59.9135,3.3156,1.0645,0.7319,0.3567])

# Define and solve the CVXPY problem.
x = cp.Variable(6)
objective = cp.Minimize(cp.sum_squares(A@x - b))
constraints = [0 <= x,x <= 1,sum(x) == 1]
prob = cp.Problem(objective,constraints)
prob.solve()

#Print result.
print("\nThe optimal value is",prob.value)
print("The optimal x is")
print(x.value)
print("The norm of the residual is ",cp.norm(A @ x - b,p=2).value)

summ = sum(x.value)
print(summ)

>>>>
The optimal value is 370.4259696888074
The optimal x is
[-3.55262420e-18  7.02949975e-01  1.81476423e-01 -6.08113127e-16
-3.36614249e-16  1.15573602e-01]
The norm of the residual is  19.24645343144574
1.000000000000002

(此方案表示输入材料的样品组成百分比。输入材料(标记为材料 1 到 4)具有以元素 A 到 J 测量的化学成分。样品组成也以元素 A 到 J 测量。{ {3}})

我尝试了许多不同的求和方法,例如cp.sum_entries(x) 和np.sum(x)。我也不知道为什么我返回的 x 值不在 0 和 1 之间...

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