Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushEncoding.cs @ 15692

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

#2665 Testet Problems, Improved Performance

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