Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2605_StackingProblems/HeuristicLab.Encodings.MoveVectorEncoding/3.3/Crossovers/SinglePointCrossover.cs @ 17800

Last change on this file since 17800 was 14278, checked in by mzehetho, 8 years ago

Initial Commit for ticket #2605

Implemented:

  • Encoding
  • Moves
  • Views
  • Blocks World Problem
  • Blocks Relocation Problem
  • Stacking Problem
File size: 1.8 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data.MoveVectorData;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5using System;
6
7namespace 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}
Note: See TracBrowser for help on using the repository browser.