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 PartialTransposition : GeneticOperator 00013 { 00017 protected double _transpositionProbability; 00022 protected int _maxTransposonLength; 00026 protected Symbol[] _transposon; 00027 00038 public PartialTransposition(double transpositionProbability, int maxTransposonLength, 00039 bool applyOnNewGeneration, GenotypeSelection.GenotypeSelection selection) 00040 : base(1, 1, false, applyOnNewGeneration, selection, "Partial Transposition") 00041 { 00042 _maxTransposonLength = maxTransposonLength; 00043 _transpositionProbability = transpositionProbability; 00044 _transposon = new Symbol[_maxTransposonLength]; 00045 } 00046 00047 public override void Apply(Genotype[] parameters, int[] selectionIndexes, Genotype[] destination, ref int destIndex) 00048 { 00049 throw new NotImplementedException(); 00050 } 00051 00052 public override void Apply(Genotype[] parameters, int[] selectionIndexes) 00053 { 00054 double rd = _random.NextDouble(); 00055 int transposonLength; 00056 int transposonSourceStartIndex; 00057 Symbol[] destinationGene; 00058 Symbol[] sourceGene; 00059 int transposonDestinationStartIndex; 00060 int bound; 00061 Genotype genotype = parameters[selectionIndexes[0]]; 00062 for (int i = 0; i < genotype.N_Genes; i++) 00063 { 00064 if (rd < _transpositionProbability) 00065 { 00066 transposonLength = _random.Next(_maxTransposonLength) + 1; 00067 transposonSourceStartIndex = _random.Next(genotype.HeadLength 00068 + genotype.TailLength - transposonLength); 00069 sourceGene = genotype._linearStructure[i]; 00070 destinationGene = genotype._linearStructure[_random.Next(genotype.N_Genes)]; 00071 if (sourceGene[transposonSourceStartIndex] == null 00072 || sourceGene[transposonSourceStartIndex].N_Parameters == 0) 00073 transposonDestinationStartIndex = 00074 _random.Next(genotype.HeadLength - transposonLength - 1) + 1; 00075 else 00076 transposonDestinationStartIndex = _random.Next(genotype.HeadLength - transposonLength); 00077 if (object.ReferenceEquals(sourceGene, destinationGene)) 00078 { 00079 for (int j = transposonSourceStartIndex, k = 0; k < transposonLength; j++, k++) 00080 { 00081 _transposon[k] = sourceGene[j]; 00082 } 00083 bound = transposonDestinationStartIndex + transposonLength; 00084 for (int j = genotype.HeadLength - transposonLength - 1, k = genotype.HeadLength - 1; 00085 bound <= j; j--, k--) 00086 { 00087 destinationGene[k] = destinationGene[j]; 00088 } 00089 for (int j = transposonDestinationStartIndex, k = 0; k < transposonLength; j++, k++) 00090 { 00091 destinationGene[j] = _transposon[k]; 00092 } 00093 } 00094 else 00095 { 00096 bound = transposonDestinationStartIndex + transposonLength; 00097 for (int j = genotype.HeadLength - transposonLength - 1, k = genotype.HeadLength - 1; 00098 bound <= j; j--, k--) 00099 { 00100 destinationGene[k] = destinationGene[j]; 00101 } 00102 bound = transposonSourceStartIndex + transposonLength; 00103 for (int j = transposonDestinationStartIndex, k = transposonSourceStartIndex; 00104 k < bound; j++, k++) 00105 { 00106 destinationGene[j] = sourceGene[k]; 00107 } 00108 } 00109 } 00110 } 00111 } 00112 00117 public PartialTransposition(PartialTransposition symbol) 00118 : base(symbol) 00119 { 00120 _maxTransposonLength = symbol._maxTransposonLength; 00121 _transpositionProbability = symbol._transpositionProbability; 00122 _transposon = new Symbol[_maxTransposonLength]; 00123 } 00124 00129 public override GEP.Core.Common.Symbol Clone() 00130 { 00131 return new PartialTransposition(this); 00132 } 00133 } 00134 }