# Import packages. import cvxpy as cp import numpy as np
# Generate a random non-trivial【非平凡】 linear program. m = 15 n = 10 np.random.seed(1) s0 = np.random.randn(m) lamb0 = np.maximum(-s0, 0) s0 = np.maximum(s0, 0) x0 = np.random.randn(n) A = np.random.randn(m, n) b = A @ x0 + s0 c = -A.T @ lamb0
# Define and solve the CVXPY problem. x = cp.Variable(n) prob = cp.Problem(cp.Minimize(c.T@x), [A @ x <= b]) prob.solve()
# Print result. print("\nThe optimal value is", prob.value) print("A solution x is") print(x.value) print("A dual solution is") print(prob.constraints[0].dual_value)
结果:
1 2 3 4 5 6 7 8 9
The optimal value is -15.220912605552863 A solution x is [-1.10133381 -0.16360111 -0.89734939 0.03216603 0.6069123 -1.12687348 1.12967856 0.88176638 0.49075229 0.8984822 ] A dual solution is [6.98805172e-10 6.11756416e-01 5.28171747e-01 1.07296862e+00 3.93759300e-09 2.30153870e+00 4.25704434e-10 7.61206896e-01 8.36906030e-09 2.49370377e-01 1.30187120e-09 2.06014070e+00 3.22417207e-01 3.84054343e-01 1.59493839e-09]
# Import packages. import cvxpy as cp import numpy as np
# Generate data. m = 20 n = 15 np.random.seed(1) A = np.random.randn(m, n) b = np.random.randn(m)
# Define and solve the CVXPY problem. x = cp.Variable(n) cost = cp.sum_squares(A @ x - b) prob = cp.Problem(cp.Minimize(cost)) 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)
结果如下:
1 2 3 4 5 6
The optimal value is 7.005909828287485 The optimal x is [ 0.17492418 -0.38102551 0.34732251 0.0173098 -0.0845784 -0.08134019 0.293119 0.27019762 0.17493179 -0.23953449 0.64097935 -0.41633637 0.12799688 0.1063942 -0.32158411] The norm of the residual is 2.6468679280023557
☺️:
这里A为20*15的矩阵,b为20维列向量。
x = cp.Variable(n) 定义了一个叫x的变量,它是一个15维列向量,具体数值这一步不确定。cp.sum_squares函数就是计算平方和的函数,prob=cp.Problem() 定义了一个“问题”,“问题”函数里填写凸优化的目标,目前的目标就是那个“平方和”cost最小,使用cp.Minimize函数表示。prob.solve() 求解,运行完这一步才能确定x的具体数值。
# Import packages. import cvxpy as cp import numpy as np
# Generate a random non-trivial linear program. m = 15 n = 10 np.random.seed(1) s0 = np.random.randn(m) lamb0 = np.maximum(-s0, 0) s0 = np.maximum(s0, 0) x0 = np.random.randn(n) A = np.random.randn(m, n) b = A @ x0 + s0 c = -A.T @ lamb0
# Define and solve the CVXPY problem. x = cp.Variable(n) prob = cp.Problem(cp.Minimize(c.T@x), [A @ x <= b]) prob.solve()
# Print result. print("\nThe optimal value is", prob.value) print("A solution x is") print(x.value) print("A dual solution is") print(prob.constraints[0].dual_value)
结果:
1 2 3 4 5 6 7 8 9
The optimal value is -15.220912605552863 A solution x is [-1.10133381 -0.16360111 -0.89734939 0.03216603 0.6069123 -1.12687348 1.12967856 0.88176638 0.49075229 0.8984822 ] A dual solution is [6.98805172e-10 6.11756416e-01 5.28171747e-01 1.07296862e+00 3.93759300e-09 2.30153870e+00 4.25704434e-10 7.61206896e-01 8.36906030e-09 2.49370377e-01 1.30187120e-09 2.06014070e+00 3.22417207e-01 3.84054343e-01 1.59493839e-09]
hhgw:
s0,lamb0,x0是构造线性规划方程组,虽然规划得有点绕,但是不重要, prob = cp.Problem(cp.Minimize(c.T@x),[A @ x <= b]) 看懂这一步就行了。