Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs @ 16947

Last change on this file since 16947 was 16947, checked in by mkommend, 5 years ago

#2521: Adapted GP basic problems to use read-only value parameters.

File size: 16.0 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using HEAL.Attic;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  [Item("SymbolicExpressionTreeEncoding", "Describes a symbolic expression tree encoding.")]
37  [StorableType("3FDE530B-771F-4280-921F-20DA50A139BA")]
38  public sealed class SymbolicExpressionTreeEncoding : Encoding<ISymbolicExpressionTree> {
39    #region Encoding Parameters
40    [Storable]
41    private IFixedValueParameter<IntValue> treeLengthParameter;
42    public IFixedValueParameter<IntValue> TreeLengthParameter {
43      get { return treeLengthParameter; }
44      set {
45        if (value == null) throw new ArgumentNullException("Tree length parameter must not be null.");
46        if (value.Value == null) throw new ArgumentNullException("Tree length parameter value must not be null.");
47        if (treeLengthParameter == value) return;
48
49        if (treeLengthParameter != null)
50          Parameters.Remove(treeLengthParameter);
51
52        treeLengthParameter = value;
53        Parameters.Add(treeLengthParameter);
54        OnLengthParameterChanged();
55      }
56    }
57
58    [Storable]
59    private IFixedValueParameter<IntValue> treeDepthParameter;
60    public IFixedValueParameter<IntValue> TreeDepthParameter {
61      get { return treeDepthParameter; }
62      set {
63        if (value == null) throw new ArgumentNullException("Tree length parameter must not be null.");
64        if (value.Value == null) throw new ArgumentNullException("Tree length parameter value must not be null.");
65        if (treeDepthParameter == value) return;
66
67        if (treeDepthParameter != null)
68          Parameters.Remove(treeDepthParameter);
69
70        treeDepthParameter = value;
71        Parameters.Add(treeDepthParameter);
72        OnDepthParameterChanged();
73      }
74    }
75
76    [Storable]
77    private IValueParameter<ISymbolicExpressionGrammar> grammarParameter;
78    public IValueParameter<ISymbolicExpressionGrammar> GrammarParameter {
79      get { return grammarParameter; }
80      set {
81        if (value == null) throw new ArgumentNullException("Grammar parameter must not be null.");
82        if (value.Value == null) throw new ArgumentNullException("Grammar parameter value must not be null.");
83        if (grammarParameter == value) return;
84
85        if (grammarParameter != null)
86          Parameters.Remove(grammarParameter);
87
88        grammarParameter = value;
89        Parameters.Add(grammarParameter);
90        OnGrammarParameterChanged();
91      }
92    }
93
94
95    [Storable]
96    private IFixedValueParameter<IntValue> functionDefinitionsParameter;
97    public IFixedValueParameter<IntValue> FunctionDefinitionsParameter {
98      get { return functionDefinitionsParameter; }
99      set {
100        if (value == null) throw new ArgumentNullException("Function definitions parameter must not be null.");
101        if (value.Value == null) throw new ArgumentNullException("Function definitions parameter value must not be null.");
102        if (functionDefinitionsParameter == value) return;
103
104        if (functionDefinitionsParameter != null)
105          Parameters.Remove(functionDefinitionsParameter);
106
107        functionDefinitionsParameter = value;
108        Parameters.Add(functionDefinitionsParameter);
109        OnFunctionDefinitionsParameterChanged();
110      }
111    }
112
113    [Storable]
114    private IFixedValueParameter<IntValue> functionArgumentsParameter;
115    public IFixedValueParameter<IntValue> FunctionArgumentsParameter {
116      get { return functionArgumentsParameter; }
117      set {
118        if (value == null) throw new ArgumentNullException("Function arguments parameter must not be null.");
119        if (value.Value == null) throw new ArgumentNullException("Function arguments parameter value must not be null.");
120        if (functionArgumentsParameter == value) return;
121
122        if (functionArgumentsParameter != null)
123          Parameters.Remove(functionArgumentsParameter);
124
125        functionArgumentsParameter = value;
126        Parameters.Add(functionArgumentsParameter);
127        OnFunctionArgumentsParameterChanged();
128      }
129    }
130    #endregion
131
132    #region Parameter properties
133    public int TreeLength {
134      get { return TreeLengthParameter.Value.Value; }
135      set { TreeLengthParameter.Value.Value = value; }
136    }
137
138    public int TreeDepth {
139      get { return TreeDepthParameter.Value.Value; }
140      set { TreeDepthParameter.Value.Value = value; }
141    }
142
143    public ISymbolicExpressionGrammar Grammar {
144      get { return GrammarParameter.Value; }
145      set { GrammarParameter.Value = value; }
146    }
147
148    public int FunctionDefinitions {
149      get { return FunctionDefinitionsParameter.Value.Value; }
150      set { FunctionDefinitionsParameter.Value.Value = value; }
151    }
152
153    public int FunctionArguments {
154      get { return FunctionArgumentsParameter.Value.Value; }
155      set { FunctionArgumentsParameter.Value.Value = value; }
156    }
157    #endregion
158
159    [StorableConstructor]
160    private SymbolicExpressionTreeEncoding(StorableConstructorFlag _) : base(_) { }
161    public SymbolicExpressionTreeEncoding() : this(new SimpleSymbolicExpressionGrammar()) { }
162    public SymbolicExpressionTreeEncoding(ISymbolicExpressionGrammar grammar) : this("SymbolicExpressionTree", grammar, 50, 50) { }
163    public SymbolicExpressionTreeEncoding(ISymbolicExpressionGrammar grammar, int maximumLength, int maximumDepth) : this("SymbolicExpressionTree", grammar, maximumLength, maximumDepth) { }
164    public SymbolicExpressionTreeEncoding(string name, ISymbolicExpressionGrammar grammar, int maximumLength, int maximumDepth)
165      : base(name) {
166      treeLengthParameter = new FixedValueParameter<IntValue>(Name + ".Maximum Tree Length", "Maximal length of the symbolic expression.", new IntValue(maximumLength));
167      treeDepthParameter = new FixedValueParameter<IntValue>(Name + ".Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(maximumDepth));
168      grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>(Name + ".Grammar", "The grammar that should be used for symbolic expression tree.", grammar);
169      grammarParameter.ReadOnly = true;
170      functionDefinitionsParameter = new FixedValueParameter<IntValue>(Name + ".Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));
171      functionArgumentsParameter = new FixedValueParameter<IntValue>(Name + ".Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));
172
173      Parameters.Add(treeLengthParameter);
174      Parameters.Add(treeDepthParameter);
175      Parameters.Add(grammarParameter);
176      Parameters.Add(functionDefinitionsParameter);
177      Parameters.Add(functionArgumentsParameter);
178
179      SolutionCreator = new ProbabilisticTreeCreator();
180      RegisterParameterEvents();
181      DiscoverOperators();
182    }
183
184    private SymbolicExpressionTreeEncoding(SymbolicExpressionTreeEncoding original, Cloner cloner)
185      : base(original, cloner) {
186      treeLengthParameter = cloner.Clone(original.TreeLengthParameter);
187      treeDepthParameter = cloner.Clone(original.TreeDepthParameter);
188      grammarParameter = cloner.Clone(original.GrammarParameter);
189      functionDefinitionsParameter = cloner.Clone(original.FunctionDefinitionsParameter);
190      functionArgumentsParameter = cloner.Clone(original.FunctionArgumentsParameter);
191      RegisterParameterEvents();
192    }
193
194    public override IDeepCloneable Clone(Cloner cloner) {
195      return new SymbolicExpressionTreeEncoding(this, cloner);
196    }
197
198    #region changed events
199    private void OnLengthParameterChanged() {
200      RegisterLengthParameterEvents();
201      ConfigureOperators(Operators);
202    }
203    private void OnDepthParameterChanged() {
204      RegisterDepthParameterEvents();
205      ConfigureOperators(Operators);
206    }
207    private void OnGrammarParameterChanged() {
208      RegisterGrammarParameterEvents();
209      FunctionArguments = Grammar.MaximumFunctionArguments;
210      FunctionDefinitions = Grammar.MaximumFunctionDefinitions;
211      ConfigureOperators(Operators);
212    }
213
214    private void OnFunctionDefinitionsParameterChanged() {
215      RegisterFunctionDefinitionsParameterEvents();
216      Grammar.MaximumFunctionDefinitions = FunctionDefinitions;
217    }
218
219    private void OnFunctionArgumentsParameterChanged() {
220      RegisterFunctionArgumentsParameterEvetns();
221      Grammar.MaximumFunctionArguments = FunctionArguments;
222    }
223
224    private void RegisterParameterEvents() {
225      RegisterLengthParameterEvents();
226      RegisterLengthParameterEvents();
227      RegisterGrammarParameterEvents();
228      RegisterFunctionDefinitionsParameterEvents();
229      RegisterFunctionArgumentsParameterEvetns();
230    }
231
232
233    private void RegisterLengthParameterEvents() { }
234    private void RegisterDepthParameterEvents() { }
235
236    private void RegisterGrammarParameterEvents() {
237      GrammarParameter.ValueChanged += (o, s) => {
238        FunctionArguments = Grammar.MaximumFunctionArguments;
239        FunctionDefinitions = Grammar.MaximumFunctionDefinitions;
240        ConfigureOperators(Operators);
241      };
242      GrammarParameter.Value.Changed += (o, s) => {
243        FunctionArguments = Grammar.MaximumFunctionArguments;
244        FunctionDefinitions = Grammar.MaximumFunctionDefinitions;
245        ConfigureOperators(Operators);
246      };
247    }
248
249    private void RegisterFunctionDefinitionsParameterEvents() {
250      FunctionDefinitionsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionDefinitions = FunctionDefinitions;
251    }
252    private void RegisterFunctionArgumentsParameterEvetns() {
253      FunctionArgumentsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionArguments = FunctionArguments;
254    }
255    #endregion
256
257    #region Operator discovery
258    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
259    static SymbolicExpressionTreeEncoding() {
260      encodingSpecificOperatorTypes = new List<Type>
261      {
262        typeof(ISymbolicExpressionTreeOperator),
263        typeof(ISymbolicExpressionTreeCreator),
264        typeof(ISymbolicExpressionTreeCrossover),
265        typeof(ISymbolicExpressionTreeManipulator),
266        typeof(ISymbolicExpressionTreeArchitectureAlteringOperator),
267        typeof(ISymbolicExpressionTreeAnalyzer),
268      };
269    }
270
271    private void DiscoverOperators() {
272      var assembly = typeof(ISymbolicExpressionTreeOperator).Assembly;
273      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
274      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
275      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
276
277      ConfigureOperators(newOperators);
278      foreach (var @operator in newOperators)
279        AddOperator(@operator);
280    }
281    #endregion
282
283    #region Specific operator wiring
284
285    public override void ConfigureOperators(IEnumerable<IItem> operators) {
286      ConfigureTreeOperators(operators.OfType<ISymbolicExpressionTreeOperator>());
287      ConfigureSizeConstraintOperators(operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>());
288      ConfigureGrammarBaseOperators(operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>());
289      ConfigureArchitectureAlteringOperators(operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>());
290
291      ConfigureAnalyzers(operators.OfType<ISymbolicExpressionTreeAnalyzer>());
292      ConfigureCreators(operators.OfType<ISymbolicExpressionTreeCreator>());
293      ConfigureCrossovers(operators.OfType<ISymbolicExpressionTreeCrossover>());
294      ConfigureManipulators(operators.OfType<ISymbolicExpressionTreeManipulator>());
295    }
296
297    private void ConfigureTreeOperators(IEnumerable<ISymbolicExpressionTreeOperator> treeOperators) {
298      foreach (var treeOperator in treeOperators) {
299        treeOperator.SymbolicExpressionTreeParameter.ActualName = Name;
300        treeOperator.SymbolicExpressionTreeParameter.Hidden = true;
301      }
302    }
303
304    private void ConfigureSizeConstraintOperators(IEnumerable<ISymbolicExpressionTreeSizeConstraintOperator> sizeConstraintOperators) {
305      foreach (var sizeConstraintOperator in sizeConstraintOperators) {
306        sizeConstraintOperator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = TreeLengthParameter.Name;
307        sizeConstraintOperator.MaximumSymbolicExpressionTreeLengthParameter.Hidden = true;
308        sizeConstraintOperator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = TreeDepthParameter.Name;
309        sizeConstraintOperator.MaximumSymbolicExpressionTreeDepthParameter.Hidden = true;
310      }
311    }
312
313    private void ConfigureGrammarBaseOperators(IEnumerable<ISymbolicExpressionTreeGrammarBasedOperator> grammarBasedOperators) {
314      foreach (var grammarBasedOperator in grammarBasedOperators) {
315        grammarBasedOperator.SymbolicExpressionTreeGrammarParameter.ActualName = GrammarParameter.Name;
316        grammarBasedOperator.SymbolicExpressionTreeGrammarParameter.Hidden = true;
317      }
318    }
319
320    private void ConfigureArchitectureAlteringOperators(IEnumerable<ISymbolicExpressionTreeArchitectureAlteringOperator> architectureAlteringOperators) {
321      foreach (var architectureAlteringOperator in architectureAlteringOperators) {
322        architectureAlteringOperator.MaximumFunctionDefinitionsParameter.ActualName = FunctionDefinitionsParameter.Name;
323        architectureAlteringOperator.MaximumFunctionDefinitionsParameter.Hidden = true;
324        architectureAlteringOperator.MaximumFunctionArgumentsParameter.ActualName = FunctionArgumentsParameter.Name;
325        architectureAlteringOperator.MaximumFunctionArgumentsParameter.Hidden = true;
326      }
327
328    }
329
330    private void ConfigureAnalyzers(IEnumerable<ISymbolicExpressionTreeAnalyzer> analyzers) {
331      foreach (var analyzer in analyzers) {
332        analyzer.SymbolicExpressionTreeParameter.ActualName = Name;
333        analyzer.SymbolicExpressionTreeParameter.Hidden = true;
334      }
335      foreach (var analyzer in analyzers.OfType<SymbolicExpressionTreeLengthAnalyzer>()) {
336        analyzer.MaximumSymbolicExpressionTreeLengthParameter.ActualName = TreeLengthParameter.Name;
337        analyzer.MaximumSymbolicExpressionTreeLengthParameter.Hidden = true;
338      }
339    }
340
341    private void ConfigureCreators(IEnumerable<ISymbolicExpressionTreeCreator> creators) {
342      //Empty interface
343      foreach (var creator in creators) { }
344    }
345
346    private void ConfigureCrossovers(IEnumerable<ISymbolicExpressionTreeCrossover> crossovers) {
347      foreach (var crossover in crossovers) {
348        crossover.ParentsParameter.ActualName = Name;
349        crossover.ParentsParameter.Hidden = true;
350      }
351    }
352
353    private void ConfigureManipulators(IEnumerable<ISymbolicExpressionTreeManipulator> manipulators) {
354      //Empty interface
355      foreach (var manipulator in manipulators) { }
356    }
357    #endregion
358  }
359}
Note: See TracBrowser for help on using the repository browser.