% Section 11.4.1, Boyd & Vandenberghe "Convex Optimization"
% Written for CVX by Almir Mutapcic - 02/18/06
%
% We consider a set of linear inequalities A*x <= b which are
% infeasible. Here A is a matrix in R^(m-by-n) and b belongs
% to R^m. We apply a heuristic to find a point x that violates
% only a small number of inequalities.
%
% We use the sum of infeasibilities heuristic:
%
%   minimize   sum( max( Ax - b ) )
%
% which is equivalent to the following LP (book pg. 580):
%
%   minimize   sum( s )
%       s.t.   Ax <= b + s
%              s >= 0
%
% with variables x in R^n and s in R^m.

% problem dimensions (m inequalities in n-dimensional space)
m = 150;
n = 10;

% fix random number generator so we can repeat the experiment
seed = 0;
randn('state',seed);

% construct infeasible inequalities
A = randn(m,n);
b = randn(m,1);

fprintf(1, ['Starting with an infeasible set of %d inequalities ' ...
            'in %d variables.\n'],m,n);

% sum of infeasibilities heuristic
cvx_begin
   variable x(n)
   minimize( sum( max( A*x - b, 0 ) ) )
cvx_end

% full LP version of the sum of infeasibilities heuristic
% cvx_begin
%   variables x(n) s(m)
%   minimize( sum( s ) )
%   subject to
%     A*x <= b + s;
%     s >= 0;
% cvx_end

% number of satisfied inequalities
nv = length( find( A*x > b ) );
fprintf(1,'\nFound an x that violates %d out of %d inequalities.\n',nv,m);
Starting with an infeasible set of 150 inequalities in 10 variables.
 
Calling SDPT3 4.0: 310 variables, 150 equality constraints
------------------------------------------------------------

 num. of constraints = 150
 dim. of linear var  = 300
 dim. of free   var  = 10 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|9.3e-01|2.5e+01|1.7e+05| 4.383995e+03  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.987|4.2e-06|6.2e-01|7.6e+03| 3.991454e+03 -1.223258e+01| 0:0:00| chol  1  1 
 2|1.000|0.798|2.6e-07|1.5e-01|1.5e+03| 1.211162e+03 -1.254138e+01| 0:0:00| chol  1  1 
 3|1.000|0.402|5.0e-07|9.0e-02|2.8e+02| 2.298041e+02 -1.026791e+01| 0:0:00| chol  1  1 
 4|1.000|0.633|1.5e-06|3.3e-02|1.1e+02| 1.061195e+02  6.723112e+00| 0:0:00| chol  1  1 
 5|1.000|0.384|1.4e-07|2.0e-02|5.5e+01| 6.689249e+01  1.531029e+01| 0:0:00| chol  1  1 
 6|1.000|0.506|1.1e-07|1.0e-02|2.6e+01| 4.976868e+01  2.503358e+01| 0:0:00| chol  1  1 
 7|1.000|0.447|2.7e-08|5.6e-03|1.3e+01| 4.252463e+01  3.045013e+01| 0:0:00| chol  1  1 
 8|0.993|0.561|6.1e-09|2.4e-03|5.3e+00| 3.992375e+01  3.486231e+01| 0:0:00| chol  1  1 
 9|0.870|0.368|9.1e-10|1.5e-03|3.3e+00| 3.942397e+01  3.623530e+01| 0:0:00| chol  1  1 
10|1.000|0.375|2.3e-10|9.7e-04|2.0e+00| 3.912697e+01  3.717954e+01| 0:0:00| chol  1  1 
11|1.000|0.573|6.7e-11|4.1e-04|8.5e-01| 3.896721e+01  3.815292e+01| 0:0:00| chol  1  1 
12|0.918|0.617|4.4e-12|1.6e-04|3.2e-01| 3.892931e+01  3.862021e+01| 0:0:00| chol  1  1 
13|1.000|0.270|7.9e-12|1.2e-04|2.4e-01| 3.892402e+01  3.869888e+01| 0:0:00| chol  1  1 
14|1.000|0.240|3.0e-12|8.8e-05|1.8e-01| 3.892422e+01  3.874972e+01| 0:0:00| chol  1  1 
15|1.000|0.315|1.2e-12|6.0e-05|1.3e-01| 3.892250e+01  3.880094e+01| 0:0:00| chol  1  1 
16|1.000|0.386|5.1e-13|3.7e-05|7.9e-02| 3.891986e+01  3.884472e+01| 0:0:00| chol  1  1 
17|0.878|0.582|2.7e-13|1.5e-05|3.3e-02| 3.891773e+01  3.888631e+01| 0:0:00| chol  1  1 
18|1.000|0.418|1.2e-13|9.0e-06|1.9e-02| 3.891707e+01  3.889892e+01| 0:0:00| chol  1  1 
19|1.000|0.552|1.3e-13|1.1e-05|8.5e-03| 3.891683e+01  3.890872e+01| 0:0:00| chol  1  1 
20|1.000|0.979|1.7e-14|4.5e-06|2.1e-04| 3.891677e+01  3.891659e+01| 0:0:00| chol  1  1 
21|1.000|0.989|8.8e-16|1.1e-07|5.0e-06| 3.891676e+01  3.891676e+01| 0:0:00| chol  1  1 
22|1.000|0.989|2.0e-15|2.6e-09|9.8e-08| 3.891676e+01  3.891676e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 22
 primal objective value =  3.89167630e+01
 dual   objective value =  3.89167629e+01
 gap := trace(XZ)       = 9.81e-08
 relative gap           = 1.24e-09
 actual relative gap    = 1.08e-09
 rel. primal infeas (scaled problem)   = 1.95e-15
 rel. dual     "        "       "      = 2.61e-09
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 1.4e+01, 7.4e+00, 1.2e+01
 norm(A), norm(b), norm(C) = 5.7e+01, 1.4e+01, 1.3e+01
 Total CPU time (secs)  = 0.28  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 6.8e-15  0.0e+00  1.7e-08  0.0e+00  1.1e-09  1.2e-09
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +38.9168
 

Found an x that violates 57 out of 150 inequalities.