Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/PushSolutionCreator.cs @ 17104

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

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

File size: 5.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator {
2
3  using HeuristicLab.Common;
4  using HeuristicLab.Core;
5  using HeuristicLab.Data;
6  using HeuristicLab.Encodings.IntegerVectorEncoding;
7  using HeuristicLab.Parameters;
8  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
10  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
11
12  /// <summary>
13  /// Generates a new random integer vector with each element uniformly distributed in a specified range.
14  /// </summary>
15  [Item("PushSolutionCreator", "An operator which creates a new random int vector with each element uniformly distributed in a specified range and considering Push specific details")]
16  [StorableClass]
17  public class PushSolutionCreator : UniformRandomIntegerVectorCreator {
18
19    private const string ERC_OPTIONS_PARAMETER_NAME = "ERC options";
20    private const string MIN_LENGTH_PARAMETER_NAME = "Min. Length";
21    private const string MAX_LENGTH_PARAMETER_NAME = "Max. Length";
22    private const string IN_INSTRUCTION_PROBABILITY = "IN Instruction Probability";
23
24    public PushSolutionCreator() {
25      if (!Parameters.ContainsKey(ERC_OPTIONS_PARAMETER_NAME))
26        Parameters.Add(new ValueParameter<ErcOptions>(ERC_OPTIONS_PARAMETER_NAME));
27
28      if (!Parameters.ContainsKey(MIN_LENGTH_PARAMETER_NAME))
29        Parameters.Add(new FixedValueParameter<IntValue>(MIN_LENGTH_PARAMETER_NAME, new IntValue(20)));
30
31      //if (!Parameters.ContainsKey(MAX_LENGTH_PARAMETER_NAME))
32      //  Parameters.Add(new FixedValueParameter<IntValue>(MAX_LENGTH_PARAMETER_NAME, new IntValue(100)));
33
34      if (!Parameters.ContainsKey(IN_INSTRUCTION_PROBABILITY))
35        Parameters.Add(new FixedValueParameter<PercentValue>(IN_INSTRUCTION_PROBABILITY, new PercentValue(0.05)));
36
37      Parameters.Add(new ValueLookupParameter<IReadOnlyExpressionsConfiguration>("Instructions", "The enabled instructions"));
38    }
39
40    [StorableConstructor]
41    public PushSolutionCreator(bool deserializing) : base(deserializing) {
42    }
43
44    public PushSolutionCreator(PushSolutionCreator origin, Cloner cloner) : base(origin, cloner) { }
45
46
47    public IValueParameter<ErcOptions> ErcOptionsParameter
48    {
49      get { return (IValueParameter<ErcOptions>)Parameters[ERC_OPTIONS_PARAMETER_NAME]; }
50    }
51
52    public ErcOptions ErcOptions
53    {
54      get { return ErcOptionsParameter.Value; }
55      set
56      {
57        ErcOptionsParameter.Value = value;
58      }
59    }
60
61    public IValueParameter<IntValue> MinLengthParameter
62    {
63      get { return (IValueParameter<IntValue>)Parameters[MIN_LENGTH_PARAMETER_NAME]; }
64    }
65
66    public int MinLength
67    {
68      get { return MinLengthParameter.Value.Value; }
69      set { MinLengthParameter.Value.Value = value; }
70    }
71
72    public IValueLookupParameter<IReadOnlyExpressionsConfiguration> InstructionsParameter
73    {
74      get { return (IValueLookupParameter<IReadOnlyExpressionsConfiguration>)Parameters["Instructions"]; }
75    }
76
77    //public IValueParameter<IntValue> MaxLengthParameter
78    //{
79    //  get { return LengthParameter; } //(IValueParameter<IntValue>)Parameters[MAX_LENGTH_PARAMETER_NAME]; }
80    //}
81
82    //public int MaxLength
83    //{
84    //  get { return MaxLengthParameter.Value.Value; }
85    //  set { MaxLengthParameter.Value.Value = value; }
86    //}
87
88    public IValueParameter<PercentValue> InInstructionProbabilityParameter
89    {
90      get { return (IValueParameter<PercentValue>)Parameters[IN_INSTRUCTION_PROBABILITY]; }
91    }
92
93    public double InInstructionProbability
94    {
95      get { return InInstructionProbabilityParameter.Value.Value; }
96      set { InInstructionProbabilityParameter.Value.Value = value; }
97    }
98
99    protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) {
100      var len = length.Value;
101      var result = new IntegerVector(len);
102
103      var contentLength = random.Next(MinLength, len);
104      var lowerBound = bounds[0, 0];
105      var upperBound = bounds[0, 1];
106      //var inInstructionRelativeProbability = ErcOptions.ErcProbability + InInstructionProbability;
107      //var instructions = InstructionsParameter.ActualValue;
108
109      for (var i = 0; i < contentLength; i++) {
110        //var x = random.NextDouble();
111
112        //if (ErcOptions.ErcProbability > 0 && x <= ErcOptions.ErcProbability) {
113        //  result[i] = PushSolutionEncoding.Erc;
114        //} else if (InInstructionProbability > 0 && x <= inInstructionRelativeProbability && instructions.InExpressionCount > 0) {
115        //  var index = random.Next(0, instructions.InExpressionCount);
116        //  var expression = ExpressionTable.InExpressionTable[index];
117
118        //  instructions.EnabledExpressions
119        //  //result[i] = PushSolutionEncoding.In;
120        //} else {
121        result[i] = random.Next(lowerBound, upperBound);
122        //}
123      }
124
125      if (contentLength < len) {
126        result[contentLength] = PushSolutionEncoding.End;
127
128        for (var i = contentLength + 1; i < len; i++) {
129          result[i] = PushSolutionEncoding.Noop;
130        }
131      }
132
133      return result;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.