% This m-file generates a file Prob.mat that stores the following:
% PP = positions, with the first n-m being sensors and the last m being
% anchors, where n=size(PP,2).
% m = number of anchors.
% dd = upper triangular matrix storing distance information.
% It then generates a file Prob1.mat that stores the following:
% dim x nd d head tail n m

clear

% input algorithm parameters

fprintf(' Random problem generator for sensor network localization\n')
fprintf(' This generates input in both matrix and head-tail format\n')
fprintf(' ********************************************************\n')
fprintf(' Enter 1 for uniform grid with certain percentage anchors,\n')
fprintf(' Enter any other numbers for uniformly generated networks.\n')
fprintf(' ********************************************************\n')

r = input(' Input an integer to pick network type: ');
fprintf(' ********************************************************\n')
dim = 2;
if r == 1
  n = input(' enter total number of sensors and anchors ');
  per = input(' enter percentage of anchors (0.1 for 10 percent )');
  nfac = input(' enter noisyfactor (e.g., 0 or .05) ');
  radiorange = input(' enter radio range (e.g., .06) ');
  m1 = floor(sqrt(per*n));
  m = m1^2;
  m2 = n-m;
  x = rand(dim,m2) - .5*ones(dim,m2);
  add = 0:m1-1;
  add = [zeros(1,m1);add];
  PP = [];
  for i = 0:m1-1
    PP = [PP (0.5-i/(m1-1))*[ones(1,m1);zeros(1,m1)]-0.5*[zeros(1,m1);ones(1,m1)]+1/(m1-1)*add];
  end
  x = [x PP];
else
  n = input(' enter total number of sensors and anchors ');
  m = input(' enter number of anchors ');
  nfac = input(' enter noisyfactor (e.g., 0 or .05) ');
  radiorange = input(' enter radio range (e.g., .06) ');
  x = rand(dim,n) - .5*ones(dim,n);
end

pdf = input(' enter 1, 2, 3 for normal, uniform, additive-Gaussian noise ');

dd = sparse(n,n);
nd = 0;

for i = n-m+1:n
  for j = 1:n-m
    dist = norm(x(1:dim,i) - x(1:dim,j));
    if dist < radiorange
      nd = nd + 1;
      if pdf == 1
        rv = randn(1);			%Gaussian noise
      else
        if pdf == 3
          rv = randn(1) - 1;
          if rand(1) < .5
            rv = rv + 2;			%Additive-Gaussian with mean -1 or 1 equi-probably
          end
        else
          if pdf == 2
            rv = 2*rand(1) - 1;		%Uniform noise on [-1,1]
          end
        end
      end
      dd(j,i) = dist*abs(1 + rv*nfac);
      d(nd) = dist*abs(1 + rv*nfac);
      head(nd) = i;
      tail(nd) = j;
    end
  end
end

for i = 1:n-m
  for j = i+1:n-m
    dist = norm(x(1:dim,i)-x(1:dim,j));
    if dist < radiorange
      nd = nd + 1;
      if pdf == 1 
        rv = randn(1);			%Gaussian noise
      else
        if pdf == 3
          rv = randn(1) - 1;
          if rand(1) < .5
            rv = rv + 2;			%Additive-Gaussian with mean -1 or 1 equi-probably
          end
        else
          if pdf == 2
            rv = 2*rand(1) - 1;		%Uniform noise on [-1,1]
          end
        end
      end
      dd(i,j) = dist*abs(1 + rv*nfac);
      d(nd) = dist*abs(1 + rv*nfac);
      head(nd) = i;
      tail(nd) = j;
    end
  end
end
PP = x;
save Prob m PP dd  
save Prob1 dim x nd d head tail n m