[15771] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[15275] | 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."));
|
---|
[15289] | 24 |
|
---|
[15275] | 25 | Parameters.Add(new ScopeTreeLookupParameter<PlushVector>("Parents", "The parent vectors which should be crossed."));
|
---|
| 26 | ParentsParameter.ActualName = "PlushVector";
|
---|
[15289] | 27 |
|
---|
[15275] | 28 | Parameters.Add(new LookupParameter<PlushVector>("Child", "The child vector resulting from the crossover."));
|
---|
| 29 | ChildParameter.ActualName = "PlushVector";
|
---|
[15289] | 30 |
|
---|
[15334] | 31 | Parameters.Add(new LookupParameter<IntValue>("MaxProgramLength", "The max length of children"));
|
---|
| 32 |
|
---|
[15275] | 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)));
|
---|
[15334] | 35 |
|
---|
[15275] | 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 |
|
---|
[15771] | 49 | public ILookupParameter<IntValue> MaxProgramLengthParameter {
|
---|
[15334] | 50 | get { return (ILookupParameter<IntValue>)Parameters["MaxProgramLength"]; }
|
---|
[15275] | 51 | }
|
---|
| 52 |
|
---|
[15771] | 53 | public int MaxProgramLength {
|
---|
[15334] | 54 | get { return MaxProgramLengthParameter.ActualValue.Value; }
|
---|
| 55 | set { MaxProgramLengthParameter.ActualValue.Value = value; }
|
---|
[15275] | 56 | }
|
---|
| 57 |
|
---|
[15771] | 58 | public IValueParameter<PercentValue> AlternationRateParameter {
|
---|
[15275] | 59 | get { return (IValueParameter<PercentValue>)Parameters["AlternationRate"]; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[15771] | 62 | public double AlternationRate {
|
---|
[15275] | 63 | get { return AlternationRateParameter.Value.Value; }
|
---|
| 64 | set { AlternationRateParameter.Value.Value = value; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[15771] | 67 | public IValueParameter<DoubleValue> AlignmentDeviationParameter {
|
---|
[15275] | 68 | get { return (IValueParameter<DoubleValue>)Parameters["AlignmentDeviation"]; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[15771] | 71 | public double AlignmentDeviation {
|
---|
[15275] | 72 | get { return AlignmentDeviationParameter.Value.Value; }
|
---|
| 73 | set { AlignmentDeviationParameter.Value.Value = value; }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[15771] | 76 | public ILookupParameter<IRandom> RandomParameter {
|
---|
[15275] | 77 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 78 | }
|
---|
[15771] | 79 | public ILookupParameter<ItemArray<PlushVector>> ParentsParameter {
|
---|
[15275] | 80 | get { return (ScopeTreeLookupParameter<PlushVector>)Parameters["Parents"]; }
|
---|
| 81 | }
|
---|
[15771] | 82 | public ILookupParameter<PlushVector> ChildParameter {
|
---|
[15275] | 83 | get { return (ILookupParameter<PlushVector>)Parameters["Child"]; }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public sealed override IOperation InstrumentedApply() {
|
---|
[15289] | 87 | ChildParameter.ActualValue = Cross(
|
---|
| 88 | RandomParameter.ActualValue,
|
---|
| 89 | ParentsParameter.ActualValue,
|
---|
| 90 | AlternationRate,
|
---|
[15334] | 91 | MaxProgramLength,
|
---|
[15289] | 92 | AlignmentDeviation);
|
---|
[15275] | 93 | return base.InstrumentedApply();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[15289] | 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);
|
---|
[15275] | 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?
|
---|
[15289] | 115 | if (random.NextDouble() < alternationRate) {
|
---|
[15275] | 116 | parentIndex = parentIndex == 0 ? 1 : 0;
|
---|
| 117 | parent = parents[parentIndex];
|
---|
[15289] | 118 | i += normalDistributedRandom.NextRounded();
|
---|
[15275] | 119 | i = Math.Max(i, 0);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | return child;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|