Solver Gurobi and OSQP QP optimisation giving contrastingly different results

685 views Asked by At

results are different The Gurobi solver output for the same specifications is correct while the OSQP gives wrong solution which is way off. Why? Below is the Gurobi Barrier solver log

    Parameter OutputFlag unchanged
   Value: 1  Min: 0  Max: 1  Default: 1
Changed value of parameter QCPDual to 1
   Prev: 0  Min: 0  Max: 1  Default: 0
Gurobi Optimizer version 9.0.0 build v9.0.0rc2 (win64)
Optimize a model with 240240 rows, 77196 columns and 489172 nonzeros
Model fingerprint: 0xdb33ff46
Model has 56012 quadratic objective terms
Coefficient statistics:
  Matrix range     [5e-02, 3e+00]
  Objective range  [3e-04, 1e+04]
  QObjective range [8e-11, 4e-04]
  Bounds range     [0e+00, 0e+00]
  RHS range        [3e-01, 1e+04]
Presolve removed 239256 rows and 76677 columns
Presolve time: 0.26s
Presolved: 984 rows, 519 columns, 2643 nonzeros
Presolved model has 815 quadratic objective terms
Ordering time: 0.00s

Barrier statistics:
 Free vars  : 90
 AA' NZ     : 6.720e+03
 Factor NZ  : 1.221e+04 (roughly 1 MByte of memory)
 Factor Ops : 2.632e+05 (less than 1 second per iteration)
 Threads    : 1

                  Objective                Residual
Iter       Primal          Dual         Primal    Dual     Compl     Time
   0   2.52540363e+06 -5.69095127e+06  2.52e+04 8.95e+02  9.05e+05     0s
   1   9.93757272e+04 -3.17876909e+06  3.99e+03 1.41e+02  1.48e+05     0s
   2   9.11301845e+04 -2.28607080e+06  1.51e+02 5.36e+00  7.27e+03     0s
   3   1.03378792e+05 -3.92723265e+05  1.11e+01 3.95e-01  7.64e+02     0s
   4   8.86245744e+04 -1.08269004e+05  2.03e+00 7.18e-02  2.00e+02     0s
   5   3.53997941e+04 -2.58776169e+04  3.90e-01 1.38e-02  4.66e+01     0s
   6   1.06137209e+04 -6.30710427e+03  6.09e-02 2.16e-03  1.11e+01     0s
   7   3.52031557e+03 -2.17212662e+03  1.59e-02 5.64e-04  3.60e+00     0s
   8   5.44622629e+02 -5.55752181e+02  1.92e-03 6.79e-05  6.80e-01     0s
   9   5.52045072e+01 -2.14523216e+02  4.00e-04 1.42e-05  1.66e-01     0s
  10   1.10554192e+01 -4.47823436e+01  1.59e-05 5.62e-07  3.42e-02     0s
  11   1.51041071e+00 -1.18890369e+01  1.59e-11 4.47e-12  8.22e-03     0s
  12  -1.08480431e+00 -4.01317984e+00  2.12e-12 5.28e-13  1.80e-03     0s
  13  -1.45897649e+00 -2.93519651e+00  8.81e-13 1.49e-13  9.05e-04     0s
  14  -1.76654941e+00 -2.25628749e+00  1.11e-14 4.28e-15  3.00e-04     0s
  15  -1.85031146e+00 -1.90760537e+00  3.77e-15 2.87e-14  3.51e-05     0s
  16  -1.86177425e+00 -1.87515813e+00  3.79e-14 1.05e-13  8.21e-06     0s
  17  -1.86490849e+00 -1.86537066e+00  9.09e-14 4.35e-15  2.83e-07     0s
  18  -1.86502277e+00 -1.86502331e+00  2.86e-13 1.31e-14  3.29e-10     0s
  19  -1.86502290e+00 -1.86502290e+00  6.58e-13 7.11e-15  3.29e-13     0s

Barrier solved model in 19 iterations and 0.33 seconds
Optimal objective -1.86502290e+00

############################################
is_dcp:                True
is_qp:                 True
status:                optimal
objective_value:       1.8650229007993528
num_scalar_variables:  25788
num_scalar_data:       9929000
num_scalar_eq_constr:  17220
num_scalar_leq_constr  51660
max_data_dimension     205
solve_time:            0.3559989929199219
setup_time:            None
num_iters:             19
############################################
The solution is:  1.8650229007993528
AxesSubplot(0.125,0.125;0.775x0.755)

    
    **ECOS Solver log:**
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS

It     pcost       dcost      gap   pres   dres    k/t    mu     step   sigma     IR    |   BT
 0  +1.092e+09  -6.958e+10  +4e+12  8e-01  2e+00  1e+00  2e+07    ---    ---    1  1  - |  -  - 
 1  +1.410e+09  -6.488e+10  +4e+12  7e-01  2e+00  3e+06  2e+07  0.2645  7e-01   1  1  1 |  0  0
 2  +8.501e+08  -4.165e+10  +3e+12  4e-01  1e+00  7e+06  1e+07  0.4430  4e-01   1  1  1 |  0  0
 3  +7.276e+08  -3.919e+10  +2e+12  4e-01  1e+00  1e+07  1e+07  0.2989  7e-01   1  1  1 |  0  0
 4  +3.586e+08  -1.424e+10  +1e+12  1e-01  5e-01  7e+06  5e+06  0.5661  8e-02   1  1  1 |  0  0
 5  +1.687e+08  -6.445e+09  +5e+11  5e-02  2e-01  3e+06  2e+06  0.6304  1e-01   1  1  1 |  0  0
 6  -7.805e+06  -2.949e+09  +3e+11  2e-02  1e-01  9e+05  1e+06  0.5757  2e-01   1  1  1 |  0  0
 7  +2.228e+06  -4.316e+08  +5e+10  3e-03  1e-02  8e+04  2e+05  0.8413  4e-02   1  1  1 |  0  0
 8  +9.190e+06  -5.985e+07  +1e+10  5e-04  2e-03  8e+03  5e+04  0.8146  1e-02   1  1  1 |  0  0
 9  +7.369e+06  -3.535e+07  +8e+09  3e-04  2e-03  5e+03  3e+04  0.3837  4e-01   1  1  1 |  0  0
10  +5.982e+06  -2.426e+07  +6e+09  2e-04  1e-03  3e+03  3e+04  0.2838  4e-01   1  1  1 |  0  0
11  +4.323e+06  -1.388e+07  +5e+09  1e-04  7e-04  2e+03  2e+04  0.4220  4e-01   1  1  1 |  0  0
12  +2.879e+06  -6.529e+06  +3e+09  5e-05  4e-04  7e+02  1e+04  0.3985  2e-01   1  1  1 |  0  0
13  +1.730e+06  -3.407e+06  +2e+09  3e-05  2e-04  3e+02  1e+04  0.4899  4e-01   1  1  1 |  0  0
14  +9.686e+05  -1.651e+06  +1e+09  1e-05  9e-05  1e+02  7e+03  0.5477  3e-01   1  1  1 |  0  0
15  +3.289e+05  -4.201e+05  +5e+08  3e-06  2e-05  4e+01  2e+03  0.6879  9e-02   1  1  1 |  0  0
16  +1.385e+05  -1.646e+05  +3e+08  1e-06  9e-06  1e+01  1e+03  0.6088  1e-01   1  1  1 |  0  0
17  +5.012e+04  -6.306e+04  +1e+08  4e-07  3e-06  4e+00  5e+02  0.6767  1e-01   1  1  1 |  0  0
18  +5.720e+03  -1.644e+04  +2e+07  7e-08  6e-07  6e-01  1e+02  0.8278  5e-02   1  1  1 |  0  0
19  +4.088e+03  -7.287e+03  +1e+07  3e-08  3e-07  3e-01  5e+01  0.6584  3e-01   1  1  1 |  0  0
20  +6.631e+02  -1.644e+03  +3e+06  7e-09  6e-08  5e-02  1e+01  0.8262  4e-02   1  1  1 |  0  0
21  +3.407e+02  -8.118e+02  +1e+06  3e-09  3e-08  2e-02  6e+00  0.6915  3e-01   1  1  1 |  0  0
22  +7.454e+01  -2.141e+02  +3e+05  8e-10  7e-09  5e-03  1e+00  0.7854  5e-02   1  1  1 |  0  0
23  +3.983e+01  -1.278e+02  +2e+05  5e-10  4e-09  3e-03  8e-01  0.6239  3e-01   1  1  1 |  0  0
24  +1.895e+01  -6.678e+01  +1e+05  2e-10  2e-09  1e-03  4e-01  0.5217  6e-02   1  1  1 |  0  0
25  +1.821e+01  -6.528e+01  +9e+04  2e-10  2e-09  1e-03  4e-01  0.0894  7e-01   1  1  1 |  0  0
26  +4.428e+00  -2.336e+01  +3e+04  8e-11  7e-10  4e-04  1e-01  0.7785  1e-01   1  1  1 |  0  0
27  +2.873e+00  -1.913e+01  +2e+04  6e-11  5e-10  3e-04  1e-01  0.4966  6e-01   1  1  1 |  0  0
28  -5.236e-01  -6.848e+00  +7e+03  2e-11  2e-10  8e-05  3e-02  0.7740  8e-02   1  1  1 |  0  0
29  -1.221e+00  -4.313e+00  +3e+03  9e-12  7e-11  4e-05  2e-02  0.6117  2e-01   1  1  1 |  0  0
30  -1.535e+00  -3.250e+00  +2e+03  5e-12  4e-11  2e-05  9e-03  0.9890  5e-01   1  1  1 |  0  0
31  -1.798e+00  -2.147e+00  +4e+02  1e-12  8e-12  4e-06  2e-03  0.9553  2e-01   1  1  1 |  0  0
32  -1.847e+00  -1.939e+00  +1e+02  3e-13  2e-12  1e-06  5e-04  0.8829  2e-01   1  1  1 |  0  0
33  -1.856e+00  -1.905e+00  +5e+01  1e-13  1e-12  6e-07  2e-04  0.7592  4e-01   1  1  1 |  0  0
34  -1.865e+00  -1.866e+00  +1e+00  4e-15  6e-13  2e-08  7e-06  0.9804  8e-03   1  1  1 |  0  0
35  -1.865e+00  -1.866e+00  +1e+00  3e-15  6e-13  1e-08  5e-06  0.4717  3e-01   1  1  1 |  0  0
36  -1.865e+00  -1.866e+00  +3e+00  3e-15  5e-12  1e-08  1e-05  0.9890  1e+00   1  1  1 |  0  0
37  -1.865e+00  -1.865e+00  +1e-01  7e-16  2e-13  4e-10  6e-07  0.9631  4e-04   1  1  1 |  0  0
38  -1.865e+00  -1.865e+00  +5e-03  2e-15  6e-13  1e-11  2e-08  0.9762  8e-04   1  1  1 |  0  0
39  -1.865e+00  -1.865e+00  +2e-03  1e-15  6e-13  2e-11  1e-08  0.9499  5e-01   1  1  1 |  0  0
40  -1.865e+00  -1.865e+00  +2e-04  5e-16  6e-13  2e-12  9e-10  0.9158  3e-03   2  1  1 |  0  0
41  -1.865e+00  -1.865e+00  +6e-05  1e-14  6e-13  3e-13  3e-10  0.9890  1e-01   2  1  1 |  0  0
42  -1.865e+00  -1.865e+00  +2e-04  3e-12  6e-13  3e-13  7e-10  0.9334  5e-01   2  1  1 |  0  0
43  -1.865e+00  -1.865e+00  +5e-04  3e-13  6e-13  1e-12  2e-09  0.9890  8e-01   2  1  1 |  0  0
44  -1.865e+00  -1.865e+00  +1e-05  4e-14  6e-13  6e-14  5e-11  0.9890  1e-02   2  1  1 |  0  0
45  -1.865e+00  -1.865e+00  +7e-04  4e-12  6e-13  1e-13  3e-09  0.9890  9e-01   3  1  1 |  0  0
46  -1.865e+00  -1.865e+00  +7e-06  7e-12  6e-13  2e-15  3e-11  0.9890  1e-04   3  1  1 |  0  0
47  -1.865e+00  -1.865e+00  +9e-04  2e-08  6e-13  8e-14  4e-09  0.9890  1e+00   6  3  2 |  0  0
Unreliable search direction detected, recovering best iterate (37) and stopping.

NUMERICAL PROBLEMS (reached feastol=2.2e-13, reltol=7.5e-02, abstol=1.4e-01).
Runtime: 21.312740 seconds.

**SCS Solver log**
----------------------------------------------------------------------------
    SCS v2.0.2 - Splitting Conic Solver
    (c) Brendan O'Donoghue, Stanford University, 2012-2017
----------------------------------------------------------------------------
Lin-sys: sparse-indirect, nnz in A = 755622, CG tol ~ 1/iter^(2.00)
eps = 1.00e-04, alpha = 1.50, max_iters = 5000, normalize = 1, scale = 1.00
acceleration_lookback = -1, rho_x = 1.00e-03
Variables n = 77364, constraints m = 246492
Cones:  primal zero / dual free vars: 17220
    linear vars: 223020
    soc vars: 6252, soc blks: 168
Setup time: 6.81e-02s
----------------------------------------------------------------------------
 Iter | pri res | dua res | rel gap | pri obj | dua obj | kap/tau | time (s)
----------------------------------------------------------------------------
     0| 2.58e+00  1.50e+00  1.00e+00 -1.81e+10 -1.81e+06  0.00e+00  4.87e-01 
   100| 6.76e-04  1.51e-03  1.00e+00  8.50e+04 -1.40e+06  3.45e-06  9.99e+00 
   200| 5.05e-04  1.07e-04  7.84e-01 -1.51e+05 -1.24e+06  6.62e-07  1.60e+01 
   300| 5.05e-04  9.74e-05  7.82e-01 -1.37e+05 -1.12e+06  6.59e-07  2.22e+01 
   400| 5.05e-04  8.81e-05  7.80e-01 -1.24e+05 -1.01e+06  6.57e-07  2.88e+01 
   500| 5.04e-04  7.96e-05  7.77e-01 -1.12e+05 -8.94e+05  6.55e-07  3.53e+01 
   600| 5.04e-04  7.43e-05  7.74e-01 -1.05e+05 -8.23e+05  3.39e-06  4.08e+01 
   700| 5.04e-04  6.98e-05  7.70e-01 -9.92e+04 -7.63e+05  1.37e-06  4.63e+01 
   800| 5.04e-04  6.55e-05  7.66e-01 -9.35e+04 -7.04e+05  6.50e-07  5.18e+01 
   900| 5.04e-04  6.13e-05  7.60e-01 -8.79e+04 -6.45e+05  1.36e-06  5.77e+01 
  1000| 5.04e-04  5.73e-05  7.54e-01 -8.22e+04 -5.86e+05  1.36e-06  6.41e+01 
  1100| 5.04e-04  5.33e-05  7.47e-01 -7.66e+04 -5.28e+05  6.46e-07  6.97e+01 
  1200| 5.03e-04  4.97e-05  7.38e-01 -7.12e+04 -4.71e+05  1.35e-06  7.54e+01 
  1300| 5.03e-04  4.61e-05  7.27e-01 -6.59e+04 -4.16e+05  6.44e-07  8.12e+01 
  1400| 5.02e-04  4.29e-05  7.13e-01 -6.10e+04 -3.64e+05  2.64e-06  8.77e+01 
  1500| 5.01e-04  3.99e-05  6.96e-01 -5.64e+04 -3.15e+05  1.35e-06  9.48e+01 
  1600| 4.91e-04  3.75e-05  6.82e-01 -5.05e+04 -2.67e+05  6.42e-07  1.03e+02 
  1700| 4.85e-04  3.21e-05  6.53e-01 -4.62e+04 -2.20e+05  6.41e-07  1.13e+02 
  1800| 4.56e-04  3.05e-05  6.25e-01 -4.01e+04 -1.74e+05  1.35e-06  1.23e+02 
  1900| 3.47e-04  2.90e-05  6.75e-01 -2.74e+04 -1.41e+05  2.63e-06  1.35e+02 
  2000| 3.24e-04  2.68e-05  6.72e-01 -2.32e+04 -1.18e+05  1.34e-06  1.46e+02 
  2100| 2.94e-04  3.04e-05  6.27e-01 -2.20e+04 -9.63e+04  6.40e-07  1.57e+02 
  2200| 2.26e-04  3.56e-05  7.00e-01 -1.41e+04 -7.97e+04  2.62e-06  1.71e+02 
  2300| 1.61e-04  4.84e-05  6.37e-01 -1.50e+04 -6.78e+04  6.40e-07  1.85e+02 
  2400| 1.50e-04  4.68e-05  9.55e-01 -1.36e+03 -5.94e+04  6.40e-07  1.98e+02 
  2500| 1.27e-04  1.56e-05  7.93e-01 -6.12e+03 -5.30e+04  6.40e-07  2.11e+02 
  2600| 1.05e-04  1.65e-05  7.92e-01 -5.50e+03 -4.74e+04  6.39e-07  2.23e+02 
  2700| 8.02e-05  1.61e-05  7.92e-01 -4.96e+03 -4.28e+04  2.62e-06  2.36e+02 
  2800| 7.19e-05  1.18e-05  8.26e-01 -3.75e+03 -3.94e+04  6.39e-07  2.49e+02 
  2900| 5.43e-05  1.60e-05  8.10e-01 -3.82e+03 -3.64e+04  6.39e-07  2.62e+02 
  3000| 5.18e-05  1.27e-05  8.27e-01 -3.20e+03 -3.38e+04  6.39e-07  2.74e+02 
  3100| 4.90e-05  9.01e-06  8.88e-01 -1.84e+03 -3.12e+04  2.62e-06  2.86e+02 
  3200| 4.67e-05  1.33e-05  1.00e+00  2.29e+02 -2.88e+04  6.39e-07  2.99e+02 
  3300| 2.45e-05  1.03e-05  9.68e-01 -4.46e+02 -2.71e+04  1.34e-06  3.12e+02 
  3400| 2.39e-05  7.46e-06  9.67e-01 -4.26e+02 -2.56e+04  6.39e-07  3.22e+02 
  3500| 2.35e-05  7.34e-06  9.70e-01 -3.66e+02 -2.41e+04  6.39e-07  3.31e+02 
  3600| 2.28e-05  7.35e-06  9.70e-01 -3.40e+02 -2.27e+04  3.32e-06  3.41e+02 
  3700| 2.17e-05  7.55e-06  9.69e-01 -3.38e+02 -2.14e+04  6.39e-07  3.50e+02 
  3800| 2.10e-05  7.20e-06  9.69e-01 -3.14e+02 -2.01e+04  6.39e-07  3.60e+02 
  3900| 2.07e-05  6.73e-06  9.72e-01 -2.64e+02 -1.89e+04  1.34e-06  3.69e+02 
  4000| 2.04e-05  6.68e-06  9.76e-01 -2.14e+02 -1.77e+04  6.39e-07  3.79e+02 
  4100| 2.01e-05  6.64e-06  9.82e-01 -1.50e+02 -1.66e+04  6.39e-07  3.88e+02 
  4200| 2.00e-05  6.84e-06  9.88e-01 -9.38e+01 -1.55e+04  6.38e-07  3.98e+02 
  4300| 1.97e-05  6.52e-06  9.94e-01 -4.40e+01 -1.44e+04  6.38e-07  4.08e+02 
  4400| 1.96e-05  6.40e-06  1.00e+00  1.60e+01 -1.34e+04  1.34e-06  4.17e+02 
  4500| 1.95e-05  6.33e-06  1.00e+00  8.05e+01 -1.24e+04  1.34e-06  4.26e+02 
  4600| 1.90e-05  6.55e-06  1.00e+00  1.37e+02 -1.15e+04  6.38e-07  4.37e+02 
  4700| 1.86e-05  6.44e-06  1.00e+00  2.06e+02 -1.06e+04  6.38e-07  4.47e+02 
  4800| 1.73e-05  6.24e-06  1.00e+00  2.34e+02 -9.78e+03  3.32e-06  4.58e+02 
  4900| 1.69e-05  5.58e-06  1.00e+00  2.82e+02 -9.04e+03  1.34e-06  4.68e+02 
  5000| 1.63e-05  6.44e-06  1.00e+00  3.04e+02 -8.39e+03  6.38e-07  4.78e+02 
----------------------------------------------------------------------------
Status: Solved/Inaccurate
Hit max_iters, solution may be inaccurate
Timing: Solve time: 4.78e+02s
    Lin-sys: avg # CG iterations: 9.44, avg solve time: 8.25e-02s
    Cones: avg projection time: 5.84e-04s
    Acceleration: avg step time: 0.00e+00s
----------------------------------------------------------------------------
Error metrics:
dist(s, K) = 2.0111e-11, dist(y, K*) = 0.0000e+00, s'y/|s||y| = -3.8323e-18
primal res: |Ax + s - b|_2 / (1 + |b|_2) = 1.6308e-05
dual res:   |A'y + c|_2 / (1 + |c|_2) = 6.4380e-06
rel gap:    |c'x + b'y| / (1 + |c'x| + |b'y|) = 9.9988e-01
----------------------------------------------------------------------------
c'x = 304.2367, -b'y = -8388.8258
============================================================================
############################################
is_dcp:                True
is_qp:                 True
status:                optimal_inaccurate
objective_value:       -304.2366584084115
num_scalar_variables:  25788
num_scalar_data:       9929000
num_scalar_eq_constr:  17220
num_scalar_leq_constr  51660
max_data_dimension     205
solve_time:            478413.86694549426
setup_time:            68.10835877191816
num_iters:             5000
############################################
The solution is:  -304.2366584084115
AxesSubplot(0.125,0.125;0.775x0.755)

**OSQP Solver log:**
-----------------------------------------------------------------
           OSQP v0.5.0  -  Operator Splitting QP Solver
              (c) Bartolomeo Stellato,  Goran Banjac
        University of Oxford  -  Stanford University 2018
-----------------------------------------------------------------
problem:  variables n = 77196, constraints m = 240240
          nnz(P) + nnz(A) = 545184
settings: linear system solver = qdldl,
          eps_abs = 1.0e-05, eps_rel = 1.0e-05,
          eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
          rho = 1.00e-01 (adaptive),
          sigma = 1.00e-06, alpha = 1.60, max_iter = 10000
          check_termination: on (interval 25),
          scaling: on, scaled_termination: off
          warm start: on, polish: on

objective    pri res    dua res    rho        time
   1  -5.7757e+06   7.16e+00   2.76e+03   1.00e-01   6.68e-01s
 200  -2.6075e+01   9.47e-04   1.39e+01   1.00e-01   5.78e+00s
 400   9.8155e-01   3.12e-04   3.45e+00   1.00e-01   1.10e+01s
 600   1.3592e-01   5.90e-05   1.63e+00   1.00e-01   1.61e+01s
 800   1.6026e-01   3.36e-05   3.55e-01   1.00e-01   2.13e+01s
1000   1.1418e-01   9.08e-06   2.29e-01   1.00e-01   2.65e+01s
1100   9.9919e-02   8.85e-06   6.92e-02   1.00e-01   2.91e+01s

status:               solved
solution polish:      unsuccessful
number of iterations: 1100
optimal objective:    0.0999
run time:             2.93e+01s
optimal rho estimate: 8.18e-02

############################################
is_dcp:                True
is_qp:                 True
status:                optimal
objective_value:       -0.09991904812176824
num_scalar_variables:  25788
num_scalar_data:       9929000
num_scalar_eq_constr:  17220
num_scalar_leq_constr  51660
max_data_dimension     205
solve_time:            29.289220095079017
setup_time:            None
num_iters:             1100
############################################
The solution is:  -0.099919048121768

A snapshot of code below: ''' optimization = Demo(datain = datain) #'CVXOPT', 'ECOS', 'ECOS_BB', 'GLPK', #'GLPK_MI', 'GUROBI', 'OSQP', 'SCS'

 solution = optimization.solve(solver = 
 'Gurobi')



class Demo:
        def __init__(self, datain):
            # validate_inputs(datain)
            self.inputs = datain
    
  
            self.trading_vecs = 
    [cp.Variable(self.I) for j in 
  range(self.J)]
    #some code
            self.objective = 
    self._define_objective()
            self.constraints = 
    self._define_constraints()
    
            self.problem = 
 











 cp.Problem(objective=
 cp.Maximize(self.objective) 
 ,                                    
 constraints=self.constraints)
    
            # outputs populated by calling 
#solve()
            self.solution = None
            self.size_metrics = None
            self.solver_stats = None
            self.h = None # holding vectors
            self.l = None # CaR vectors
            self.v = None # unsecured 
funding vector
    
        def solve(self, solver="CPLEX", 
verbose=True, **kwargs):
            """Solve the CVXPY problem and 
update the instance variables:
                 solution     : optimal 
objective value
                 size_metrics : problem size 
metrics
                 solver_stats : solver 
statistics
                 h            : holding 
vectors, a J-long list of I-long numpy 
arrays
                 l            : CaR vectors, 
a J-long list of I-long numpy arrays
                 v            : unsecured 
funding vector,a J-long numpy array
    
            Parameters
            ---------
            solver  : string, defaults to 
'CPLEX'; if CPLEX is not available, 'ECOS'
                      has also been tested 
successfully, but it is slower
            verbose : boolean, defaults to 
 True
            kwargs  : CVXPY solver-specific 
 parameters; 

    
            Returns
            -------
            float value of the optimal 
 objective
            """
    
            self.solution = 
 self.problem.solve(solver=solver,
                                               
 verbose=verbose,
                                               
 **kwargs)
            self.size_metrics = 
  self.problem.size_metrics
            self.solver_stats = 
self.problem.solver_stats
    
            if verbose:
                self._print_problem_stats()
    
            self.h = 
[np.array(self.holding_vecs[j].value) \
                      for j in 
range(self.J)]
            self.l = 
[np.array(self.car_holding_vecs[j].value) \
                      for j in 
range(self.J)]
            self.v = 
[np.array(self.unsec_funding_vec[j].value) \
                      for j in 
range(self.J)]
    
            return self.solution
    
        def _define_constraints(self):
        """Define the CVXPY problem 
  constraints"""

        prev_holding_vecs = 
 [self.inputs['backbook_holding_vec']] + \
                            
 [self.holding_vecs[j] for j in 
range(self.J-1)]

        x_equations = [self.holding_vecs[j] 
 == self.inputs['trade_rolling_mats'][j] @ \
                              
                              
  self.trading_vecs[j] == 0 for j in 
range(self.J)]

#some other code

        constraints = x_equations + \
                      marketable_trading + \
                      no_short_position + \
                      no_neg_borrowing + \
                      
                 no_discretionary_unwind + \
                      within_cash_budget + \
                      car_lower_bounds + \
                      car_upper_bounds + \
                      car_funding + \
                      size_upper_limits

        return constraints

    def _define_objective(self):
        """Define the CVXPY objective"""
        post_transaction_cost_return = 
 list(map(lambda r, h, phi, u: 
 _dot_product(r, h) - phi(u),
                                                
  self.inputs['expected_return_vecs'],\
                                                
  self.holding_vecs,\
                                                                                                        
  self.trading_vecs))

    
#some code#                                         

objectives.append(self.inputs['discount_vec'] [j] * \ \

  self.inputs['lambda_3'] * 
 car_unwind_transaction_cost[j] - \
                              
 external_funding_cost[j]))

        return cp.sum(objectives)

'''

0

There are 0 answers