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 TwoPointCrossover : GeneticOperator 00013 { 00018 public TwoPointCrossover(GenotypeSelection.GenotypeSelection selection) 00019 : base(2, 1, true, false, selection, "Two-point Crossover") 00020 { 00021 00022 } 00023 00024 public override void Apply(Genotype[] parameters, int[] selectionIndexes, 00025 Genotype[] destination, ref int destIndex) 00026 { 00027 int geneLength = parameters[selectionIndexes[0]].GeneLength; 00028 int detachmentIndex12 = _random.Next(parameters[selectionIndexes[0]].GeneLength); 00029 int detachmentIndex22 = _random.Next(parameters[selectionIndexes[0]].GeneLength); 00030 int geneIndex12 = _random.Next(parameters[selectionIndexes[0]].N_Genes); 00031 int geneIndex22 = _random.Next(parameters[selectionIndexes[0]].N_Genes); 00032 if (geneIndex12 > geneIndex22) 00033 { 00034 int temp = geneIndex12; 00035 geneIndex12 = geneIndex22; 00036 geneIndex22 = temp; 00037 } 00038 else if (geneIndex12 == geneIndex22) 00039 { 00040 if (detachmentIndex12 > detachmentIndex22) 00041 { 00042 int temp = detachmentIndex12; 00043 detachmentIndex12 = detachmentIndex22; 00044 detachmentIndex22 = temp; 00045 } 00046 while (Math.Abs(detachmentIndex12 - detachmentIndex22) < 2) 00047 { 00048 if (detachmentIndex12 <= 1) 00049 detachmentIndex22++; 00050 else 00051 detachmentIndex12--; 00052 } 00053 } 00054 int detachmentIndex11; 00055 int detachmentIndex21; 00056 int geneIndex11 = geneIndex12; 00057 int geneIndex21 = geneIndex22; 00058 if (detachmentIndex12 == 0) 00059 { 00060 detachmentIndex11 = geneLength - 1; 00061 if (geneIndex12 != 0) 00062 geneIndex11--; 00063 else 00064 { 00065 detachmentIndex11 = 0; 00066 detachmentIndex12 = 1; 00067 } 00068 } 00069 else 00070 { 00071 detachmentIndex11 = detachmentIndex12 - 1; 00072 } 00073 if (detachmentIndex22 == 0) 00074 { 00075 detachmentIndex21 = geneLength - 1; 00076 if (geneIndex22 == 0) 00077 geneIndex21--; 00078 } 00079 else 00080 { 00081 detachmentIndex21 = detachmentIndex22 - 1; 00082 } 00083 00084 Genotype child = parameters[selectionIndexes[0]].CloneWithoutLinearStructure(); 00085 if (_random.Next(2) == 0) 00086 { 00087 Genotype.CopyLinearStructure(parameters[selectionIndexes[0]], child, 0, geneIndex11, 0, detachmentIndex11); 00088 Genotype.CopyLinearStructure(parameters[selectionIndexes[0]], child, geneIndex22, child.N_Genes - 1, 00089 detachmentIndex22, geneLength - 1); 00090 Genotype.CopyLinearStructure(parameters[selectionIndexes[1]], child, geneIndex12, geneIndex21, 00091 detachmentIndex12, detachmentIndex21); 00092 } 00093 else 00094 { 00095 Genotype.CopyLinearStructure(parameters[selectionIndexes[1]], child, 0, geneIndex11, 0, detachmentIndex11); 00096 Genotype.CopyLinearStructure(parameters[selectionIndexes[1]], child, geneIndex22, child.N_Genes - 1, 00097 detachmentIndex22, geneLength - 1); 00098 Genotype.CopyLinearStructure(parameters[selectionIndexes[0]], child, geneIndex12, geneIndex21, 00099 detachmentIndex12, detachmentIndex21); 00100 } 00101 destination[destIndex++] = child; 00102 } 00103 00104 public override void Apply(Genotype[] parameters, int[] selectionIndexes) 00105 { 00106 throw new NotImplementedException(); 00107 } 00108 00113 public TwoPointCrossover(TwoPointCrossover symbol) 00114 :base(symbol) 00115 { 00116 00117 } 00118 00123 public override GEP.Core.Common.Symbol Clone() 00124 { 00125 return new TwoPointCrossover(this); 00126 } 00127 } 00128 }