Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.RealVectorEncoding/3.3/Crossovers/CopyCrossover.cs

Last change on this file was 18220, checked in by gkronber, 3 years ago

#3136: reintegrated structure-template GP branch into trunk

File size: 1.8 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4
5namespace HeuristicLab.Encodings.RealVectorEncoding {
6  [Item("CopyCrossover", "This operator creates an offspring by creating a clone of a randomly chosen parent. It can be used in situations where no crossover should occur after selection.")]
7  [StorableType("4F639A12-9D80-457C-B098-19970BC37560")]
8  public class CopyCrossover : RealVectorCrossover {
9    [StorableConstructor]
10    protected CopyCrossover(StorableConstructorFlag _) : base(_) { }
11    protected CopyCrossover(CopyCrossover original, Cloner cloner) : base(original, cloner) { }
12    public CopyCrossover() : base() { }
13
14    public override IDeepCloneable Clone(Cloner cloner) {
15      return new CopyCrossover(this, cloner);
16    }
17
18    /// <summary>
19    /// Performs the copy crossover.
20    /// </summary>
21    /// <remarks>
22    /// There can be more than two parents.
23    /// </remarks>
24    /// <param name="random">The random number generator.</param>
25    /// <param name="parents">The list of parents.</param>
26    /// <returns>The child vector, a copy of one of the parents.</returns>
27    public static RealVector Apply(IRandom random, ItemArray<RealVector> parents) {
28      int index = random.Next(parents.Length);
29      var selectedParent = parents[index];
30      var result = (RealVector)selectedParent.Clone();
31
32      return result;
33    }
34
35    /// <summary>
36    /// Forwards the call to <see cref="Apply(IRandom, ItemArray<RealVector>)"/>.
37    /// </summary>
38    /// <param name="random">The random number generator.</param>
39    /// <param name="parents">The list of parents.</param>
40    /// <returns>The child vector, a copy of one of the parents.</returns>
41    protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
42      return Apply(random, parents);
43    }
44  }
45}
46
Note: See TracBrowser for help on using the repository browser.