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