通过使用CVXPY,我想解决这个简单的凸优化问题:-
求z= Ax -b,其中给出了A和b。
守则如下:
x = cp.Variable(n)
z = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(z))
constraints = [z == A@x - b]
prob = cp.Problem(objective, constraints)
result = prob.solve(solver = cp.SCS, verbose = True)我得到了:-
===============================================================================
CVXPY
v1.2.0
===============================================================================
(CVXPY) May 03 09:50:28 AM: Your problem has 20 variables, 1 constraint, and 0 parameters.
(CVXPY) May 03 09:50:28 AM: It is compliant with the following grammar: DCP, DQCP
(CVXPY) May 03 09:50:28 AM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) May 03 09:50:28 AM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) May 03 09:50:28 AM: Compiling problem (target solver=SCS).
(CVXPY) May 03 09:50:28 AM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> SCS
(CVXPY) May 03 09:50:28 AM: Applying reduction Dcp2Cone
(CVXPY) May 03 09:50:28 AM: Applying reduction CvxAttr2Constr
(CVXPY) May 03 09:50:28 AM: Applying reduction ConeMatrixStuffing
(CVXPY) May 03 09:50:28 AM: Applying reduction SCS
(CVXPY) May 03 09:50:28 AM: Finished problem compilation (took 1.690e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) May 03 09:50:28 AM: Invoking solver SCS to obtain a solution.
------------------------------------------------------------------
SCS v3.2.0 - Splitting Conic Solver
(c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem: variables n: 21, constraints m: 22
cones: z: primal zero / dual free vars: 10
q: soc vars: 12, qsize: 1
settings: eps_abs: 1.0e-05, eps_rel: 1.0e-05, eps_infeas: 1.0e-07
alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
max_iters: 100000, normalize: 1, rho_x: 1.00e-06
acceleration_lookback: 10, acceleration_interval: 10
lin-sys: sparse-direct
nnz(A): 117, nnz(P): 0
------------------------------------------------------------------
iter | pri res | dua res | gap | obj | scale | time (s)
------------------------------------------------------------------
0| 2.10e+01 1.00e+00 2.00e+01 -1.00e+01 1.00e-01 1.02e-02
50| 1.06e-12 1.28e-14 1.06e-12 -5.27e-13 1.00e-01 1.10e-02
------------------------------------------------------------------
status: solved
timings: total: 1.30e-02s = setup: 2.04e-03s + solve: 1.10e-02s
lin-sys: 8.78e-05s, cones: 2.06e-05s, accel: 7.90e-06s
------------------------------------------------------------------
objective = -0.000000
------------------------------------------------------------------
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) May 03 09:50:28 AM: Problem status: optimal
(CVXPY) May 03 09:50:28 AM: Optimal value: 1.426e-30
(CVXPY) May 03 09:50:28 AM: Compilation took 1.690e-02 seconds
(CVXPY) May 03 09:50:28 AM: Solver (including time spent in interface) took 1.658e-02 secondsMy problem:我想在每次迭代时访问解决方案值x。
正如您在上面看到的,可以使用verbose = True获得一些细节。然而,当我与许多求解者一起尝试时,它并没有提供有关问题解决方案的信息。
发布于 2022-05-05 17:02:39
从CVXPY的输出中可以看出,您的问题并不大。如果确实如此,一种解决方法是在一个循环中迭代地解决您的问题,在每次迭代时将SCS的'max_iters'选项设置为循环迭代数。有关修改解决程序设置的信息,请参见此页。
https://stackoverflow.com/questions/72086988
复制相似问题