'Displaying the solution at each iteration by using CVXPY
By using CVXPY, I want to solve this simple convex optimization problem:-
min ||z||^2 subject to z = Ax - b where A and b are given
The code is as follows:-
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)
And I got:-
===============================================================================
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 seconds
My problem: I want to access the solution value x at each iteration.
As you see above, it's possible to get some details by using verbose = True
. However, as I tried it with many solvers, it doesn't provide information about the solution to the problem.
Solution 1:[1]
It appears from CVXPY's output that your problem isn't very large. If that is indeed the case, one workaround would be to solve your problem iteratively in a loop, at each iteration setting SCS's 'max_iters'
option to the loop iteration number. See this page for information on modifying the solver's settings.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user6745003 |