Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2320: Corrected event registration in SymbolicExpressionTreeEncoding.

File size: 7.0 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.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
35  [Item("SymbolicExpressionTreeEncoding", "Describes a symbolic expression tree encoding.")]
36  [StorableClass]
37  public class SymbolicExpressionTreeEncoding : Encoding<ISymbolicExpressionTreeCreator> {
38    #region Encoding Parameters
39    [Storable]
40    private IFixedValueParameter<IntValue> treeLengthParameter;
41    public IFixedValueParameter<IntValue> TreeLengthParameter {
42      get { return treeLengthParameter; }
43      set {
44        if (value == null) throw new ArgumentNullException("Tree length parameter must not be null.");
45        if (value.Value == null) throw new ArgumentNullException("Tree length parameter value must not be null.");
46        if (treeLengthParameter == value) return;
47
48        if (treeLengthParameter != null)
49          Parameters.Remove(treeLengthParameter);
50
51        treeLengthParameter = value;
52        Parameters.Add(treeLengthParameter);
53        OnLengthParameterChanged();
54      }
55    }
56
57    [Storable]
58    private IFixedValueParameter<IntValue> treeDepthParameter;
59    public IFixedValueParameter<IntValue> TreeDepthParameter {
60      get { return treeDepthParameter; }
61      set {
62        if (value == null) throw new ArgumentNullException("Tree length parameter must not be null.");
63        if (value.Value == null) throw new ArgumentNullException("Tree length parameter value must not be null.");
64        if (treeDepthParameter == value) return;
65
66        if (treeDepthParameter != null)
67          Parameters.Remove(treeDepthParameter);
68
69        treeDepthParameter = value;
70        Parameters.Add(treeDepthParameter);
71        OnDepthParameterChanged();
72      }
73    }
74    #endregion
75
76
77    [StorableConstructor]
78    public SymbolicExpressionTreeEncoding(bool deserializing) : base(deserializing) { }
79    public SymbolicExpressionTreeEncoding() : this("SymbolicExpressionTree") { }
80    public SymbolicExpressionTreeEncoding(string name) : base(name) { }
81
82    private SymbolicExpressionTreeEncoding(SymbolicExpressionTreeEncoding original, Cloner cloner)
83      : base(original, cloner) {
84      TreeLengthParameter = cloner.Clone(original.TreeLengthParameter);
85      TreeDepthParameter = cloner.Clone(original.TreeDepthParameter);
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new SymbolicExpressionTreeEncoding(this, cloner);
90    }
91
92 
93
94    private void OnLengthParameterChanged() {
95      RegisterLengthParameterEvents();
96      ConfigureOperators(Operators);
97    }
98
99    private void OnDepthParameterChanged() {
100      RegisterDepthParameterEvents();
101      ConfigureOperators(Operators);
102    }
103
104    private void RegisterEvents() {
105      RegisterLengthParameterEvents();
106      RegisterLengthParameterEvents();
107    }
108
109    private void RegisterLengthParameterEvents() {
110      TreeLengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
111      TreeLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
112    }
113
114    private void RegisterDepthParameterEvents() {
115      TreeDepthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
116      TreeDepthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
117    }
118
119    #region Operator discovery
120    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
121    static SymbolicExpressionTreeEncoding() {
122      encodingSpecificOperatorTypes = new List<Type>
123      {
124        typeof(ISymbolicExpressionTreeOperator),
125        typeof(ISymbolicExpressionTreeCreator),
126        typeof(ISymbolicExpressionTreeCrossover),
127        typeof(ISymbolicExpressionTreeManipulator)
128      };
129    }
130
131    private void DiscoverOperators() {
132      var assembly = typeof(ISymbolicExpressionTreeOperator).Assembly;
133      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
134      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
135      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
136
137      ConfigureOperators(newOperators);
138      foreach (var @operator in newOperators)
139        AddOperator(@operator);
140    }
141    #endregion
142
143    #region Specific operator wiring
144
145    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
146      ConfigureCreators(operators.OfType<ISymbolicExpressionTreeCreator>());
147      ConfigureCrossovers(operators.OfType<ISymbolicExpressionTreeCrossover>());
148      ConfigureManipulators(operators.OfType<ISymbolicExpressionTreeManipulator>());
149    }
150
151    private void ConfigureCreators(IEnumerable<ISymbolicExpressionTreeCreator> creators) {
152      foreach (var creator in creators) {
153        creator.SymbolicExpressionTreeParameter.ActualName = Name;
154        // creator should have depth and length parameters?
155      }
156    }
157
158    private void ConfigureCrossovers(IEnumerable<ISymbolicExpressionTreeCrossover> crossovers) {
159      foreach (var crossover in crossovers) {
160        crossover.ParentsParameter.ActualName = Name;
161        crossover.ChildParameter.ActualName = Name;
162      }
163    }
164
165    private void ConfigureManipulators(IEnumerable<ISymbolicExpressionTreeManipulator> manipulators) {
166      foreach (var manipulator in manipulators) {
167        manipulator.SymbolicExpressionTreeParameter.ActualName = Name;
168        manipulator.SymbolicExpressionTreeParameter.Hidden = true;
169      }
170    }
171    #endregion
172  }
173
174  public static class IndividualExtensionMethods {
175    public static ISymbolicExpressionTree SymbolicExpressionTree(this Individual individual) {
176      var encoding = individual.GetEncoding<SymbolicExpressionTreeEncoding>();
177      return individual.SymbolicExpressionTree(encoding.Name);
178    }
179
180    public static ISymbolicExpressionTree SymbolicExpressionTree(this Individual individual, string name) {
181      return (ISymbolicExpressionTree)individual[name];
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.