[14278] | 1 | using HeuristicLab.Common;
|
---|
| 2 | using HeuristicLab.Core;
|
---|
| 3 | using HeuristicLab.Data.MoveVectorData;
|
---|
| 4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 5 | using System;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Encodings.MoveVectorEncoding.Crossovers
|
---|
| 8 | {
|
---|
| 9 | [Item("SinglePointCrossover", "Single point crossover for move vectors.")]
|
---|
| 10 | [StorableClass]
|
---|
| 11 | public class SinglePointCrossover : MoveVectorCrossover
|
---|
| 12 | {
|
---|
| 13 | [StorableConstructor]
|
---|
| 14 | protected SinglePointCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 15 | protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 16 | public SinglePointCrossover() : base() { }
|
---|
| 17 |
|
---|
| 18 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 19 | {
|
---|
| 20 | return new SinglePointCrossover(this, cloner);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public static MoveVector Apply(IRandom random, MoveVector parent1, MoveVector parent2)
|
---|
| 24 | {
|
---|
| 25 | if (parent1.Length != parent2.Length)
|
---|
| 26 | throw new ArgumentException("SinglePointCrossover: The parents are of different length.");
|
---|
| 27 |
|
---|
| 28 | int length = parent1.Length;
|
---|
| 29 | MoveVector result = new MoveVector(length, parent1.MoveTypes);
|
---|
| 30 | int breakPoint = random.Next(1, length);
|
---|
| 31 |
|
---|
| 32 | for (int i = 0; i < breakPoint; i++)
|
---|
| 33 | result[i] = parent1[i];
|
---|
| 34 | for (int i = breakPoint; i < length; i++)
|
---|
| 35 | result[i] = parent2[i];
|
---|
| 36 |
|
---|
| 37 | return result;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | protected override MoveVector Cross(IRandom random, ItemArray<MoveVector> parents)
|
---|
| 41 | {
|
---|
| 42 | if (parents.Length != 2) throw new ArgumentException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
|
---|
| 43 | return Apply(random, parents[0], parents[1]);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|