How to change problem parameter in cvxpy without recompiling?

65 views Asked by At

I have a SDP problem that takes a couple MOSEK solver seconds to run but a couple minutes to compile in cvxpy. I hope to be able to change parameters and solve again without having to recompile, and see what form it's compiled into, so that I can directly call MOSEK next time. Putting the same problem into MOSEK fusion also takes a few minutes to compile, so it's definitely not the most efficient formulation.

Is there a way to rerun cvxpy without recompiling a problem, or find the mosek.task it created?

Output:

Total num constraints: 3070
===============================================================================
                                     CVXPY                                     
                                     v1.4.1                                    
===============================================================================
(CVXPY) Nov 09 09:28:10 PM: Your problem has 1764 variables, 3070 constraints, and 0 parameters.
(CVXPY) Nov 09 09:28:10 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Nov 09 09:28:10 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Nov 09 09:28:10 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
(CVXPY) Nov 09 09:28:10 PM: Your problem is compiled with the CPP canonicalization backend.
-------------------------------------------------------------------------------
                                  Compilation                                  
-------------------------------------------------------------------------------
(CVXPY) Nov 09 09:28:10 PM: Compiling problem (target solver=MOSEK).
(CVXPY) Nov 09 09:28:10 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> MOSEK
(CVXPY) Nov 09 09:28:10 PM: Applying reduction Dcp2Cone
(CVXPY) Nov 09 09:28:11 PM: Applying reduction CvxAttr2Constr
(CVXPY) Nov 09 09:28:11 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Nov 09 09:30:26 PM: Applying reduction MOSEK
(CVXPY) Nov 09 09:30:34 PM: Finished problem compilation (took 1.436e+02 seconds).
-------------------------------------------------------------------------------
                                Numerical solver                               
-------------------------------------------------------------------------------
(CVXPY) Nov 09 09:30:34 PM: Invoking solver MOSEK  to obtain a solution.


(CVXPY) Nov 09 09:30:37 PM: Problem
(CVXPY) Nov 09 09:30:37 PM:   Name                   :                 
(CVXPY) Nov 09 09:30:37 PM:   Objective sense        : maximize        
(CVXPY) Nov 09 09:30:37 PM:   Type                   : CONIC (conic optimization problem)
(CVXPY) Nov 09 09:30:37 PM:   Constraints            : 903             
(CVXPY) Nov 09 09:30:37 PM:   Affine conic cons.     : 0               
(CVXPY) Nov 09 09:30:37 PM:   Disjunctive cons.      : 0               
(CVXPY) Nov 09 09:30:37 PM:   Cones                  : 0               
(CVXPY) Nov 09 09:30:37 PM:   Scalar variables       : 3069            
(CVXPY) Nov 09 09:30:37 PM:   Matrix variables       : 1 (scalarized: 903)
(CVXPY) Nov 09 09:30:37 PM:   Integer variables      : 0               
(CVXPY) Nov 09 09:30:37 PM: 
(CVXPY) Nov 09 09:30:37 PM: Optimizer started.
(CVXPY) Nov 09 09:30:38 PM: Presolve started.
(CVXPY) Nov 09 09:30:38 PM: Linear dependency checker started.
(CVXPY) Nov 09 09:30:38 PM: Linear dependency checker terminated.
(CVXPY) Nov 09 09:30:38 PM: Eliminator started.
(CVXPY) Nov 09 09:30:39 PM: Freed constraints in eliminator : 0
(CVXPY) Nov 09 09:30:39 PM: Eliminator terminated.
(CVXPY) Nov 09 09:30:39 PM: Eliminator - tries                  : 1                 time                   : 0.00            
(CVXPY) Nov 09 09:30:39 PM: Lin. dep.  - tries                  : 1                 time                   : 0.05            
(CVXPY) Nov 09 09:30:39 PM: Lin. dep.  - primal attempts        : 1                 successes              : 1               
(CVXPY) Nov 09 09:30:39 PM: Lin. dep.  - dual attempts          : 0                 successes              : 0               
(CVXPY) Nov 09 09:30:39 PM: Lin. dep.  - primal deps.           : 0                 dual deps.             : 0               
(CVXPY) Nov 09 09:30:39 PM: Presolve terminated. Time: 1.40    
(CVXPY) Nov 09 09:30:40 PM: GP based matrix reordering started.
(CVXPY) Nov 09 09:30:40 PM: GP based matrix reordering terminated.
(CVXPY) Nov 09 09:30:40 PM: Optimizer  - threads                : 16              
(CVXPY) Nov 09 09:30:40 PM: Optimizer  - solved problem         : the primal      
(CVXPY) Nov 09 09:30:40 PM: Optimizer  - Constraints            : 903             
(CVXPY) Nov 09 09:30:40 PM: Optimizer  - Cones                  : 1               
(CVXPY) Nov 09 09:30:40 PM: Optimizer  - Scalar variables       : 2977              conic                  : 2               
(CVXPY) Nov 09 09:30:40 PM: Optimizer  - Semi-definite variables: 1                 scalarized             : 903             
(CVXPY) Nov 09 09:30:40 PM: Factor     - setup time             : 0.42            
(CVXPY) Nov 09 09:30:40 PM: Factor     - dense det. time        : 0.09              GP order time          : 0.01            
(CVXPY) Nov 09 09:30:40 PM: Factor     - nonzeros before factor : 4.08e+05          after factor           : 4.08e+05        
(CVXPY) Nov 09 09:30:40 PM: Factor     - dense dim.             : 0                 flops                  : 2.00e+09        
(CVXPY) Nov 09 09:30:40 PM: Factor     - GP saved nzs           : 0                 GP saved flops         : 1.68e+09        
(CVXPY) Nov 09 09:30:40 PM: ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
(CVXPY) Nov 09 09:30:40 PM: 0   1.0e+00  1.0e+00  1.0e+00  0.00e+00   -0.000000000e+00  -0.000000000e+00  1.0e+00  3.28  
(CVXPY) Nov 09 09:30:40 PM: 1   9.5e-02  9.5e-02  2.2e-02  1.56e+00   1.400359896e-01   1.569570400e-01   9.5e-02  3.68  
(CVXPY) Nov 09 09:30:41 PM: 2   2.7e-03  2.7e-03  2.4e-04  8.33e-01   1.203192748e-01   1.152889102e-01   2.7e-03  4.12  
(CVXPY) Nov 09 09:30:41 PM: 3   2.9e-04  2.9e-04  2.4e-05  1.21e-01   1.155096043e-01   1.090407726e-01   2.9e-04  4.54  
(CVXPY) Nov 09 09:30:42 PM: 4   1.8e-05  1.8e-05  1.7e-06  -1.12e-02  9.341400974e-02   8.411031679e-02   1.8e-05  5.02  
(CVXPY) Nov 09 09:30:42 PM: 5   9.3e-06  9.3e-06  8.8e-07  -4.30e-01  7.973164702e-02   7.085896904e-02   9.3e-06  5.26  
(CVXPY) Nov 09 09:30:42 PM: 6   6.0e-06  6.0e-06  6.1e-07  -2.58e-01  7.104737469e-02   6.077764899e-02   6.0e-06  5.51  
(CVXPY) Nov 09 09:30:43 PM: 7   9.2e-07  9.2e-07  3.9e-08  6.36e-01   2.842954592e-02   2.664187693e-02   9.2e-07  6.04  
(CVXPY) Nov 09 09:30:43 PM: 8   3.3e-07  3.3e-07  6.9e-09  3.10e-01   1.312244937e-02   1.268182239e-02   3.3e-07  6.41  
(CVXPY) Nov 09 09:30:43 PM: 9   1.6e-07  1.6e-07  2.7e-09  4.78e-01   7.677685919e-03   7.388148520e-03   1.6e-07  6.70  
(CVXPY) Nov 09 09:30:44 PM: 10  5.1e-09  5.1e-09  1.5e-11  9.99e-01   1.358048728e-03   1.349482175e-03   5.1e-09  7.22  
(CVXPY) Nov 09 09:30:44 PM: 11  4.9e-09  4.4e-09  1.5e-11  -6.94e-01  1.360571367e-03   1.349826155e-03   4.4e-09  7.50  
(CVXPY) Nov 09 09:30:44 PM: 12  4.2e-09  2.9e-09  8.0e-12  1.89e-01   1.049631511e-03   1.042161814e-03   2.9e-09  7.74  
(CVXPY) Nov 09 09:30:45 PM: 13  3.2e-09  2.1e-10  2.7e-13  4.00e-01   3.854344216e-04   3.837301023e-04   2.1e-10  8.10  
(CVXPY) Nov 09 09:30:45 PM: 14  2.2e-09  1.4e-10  1.9e-13  1.17e-01   3.601089329e-04   3.583670425e-04   1.4e-10  8.58  
(CVXPY) Nov 09 09:30:46 PM: 15  6.4e-10  4.1e-11  2.7e-14  7.15e-01   2.734589867e-04   2.730397834e-04   4.1e-11  8.90  
(CVXPY) Nov 09 09:30:46 PM: 16  1.3e-10  8.5e-12  3.0e-15  7.14e-01   2.505408359e-04   2.504174325e-04   8.5e-12  9.06  
(CVXPY) Nov 09 09:30:46 PM: 17  2.8e-11  1.8e-12  2.9e-16  8.98e-01   2.444877868e-04   2.444619070e-04   1.8e-12  9.20  
(CVXPY) Nov 09 09:30:46 PM: 18  3.6e-11  1.7e-13  8.2e-18  1.00e+00   2.431954395e-04   2.431930224e-04   1.7e-13  9.26  
(CVXPY) Nov 09 09:30:46 PM: Optimizer terminated. Time: 9.32    
(CVXPY) Nov 09 09:30:46 PM: 
(CVXPY) Nov 09 09:30:46 PM: 
(CVXPY) Nov 09 09:30:46 PM: Interior-point solution summary
(CVXPY) Nov 09 09:30:46 PM:   Problem status  : PRIMAL_AND_DUAL_FEASIBLE
(CVXPY) Nov 09 09:30:46 PM:   Solution status : OPTIMAL
(CVXPY) Nov 09 09:30:46 PM:   Primal.  obj: 2.4319543953e-04    nrm: 2e-01    Viol.  con: 4e-09    var: 2e-11    barvar: 0e+00  
(CVXPY) Nov 09 09:30:46 PM:   Dual.    obj: 2.4319302238e-04    nrm: 2e+02    Viol.  con: 0e+00    var: 2e-11    barvar: 2e-11  
-------------------------------------------------------------------------------
                                    Summary                                    
-------------------------------------------------------------------------------
(CVXPY) Nov 09 09:30:46 PM: Problem status: optimal
(CVXPY) Nov 09 09:30:46 PM: Optimal value: 2.432e-04
(CVXPY) Nov 09 09:30:46 PM: Compilation took 1.436e+02 seconds
(CVXPY) Nov 09 09:30:46 PM: Solver (including time spent in interface) took 1.223e+01 seconds
Solver time: 9.324919939041138 (s)
CPU times: user 1min 34s, sys: 41.8 s, total: 2min 16s
Wall time: 2min 37

Thanks

1

There are 1 answers

0
That Frank Guy On

To change parameter without recompiling, set the quantity as a parameter by

lam = cvxpy.Parameter()
...
prob = cvxpy.Problem(cvxpy.Minimize(...))

Updating its value before calling prob.solve does not trigger recompile:

lam.value=12334
prob.solve()

This is not put very clearly in the docs.