Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/PlushCreator.cs @ 17021

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

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

File size: 4.8 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Problems.ProgramSynthesis {
11  [Item("PlushCreator", "An operator which creates a new random plush vector with each element uniformly distributed")]
12  [StorableClass]
13  public class PlushCreator : InstrumentedOperator, IPlushCreator, IStochasticOperator {
14
15    public PlushCreator() {
16      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
17      Parameters.Add(new LookupParameter<PlushVector>("PlushVector", "The vector which should be manipulated."));
18      Parameters.Add(new ValueLookupParameter<IntValue>("MinLength", "The min length of the vector."));
19      Parameters.Add(new ValueLookupParameter<IntValue>("MaxLength", "The max length of the vector."));
20      Parameters.Add(new ValueLookupParameter<IntValue>("MaxClose", "The max close variable for a plush genome entry."));
21      Parameters.Add(new ValueLookupParameter<DoubleValue>("CloseBiasLevel", "Determines how strongly the random close variable is biased towards 0."));
22      Parameters.Add(new ValueLookupParameter<IReadOnlyExpressionsConfiguration>("Instructions", "The enabled instructions"));
23      Parameters.Add(new ValueLookupParameter<IReadOnlyErcOptions>("ErcOptions", "ERC options"));
24      Parameters.Add(new ValueLookupParameter<PercentValue>("InInstructionProbability", "The probability of IN Instructions"));
25    }
26
27    [StorableConstructor]
28    public PlushCreator(bool deserializing) : base(deserializing) { }
29
30    public PlushCreator(PlushCreator origin, Cloner cloner) : base(origin, cloner) { }
31
32    public override bool CanChangeName {
33      get { return false; }
34    }
35    public ILookupParameter<IRandom> RandomParameter {
36      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
37    }
38    public ILookupParameter<PlushVector> PlushVectorParameter {
39      get { return (ILookupParameter<PlushVector>)Parameters["PlushVector"]; }
40    }
41    public IValueLookupParameter<IntValue> MinLengthParameter {
42      get { return (IValueLookupParameter<IntValue>)Parameters["MinLength"]; }
43    }
44    public IValueLookupParameter<IntValue> MaxLengthParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["MaxLength"]; }
46    }
47    public IValueLookupParameter<IReadOnlyExpressionsConfiguration> InstructionsParameter {
48      get { return (IValueLookupParameter<IReadOnlyExpressionsConfiguration>)Parameters["Instructions"]; }
49    }
50    public IValueLookupParameter<IReadOnlyErcOptions> ErcOptionsParameter {
51      get { return (IValueLookupParameter<IReadOnlyErcOptions>)Parameters["ErcOptions"]; }
52    }
53    public IValueLookupParameter<PercentValue> InInstructionProbabilityParameter {
54      get { return (IValueLookupParameter<PercentValue>)Parameters["InInstructionProbability"]; }
55    }
56    public IValueLookupParameter<IntValue> MaxCloseParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters["MaxClose"]; }
58    }
59    public IValueLookupParameter<DoubleValue> CloseBiasLevelParameter {
60      get { return (IValueLookupParameter<DoubleValue>)Parameters["CloseBiasLevel"]; }
61    }
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new PlushCreator(this, cloner);
64    }
65
66    public sealed override IOperation InstrumentedApply() {
67      PlushVectorParameter.ActualValue = Create();
68      return base.InstrumentedApply();
69    }
70
71    private PlushVector Create() {
72      var random = RandomParameter.ActualValue;
73      var minLength = MinLengthParameter.ActualValue.Value;
74      var maxLength = MaxLengthParameter.ActualValue.Value;
75      var ercOptions = ErcOptionsParameter.ActualValue;
76      var instructions = InstructionsParameter.ActualValue;
77      var inInstructionProbability = InInstructionProbabilityParameter.ActualValue.Value;
78      var maxClose = MaxCloseParameter.ActualValue.Value;
79      var biasLevel = CloseBiasLevelParameter.ActualValue.Value;
80
81      if (minLength > maxLength)
82        throw new InvalidOperationException("MinLength > MaxLength");
83
84      var length = random.Next(minLength, maxLength);
85      var result = new PlushVector(length);
86
87      for (var i = 0; i < length; i++) {
88        var expression = CodeGeneratorUtils.GetRandomExpression(
89          random,
90          ercOptions,
91          instructions,
92          inInstructionProbability);
93
94        var close = maxClose == 0 ? 1 : random.NextBiased(0, maxClose, biasLevel);
95
96        result.Add(new PlushEntry {
97          Close = close,
98          Instruction = expression,
99        });
100      }
101
102      return result;
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.