Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/PlushCreator.cs @ 15334

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

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

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