Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs @ 14344

Last change on this file since 14344 was 14344, checked in by gkronber, 7 years ago

#2689: added encoding name as prefix to parameter names in SymbolicExpressionTreeEncoding

File size: 16.4 KB
RevLine 
[12196]1#region License Information
2
3/* HeuristicLab
[14185]4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12196]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;
[12316]31using HeuristicLab.Parameters;
[12196]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]
[12319]38  public sealed class SymbolicExpressionTreeEncoding : Encoding<ISymbolicExpressionTreeCreator> {
[12196]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);
[12266]54        OnLengthParameterChanged();
[12196]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);
[12266]72        OnDepthParameterChanged();
[12196]73      }
74    }
[12316]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    }
[12196]130    #endregion
131
[12316]132    #region Parameter properties
133    public int TreeLength {
134      get { return TreeLengthParameter.Value.Value; }
135      set { TreeLengthParameter.Value.Value = value; }
136    }
[12196]137
[12316]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
[12196]159    [StorableConstructor]
[12319]160    private SymbolicExpressionTreeEncoding(bool deserializing) : base(deserializing) { }
[13471]161    public SymbolicExpressionTreeEncoding() : this(new SimpleSymbolicExpressionGrammar()) { }
[12319]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)
[12316]165      : base(name) {
[14344]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      functionDefinitionsParameter = new FixedValueParameter<IntValue>(Name + ".Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));
170      functionArgumentsParameter = new FixedValueParameter<IntValue>(Name + ".Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));
[12316]171
172      Parameters.Add(treeLengthParameter);
173      Parameters.Add(treeDepthParameter);
174      Parameters.Add(grammarParameter);
175      Parameters.Add(functionDefinitionsParameter);
176      Parameters.Add(functionArgumentsParameter);
177
178      SolutionCreator = new ProbabilisticTreeCreator();
179      RegisterParameterEvents();
180      DiscoverOperators();
181    }
182
[12196]183    private SymbolicExpressionTreeEncoding(SymbolicExpressionTreeEncoding original, Cloner cloner)
184      : base(original, cloner) {
[12319]185      treeLengthParameter = cloner.Clone(original.TreeLengthParameter);
186      treeDepthParameter = cloner.Clone(original.TreeDepthParameter);
187      grammarParameter = cloner.Clone(original.GrammarParameter);
188      functionDefinitionsParameter = cloner.Clone(original.FunctionDefinitionsParameter);
189      functionArgumentsParameter = cloner.Clone(original.FunctionArgumentsParameter);
[12316]190      RegisterParameterEvents();
[12196]191    }
192
193    public override IDeepCloneable Clone(Cloner cloner) {
194      return new SymbolicExpressionTreeEncoding(this, cloner);
195    }
196
[12316]197    #region changed events
[12196]198    private void OnLengthParameterChanged() {
199      RegisterLengthParameterEvents();
200      ConfigureOperators(Operators);
201    }
202    private void OnDepthParameterChanged() {
203      RegisterDepthParameterEvents();
204      ConfigureOperators(Operators);
205    }
[12316]206    private void OnGrammarParameterChanged() {
207      RegisterGrammarParameterEvents();
[12319]208      FunctionArguments = Grammar.MaximumFunctionArguments;
209      FunctionDefinitions = Grammar.MaximumFunctionDefinitions;
[12316]210      ConfigureOperators(Operators);
211    }
[12196]212
[12316]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() {
[12266]224      RegisterLengthParameterEvents();
225      RegisterLengthParameterEvents();
[12316]226      RegisterGrammarParameterEvents();
227      RegisterFunctionDefinitionsParameterEvents();
228      RegisterFunctionArgumentsParameterEvetns();
[12266]229    }
230
[12316]231
[12319]232    private void RegisterLengthParameterEvents() { }
233    private void RegisterDepthParameterEvents() { }
[12196]234
[12316]235    private void RegisterGrammarParameterEvents() {
[12319]236      GrammarParameter.ValueChanged += (o, s) => {
237        FunctionArguments = Grammar.MaximumFunctionArguments;
238        FunctionDefinitions = Grammar.MaximumFunctionDefinitions;
239        ConfigureOperators(Operators);
240      };
241      GrammarParameter.Value.Changed += (o, s) => {
242        FunctionArguments = Grammar.MaximumFunctionArguments;
243        FunctionDefinitions = Grammar.MaximumFunctionDefinitions;
244        ConfigureOperators(Operators);
245      };
[12316]246    }
[12196]247
[12316]248    private void RegisterFunctionDefinitionsParameterEvents() {
249      FunctionDefinitionsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionDefinitions = FunctionDefinitions;
250    }
251    private void RegisterFunctionArgumentsParameterEvetns() {
252      FunctionArgumentsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionArguments = FunctionArguments;
253    }
254    #endregion
255
[12196]256    #region Operator discovery
257    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
258    static SymbolicExpressionTreeEncoding() {
259      encodingSpecificOperatorTypes = new List<Type>
260      {
261        typeof(ISymbolicExpressionTreeOperator),
262        typeof(ISymbolicExpressionTreeCreator),
263        typeof(ISymbolicExpressionTreeCrossover),
[12316]264        typeof(ISymbolicExpressionTreeManipulator),
265        typeof(ISymbolicExpressionTreeArchitectureAlteringOperator),
266        typeof(ISymbolicExpressionTreeAnalyzer),
[12196]267      };
268    }
269
270    private void DiscoverOperators() {
271      var assembly = typeof(ISymbolicExpressionTreeOperator).Assembly;
272      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
273      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
274      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
275
276      ConfigureOperators(newOperators);
277      foreach (var @operator in newOperators)
278        AddOperator(@operator);
279    }
280    #endregion
281
282    #region Specific operator wiring
[12266]283
284    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
[12316]285      ConfigureTreeOperators(operators.OfType<ISymbolicExpressionTreeOperator>());
286      ConfigureSizeConstraintOperators(operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>());
287      ConfigureGrammarBaseOperators(operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>());
288      ConfigureArchitectureAlteringOperators(operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>());
289
290      ConfigureAnalyzers(operators.OfType<ISymbolicExpressionTreeAnalyzer>());
[12266]291      ConfigureCreators(operators.OfType<ISymbolicExpressionTreeCreator>());
292      ConfigureCrossovers(operators.OfType<ISymbolicExpressionTreeCrossover>());
293      ConfigureManipulators(operators.OfType<ISymbolicExpressionTreeManipulator>());
294    }
295
[12316]296    private void ConfigureTreeOperators(IEnumerable<ISymbolicExpressionTreeOperator> treeOperators) {
297      foreach (var treeOperator in treeOperators) {
298        treeOperator.SymbolicExpressionTreeParameter.ActualName = Name;
299        treeOperator.SymbolicExpressionTreeParameter.Hidden = true;
[12196]300      }
301    }
302
[12316]303    private void ConfigureSizeConstraintOperators(IEnumerable<ISymbolicExpressionTreeSizeConstraintOperator> sizeConstraintOperators) {
304      foreach (var sizeConstraintOperator in sizeConstraintOperators) {
305        sizeConstraintOperator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = TreeLengthParameter.Name;
[12421]306        sizeConstraintOperator.MaximumSymbolicExpressionTreeLengthParameter.Hidden = true;
[12316]307        sizeConstraintOperator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = TreeDepthParameter.Name;
[12421]308        sizeConstraintOperator.MaximumSymbolicExpressionTreeDepthParameter.Hidden = true;
[12316]309      }
310    }
311
312    private void ConfigureGrammarBaseOperators(IEnumerable<ISymbolicExpressionTreeGrammarBasedOperator> grammarBasedOperators) {
313      foreach (var grammarBasedOperator in grammarBasedOperators) {
314        grammarBasedOperator.SymbolicExpressionTreeGrammarParameter.ActualName = GrammarParameter.Name;
[12421]315        grammarBasedOperator.SymbolicExpressionTreeGrammarParameter.Hidden = true;
[12316]316      }
317    }
318
319    private void ConfigureArchitectureAlteringOperators(IEnumerable<ISymbolicExpressionTreeArchitectureAlteringOperator> architectureAlteringOperators) {
320      foreach (var architectureAlteringOperator in architectureAlteringOperators) {
321        architectureAlteringOperator.MaximumFunctionDefinitionsParameter.ActualName = FunctionDefinitionsParameter.Name;
[12421]322        architectureAlteringOperator.MaximumFunctionDefinitionsParameter.Hidden = true;
[12316]323        architectureAlteringOperator.MaximumFunctionArgumentsParameter.ActualName = FunctionArgumentsParameter.Name;
[12421]324        architectureAlteringOperator.MaximumFunctionArgumentsParameter.Hidden = true;
[12316]325      }
326
327    }
328
329    private void ConfigureAnalyzers(IEnumerable<ISymbolicExpressionTreeAnalyzer> analyzers) {
330      foreach (var analyzer in analyzers) {
331        analyzer.SymbolicExpressionTreeParameter.ActualName = Name;
[12421]332        analyzer.SymbolicExpressionTreeParameter.Hidden = true;
[12316]333      }
[12334]334      foreach (var analyzer in analyzers.OfType<SymbolicExpressionTreeLengthAnalyzer>()) {
335        analyzer.MaximumSymbolicExpressionTreeLengthParameter.ActualName = TreeLengthParameter.Name;
[12421]336        analyzer.MaximumSymbolicExpressionTreeLengthParameter.Hidden = true;
[12334]337      }
[12316]338    }
339
340    private void ConfigureCreators(IEnumerable<ISymbolicExpressionTreeCreator> creators) {
341      //Empty interface
342      foreach (var creator in creators) { }
343    }
344
[12196]345    private void ConfigureCrossovers(IEnumerable<ISymbolicExpressionTreeCrossover> crossovers) {
346      foreach (var crossover in crossovers) {
347        crossover.ParentsParameter.ActualName = Name;
[12421]348        crossover.ParentsParameter.Hidden = true;
[12196]349      }
350    }
351
352    private void ConfigureManipulators(IEnumerable<ISymbolicExpressionTreeManipulator> manipulators) {
[12316]353      //Empty interface
354      foreach (var manipulator in manipulators) { }
[12196]355    }
356    #endregion
357  }
358
359  public static class IndividualExtensionMethods {
360    public static ISymbolicExpressionTree SymbolicExpressionTree(this Individual individual) {
361      var encoding = individual.GetEncoding<SymbolicExpressionTreeEncoding>();
362      return individual.SymbolicExpressionTree(encoding.Name);
363    }
364
365    public static ISymbolicExpressionTree SymbolicExpressionTree(this Individual individual, string name) {
366      return (ISymbolicExpressionTree)individual[name];
367    }
368  }
369}
Note: See TracBrowser for help on using the repository browser.