00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00006 namespace GEP.Core.Common.GeneticOperators
00007 {
00011 [Serializable]
00012 public class GeneTransposition : GeneticOperator
00013 {
00017 protected double _transpositionProbability;
00021 protected int _maxTransposonLength;
00025 protected Symbol[][] _transposon;
00026
00036 public GeneTransposition(double transpositionProbability, int maxTransposonLength,
00037 bool applyOnNewGeneration, GenotypeSelection.GenotypeSelection selection)
00038 : base(1, 1, false, applyOnNewGeneration, selection, "Gene Transposition")
00039 {
00040 _maxTransposonLength = maxTransposonLength;
00041 _transpositionProbability = transpositionProbability;
00042 _transposon = new Symbol[_maxTransposonLength][];
00043 }
00044
00045 public override void Apply(Genotype[] parameters, int[] selectionIndexes, Genotype[] destination, ref int destIndex)
00046 {
00047 throw new NotImplementedException();
00048 }
00049
00050 public override void Apply(Genotype[] parameters, int[] selectionIndexes)
00051 {
00052
00053 double rd = _random.NextDouble();
00054 int transposonLength;
00055 int transposonSourceStartIndex;
00056 int transposonDestinationStartIndex;
00057 int bound;
00058 Genotype genotype = parameters[selectionIndexes[0]];
00059 if (rd < _transpositionProbability)
00060 {
00061 transposonLength = _random.Next(_maxTransposonLength) + 1;
00062 transposonSourceStartIndex = _random.Next(genotype.N_Genes - transposonLength);
00063 transposonDestinationStartIndex = _random.Next(genotype.N_Genes - transposonLength);
00064 for (int j = transposonSourceStartIndex, k = 0; k < transposonLength; j++, k++)
00065 {
00066 _transposon[k] = genotype._linearStructure[j];
00067 }
00068 bound = transposonDestinationStartIndex + transposonLength;
00069 for (int j = genotype.N_Genes - transposonLength - 1, k = genotype.N_Genes - 1;
00070 bound <= j; j--, k--)
00071 {
00072 genotype._linearStructure[k] = genotype._linearStructure[j];
00073 }
00074 for (int j = transposonDestinationStartIndex, k = 0; k < transposonLength; j++, k++)
00075 {
00076 genotype._linearStructure[j] = _transposon[k];
00077 }
00078 }
00079 }
00080
00081 public GeneTransposition(GeneTransposition symbol)
00082 :base(symbol)
00083 {
00084 _maxTransposonLength = symbol._maxTransposonLength;
00085 _transpositionProbability = symbol._transpositionProbability;
00086 _transposon = new Symbol[_maxTransposonLength][];
00087 }
00088
00089 public override GEP.Core.Common.Symbol Clone()
00090 {
00091 return new GeneTransposition(this);
00092 }
00093 }
00094 }