function plot_traced(id, traced, parentof, cols)
% Shows the history of selected traced values (two, as of now).
%
% id       - ID of the individual to trace back to its original parent(s)
%
% traced   - traced values for all individuals, i.e., ID's 1..last
%
% parentof - parent(s) of each individual
%
% cols     - columns to show (exactly two allowed in one plot, as
%            of now)
%
% One value is shown as a "convergence graph" for each epoch.
%
% Two values are shown as a trajectory in 2D; final point is marked
%
% Three or more values are shown as several convergence graphs overlaid.

%----------------------------------------------------
% FIXME: So far only one parent allowed. TODO: Implement recursion.
%----------------------------------------------------

ntraces = numel(cols);

% build the trace graph of one individual:
g=[]
while (id~=0)
  g=[g ; traced(id,2:end)];
  id = parentof{id};
end

% reverse rows, because the trace was gathered child-to-grandpa above:
g = g(end:-1:1,:)


if ntraces == 1
  % FIXME: How do we know the epoch of creation!!? Should be traced, too
  plot(g(:,cols));
  hold on;
  plot(g(end,cols),'ko');
elseif ntraces == 2
  plot(g(:,cols(1)),g(:,cols(2)))
  hold on;
  plot(g(end,cols(1)),g(end,cols(2)),'ko')
  plot(g(1,cols(1)),g(1,cols(2)),'k.')
elseif ntraces == 3
  plot3(g(:,cols(1)),g(:,cols(2)),g(:,cols(3)))
  hold on;
  plot3(g(end,cols(1)),g(end,cols(2)),g(end,cols(3)),'ko');
  plot3(g(1,cols(1)),g(1,cols(2)), g(1,cols(3)),'k.');
end

%FIXME: Mock-up only.. plot 2D trajectory (can do 3d and maybe 4d
%with some color for 4th dimension.. 5d quite impossible already):
%plot(g(:,1),g(:,3));
