- Timestamp:
- 03/28/08 14:28:10 (17 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/DiscreteMultiCrossover.cs
r110 r111 27 27 28 28 namespace HeuristicLab.RealVector { 29 public class Discrete Recombination : OperatorBase {29 public class DiscreteMultiCrossover : RealVectorMultiCrossoverBase { 30 30 public override string Description { 31 31 get { 32 return @" Discrete/dominant recombinationcreates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent";32 return @"This creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent"; 33 33 } 34 34 } 35 35 36 public DiscreteRecombination() 37 : base() { 38 AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In)); 39 AddVariableInfo(new VariableInfo("Rho", "Amount of parents to recombine", typeof(IntData), VariableKind.In)); 40 AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New)); 36 public static double[] Apply(IRandom random, IList<double[]> parents) { 37 int length = parents[0].Length; 38 double[] result = new double[length]; 39 try { 40 for (int i = 0; i < length; i++) { 41 result[i] = parents[random.Next(parents.Count)][i]; 42 } 43 } catch (IndexOutOfRangeException) { 44 throw new InvalidOperationException("Cannot apply multicrossover to real vectors of different length."); 45 } 46 return result; 41 47 } 42 48 43 public override IOperation Apply(IScope scope) { 44 int rho = GetVariableValue<IntData>("Rho", scope, true).Data; 45 // with just 1 parent no recombination is necessary/possible 46 if (rho == 1) return null; 47 IRandom random = GetVariableValue<IRandom>("Random", scope, true); 48 49 if (scope.SubScopes.Count % rho != 0) 50 throw new InvalidOperationException("Number of parents is not a multiple of rho"); 51 int lambda = scope.SubScopes.Count / rho; 52 IList<double[]> parents = new List<double[]>(rho); 53 54 for (int i = 0; i < lambda; i++) { 55 IScope childScope = new Scope(i.ToString()); 56 double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone(); 57 parents.Clear(); 58 for (int j = 0; j < rho; j++) { 59 IScope parent = scope.SubScopes[0]; 60 parents.Add(parent.GetVariableValue<DoubleArrayData>("RealVector", false).Data); 61 scope.RemoveSubScope(parent); 62 } 63 // actual discrete recombination 64 for (int x = 0; x < childGene.Length; x++) { 65 childGene[x] = parents[random.Next(rho)][x]; 66 } 67 childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene))); 68 scope.AddSubScope(childScope); 69 } 70 return null; 49 protected override double[] Cross(IScope scope, IRandom random, IList<double[]> parents) { 50 return Apply(random, parents); 71 51 } 72 52 }
Note: See TracChangeset
for help on using the changeset viewer.