# Section 4.5.4: Minimum spectral radius via Peron-Frobenius theory (GP)

    % Boyd & Vandenberghe "Convex Optimization"
    % Joelle Skaf - 01/29/06
    % Updated to use CVX mode by Almir Mutapcic 02/08/06
    %
    % The goal is to minimize the spectral radius of a square matrix A
    % which is elementwise nonnegative, Aij >= 0 for all i,j. In this
    % case A has a positive real eigenvalue lambda_pf (the Perron-Frobenius
    % eigenvalue) which is equal to the spectral radius, and thus gives
    % the fastest decay rate or slowest growth rate.
    % The problem of minimizing the Perron-Frobenius eigenvalue of A,
    % possibly subject to posynomial inequalities in some underlying
    % variable x can be posed as a GP (for example):
    %
    %   minimize   lambda_pf( A(x) )
    %       s.t.   f_i(x) <= 1   for i = 1,...,p
    %
    % where matrix A entries are some posynomial functions of variable x,
    % and f_i are posynomials.
    %
    % We consider a specific example in which we want to find the fastest
    % decay or slowest growth rate for the bacteria population governed
    % by a simple dynamic model (see page 166). The problem is a GP:
    %   minimize   lambda
    %       s.t.   b1*v1 + b2*v2 + b3*v3 + b4*v4 <= lambda*v1
    %              s1*v1 <= lambda*v2
    %              s2*v2 <= lambda*v3
    %              s3*v3 <= lambda*v4
    %              1/2 <= ci <= 2
    %              bi == bi^{nom}*(c1/c1^{nom})^alpha_i*(c2/c2^{nom})^beta_i
    %              si == si^{nom}*(c1/c1^{nom})^gamma_i*(c2/c2^{nom})^delta_i
    %
    % with variables bi, si, ci, vi, lambda.

    % constants
    c_nom = [1 1]';
    b_nom = [2 3 2 1]';
    alpha = [1 1 1 1]'; beta  = [1 1 1 1]';
    s_nom = [1 1 3]';
    gamma = [1 1 1]'; delta = [1 1 1]';

    cvx_begin gp
      % optimization variables
      variables lambda b(4) s(3) v(4) c(2)

      % objective is the Perron-Frobenius eigenvalue
      minimize( lambda )
      subject to
        % inequality constraints
        b'*v      <= lambda*v(1);
        s(1)*v(1) <= lambda*v(2);
        s(2)*v(2) <= lambda*v(3);
        s(3)*v(3) <= lambda*v(4);
        [0.5; 0.5] <= c; c <= [2; 2];
        % equality constraints
        b == b_nom.*((ones(4,1)*(c(1)/c_nom(1))).^alpha).*...
                    ((ones(4,1)*(c(2)/c_nom(2))).^beta);
        s == s_nom.*((ones(3,1)*(c(1)/c_nom(1))).^gamma).*...
                    ((ones(3,1)*(c(2)/c_nom(2))).^delta);
    cvx_end

    % displaying results
    disp(' ')
    if lambda < 1
      fprintf(1,'The fastest decay rate of the bacteria population is %3.2f.\n', lambda);
    else
      fprintf(1,'The slowest growth rate of the bacteria population is %3.2f.\n', lambda);
    end
    disp(' ')
    fprintf(1,'The concentration of chemical 1 achieving this result is %3.2f.\n', c(1));
    fprintf(1,'The concentration of chemical 2 achieving this result is %3.2f.\n', c(2));
    disp(' ')

    % construct matrix A
    A = zeros(4,4);
    A(1,:) = b';
    A(2,1) = s(1);
    A(3,2) = s(2);
    A(4,3) = s(3);

    % eigenvalues of matrix A
    disp('Eigenvalues of matrix A are: ')
    eigA = eig(A)

    Calling SeDuMi: 87 variables (5 free), 48 equality constraints
    ------------------------------------------------------------------------
    SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
    Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
    Split 5 free variables
    eqs m = 48, order n = 48, dim = 138, blocks = 4
    nnz(A) = 184 + 0, nnz(ADA) = 586, nnz(L) = 325
     it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
      0 :            1.30E+000 0.000
      1 :  2.95E-001 3.78E-001 0.000 0.2904 0.9000 0.9000   2.33  1  1  2.6E+000
      2 :  1.93E-001 1.15E-001 0.000 0.3052 0.9000 0.9000   2.14  1  1  6.0E-001
      3 : -1.44E-001 3.62E-002 0.000 0.3135 0.9000 0.9000   1.24  1  1  2.3E-001
      4 : -1.84E-001 1.22E-002 0.000 0.3359 0.9000 0.9000   1.10  1  1  6.8E-002
      5 : -2.04E-001 4.19E-003 0.000 0.3444 0.9000 0.9000   1.09  1  1  2.2E-002
      6 : -2.13E-001 1.22E-003 0.000 0.2920 0.9000 0.9000   1.09  1  1  6.1E-003
      7 : -2.18E-001 8.24E-005 0.000 0.0673 0.9900 0.9900   1.10  1  1  3.9E-004
      8 : -2.18E-001 6.40E-006 0.000 0.0777 0.9900 0.9900   1.06  1  1  2.9E-005
      9 : -2.18E-001 1.16E-006 0.074 0.1818 0.9000 0.9000   1.01  1  1  5.3E-006
     10 : -2.18E-001 1.06E-007 0.490 0.0915 0.9000 0.0000   1.00  1  2  1.3E-006
     11 : -2.18E-001 4.35E-009 0.000 0.0409 0.9903 0.9900   1.00  2  2  6.0E-008
     12 : -2.18E-001 8.75E-011 0.037 0.0201 0.9900 0.9902   1.00  3  4  1.2E-009

    iter seconds digits       c*x               b*y
     12      0.1   Inf -2.1798542256e-001 -2.1798542227e-001
    |Ax-b| =  2.8e-009, [Ay-c]_+ =  3.2E-010, |x|= 9.4e+000, |y|= 2.6e+000

    Detailed timing (sec)
       Pre          IPM          Post
    0.000E+000    1.001E-001    0.000E+000
    Max-norms: ||b||=4.380570e+000, ||c|| = 1,
    Cholesky |add|=0, |skip| = 0, ||L.L|| = 72.379.
    ------------------------------------------------------------------------
    Status: Solved
    Optimal value (cvx_optval): +0.804137

    The fastest decay rate of the bacteria population is 0.80.

    The concentration of chemical 1 achieving this result is 0.50.
    The concentration of chemical 2 achieving this result is 0.50.

    Eigenvalues of matrix A are:

    eigA =

       0.8041
      -0.2841
      -0.0100 + 0.2263i
      -0.0100 - 0.2263i