Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/PushSolutionCreator.cs @ 18183

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

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

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