Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Crossover/AlternationCrossover.cs @ 17839

Last change on this file since 17839 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 5.1 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Crossover {
2  using System;
3  using System.Linq;
4
5  using HeuristicLab.Common;
6  using HeuristicLab.Core;
7  using HeuristicLab.Data;
8  using HeuristicLab.Operators;
9  using HeuristicLab.Optimization;
10  using HeuristicLab.Parameters;
11  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12  using HeuristicLab.Problems.ProgramSynthesis.Base.Extensions;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding;
14  using HeuristicLab.Random;
15
16  /// <summary>
17  /// Alternation crossover for plush vectors.
18  /// </summary>
19  [Item("AlternationCrossover", "Alternation crossover for plush vectors.")]
20  [StorableClass]
21  public class AlternationCrossover : InstrumentedOperator, IPlushCrossover, IStochasticOperator {
22    private const double Mean = 0.0;
23
24    public AlternationCrossover() {
25      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
26
27      Parameters.Add(new ScopeTreeLookupParameter<PlushVector>("Parents", "The parent vectors which should be crossed."));
28      ParentsParameter.ActualName = "PlushVector";
29
30      Parameters.Add(new LookupParameter<PlushVector>("Child", "The child vector resulting from the crossover."));
31      ChildParameter.ActualName = "PlushVector";
32
33      Parameters.Add(new LookupParameter<IntValue>("MaxProgramLength", "The max length of children"));
34
35      Parameters.Add(new FixedValueParameter<PercentValue>("AlternationRate", "Specifies the probability of switching to another parent.", new PercentValue(0.5)));
36      Parameters.Add(new FixedValueParameter<DoubleValue>("AlignmentDeviation", "When alternating between parents, the index at which to continue copying may be offset backward or forward some amount based on a random sample from a normal distribution with mean 0 and standard deviation set by the alignment deviation parameter", new DoubleValue(1.0)));
37
38    }
39
40    [StorableConstructor]
41    public AlternationCrossover(bool deserializing) : base(deserializing) {
42    }
43
44    public AlternationCrossover(AlternationCrossover origin, Cloner cloner) : base(origin, cloner) {
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new AlternationCrossover(this, cloner);
49    }
50
51    public ILookupParameter<IntValue> MaxProgramLengthParameter
52    {
53      get { return (ILookupParameter<IntValue>)Parameters["MaxProgramLength"]; }
54    }
55
56    public int MaxProgramLength
57    {
58      get { return MaxProgramLengthParameter.ActualValue.Value; }
59      set { MaxProgramLengthParameter.ActualValue.Value = value; }
60    }
61
62    public IValueParameter<PercentValue> AlternationRateParameter
63    {
64      get { return (IValueParameter<PercentValue>)Parameters["AlternationRate"]; }
65    }
66
67    public double AlternationRate
68    {
69      get { return AlternationRateParameter.Value.Value; }
70      set { AlternationRateParameter.Value.Value = value; }
71    }
72
73    public IValueParameter<DoubleValue> AlignmentDeviationParameter
74    {
75      get { return (IValueParameter<DoubleValue>)Parameters["AlignmentDeviation"]; }
76    }
77
78    public double AlignmentDeviation
79    {
80      get { return AlignmentDeviationParameter.Value.Value; }
81      set { AlignmentDeviationParameter.Value.Value = value; }
82    }
83
84    public ILookupParameter<IRandom> RandomParameter
85    {
86      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
87    }
88    public ILookupParameter<ItemArray<PlushVector>> ParentsParameter
89    {
90      get { return (ScopeTreeLookupParameter<PlushVector>)Parameters["Parents"]; }
91    }
92    public ILookupParameter<PlushVector> ChildParameter
93    {
94      get { return (ILookupParameter<PlushVector>)Parameters["Child"]; }
95    }
96
97    public sealed override IOperation InstrumentedApply() {
98      ChildParameter.ActualValue = Cross(
99        RandomParameter.ActualValue,
100        ParentsParameter.ActualValue,
101        AlternationRate,
102        MaxProgramLength,
103        AlignmentDeviation);
104      return base.InstrumentedApply();
105    }
106
107    private static PlushVector Cross(
108      IRandom random,
109      ItemArray<PlushVector> parents,
110      double alternationRate,
111      int maxChildLength,
112      double alignmentDeviation) {
113      var normalDistributedRandom = new NormalDistributedRandom(random, Mean, alignmentDeviation);
114      var maxLength = parents.Max(p => p.Entries.Count);
115      var parentIndex = random.Next(0, 2);
116      var parent = parents[parentIndex];
117      var child = new PlushVector(maxLength);
118
119      for (var i = 0; i < maxLength && child.Entries.Count <= maxChildLength; i++) {
120
121        // if parent is shorter than the other, then ignore those entries
122        if (i < parent.Entries.Count)
123          child.Add(parent[i]);
124
125        // switch parent?
126        if (random.NextDouble() < alternationRate) {
127          parentIndex = parentIndex == 0 ? 1 : 0;
128          parent = parents[parentIndex];
129          i += normalDistributedRandom.NextRounded();
130          i = Math.Max(i, 0);
131        }
132      }
133
134      return child;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.