% Boyd, Kim, Vandenberghe, and Hassibi, "A Tutorial on Geometric Programming"
% Written for CVX by Almir Mutapcic 02/08/06
% (a figure is generated)
%
% Solves the problem of configuring and placing rectangles such
% that they do not overlap and that they minimize the area of the
% bounding box. This code solves the specific instances given
% in the GP tutorial. We have four rectangles with variable
% width w_i and height h_i. They need to satisfy area and aspect
% ration constraints. The GP is formulated as:
%
%   minimize   max(wa+wb,wc+wd)*(max(ha,hb)+max(hc,hd))
%       s.t.   wa*ha == area_a, wb*hb == area_b, ...
%              1/alpha_max <= ha/wa <= alpha_max, ...
%
% where variables are rectangle widths w's and heights h's.

% constants
a = 0.2;
b = 0.5;
c = 1.5;
d = 0.5;

% alpha is the changing parameter
N = 20;
alpha = linspace(1.01,4,N);

fprintf(1,'Solving for the optimal tradeoff curve...\n');
min_area = zeros(N,1);
for n = 1:N
  % GP variables
  fprintf( 'alpha = %.2f ... ', alpha(n) );
  cvx_begin gp quiet
    variables wa wb wc wd ha hb hc hd
    % objective function is the area of the bounding box
    minimize( max(wa+wb,wc+wd)*(max(ha,hb)+max(hc,hd)) )
    subject to
      % constraints (now impose the non-changing constraints)
      ha*wa == a; hb*wb == b; hc*wc == c; hd*wd == d;
      1/alpha(n) <= ha/wa <= alpha(n);
      1/alpha(n) <= hb/wb <= alpha(n);
      1/alpha(n) <= hc/wc <= alpha(n);
      1/alpha(n) <= hd/wd <= alpha(n);
  cvx_end
  fprintf( 'area = %.2f\n', cvx_optval );
  min_area(n) = cvx_optval;
end

figure, clf
plot(alpha,min_area);
xlabel('alpha'); ylabel('min area');
axis([1 4 2.5 4]);
disp('Optimal tradeoff curve is plotted.')
Solving for the optimal tradeoff curve...
alpha = 1.01 ... area = 3.72
alpha = 1.17 ... area = 3.53
alpha = 1.32 ... area = 3.38
alpha = 1.48 ... area = 3.26
alpha = 1.64 ... area = 3.16
alpha = 1.80 ... area = 3.07
alpha = 1.95 ... area = 2.99
alpha = 2.11 ... area = 2.93
alpha = 2.27 ... area = 2.87
alpha = 2.43 ... area = 2.82
alpha = 2.58 ... area = 2.77
alpha = 2.74 ... area = 2.73
alpha = 2.90 ... area = 2.70
alpha = 3.06 ... area = 2.70
alpha = 3.21 ... area = 2.70
alpha = 3.37 ... area = 2.70
alpha = 3.53 ... area = 2.70
alpha = 3.69 ... area = 2.70
alpha = 3.84 ... area = 2.70
alpha = 4.00 ... area = 2.70
Optimal tradeoff curve is plotted.