% S. Boyd, et. al., "Convex Optimization of Graph Laplacian Eigenvalues"
% ICM'06 talk examples (www.stanford.edu/~boyd/cvx_opt_graph_lapl_eigs.html)
% Written for CVX by Almir Mutapcic 08/29/06
% (figures are generated)
%
% In this example we consider a graph described by the incidence matrix A.
% Each edge has a weight W_i, and we optimize various functions of the
% edge weights as described in the referenced paper; in particular,
%
% - the fastest distributed linear averaging (FDLA) problem (fdla.m)
% - the fastest mixing Markov chain (FMMC) problem (fmmc.m)
%
% Then we compare these solutions to the heuristics listed below:
%
% - maximum-degree heuristic (max_deg.m)
% - constant weights that yield fastest averaging (best_const.m)
% - Metropolis-Hastings heuristic (mh.m)

% small example (incidence matrix A)
A = [ 1  0  0  1  0  0  0  0  0  0  0  0  0;
     -1  1  0  0  1  1  0  0  0  0  0  0  1;
      0 -1  1  0  0  0  0  0 -1  0  0  0  0;
      0  0 -1  0  0 -1  0  0  0 -1  0  0  0;
      0  0  0 -1  0  0 -1  1  0  0  0  0  0;
      0  0  0  0  0  0  1  0  0  0  1  0  0;
      0  0  0  0  0  0  0 -1  1  0 -1  1 -1;
      0  0  0  0 -1  0  0  0  0  1  0 -1  0];

% x and y locations of the graph nodes
xy = [ 1 2   3 3 1 1 2   3 ; ...
       3 2.5 3 2 2 1 1.5 1 ]';

% Compute edge weights: some optimal, some based on heuristics
[n,m] = size(A);

[ w_fdla, rho_fdla ] = fdla(A);
[ w_fmmc, rho_fmmc ] = fmmc(A);
[ w_md,   rho_md   ] = max_deg(A);
[ w_bc,   rho_bc   ] = best_const(A);
[ w_mh,   rho_mh   ] = mh(A);

tau_fdla = 1/log(1/rho_fdla);
tau_fmmc = 1/log(1/rho_fmmc);
tau_md   = 1/log(1/rho_md);
tau_bc   = 1/log(1/rho_bc);
tau_mh   = 1/log(1/rho_mh);

fprintf(1,'\nResults:\n');
fprintf(1,'FDLA weights:\t\t rho = %5.4f \t tau = %5.4f\n',rho_fdla,tau_fdla);
fprintf(1,'FMMC weights:\t\t rho = %5.4f \t tau = %5.4f\n',rho_fmmc,tau_fmmc);
fprintf(1,'M-H weights:\t\t rho = %5.4f \t tau = %5.4f\n',rho_mh,tau_mh);
fprintf(1,'MAX_DEG weights:\t rho = %5.4f \t tau = %5.4f\n',rho_md,tau_md);
fprintf(1,'BEST_CONST weights:\t rho = %5.4f \t tau = %5.4f\n',rho_bc,tau_bc);

% Plot results
figure(1), clf
plotgraph(A,xy,w_fdla);
text(0.55,1.05,'FDLA optimal weights')

figure(2), clf
plotgraph(A,xy,w_fmmc);
text(0.55,1.05,'FMMC optimal weights')

figure(3), clf
plotgraph(A,xy,w_md);
text(0.5,1.05,'Max degree optimal weights')

figure(4), clf
plotgraph(A,xy,w_bc);
text(0.5,1.05,'Best constant optimal weights')

figure(5), clf
plotgraph(A,xy,w_mh);
text(0.46,1.05,'Metropolis-Hastings optimal weights')
 
Calling Mosek 9.1.9: 75 variables, 17 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

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            : 17              
  Cones                  : 0               
  Scalar variables       : 3               
  Matrix variables       : 2               
  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 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            : 17              
  Cones                  : 0               
  Scalar variables       : 3               
  Matrix variables       : 2               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 17
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 4                 conic                  : 4               
Optimizer  - Semi-definite variables: 2                 scalarized             : 72              
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 : 153               after factor           : 153             
Factor     - dense dim.             : 0                 flops                  : 9.95e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   3.8e+00  1.9e+00  1.0e+00  0.00e+00   0.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   7.0e-01  3.5e-01  4.3e-02  2.77e-01   -6.767298350e-01  -8.217645913e-01  1.9e-01  0.01  
2   1.9e-01  9.5e-02  6.6e-03  1.88e+00   -6.394937767e-01  -6.617142652e-01  5.1e-02  0.01  
3   3.2e-02  1.6e-02  4.5e-04  1.09e+00   -6.486970230e-01  -6.522790767e-01  8.6e-03  0.01  
4   3.7e-03  1.9e-03  1.8e-05  1.04e+00   -6.438693424e-01  -6.442680053e-01  1.0e-03  0.01  
5   1.0e-04  5.1e-05  8.3e-08  1.00e+00   -6.433509076e-01  -6.433615275e-01  2.7e-05  0.01  
6   5.5e-06  2.8e-06  1.0e-09  1.00e+00   -6.433319152e-01  -6.433324893e-01  1.5e-06  0.01  
7   6.4e-07  3.2e-07  4.1e-11  1.00e+00   -6.433314147e-01  -6.433314807e-01  1.7e-07  0.01  
8   5.5e-08  2.7e-08  1.0e-12  1.00e+00   -6.433314013e-01  -6.433314069e-01  1.5e-08  0.01  
9   3.2e-09  1.4e-09  1.5e-14  1.00e+00   -6.433314025e-01  -6.433314028e-01  8.6e-10  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -6.4333140246e-01   nrm: 1e+00    Viol.  con: 9e-09    var: 0e+00    barvar: 0e+00  
  Dual.    obj: -6.4333140278e-01   nrm: 1e+00    Viol.  con: 0e+00    var: 9e-10    barvar: 1e-09  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 9         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.643331
 
 
Calling Mosek 9.1.9: 99 variables, 20 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

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            : 20              
  Cones                  : 0               
  Scalar variables       : 27              
  Matrix variables       : 2               
  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 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            : 20              
  Cones                  : 0               
  Scalar variables       : 27              
  Matrix variables       : 2               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 20
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 28                conic                  : 7               
Optimizer  - Semi-definite variables: 2                 scalarized             : 72              
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 : 210               after factor           : 210             
Factor     - dense dim.             : 0                 flops                  : 1.12e+04        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.5e+01  1.8e+00  1.0e+00  0.00e+00   0.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   5.4e+00  6.3e-01  1.2e-01  2.00e+00   -3.229145677e-01  -4.222468406e-01  3.6e-01  0.01  
2   1.3e+00  1.5e-01  1.3e-02  1.10e+00   -6.485072010e-01  -6.729794641e-01  8.6e-02  0.01  
3   2.5e-01  2.9e-02  1.0e-03  1.16e+00   -6.872812820e-01  -6.919602866e-01  1.7e-02  0.01  
4   5.9e-02  6.9e-03  1.2e-04  1.06e+00   -6.804545819e-01  -6.815129402e-01  4.0e-03  0.01  
5   1.1e-02  1.3e-03  9.9e-06  1.01e+00   -6.810494449e-01  -6.812488478e-01  7.6e-04  0.01  
6   1.9e-03  2.2e-04  6.7e-07  9.95e-01   -6.809579801e-01  -6.809910718e-01  1.3e-04  0.01  
7   9.4e-06  1.1e-06  2.3e-10  1.00e+00   -6.809607871e-01  -6.809609518e-01  6.3e-07  0.01  
8   4.2e-07  4.9e-08  2.2e-12  1.00e+00   -6.809606826e-01  -6.809606900e-01  2.8e-08  0.01  
9   4.5e-08  5.7e-09  7.9e-14  1.00e+00   -6.809606766e-01  -6.809606774e-01  3.0e-09  0.02  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -6.8096067659e-01   nrm: 1e+00    Viol.  con: 2e-08    var: 5e-10    barvar: 0e+00  
  Dual.    obj: -6.8096067738e-01   nrm: 1e+00    Viol.  con: 0e+00    var: 2e-09    barvar: 3e-09  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 9         time: 0.02    
      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.680961
 

Results:
FDLA weights:		 rho = 0.6433 	 tau = 2.2671
FMMC weights:		 rho = 0.6810 	 tau = 2.6025
M-H weights:		 rho = 0.7743 	 tau = 3.9094
MAX_DEG weights:	 rho = 0.7793 	 tau = 4.0093
BEST_CONST weights:	 rho = 0.7119 	 tau = 2.9422