Changeset 14429 for branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers
- Timestamp:
- 11/29/16 15:46:48 (8 years ago)
- Location:
- branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers/NPointCrossover.cs
r12012 r14429 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Optimization; 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 70 71 /// <param name="n">Number of crossover points.</param> 71 72 /// <returns>The newly created binary vector, resulting from the N point crossover.</returns> 72 public static BinaryVector Apply(IRandom random, BinaryVector parent1, BinaryVector parent2, IntValuen) {73 public static BinaryVector Apply(IRandom random, BinaryVector parent1, BinaryVector parent2, int n) { 73 74 if (parent1.Length != parent2.Length) 74 75 throw new ArgumentException("NPointCrossover: The parents are of different length."); 75 76 76 if (n .Value> parent1.Length)77 if (n > parent1.Length) 77 78 throw new ArgumentException("NPointCrossover: There cannot be more breakpoints than the size of the parents."); 78 79 79 if (n .Value< 1)80 if (n < 1) 80 81 throw new ArgumentException("NPointCrossover: N cannot be < 1."); 81 82 82 83 int length = parent1.Length; 83 84 bool[] result = new bool[length]; 84 int[] breakpoints = new int[n .Value];85 int[] breakpoints = new int[n]; 85 86 86 87 //choose break points … … 90 91 breakpointPool.Add(i); 91 92 92 for (int i = 0; i < n .Value; i++) {93 for (int i = 0; i < n; i++) { 93 94 int index = random.Next(breakpointPool.Count); 94 95 breakpoints[i] = breakpointPool[index]; … … 137 138 if (NParameter.ActualValue == null) throw new InvalidOperationException("NPointCrossover: Parameter " + NParameter.ActualName + " could not be found."); 138 139 139 return Apply(random, parents[0], parents[1], NParameter.ActualValue); 140 return Apply(random, parents[0], parents[1], NParameter.ActualValue.Value); 141 } 142 } 143 144 [Item("N-point Crossover", "", ExcludeGenericTypeInfo = true)] 145 [StorableClass] 146 public sealed class NPointCrossover<TContext> : ParameterizedNamedItem, IBinaryCrossover<TContext> 147 where TContext : IMatingContext<BinaryVector>, IStochasticContext { 148 149 [Storable] 150 private IValueParameter<IntValue> nparameter; 151 public int N { 152 get { return nparameter.Value.Value; } 153 set { 154 if (value < 1) throw new ArgumentException("Cannot set N to less than 1."); 155 nparameter.Value.Value = value; 156 } 157 } 158 159 [StorableConstructor] 160 private NPointCrossover(bool deserializing) : base(deserializing) { } 161 private NPointCrossover(NPointCrossover<TContext> original, Cloner cloner) 162 : base(original, cloner) { 163 nparameter = cloner.Clone(original.nparameter); 164 } 165 public NPointCrossover() { 166 Parameters.Add(nparameter = new ValueParameter<IntValue>("N", "The number of crossover points.", new IntValue(1))); 167 } 168 169 170 public override IDeepCloneable Clone(Cloner cloner) { 171 return new NPointCrossover<TContext>(this, cloner); 172 } 173 174 public void Cross(TContext context) { 175 context.Child.Solution = NPointCrossover.Apply(context.Random, context.Parents.Item1.Solution, context.Parents.Item2.Solution, N); 140 176 } 141 177 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers/SinglePointCrossover.cs
r12012 r14429 57 57 if (parents.Length != 2) throw new ArgumentException("ERROR in SinglePointCrossover: The number of parents is not equal to 2"); 58 58 59 return NPointCrossover.Apply(random, parents[0], parents[1], new IntValue(1));59 return NPointCrossover.Apply(random, parents[0], parents[1], 1); 60 60 } 61 61 }
Note: See TracChangeset
for help on using the changeset viewer.