function Result = runtrials(Entry,NumTrials)
%RUNTRIALS Tests an entry on several inputs.
% Result = runtrials(EntryName,NumberOfTrials)
%
% This function can be used to test your entries.
% We've provided two example solutions as subfunctions,
% you can work with them or write your own routines.
% Here's an example of how you can test this routine:
%
% Results = runtrials('sample_entry2',10)
%
% This will run the entry 'sample_entry2' 10 times.
Result = zeros(NumTrials,3);
for i = 1:NumTrials
% Below we call the GENES function to create a sample
% sequence. You can vary the parameters passed to GENES
% to vary the inputs passed to your function.
[Sequences, OriginalGene]= genes(20,30,8,0);
% Start timer, and evaluate entry.
t0 = cputime;
Your_Solution = feval(Entry,Sequences);
time_elapsed = cputime-t0;
% Check the sequence to see if it's valid.
Check = checksequence(Sequences,Your_Solution);
if Check == 1
Result(i,1) = length(Your_Solution);
Result(i,2) = time_elapsed;
Result(i,3) = length(OriginalGene);
else
Result(i,:) = Inf;
end
% Display some output as we go along...
disp(['Trial ' num2str(i) ' Score ' num2str(Result(i,1)),...
' Time ' num2str(time_elapsed),...
' Original Length ' num2str(length(OriginalGene))])
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function a = sample_entry1(a)
% This is a simple routine just places the segments together
% end-to-end. There's another example that you can look at
% by selecting "New Submission" from http://www.mathworks.com/contest
a = a';
a = a(:)';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function finalGene = sample_entry2(a)
% Return the final gene based on fragments provided in matrix a
% finalGen = slowgene(segments)
% This is a more elaborate example, which you might want to use
% for ideas...
% Deal all the rows into a cell array
for n = 1:size(a,1),
s{n} = a(n,:);
end
iter=0;
while length(s)>1,
% To prevent getting stuck in blind alleys, restart the process
% if it's taking too long.
iter = iter + 1;
% The limit to the number of iterations is a dubious guess.
if iter > size(a,1)*2
% Just concatenate what's left and stop
finalGene = [s{:}];
return
end
[out, i] = sort(rand(1,length(s)));
newSeg = matchsegs(s{i(1)}, s{i(2)});
if ~isempty(newSeg),
% If there is a fit, replace one of the segments with the new
% longer segment and eliminate the other one.
s{i(1)} = newSeg;
s(i(2)) = [];
iter = 0;
end
end
finalGene = s{1};
function bigSeg = matchsegs(seg1, seg2)
% Look for the end of seg1 matching the front of seg2
% seg1 should always be the shorter of the segments
if length(seg1) > length(seg2),
segbuf=seg1; seg1=seg2; seg2=segbuf;
end
seg1Len = length(seg1);
seg2Len = length(seg2);
% Quick check to see if seg1 is wholly contained in seg2
if findstr(seg1,seg2),
bigSeg = seg2;
return
end
% We're going to slide the two segments past each
% other to see if they "fit" like so:
% seg1 seg1 seg1
% seg2 seg2 seg2
for n = 2:seg1Len
if seg1(n:end) == seg2(1:(seg1Len-n+1)),
bigSeg = [seg1(1:(n-1)) seg2];
return
end
end
% No match; go home empty-handed
bigSeg = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [segments, finalGene] = genes(geneLen, numPieces, segLen, displayFlag)
%GENES Cut a gene into random pieces.
% [segments, finalGene] = genes(geneLen, numPieces, segLen, displayFlag)
% All segments are the same length and are returned as rows in the matrix segments.
if nargin<1, geneLen=50; end
if nargin<2, numPieces=5; end
if nargin<3, segLen=15; end
if nargin<4, displayFlag=1; end
% A = adenine, G = guanine, C = cytosine, T = thymine
a='ACTG';
% Generate the random gene geneLen base pairs long
x=floor(4*rand(1,geneLen))+1;
% We need to guarantee that both ends are covered
% 1:segLen:geneLen
coverageIncomplete=1;
n=0;
maxCount=200;
while coverageIncomplete,
initPts=[1 geneLen-segLen+1 floor((geneLen-segLen+1)*rand(1,numPieces-2)+1)];
% If the gap between any two initial points is less than the piece length
% then the coverage is complete
if max(diff(sort(initPts))) maxCount),
fprintf('Couldn''t generate complete coverage after %d tries\n',maxCount)
segments = -1;
return
end
end
% Return in sorted order
%initPts=sort(initPts);
% Return in random order
initPts=initPts(randperm(length(initPts)));
segments=zeros(numPieces,segLen);
for n=1:numPieces,
segments(n,:) = x(initPts(n)-1 + (1:segLen));
end
if displayFlag,
dashes=char(abs('-')*ones(1,geneLen));
% Display the gene and its constituent pieces
fprintf('\nThe original gene:\n%s\n',a(x))
fprintf('%s\n',dashes)
for n=1:numPieces,
spaces=char(32*ones(1,initPts(n)-1));
fprintf('%s%s\n',spaces,a(segments(n,:)));
end
fprintf('%s\n',dashes)
end
% Return the segments as strings
segments = a(segments);
finalGene = a(x);
function ok=checksequence(segments,sequence);
% CHECKSEQUENCE(segments,sequence) verifies that the sequence contains
% every segment and returns the length of the sequence.
for i = 1:size(segments,1)
if isempty(findstr(segments(i,:),sequence))
%This sequence does not contain every segment;
ok(i) = 0;
else
%This sequence contains every segment;
ok(i) = 1;
end
end
if all(ok==1)
ok = 1;
else
ok = 0;
end