AForge.Genetic.GEPChromosome.TransposeRoot C# (CSharp) Method

TransposeRoot() protected method

Root transposition.

The method performs root transposition of the GEP chromosome - inserting new root of the chromosome and shifting existing one. The method first of all randomly selects a function gene in chromosome's head - starting point of the sequence to put into chromosome's head. Then it randomly selects the length of the sequence making sure that the entire sequence is located within head. Once the starting point and the length of the sequence are known, it is copied into chromosome's head shifting existing elements in the head.

protected TransposeRoot ( ) : void
return void
		protected void TransposeRoot( )
		{
			// select source point (may be any point in the head of the chromosome)
			int sourcePoint = rand.Next( headLength );
			// scan downsrteam the head searching for function gene
			while ( ( genes[sourcePoint].GeneType != GPGeneType.Function ) && ( sourcePoint < headLength ) )
			{
				sourcePoint++;
			}
			// return (do nothing) if function gene was not found
			if ( sourcePoint == headLength )
				return;

			// calculate maxim source length
			int maxSourceLength = headLength - sourcePoint;
			// select randomly transposon length
			int transposonLength = rand.Next( maxSourceLength ) + 1;
			// genes copy
			IGPGene[] genesCopy = new IGPGene[transposonLength];

			// copy genes from source point
			for ( int i = sourcePoint, j = 0; j < transposonLength; i++, j++ )
			{
				genesCopy[j] = genes[i].Clone( );
			}

			// shift the head
			for ( int i = headLength - 1; i >= transposonLength; i-- )
			{
				genes[i] = genes[i - transposonLength];
			}

			// put new root
			for ( int i = 0; i < transposonLength; i++ )
			{
				genes[i] = genesCopy[i];
			}
		}