% Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 08/17/05
%
% The penalty function approximation problem has the form:
%               minimize    sum(deadzone(Ax - b))
% where 'deadzone' is the deadzone penalty function
%               deadzone(y) = max(abs(y)-1,0)

% Input data
randn('state',0);
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);

% deadzone penalty
% original formulation
fprintf(1,'Computing the optimal solution of the deadzone approximation problem: \n');

cvx_begin
    variable x(n)
    minimize( sum(max(abs(A*x-b)-1,0)) )
cvx_end

fprintf(1,'Done! \n');

% Compare
disp( sprintf( '\nResults:\n--------\nsum(max(abs(A*x-b)-1,0)): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', sum(max(abs(A*x-b)-1,0)), cvx_optval, cvx_status ) );
disp( 'Optimal vector:' );
disp( [ '   x     = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( 'Residual vector:' );
disp( [ '   A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
Computing the optimal solution of the deadzone approximation problem: 
 
Calling Mosek 9.1.9: 72 variables, 32 equality constraints
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 32              
  Cones                  : 16              
  Scalar variables       : 72              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 32              
  Cones                  : 16              
  Scalar variables       : 72              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the dual        
Optimizer  - Constraints            : 8
Optimizer  - Cones                  : 16
Optimizer  - Scalar variables       : 32                conic                  : 32              
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 36                after factor           : 36              
Factor     - dense dim.             : 0                 flops                  : 3.66e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.5e+00  1.0e+00  1.7e+01  0.00e+00   0.000000000e+00   -1.600000000e+01  1.0e+00  0.00  
1   3.8e-01  2.5e-01  2.0e+00  1.61e+00   -9.609040001e-01  -4.039702925e+00  2.5e-01  0.01  
2   1.3e-01  8.9e-02  3.9e-01  1.18e+00   -5.137880081e-01  -1.521386379e+00  8.9e-02  0.01  
3   5.0e-03  3.4e-03  2.2e-03  1.10e+00   -6.193710529e-03  -4.355232598e-02  3.4e-03  0.01  
4   1.1e-05  7.1e-06  2.1e-07  1.00e+00   -1.380757740e-05  -9.214039101e-05  7.1e-06  0.01  
5   2.1e-10  1.4e-10  4.2e-12  1.00e+00   -8.101791968e-10  -2.345960260e-09  1.4e-10  0.01  
Optimizer terminated. Time: 0.01    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -8.1017919679e-10   nrm: 1e+00    Viol.  con: 4e-16    var: 9e-11    cones: 0e+00  
  Dual.    obj: -2.3459602595e-09   nrm: 1e+00    Viol.  con: 0e+00    var: 4e-20    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.01    
    Interior-point          - iterations : 5         time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -8.10179e-10
 
Done! 

Results:
--------
sum(max(abs(A*x-b)-1,0)): 0.0000
cvx_optval: -0.0000
cvx_status: Solved

Optimal vector:
   x     = [  0.2900  0.1323 -0.3552  0.0633  0.6609  0.4022 -0.6744  0.7018 ]
Residual vector:
   A*x-b = [  0.6175  0.4820 -0.7431 -0.3238  0.4264  0.4448 -0.6833 -0.7085 -0.4855  0.7475  0.1256 -0.1987  0.5326  0.7285  0.4074 -0.4037 ]