function [clas,s]=activelearn(xstart,ystart,k,grid,reqsize)
% [CLAS,SELECTED]=ACTIVELEARN(XSTART,YSTART,K,GRID,REQSIZE)
% Runs active learner with kNN classifier until the classifier has
% REQSIZE labeled points.
% Starts with labeled set (XSTART,YSTART) (assumed to have at least K
% points), and proceeds by quering the oracle at each time about a single
% point chosen as one with the highest uncertainty in label. Points are
% chosen from the GRID
% Returns in CLAS the final classifier, and in SELECTED the points on the
% grid on which the classifier is based.
%

% SEE ALSO:     ORACLE MAKE2DGRID 

% Greg Shakhnarovich  2002


s=xstart; 
y=ystart;
d = size(s,2);


while (size(s,1)<reqsize)
  % train the kNN with currently labeled points
  clas = knn(d,k,s,y);
  
  % find the confidence in label prediction on the whole grid,
  nn_count=knntest(clas,grid);
  C=abs(nn_count);
  
  % query the worst point
  fp=find(C==min(C));
  nexti=fp(unidrnd(length(fp)));
  s=[s;grid(nexti,:)];
  y = [y; oracle(grid(nexti,:))];
  
  grid = grid([1:nexti-1,nexti+1:size(grid,1)],:);

end


