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 OnePointCrossover : GeneticOperator 00013 { 00014 00019 public OnePointCrossover(GenotypeSelection.GenotypeSelection selection) 00020 : base(2, 1, true, false, selection, "One-point Crossover") 00021 { 00022 00023 } 00024 00025 public override void Apply(Genotype[] parameters, int[] selectionIndexes, 00026 Genotype[] destination, ref int destIndex) 00027 { 00028 int geneLength = parameters[selectionIndexes[0]].GeneLength; 00029 Genotype child = parameters[selectionIndexes[0]].CloneWithoutLinearStructure(); 00030 int detachmentIndex2 = _random.Next(geneLength); 00031 int detachmentIndex1; 00032 int geneIndex2 = _random.Next(parameters[selectionIndexes[0]].N_Genes); 00033 int geneIndex1 = geneIndex2; 00034 if (detachmentIndex2 == 0) 00035 { 00036 detachmentIndex1 = geneLength - 1; 00037 if (geneIndex2 != 0) 00038 geneIndex1--; 00039 else 00040 { 00041 detachmentIndex1 = 0; 00042 detachmentIndex2 = 1; 00043 } 00044 } 00045 else 00046 { 00047 detachmentIndex1 = detachmentIndex2 - 1; 00048 } 00049 00050 if (_random.Next(2) == 0) 00051 { 00052 Genotype.CopyLinearStructure(parameters[selectionIndexes[0]], child, 0, geneIndex1, 0, detachmentIndex1); 00053 Genotype.CopyLinearStructure(parameters[selectionIndexes[1]], child, geneIndex2, child.N_Genes - 1, 00054 detachmentIndex2, child.GeneLength - 1); 00055 } 00056 else 00057 { 00058 Genotype.CopyLinearStructure(parameters[selectionIndexes[0]], child, geneIndex2, child.N_Genes - 1, 00059 detachmentIndex2, child.GeneLength - 1); 00060 Genotype.CopyLinearStructure(parameters[selectionIndexes[1]], child, 0, geneIndex1, 0, detachmentIndex1); 00061 } 00062 destination[destIndex++] = child; 00063 } 00064 00065 public override void Apply(Genotype[] parameters, int[] selectionIndexes) 00066 { 00067 throw new NotImplementedException(); 00068 } 00069 00074 public OnePointCrossover(OnePointCrossover symbol) 00075 :base(symbol) 00076 { 00077 00078 } 00079 00084 public override GEP.Core.Common.Symbol Clone() 00085 { 00086 return new OnePointCrossover(this); 00087 } 00088 } 00089 }