Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs @ 12316

Last change on this file since 12316 was 12316, checked in by mkommend, 9 years ago

#2320: Implemented symbolic expression tree encoding.

File size: 14.9 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  [Item("SymbolicExpressionTreeEncoding", "Describes a symbolic expression tree encoding.")]
37  [StorableClass]
38  public class SymbolicExpressionTreeEncoding : Encoding<ISymbolicExpressionTreeCreator> {
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
160    [StorableConstructor]
161    public SymbolicExpressionTreeEncoding(bool deserializing) : base(deserializing) { }
162    public SymbolicExpressionTreeEncoding() : this("SymbolicExpressionTree") { }
163
164    public SymbolicExpressionTreeEncoding(string name)
165      : base(name) {
166      treeLengthParameter = new FixedValueParameter<IntValue>("Maximium Tree Length", "Maximal length of the symbolic expression.", new IntValue(50));
167      treeDepthParameter = new FixedValueParameter<IntValue>("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(50));
168      grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that should be used for symbolic expression tree.");
169      functionDefinitionsParameter = new FixedValueParameter<IntValue>("Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));
170      functionArgumentsParameter = new FixedValueParameter<IntValue>("Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));
171
172      //TODO set default grammar
173
174      Parameters.Add(treeLengthParameter);
175      Parameters.Add(treeDepthParameter);
176      Parameters.Add(grammarParameter);
177      Parameters.Add(functionDefinitionsParameter);
178      Parameters.Add(functionArgumentsParameter);
179
180      SolutionCreator = new ProbabilisticTreeCreator();
181      RegisterParameterEvents();
182      DiscoverOperators();
183    }
184
185    private SymbolicExpressionTreeEncoding(SymbolicExpressionTreeEncoding original, Cloner cloner)
186      : base(original, cloner) {
187      TreeLengthParameter = cloner.Clone(original.TreeLengthParameter);
188      TreeDepthParameter = cloner.Clone(original.TreeDepthParameter);
189      GrammarParameter = cloner.Clone(original.GrammarParameter);
190      FunctionDefinitionsParameter = cloner.Clone(original.FunctionDefinitionsParameter);
191      FunctionArgumentsParameter = cloner.Clone(original.FunctionArgumentsParameter);
192      RegisterParameterEvents();
193    }
194
195    public override IDeepCloneable Clone(Cloner cloner) {
196      return new SymbolicExpressionTreeEncoding(this, cloner);
197    }
198
199    #region changed events
200    private void OnLengthParameterChanged() {
201      RegisterLengthParameterEvents();
202      ConfigureOperators(Operators);
203    }
204    private void OnDepthParameterChanged() {
205      RegisterDepthParameterEvents();
206      ConfigureOperators(Operators);
207    }
208    private void OnGrammarParameterChanged() {
209      RegisterGrammarParameterEvents();
210      ConfigureOperators(Operators);
211    }
212
213    private void OnFunctionDefinitionsParameterChanged() {
214      RegisterFunctionDefinitionsParameterEvents();
215      Grammar.MaximumFunctionDefinitions = FunctionDefinitions;
216    }
217
218    private void OnFunctionArgumentsParameterChanged() {
219      RegisterFunctionArgumentsParameterEvetns();
220      Grammar.MaximumFunctionArguments = FunctionArguments;
221    }
222
223    private void RegisterParameterEvents() {
224      RegisterLengthParameterEvents();
225      RegisterLengthParameterEvents();
226      RegisterGrammarParameterEvents();
227      RegisterFunctionDefinitionsParameterEvents();
228      RegisterFunctionArgumentsParameterEvetns();
229    }
230
231
232    private void RegisterLengthParameterEvents() {
233      TreeLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
234    }
235
236    private void RegisterDepthParameterEvents() {
237      TreeDepthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
238    }
239    private void RegisterGrammarParameterEvents() {
240      GrammarParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
241      GrammarParameter.Value.Changed += (o, s) => ConfigureOperators(Operators);
242    }
243
244    private void RegisterFunctionDefinitionsParameterEvents() {
245      FunctionDefinitionsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionDefinitions = FunctionDefinitions;
246    }
247    private void RegisterFunctionArgumentsParameterEvetns() {
248      FunctionArgumentsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionArguments = FunctionArguments;
249    }
250    #endregion
251
252    #region Operator discovery
253    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
254    static SymbolicExpressionTreeEncoding() {
255      encodingSpecificOperatorTypes = new List<Type>
256      {
257        typeof(ISymbolicExpressionTreeOperator),
258        typeof(ISymbolicExpressionTreeCreator),
259        typeof(ISymbolicExpressionTreeCrossover),
260        typeof(ISymbolicExpressionTreeManipulator),
261        typeof(ISymbolicExpressionTreeArchitectureAlteringOperator),
262        typeof(ISymbolicExpressionTreeAnalyzer),
263      };
264    }
265
266    private void DiscoverOperators() {
267      var assembly = typeof(ISymbolicExpressionTreeOperator).Assembly;
268      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
269      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
270      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
271
272      ConfigureOperators(newOperators);
273      foreach (var @operator in newOperators)
274        AddOperator(@operator);
275    }
276    #endregion
277
278    #region Specific operator wiring
279
280    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
281      ConfigureTreeOperators(operators.OfType<ISymbolicExpressionTreeOperator>());
282      ConfigureSizeConstraintOperators(operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>());
283      ConfigureGrammarBaseOperators(operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>());
284      ConfigureArchitectureAlteringOperators(operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>());
285
286      ConfigureAnalyzers(operators.OfType<ISymbolicExpressionTreeAnalyzer>());
287      ConfigureCreators(operators.OfType<ISymbolicExpressionTreeCreator>());
288      ConfigureCrossovers(operators.OfType<ISymbolicExpressionTreeCrossover>());
289      ConfigureManipulators(operators.OfType<ISymbolicExpressionTreeManipulator>());
290    }
291
292    private void ConfigureTreeOperators(IEnumerable<ISymbolicExpressionTreeOperator> treeOperators) {
293      foreach (var treeOperator in treeOperators) {
294        treeOperator.SymbolicExpressionTreeParameter.ActualName = Name;
295        treeOperator.SymbolicExpressionTreeParameter.Hidden = true;
296      }
297    }
298
299    private void ConfigureSizeConstraintOperators(IEnumerable<ISymbolicExpressionTreeSizeConstraintOperator> sizeConstraintOperators) {
300      foreach (var sizeConstraintOperator in sizeConstraintOperators) {
301        sizeConstraintOperator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = TreeLengthParameter.Name;
302        sizeConstraintOperator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = TreeDepthParameter.Name;
303      }
304    }
305
306    private void ConfigureGrammarBaseOperators(IEnumerable<ISymbolicExpressionTreeGrammarBasedOperator> grammarBasedOperators) {
307      foreach (var grammarBasedOperator in grammarBasedOperators) {
308        grammarBasedOperator.SymbolicExpressionTreeGrammarParameter.ActualName = GrammarParameter.Name;
309      }
310    }
311
312    private void ConfigureArchitectureAlteringOperators(IEnumerable<ISymbolicExpressionTreeArchitectureAlteringOperator> architectureAlteringOperators) {
313      foreach (var architectureAlteringOperator in architectureAlteringOperators) {
314        architectureAlteringOperator.MaximumFunctionDefinitionsParameter.ActualName = FunctionDefinitionsParameter.Name;
315        architectureAlteringOperator.MaximumFunctionArgumentsParameter.ActualName = FunctionArgumentsParameter.Name;
316      }
317
318    }
319
320    private void ConfigureAnalyzers(IEnumerable<ISymbolicExpressionTreeAnalyzer> analyzers) {
321      foreach (var analyzer in analyzers) {
322        analyzer.SymbolicExpressionTreeParameter.ActualName = Name;
323      }
324    }
325
326    private void ConfigureCreators(IEnumerable<ISymbolicExpressionTreeCreator> creators) {
327      //Empty interface
328      foreach (var creator in creators) { }
329    }
330
331    private void ConfigureCrossovers(IEnumerable<ISymbolicExpressionTreeCrossover> crossovers) {
332      foreach (var crossover in crossovers) {
333        crossover.ParentsParameter.ActualName = Name;
334      }
335    }
336
337    private void ConfigureManipulators(IEnumerable<ISymbolicExpressionTreeManipulator> manipulators) {
338      //Empty interface
339      foreach (var manipulator in manipulators) { }
340    }
341    #endregion
342  }
343
344  public static class IndividualExtensionMethods {
345    public static ISymbolicExpressionTree SymbolicExpressionTree(this Individual individual) {
346      var encoding = individual.GetEncoding<SymbolicExpressionTreeEncoding>();
347      return individual.SymbolicExpressionTree(encoding.Name);
348    }
349
350    public static ISymbolicExpressionTree SymbolicExpressionTree(this Individual individual, string name) {
351      return (ISymbolicExpressionTree)individual[name];
352    }
353  }
354}
Note: See TracBrowser for help on using the repository browser.