function [ avegaps ] = linkage2(numgens, numactive, initpop, numit, thepower, nummuts, splicesize)
popn=zeros(initpop,numgens,2);
for i=1:initpop
   popn(i,:,1)=randarr(numgens,1,numgens);
   for j=1:numgens
      popn(i,j,2)=irand(0,1);
   end
end
maxlinkage=zeros(numit+1,1);
minlinkage=zeros(numit+1,1);
avelinkage=zeros(numit+1,1);
maxones=zeros(numit+1,1);
minones=zeros(numit+1,1);
aveones=zeros(numit+1,1);
for i=1:numit
   maxgenegaps = gaps(numgens, numactive, initpop, popn);
   avelinkage(i)=numgens-mean(maxgenegaps);
   maxlinkage(i)=numgens-max(maxgenegaps);
   minlinkage(i)=numgens-min(maxgenegaps);
   oness = numones(numgens,numactive,initpop,popn);
   aveones(i)=mean(oness);
   maxones(i)=max(oness);
   minones(i)=min(oness);
   popn = newpop(numgens, numactive, initpop, popn, oness, thepower,splicesize);
   popn = mutate(numgens, initpop, popn, nummuts);
end
maxgenegaps = gaps(numgens, numactive, initpop, popn);
avelinkage(numit+1)=numgens-mean(maxgenegaps);
maxlinkage(numit+1)=numgens-max(maxgenegaps);
minlinkage(numit+1)=numgens-min(maxgenegaps);
oness = numones(numgens,numactive,initpop,popn);
aveones(numit+1)=mean(oness);
maxones(numit+1)=max(oness);
minones(numit+1)=min(oness);

figure,subplot(2,1,1),plot(linspace(0,numit,numit+1),avelinkage,'r',linspace(0,numit,numit+1),maxlinkage,'g',linspace(0,numit,numit+1),minlinkage,'b'),xlabel('No. of Iterations'),ylabel('Average Active Genespace'),title(strcat(sprintf('No. of genes: \t'),num2str(numgens),sprintf(', No. of active genes: \t'),num2str(numactive),sprintf(',\nGenerational population: \t'),num2str(initpop), sprintf(', Fitness function raising power: \t'),num2str(thepower))),axis([0, numit, 0, numgens]),subplot(2,1,2),plot(linspace(0,numit,numit+1),aveones,'r',linspace(0,numit,numit+1),maxones,'g',linspace(0,numit,numit+1),minones,'b'),xlabel('No. of Iterations'),ylabel('Number of Active Genes = 1'),title('Number of Active Genes = 1 Over Time'),axis([0, numit, 0, numactive*1.1]);

function [donor,recepient] = donorrecepient(probs, popul, cumgaps)
donor=1;
recepient=1;
dval=irand(1,probs);
rval=irand(1,probs);
for j=1:popul-1
   if (dval>cumgaps(j)) & (dval<=cumgaps(j+1))
      donor=j+1;
   end
   if (rval>cumgaps(j)) & (rval<=cumgaps(j+1))
      recepient=j+1;
   end
end 

while (eq(donor,recepient))
   rval=irand(1,probs);
   for j=1:popul-1
      if (rval>cumgaps(j)) & (rval<=cumgaps(j+1))
      	recepient=j+1;
      end
   end
end

function random = irand(lb, ub)
num = rand(1);
random = lb + floor(num*(ub-lb+1));

function randomarray = randarr(arrSize,lb,ub)
randomarray=zeros(arrSize,1);
for i=1:arrSize
   randomarray(i)=i;
end
for i=1:arrSize*2;
   rand1=irand(1,arrSize);
   rand2=irand(1,arrSize);
   temp=randomarray(rand1);
   randomarray(rand1)=randomarray(rand2);
   randomarray(rand2)=temp;
end

function maxgenegaps = gaps(numgens,numactive,initpop,popn)
   genespace=zeros(initpop,1);
   maxgenegaps=zeros(initpop,1);
   for i=1:initpop
      gennums=0;
      startgene=0;
      stopgene=0;
      genegap=0;
      maxgenegap=0;
      lastgene=0;
      for j=1:numgens
         if (gennums==0) 
            startgene=j+1;
         end
         if (popn(i,j,1)<=numactive)
            gennums=gennums+1;
            genegap=j-lastgene-2;
            if (genegap>maxgenegap) & (lastgene~=0)
               maxgenegap=genegap;
            end
            if (gennums==numactive) 
               stopgene=j;
            end
            lastgene=j;
         end 
      end
      genegap=numgens-stopgene+startgene-2;
      if (genegap>maxgenegap)
         maxgenegap=genegap;
      end
      genespace(i)=numgens-maxgenegap;
      maxgenegaps(i)=maxgenegap;
   end
   
function newpop = newpop(numgens,numactive,initpop,popn,oness,thepower,splicesize)
for i=1:size(oness)
   oness(i)=power(oness(i),thepower);
end
totprob=sum(oness);
cummgg=cumsum(oness);
newpop=zeros(initpop,numgens,2);
for i=1:initpop
   [don,recep]=donorrecepient(totprob,initpop,cummgg);
   startgene=irand(1,numgens);
   if (splicesize==0)
      stopgene=irand(1,numgens);
   else
      stopgene=startgene+splicesize;
   end
   if (stopgene>100) 
      stopgene=stopgene-numgens;
   end
   splicepoint=irand(0,numgens);
   ncsize=0;
   if (stopgene<startgene)
      ncsize=numgens-startgene+stopgene+1;
      spsection=zeros(ncsize,2);
      k=0;
      for j=startgene:numgens
         k=k+1;
         spsection(k,:)=popn(don,j,:);
      end
      for j=1:stopgene
         k=k+1;
         spsection(k,:)=popn(don,j,:);
		end
   else
      ncsize=stopgene-startgene+1;
      spsection=zeros(ncsize,2);
      k=0;
      for j=startgene:stopgene
         k=k+1;
         spsection(k,:)=popn(don,j,:);
      end
   end
	newchromo=zeros(ncsize,2);
   k=0;
   for j=1:splicepoint
      k=k+1;
      newchromo(k,:)=popn(recep,j,:);
   end 
   for j=1:size(spsection)
      k=k+1;
      newchromo(k,:)=spsection(j,:);
   end
   for j=splicepoint+1:numgens
      k=k+1;
      newchromo(k,:)=popn(recep,j,:);
   end
   genepres = zeros(numgens);
   newchromo2 = zeros(numgens,2);
   k=0;
   for j=splicepoint+1:size(newchromo)
      if (eq(genepres(newchromo(j,1)),0))
         k=k+1;
         genepres(newchromo(j,1))=1;
         newchromo2(k,:)=newchromo(j,:);
      end
   end
   for j=1:splicepoint
      if (eq(genepres(newchromo(j,1)),0))
         k=k+1;
         genepres(newchromo(j,1))=1;
         newchromo2(k,:)=newchromo(j,:);
      end
   end
   newpop(i,:,:)=newchromo2(:,:);
end

function newpop = mutate(numgens, initpop, popn, nummuts)
totmuts=irand(0,nummuts*initpop*2);
for j=1:totmuts
   chromo=irand(1,initpop);
   gene=irand(1,numgens);
   popn(chromo,gene,2)=-(popn(chromo,gene,2)-1);
end
newpop=popn;

function newones = numones(numgens,numactive,initpop,popn)
oness=zeros(initpop,1);
for i=1:initpop
   for j=1:numgens
      if (popn(i,j,1)<=numactive)
         if (popn(i,j,2)==1)
            oness(i)=oness(i)+1;
         end
      end
   end
end
newones=oness;
   




   
   





