Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/MultiSymbolicDataAnalysisExpressionCrossover.cs @ 12547

Last change on this file since 12547 was 9835, checked in by bburlacu, 11 years ago

#1772: Merged remaining trunk changes into the EvolutionaryTracking branch.

File size: 12.4 KB
RevLine 
[7476]1#region License Information
2/* HeuristicLab
[9835]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7476]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
[7506]35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[9835]36  [StorableClass]
[7481]37  [Item("MultiSymbolicDataAnalysisExpressionCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
[9241]38  public class MultiSymbolicDataAnalysisExpressionCrossover<T> : StochasticMultiBranch<ISymbolicExpressionTreeCrossover>, ITracingSymbolicExpressionTreeOperator,
[7506]39    ISymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
40    private const string ParentsParameterName = "Parents";
41    private const string ChildParameterName = "Child";
[7476]42    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
43    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
[7506]44    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
[7476]45    private const string EvaluatorParameterName = "Evaluator";
46    private const string SymbolicDataAnalysisEvaluationPartitionParameterName = "EvaluationPartition";
[7506]47    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
48    private const string ProblemDataParameterName = "ProblemData";
[7476]49
[9241]50    private const string SymbolicExpressionTreeNodeComparerParameterName = "SymbolicExpressionTreeNodeComparer";
51    private const string SymbolicExpressionTreeNodeComparerParameterDescription = "The comparison operator used to check if two symbolic expression tree nodes are equal or similar.";
52
[7476]53    protected override bool CreateChildOperation {
54      get { return true; }
55    }
56
[7521]57    public override bool CanChangeName {
58      get { return false; }
59    }
60
[7476]61    #region parameter properties
[7506]62    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
63      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
64    }
[7476]65    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
66      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
67    }
68    public ILookupParameter<ISymbolicExpressionTree> ChildParameter {
69      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; }
70    }
71    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
72      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
73    }
74    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
75      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
76    }
77    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
78      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
79    }
[7506]80    public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
[7476]81      get { return (IValueLookupParameter<IntRange>)Parameters[SymbolicDataAnalysisEvaluationPartitionParameterName]; }
82    }
[7506]83    public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
84      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
85    }
86    public IValueLookupParameter<T> ProblemDataParameter {
87      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
88    }
[9423]89    public ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer> SymbolicExpressionTreeNodeComparerParameter {
90      get { return (ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer>)Parameters[SymbolicExpressionTreeNodeComparerParameterName]; }
[9241]91    }
[7476]92    #endregion
93
[9241]94    #region Properties
95    public ISymbolicExpressionTreeNodeComparer SymbolicExpressionTreeNodeComparer {
96      get { return (ISymbolicExpressionTreeNodeComparer)SymbolicExpressionTreeNodeComparerParameter.ActualValue; }
97    }
98    #endregion
99
[9835]100    [StorableHook(HookType.AfterDeserialization)]
101    private void AfterDeserialization() {
102      if (!Parameters.ContainsKey(SymbolicExpressionTreeNodeComparerParameterName))
103        Parameters.Add(new ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer>(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription));
104    }
105
[7476]106    [StorableConstructor]
107    protected MultiSymbolicDataAnalysisExpressionCrossover(bool deserializing) : base(deserializing) { }
108    protected MultiSymbolicDataAnalysisExpressionCrossover(MultiSymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) : base(original, cloner) { }
109    public override IDeepCloneable Clone(Cloner cloner) { return new MultiSymbolicDataAnalysisExpressionCrossover<T>(this, cloner); }
110    public MultiSymbolicDataAnalysisExpressionCrossover()
111      : base() {
[7506]112      Parameters.Add(new ValueLookupParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index."));
113      Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
114      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
[7476]115      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
116      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
117      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(EvaluatorParameterName, "The single objective solution evaluator"));
118      Parameters.Add(new ValueLookupParameter<IntRange>(SymbolicDataAnalysisEvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
119      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
120      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
[9423]121      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer>(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription));
[7476]122
[7506]123      EvaluatorParameter.Hidden = true;
124      EvaluationPartitionParameter.Hidden = true;
125      SymbolicDataAnalysisTreeInterpreterParameter.Hidden = true;
126      ProblemDataParameter.Hidden = true;
127      RelativeNumberOfEvaluatedSamplesParameter.Hidden = true;
128
129      InitializeOperators();
[7521]130      name = "MultiSymbolicDataAnalysisExpressionCrossover";
[7506]131    }
132
133    private void InitializeOperators() {
[7481]134      var list = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeCrossover>().ToList();
135      var dataAnalysisCrossovers = from type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisExpressionCrossover<T>))
136                                   where this.GetType().Assembly == type.Assembly
137                                   where !typeof(IMultiOperator<ISymbolicExpressionTreeCrossover>).IsAssignableFrom(type)
138                                   select (ISymbolicDataAnalysisExpressionCrossover<T>)Activator.CreateInstance(type);
139      list.AddRange(dataAnalysisCrossovers);
[7476]140
[7481]141      var checkedItemList = new CheckedItemList<ISymbolicExpressionTreeCrossover>();
[7476]142      checkedItemList.AddRange(list.OrderBy(op => op.Name));
[7489]143      Operators = checkedItemList;
[7481]144      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>>(Operators.CheckedItems));
[7476]145    }
146
147    public ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
148      double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
149      if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
150      double r = random.NextDouble() * sum;
151      sum = 0;
152      int index = -1;
153      foreach (var indexedItem in Operators.CheckedItems) {
154        sum += Probabilities[indexedItem.Index];
155        if (sum > r) {
156          index = indexedItem.Index;
157          break;
158        }
159      }
160      return Operators[index].Crossover(random, parent0, parent1);
161    }
162
[7481]163    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>> e) {
[7476]164      base.Operators_ItemsReplaced(sender, e);
165      ParameterizeCrossovers();
166    }
167
[7481]168    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>> e) {
[7476]169      base.Operators_ItemsAdded(sender, e);
170      ParameterizeCrossovers();
171    }
172
173    private void ParameterizeCrossovers() {
[7506]174      foreach (ISymbolicExpressionTreeCrossover op in Operators) {
175        op.ChildParameter.ActualName = ChildParameter.Name;
176        op.ParentsParameter.ActualName = ParentsParameter.Name;
177      }
[7476]178      foreach (IStochasticOperator op in Operators.OfType<IStochasticOperator>()) {
179        op.RandomParameter.ActualName = RandomParameter.Name;
180      }
181      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
182        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
183        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
184      }
[7506]185      foreach (ISymbolicDataAnalysisInterpreterOperator op in Operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
186        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicDataAnalysisTreeInterpreterParameter.Name;
[7476]187      }
[7506]188      foreach (var op in Operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
189        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
190        op.EvaluationPartitionParameter.ActualName = EvaluationPartitionParameter.Name;
191        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
192        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
193      }
[9423]194      var comparers = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeNodeSimilarityComparer>();
[9241]195      foreach (var op in Operators.OfType<ITracingSymbolicExpressionTreeOperator>()) {
196        op.SymbolicExpressionTreeNodeComparerParameter.ActualValue = comparers.First();
197      }
[7476]198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.