Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Crossover/AlternationCrossover.cs @ 15771

Last change on this file since 15771 was 15771, checked in by bburlacu, 6 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 4.9 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis {
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.Random;
13
14  /// <summary>
15  /// Alternation crossover for plush vectors.
16  /// </summary>
17  [Item("AlternationCrossover", "Alternation crossover for plush vectors.")]
18  [StorableClass]
19  public class AlternationCrossover : InstrumentedOperator, IPlushCrossover, IStochasticOperator {
20    private const double Mean = 0.0;
21
22    public AlternationCrossover() {
23      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
24
25      Parameters.Add(new ScopeTreeLookupParameter<PlushVector>("Parents", "The parent vectors which should be crossed."));
26      ParentsParameter.ActualName = "PlushVector";
27
28      Parameters.Add(new LookupParameter<PlushVector>("Child", "The child vector resulting from the crossover."));
29      ChildParameter.ActualName = "PlushVector";
30
31      Parameters.Add(new LookupParameter<IntValue>("MaxProgramLength", "The max length of children"));
32
33      Parameters.Add(new FixedValueParameter<PercentValue>("AlternationRate", "Specifies the probability of switching to another parent.", new PercentValue(0.5)));
34      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)));
35
36    }
37
38    [StorableConstructor]
39    public AlternationCrossover(bool deserializing) : base(deserializing) {
40    }
41
42    public AlternationCrossover(AlternationCrossover origin, Cloner cloner) : base(origin, cloner) {
43    }
44
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new AlternationCrossover(this, cloner);
47    }
48
49    public ILookupParameter<IntValue> MaxProgramLengthParameter {
50      get { return (ILookupParameter<IntValue>)Parameters["MaxProgramLength"]; }
51    }
52
53    public int MaxProgramLength {
54      get { return MaxProgramLengthParameter.ActualValue.Value; }
55      set { MaxProgramLengthParameter.ActualValue.Value = value; }
56    }
57
58    public IValueParameter<PercentValue> AlternationRateParameter {
59      get { return (IValueParameter<PercentValue>)Parameters["AlternationRate"]; }
60    }
61
62    public double AlternationRate {
63      get { return AlternationRateParameter.Value.Value; }
64      set { AlternationRateParameter.Value.Value = value; }
65    }
66
67    public IValueParameter<DoubleValue> AlignmentDeviationParameter {
68      get { return (IValueParameter<DoubleValue>)Parameters["AlignmentDeviation"]; }
69    }
70
71    public double AlignmentDeviation {
72      get { return AlignmentDeviationParameter.Value.Value; }
73      set { AlignmentDeviationParameter.Value.Value = value; }
74    }
75
76    public ILookupParameter<IRandom> RandomParameter {
77      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
78    }
79    public ILookupParameter<ItemArray<PlushVector>> ParentsParameter {
80      get { return (ScopeTreeLookupParameter<PlushVector>)Parameters["Parents"]; }
81    }
82    public ILookupParameter<PlushVector> ChildParameter {
83      get { return (ILookupParameter<PlushVector>)Parameters["Child"]; }
84    }
85
86    public sealed override IOperation InstrumentedApply() {
87      ChildParameter.ActualValue = Cross(
88        RandomParameter.ActualValue,
89        ParentsParameter.ActualValue,
90        AlternationRate,
91        MaxProgramLength,
92        AlignmentDeviation);
93      return base.InstrumentedApply();
94    }
95
96    private static PlushVector Cross(
97      IRandom random,
98      ItemArray<PlushVector> parents,
99      double alternationRate,
100      int maxChildLength,
101      double alignmentDeviation) {
102      var normalDistributedRandom = new NormalDistributedRandom(random, Mean, alignmentDeviation);
103      var maxLength = parents.Max(p => p.Entries.Count);
104      var parentIndex = random.Next(0, 2);
105      var parent = parents[parentIndex];
106      var child = new PlushVector(maxLength);
107
108      for (var i = 0; i < maxLength && child.Entries.Count <= maxChildLength; i++) {
109
110        // if parent is shorter than the other, then ignore those entries
111        if (i < parent.Entries.Count)
112          child.Add(parent[i]);
113
114        // switch parent?
115        if (random.NextDouble() < alternationRate) {
116          parentIndex = parentIndex == 0 ? 1 : 0;
117          parent = parents[parentIndex];
118          i += normalDistributedRandom.NextRounded();
119          i = Math.Max(i, 0);
120        }
121      }
122
123      return child;
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.