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 Inversion : GeneticOperator 00013 { 00017 protected double _inversionProbability; 00021 protected int _maxInversionSectorLength; 00022 00023 00034 public Inversion(double inversionProbability, int maxInversionSectorLength, 00035 bool applyOnNewGeneration, GenotypeSelection.GenotypeSelection selection) 00036 : base(1, 1, false, applyOnNewGeneration, selection, "Inversion") 00037 { 00038 _maxInversionSectorLength = maxInversionSectorLength; 00039 _inversionProbability = inversionProbability; 00040 } 00041 00042 public override void Apply(Genotype[] parameters, int[] selectionIndexes, Genotype[] destination, ref int destIndex) 00043 { 00044 throw new NotImplementedException(); 00045 } 00046 00047 public override void Apply(Genotype[] parameters, int[] selectionIndexes) 00048 { 00049 Genotype genotype = parameters[selectionIndexes[0]]; 00050 int n_genes = genotype.N_Genes; 00051 Symbol[][] linearStructure = genotype._linearStructure; 00052 double rd; 00053 int inversionSectorLength; 00054 int inversionSectorStartIndex; 00055 int inversionSectorEndIndex; 00056 int innerLoopBound; 00057 Symbol[] gene; 00058 Symbol temp; 00059 for (int i = 0; i < n_genes; i++) 00060 { 00061 rd = _random.NextDouble(); 00062 if (rd < _inversionProbability) 00063 { 00064 inversionSectorLength = _random.Next(_maxInversionSectorLength + 1); 00065 inversionSectorStartIndex = _random.Next(genotype.HeadLength - inversionSectorLength); 00066 inversionSectorEndIndex = inversionSectorStartIndex + inversionSectorLength - 1; 00067 gene = linearStructure[i]; 00068 innerLoopBound = inversionSectorLength / 2; 00069 for (int j = 0; j < innerLoopBound; j++) 00070 { 00071 temp = gene[inversionSectorStartIndex + j]; 00072 gene[inversionSectorStartIndex + j] = gene[inversionSectorEndIndex - j]; 00073 gene[inversionSectorEndIndex - j] = temp; 00074 } 00075 } 00076 } 00077 } 00078 00083 public Inversion(Inversion symbol) 00084 :base(symbol) 00085 { 00086 _maxInversionSectorLength = symbol._maxInversionSectorLength; 00087 _inversionProbability = symbol._inversionProbability; 00088 } 00089 00094 public override GEP.Core.Common.Symbol Clone() 00095 { 00096 return new Inversion(this); 00097 } 00098 } 00099 }