- Timestamp:
- 03/28/08 14:28:10 (16 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/SelfAdaptiveIntermediateMultiCrossover.cs
r110 r111 27 27 28 28 namespace HeuristicLab.RealVector { 29 public class SelfAdaptiveIntermediate Recombination : DiscreteRecombination{29 public class SelfAdaptiveIntermediateMultiCrossover : RealVectorSelfAdaptiveMultiCrossoverBase { 30 30 public override string Description { 31 31 get { 32 return @" Self adaptive intermediate recombinationcreates a new offspring by computing the centroid of the parents. It will also use the same strategy to combine the endogenous strategy parameter vector.";32 return @"This creates a new offspring by computing the centroid of the parents. It will also use the same strategy to combine the endogenous strategy parameter vector."; 33 33 } 34 34 } 35 35 36 public SelfAdaptiveIntermediateRecombination()37 : base() {38 AddVariableInfo(new VariableInfo("StrategyVector", "Vector containing the endogenous strategy parameters", typeof(DoubleArrayData), VariableKind.In));36 public static void Apply(IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) { 37 childIndividual = IntermediateMultiCrossover.Apply(parents); 38 strategyParameters = IntermediateMultiCrossover.Apply(strategyParametersList); 39 39 } 40 40 41 public override IOperation Apply(IScope scope) { 42 int rho = GetVariableValue<IntData>("Rho", scope, true).Data; 43 // with just 1 parent no recombination is necessary/possible 44 if (rho == 1) return null; 45 IRandom random = GetVariableValue<IRandom>("Random", scope, true); 46 47 if (scope.SubScopes.Count % rho != 0) 48 throw new InvalidOperationException("Number of parents is not a multiple of rho"); 49 int lambda = scope.SubScopes.Count / rho; 50 IList<double[]> parents = new List<double[]>(rho); 51 IList<double[]> parentsStrategy = new List<double[]>(rho); 52 53 for (int i = 0; i < lambda; i++) { 54 IScope childScope = new Scope(i.ToString()); 55 double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone(); 56 double[] strategyParams = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("StrategyVector", 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 parentsStrategy.Add(parent.GetVariableValue<DoubleArrayData>("StrategyVector", false).Data); 62 scope.RemoveSubScope(parent); 63 } 64 // actual discrete recombination 65 if (childGene.Length != strategyParams.Length) 66 throw new InvalidOperationException("ERROR: strategy vector must be as long as there are dimensions"); 67 68 for (int x = 0; x < childGene.Length; x++) { 69 double sum = 0.0, sumStrategy = 0.0; 70 for (int y = 0; y < rho; y++) { 71 sum += parents[y][x]; 72 sumStrategy += parentsStrategy[y][x]; 73 } 74 childGene[x] = sum / rho; 75 strategyParams[x] = sumStrategy / rho; 76 } 77 childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene))); 78 childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("StrategyVector"), new DoubleArrayData(strategyParams))); 79 scope.AddSubScope(childScope); 80 } 81 return null; 41 protected override void Cross(IScope scope, IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) { 42 Apply(parents, strategyParametersList, out childIndividual, out strategyParameters); 82 43 } 83 44 }
Note: See TracChangeset
for help on using the changeset viewer.