% 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 Mosek 9.1.9: 49 variables, 11 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            : 11              
  Cones                  : 4               
  Scalar variables       : 49              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 1
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 2                 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            : 11              
  Cones                  : 4               
  Scalar variables       : 49              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 4
Optimizer  - Cones                  : 4
Optimizer  - Scalar variables       : 16                conic                  : 12              
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 : 10                after factor           : 10              
Factor     - dense dim.             : 0                 flops                  : 1.48e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   4.2e+00  1.3e+00  5.0e+00  0.00e+00   1.346912185e-01   -3.913555187e+00  1.0e+00  0.00  
1   5.2e-01  1.6e-01  2.9e-01  4.33e-01   4.973840520e-01   -9.468557263e-02  1.2e-01  0.01  
2   9.0e-02  2.8e-02  1.6e-02  1.30e+00   -8.974357115e-02  -1.849264324e-01  2.2e-02  0.01  
3   1.1e-02  3.5e-03  6.9e-04  1.14e+00   -2.052065597e-01  -2.164218567e-01  2.7e-03  0.01  
4   1.5e-03  4.6e-04  3.3e-05  1.02e+00   -2.164139095e-01  -2.178955683e-01  3.6e-04  0.01  
5   5.3e-05  1.6e-05  2.2e-07  1.00e+00   -2.179991570e-01  -2.180516153e-01  1.3e-05  0.01  
6   8.2e-07  2.5e-07  4.2e-10  1.00e+00   -2.180711074e-01  -2.180719161e-01  2.0e-07  0.01  
7   1.0e-07  3.2e-08  1.9e-11  1.00e+00   -2.180720730e-01  -2.180721757e-01  2.5e-08  0.01  
8   4.9e-09  1.6e-09  2.2e-13  1.00e+00   -2.180721939e-01  -2.180721991e-01  1.3e-09  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -2.1807219386e-01   nrm: 4e+00    Viol.  con: 5e-09    var: 2e-09    cones: 3e-11  
  Dual.    obj: -2.1807219907e-01   nrm: 1e+00    Viol.  con: 0e+00    var: 1e-09    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 8         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): +0.804067
 
 
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.0000i
  -0.2841 + 0.0000i
  -0.0100 + 0.2263i
  -0.0100 - 0.2263i