Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushEncoding.cs @ 15820

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

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

File size: 15.2 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis {
4  using System;
5  using System.Linq;
6
7  using HeuristicLab.Common;
8  using HeuristicLab.Core;
9  using HeuristicLab.Data;
10  using HeuristicLab.Optimization;
11  using HeuristicLab.Parameters;
12  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
13  using HeuristicLab.PluginInfrastructure;
14
15  [Item("PlushEncoding", "Describes an linear push (Plush) encoding.")]
16  [StorableClass]
17  public class PlushEncoding : Encoding<IPlushCreator> {
18
19    public PlushEncoding() : this("Plush") { }
20
21    public PlushEncoding(string name) : base(name) {
22      InitParameters();
23
24      SolutionCreator = new PlushCreator();
25      RegisterParameterEvents();
26      DiscoverOperators();
27    }
28
29    [StorableConstructor]
30    public PlushEncoding(bool deserializing)
31      : base(deserializing) {
32    }
33
34    public PlushEncoding(PlushEncoding original, Cloner cloner)
35      : base(original, cloner) {
36      minLengthParameter = cloner.Clone(original.minLengthParameter);
37      maxLengthParameter = cloner.Clone(original.maxLengthParameter);
38      maxCloseParameter = cloner.Clone(original.maxCloseParameter);
39      closeBiasLevelParameter = cloner.Clone(original.closeBiasLevelParameter);
40      instructionsParameter = cloner.Clone(original.instructionsParameter);
41      ercOptionsParameter = cloner.Clone(original.ercOptionsParameter);
42      inInstructionProbabilityParameter = cloner.Clone(original.inInstructionProbabilityParameter);
43
44      RegisterParameterEvents();
45    }
46
47    [StorableHook(HookType.AfterDeserialization)]
48    private void AfterDeserialization() {
49      InitParameters();
50      RegisterParameterEvents();
51      DiscoverOperators();
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new PlushEncoding(this, cloner);
56    }
57
58    private void InitParameters() {
59      if (!Parameters.ContainsKey(Name + ".MinLength")) {
60        minLengthParameter = new FixedValueParameter<IntValue>(Name + ".MinLength", new IntValue(0));
61        Parameters.Add(minLengthParameter);
62      }
63
64      if (!Parameters.ContainsKey(Name + ".MaxLength")) {
65        maxLengthParameter = new FixedValueParameter<IntValue>(Name + ".MaxLength", new IntValue(100));
66        Parameters.Add(maxLengthParameter);
67      }
68
69      if (!Parameters.ContainsKey(Name + ".MaxClose")) {
70        maxCloseParameter = new FixedValueParameter<IntValue>(Name + ".MaxClose", new IntValue(3));
71        Parameters.Add(maxCloseParameter);
72      }
73
74      if (!Parameters.ContainsKey(Name + ".CloseBiasLevel")) {
75        closeBiasLevelParameter = new FixedValueParameter<DoubleValue>(Name + ".CloseBiasLevel", new DoubleValue(3d));
76        Parameters.Add(closeBiasLevelParameter);
77      }
78
79      if (!Parameters.ContainsKey(Name + ".Instructions")) {
80        instructionsParameter = new ValueParameter<IExpressionsConfiguration>(Name + ".Instructions");
81        Parameters.Add(instructionsParameter);
82      }
83
84      if (!Parameters.ContainsKey(Name + ".ErcOptions")) {
85        ercOptionsParameter = new ValueParameter<ErcOptions>(Name + ".ErcOptions");
86        Parameters.Add(ercOptionsParameter);
87      }
88
89      if (!Parameters.ContainsKey(Name + ".InInstructionProbability")) {
90        inInstructionProbabilityParameter = new FixedValueParameter<PercentValue>(Name + ".InInstructionProbability", new PercentValue(0.1));
91        Parameters.Add(inInstructionProbabilityParameter);
92      }
93    }
94
95    #region events
96
97    private void OnMinLengthParameterChanged() {
98      RegisterMinLengthParameterEvents();
99      ConfigureOperators(Operators);
100    }
101    private void OnMaxLengthParameterChanged() {
102      RegisterMaxLengthParameterEvents();
103      ConfigureOperators(Operators);
104    }
105    private void OnMaxCloseParameterChanged() {
106      RegisterMaxCloseParameterEvents();
107      ConfigureOperators(Operators);
108    }
109    private void OnCloseBiasLevelParameterChanged() {
110      RegisterCloseBiasLevelParameterEvents();
111      ConfigureOperators(Operators);
112    }
113    private void OnErcOptionsParameterChanged() {
114      RegisterErcOptionsParameterEvents();
115      ConfigureOperators(Operators);
116    }
117    private void OnInstructionsParameterChanged() {
118      RegisterInstructionsParameterEvents();
119      ConfigureOperators(Operators);
120    }
121    private void OnInInstructionProbabilityParameterChanged() {
122      RegisterInInstructionProbabilityParameterEvents();
123      ConfigureOperators(Operators);
124    }
125
126    private void RegisterParameterEvents() {
127      RegisterMinLengthParameterEvents();
128      RegisterMaxLengthParameterEvents();
129      RegisterMaxCloseParameterEvents();
130      RegisterCloseBiasLevelParameterEvents();
131      RegisterErcOptionsParameterEvents();
132      RegisterInstructionsParameterEvents();
133      RegisterInInstructionProbabilityParameterEvents();
134    }
135
136    private void RegisterMinLengthParameterEvents() {
137      MinLengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
138      MinLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
139    }
140
141    private void RegisterMaxLengthParameterEvents() {
142      MaxLengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
143      MaxLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
144    }
145
146    private void RegisterMaxCloseParameterEvents() {
147      MaxCloseParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
148      MaxCloseParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
149    }
150
151    private void RegisterCloseBiasLevelParameterEvents() {
152      CloseBiasLevelParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
153      CloseBiasLevelParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
154    }
155
156    private void RegisterInInstructionProbabilityParameterEvents() {
157      InInstructionProbabilityParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
158      InInstructionProbabilityParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
159    }
160
161    private void RegisterErcOptionsParameterEvents() {
162      ErcOptionsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
163    }
164
165    private void RegisterInstructionsParameterEvents() {
166      InstructionsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
167    }
168
169    #endregion
170
171    #region Encoding Parameters
172    [Storable]
173    private IValueParameter<IntValue> minLengthParameter;
174    public IValueParameter<IntValue> MinLengthParameter {
175      get { return minLengthParameter; }
176      set {
177        if (value == null) throw new ArgumentNullException("Min length parameter must not be null.");
178        if (value.Value == null) throw new ArgumentNullException("Min length parameter value must not be null.");
179        if (minLengthParameter == value) return;
180
181        if (minLengthParameter != null) Parameters.Remove(minLengthParameter);
182        minLengthParameter = value;
183        Parameters.Add(minLengthParameter);
184        OnMinLengthParameterChanged();
185      }
186    }
187
188    [Storable]
189    private IValueParameter<IntValue> maxLengthParameter;
190    public IValueParameter<IntValue> MaxLengthParameter {
191      get { return maxLengthParameter; }
192      set {
193        if (value == null) throw new ArgumentNullException("Max length parameter must not be null.");
194        if (value.Value == null) throw new ArgumentNullException("Max length parameter value must not be null.");
195        if (maxLengthParameter == value) return;
196
197        if (maxLengthParameter != null) Parameters.Remove(maxLengthParameter);
198        maxLengthParameter = value;
199        Parameters.Add(maxLengthParameter);
200        OnMaxLengthParameterChanged();
201      }
202    }
203
204    [Storable]
205    private IValueParameter<IntValue> maxCloseParameter;
206    public IValueParameter<IntValue> MaxCloseParameter {
207      get { return maxCloseParameter; }
208      set {
209        if (value == null) throw new ArgumentNullException("Max close parameter must not be null.");
210        if (value.Value == null) throw new ArgumentNullException("Max close parameter value must not be null.");
211        if (maxCloseParameter == value) return;
212
213        if (maxCloseParameter != null) Parameters.Remove(maxCloseParameter);
214        maxCloseParameter = value;
215        Parameters.Add(maxCloseParameter);
216        OnMaxCloseParameterChanged();
217      }
218    }
219
220    [Storable]
221    private IValueParameter<DoubleValue> closeBiasLevelParameter;
222    public IValueParameter<DoubleValue> CloseBiasLevelParameter {
223      get { return closeBiasLevelParameter; }
224      set {
225        if (value == null) throw new ArgumentNullException("Close bias level parameter must not be null.");
226        if (value.Value == null) throw new ArgumentNullException("Close bias level parameter value must not be null.");
227        if (closeBiasLevelParameter == value) return;
228
229        if (closeBiasLevelParameter != null) Parameters.Remove(closeBiasLevelParameter);
230        closeBiasLevelParameter = value;
231        Parameters.Add(closeBiasLevelParameter);
232        OnCloseBiasLevelParameterChanged();
233      }
234    }
235
236    [Storable]
237    private IValueParameter<PercentValue> inInstructionProbabilityParameter;
238    public IValueParameter<PercentValue> InInstructionProbabilityParameter {
239      get { return inInstructionProbabilityParameter; }
240      set {
241        if (value == null) throw new ArgumentNullException("In instruciton probability parameter must not be null.");
242        if (value.Value == null) throw new ArgumentNullException("In instruciton probability parameter value must not be null.");
243        if (inInstructionProbabilityParameter == value) return;
244
245        if (inInstructionProbabilityParameter != null) Parameters.Remove(inInstructionProbabilityParameter);
246        inInstructionProbabilityParameter = value;
247        Parameters.Add(inInstructionProbabilityParameter);
248        OnInInstructionProbabilityParameterChanged();
249      }
250    }
251
252    [Storable]
253    private IValueParameter<IExpressionsConfiguration> instructionsParameter;
254    public IValueParameter<IExpressionsConfiguration> InstructionsParameter {
255      get { return instructionsParameter; }
256      set {
257        if (value == null) throw new ArgumentNullException("Instructions paramter must not be null");
258        if (instructionsParameter == value) return;
259
260        if (instructionsParameter != null) Parameters.Remove(instructionsParameter);
261        instructionsParameter = value;
262        Parameters.Add(instructionsParameter);
263        OnInstructionsParameterChanged();
264      }
265    }
266
267    [Storable]
268    private IValueParameter<ErcOptions> ercOptionsParameter;
269    public IValueParameter<ErcOptions> ErcOptionsParameter {
270      get { return ercOptionsParameter; }
271      set {
272        if (value == null) throw new ArgumentNullException("ErcOptions paramter must not be null");
273        if (ercOptionsParameter == value) return;
274
275        if (ercOptionsParameter != null) Parameters.Remove(ercOptionsParameter);
276        ercOptionsParameter = value;
277        Parameters.Add(ercOptionsParameter);
278        OnErcOptionsParameterChanged();
279      }
280    }
281
282    public IExpressionsConfiguration Instructions {
283      get { return InstructionsParameter.Value; }
284      set { InstructionsParameter.Value = value; }
285    }
286
287    public ErcOptions ErcOptions {
288      get { return ErcOptionsParameter.Value; }
289      set { ErcOptionsParameter.Value = value; }
290    }
291
292    public int MinLength {
293      get { return MinLengthParameter.Value.Value; }
294      set { MinLengthParameter.Value.Value = value; }
295    }
296
297    public int MaxLength {
298      get { return MaxLengthParameter.Value.Value; }
299      set { MaxLengthParameter.Value.Value = value; }
300    }
301
302    public int MaxClose {
303      get { return MaxCloseParameter.Value.Value; }
304      set { MaxCloseParameter.Value.Value = value; }
305    }
306
307    public double InInstructionProbability {
308      get { return InInstructionProbabilityParameter.Value.Value; }
309      set { InInstructionProbabilityParameter.Value.Value = value; }
310    }
311    #endregion
312
313    #region Operator Discovery
314    private static readonly IEnumerable<Type> EncodingSpecificOperatorTypes;
315    static PlushEncoding() {
316      EncodingSpecificOperatorTypes = new List<Type>() {
317        typeof (IPlushOperator),
318        typeof (IPlushCreator),
319        typeof (IPlushCrossover),
320        typeof (IPlushManipulator),
321      };
322    }
323
324    private void DiscoverOperators() {
325      var assembly = typeof(IPlushOperator).Assembly;
326      var discoveredTypes = ApplicationManager.Manager.GetTypes(EncodingSpecificOperatorTypes, assembly, true, false, false);
327      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
328      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
329
330      ConfigureOperators(newOperators);
331      foreach (var @operator in newOperators)
332        AddOperator(@operator);
333    }
334    #endregion
335
336    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
337      ConfigureCreators(operators.OfType<IPlushCreator>());
338      ConfigureCrossovers(operators.OfType<IPlushCrossover>());
339      ConfigureManipulators(operators.OfType<IPlushManipulator>());
340    }
341
342    private void ConfigureCreators(IEnumerable<IPlushCreator> creators) {
343      foreach (var creator in creators) {
344        creator.PlushVectorParameter.ActualName = Name;
345        creator.MinLengthParameter.ActualName = MinLengthParameter.Name;
346        creator.MaxLengthParameter.ActualName = MaxLengthParameter.Name;
347        creator.MaxCloseParameter.ActualName = MaxCloseParameter.Name;
348        creator.CloseBiasLevelParameter.ActualName = CloseBiasLevelParameter.Name;
349        creator.ErcOptionsParameter.ActualName = ErcOptionsParameter.Name;
350        creator.InstructionsParameter.ActualName = InstructionsParameter.Name;
351        creator.InInstructionProbabilityParameter.ActualName = InInstructionProbabilityParameter.Name;
352      }
353    }
354
355    private void ConfigureCrossovers(IEnumerable<IPlushCrossover> crossovers) {
356      foreach (var crossover in crossovers) {
357        crossover.ChildParameter.ActualName = Name;
358        crossover.ParentsParameter.ActualName = Name;
359      }
360    }
361
362    private void ConfigureManipulators(IEnumerable<IPlushManipulator> manipulators) {
363      foreach (var manipulator in manipulators) {
364        manipulator.PlushVectorParameter.ActualName = Name;
365        manipulator.PlushVectorParameter.Hidden = true;
366
367        manipulator.ErcOptionsParameter.ActualName = ErcOptionsParameter.Name;
368        manipulator.InstructionsParameter.ActualName = InstructionsParameter.Name;
369        manipulator.InInstructionProbabilityParameter.ActualName = InInstructionProbabilityParameter.Name;
370      }
371    }
372  }
373
374  public static class IndividualExtensionMethods {
375    public static PlushVector PlushVector(this Individual individual) {
376      var encoding = individual.GetEncoding<PlushEncoding>();
377      return individual.PlushVector(encoding.Name);
378    }
379
380    public static PlushVector PlushVector(this Individual individual, string name) {
381      return (PlushVector)individual[name];
382    }
383  }
384}
Note: See TracBrowser for help on using the repository browser.