% [A, B, Y] = FacilitiesData()
%
% Generates data that includes a matrix B of connections between x
% locations (facilities), A the matrix representing connections y
% (source) locations and x (facility) locations, and Y of size m-by-2
% (m == 4) representing source locations.
function [A, B, Y] = FacilitiesData()
  Y = [0 0; 1 0; 0 1; 1 1];
  % Connect resource sources to facilities on the sides, respectively
  super_low_cost = .01;
  low_cost = .94;
  high_cost = 1.0;
  A = (low_cost * [0 1 0 0; 1 0 0 0; 0 0 1 0; 0 0 0 1] + ...
       high_cost * [1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0] + ...
       super_low_cost * [0 0 1 0; 0 0 0 1; 0 1 0 0; 1 0 0 0]);
  % Connect facilities in a cycle
  B = .05 * [0 1 0 0; 0 0 1 0; 0 0 0 1; 1 0 0 0];
  PlotFacilities(A, B, Y,rand(4,2));
end

% PlotFacilities(A, B, X, Y)
%
% Plots facility and source locations, along with active paths between
% them.  Facilities are printed as dark boxes, locations as light
% (white) boxes. Arguments:
%
%  A - prices per unit transport between sources and facilities (m-by-n)
%  B - prices per unit transport between facilities (n-by-n)
%  X - n-by-2 matrix of facility locations
%  Y - m-by-2 matrix of source locations
function PlotFacilities(A, B, X, Y)
  figure();
  nn = size(X, 1);
  mm = size(Y, 1);
  % Plot the Y locations, the source points
  for ii = 1:mm
    plot(Y(ii, 1), Y(ii, 2), 'ko');
    hold on
  end
  % First, plot the X locations, which are the actual placed facilities
  for ii = 1:nn
    plot(X(ii, 1), X(ii, 2), 'k*');
    hold on
  end
  % Plot the edges between X and Y
  for ii = 1:mm
    for jj = 1:nn
      if (A(ii, jj) > 0)
        % Draw edge between Y[ii, :] and X[jj, :]
        plot([Y(ii, 1), X(jj, 1)], [Y(ii, 2), X(jj, 2)], 'r');
        hold on
      end
    end
  end
  % Plot the edges between X and X
  for ii = 1:nn
    for jj = 1:nn
      if (B(ii, jj) > 0)
        plot([X(ii, 1), X(jj, 1)], [X(ii, 2), X(jj, 2)], 'b');
        hold on
      end
    end
  end
  horiz_min = min([X(:, 1);Y(:, 1)]); horiz_max = max([X(:, 1);Y(:, 1)]);
  vert_min = min([X(:, 2);Y(:, 2)]); vert_max = max([X(:, 2);Y(:, 2)]);
  dh = .05 * (horiz_max - horiz_min)
  dv = .05 * (vert_max - vert_min)
  axis([horiz_min - dh, horiz_max + dh, vert_min - dv, vert_max + dv]);
end