Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7489 was 7489, checked in by mkommend, 12 years ago

#1682: Improved DepthConstrainedCrossover and removed readonly attribute from the operators collection in the MultiCrossover.

File size: 9.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Crossovers {
36  [Item("MultiSymbolicDataAnalysisExpressionCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
37  public class MultiSymbolicDataAnalysisExpressionCrossover<T> : StochasticMultiBranch<ISymbolicExpressionTreeCrossover>,
38    ISymbolicDataAnalysisExpressionCrossover<T>,
39    ISymbolicExpressionTreeSizeConstraintOperator,
40    ISymbolicExpressionTreeGrammarBasedOperator where T : class, IDataAnalysisProblemData {
41    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
42    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
43    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
44    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
45    private const string EvaluatorParameterName = "Evaluator";
46    private const string SymbolicDataAnalysisEvaluationPartitionParameterName = "EvaluationPartition";
47    private const string ParentsParameterName = "Parents";
48    private const string ChildParameterName = "Child";
49
50    public override bool CanChangeName {
51      get { return true; }
52    }
53    protected override bool CreateChildOperation {
54      get { return true; }
55    }
56
57    #region parameter properties
58    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
59      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
60    }
61    public ILookupParameter<ISymbolicExpressionTree> ChildParameter {
62      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; }
63    }
64    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
65      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
66    }
67    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
68      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
69    }
70    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
71      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
72    }
73    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
74      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
75    }
76    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
77      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
78    }
79    public IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter {
80      get { return (IValueLookupParameter<IntRange>)Parameters[SymbolicDataAnalysisEvaluationPartitionParameterName]; }
81    }
82    #endregion
83
84    [StorableConstructor]
85    protected MultiSymbolicDataAnalysisExpressionCrossover(bool deserializing) : base(deserializing) { }
86    protected MultiSymbolicDataAnalysisExpressionCrossover(MultiSymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) : base(original, cloner) { }
87    public override IDeepCloneable Clone(Cloner cloner) { return new MultiSymbolicDataAnalysisExpressionCrossover<T>(this, cloner); }
88    public MultiSymbolicDataAnalysisExpressionCrossover()
89      : base() {
90      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
91      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
92      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
93      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
94      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(EvaluatorParameterName, "The single objective solution evaluator"));
95      Parameters.Add(new ValueLookupParameter<IntRange>(SymbolicDataAnalysisEvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
96      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
97      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
98
99      var list = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeCrossover>().ToList();
100      var dataAnalysisCrossovers = from type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisExpressionCrossover<T>))
101                                   where this.GetType().Assembly == type.Assembly
102                                   where !typeof(IMultiOperator<ISymbolicExpressionTreeCrossover>).IsAssignableFrom(type)
103                                   select (ISymbolicDataAnalysisExpressionCrossover<T>)Activator.CreateInstance(type);
104      list.AddRange(dataAnalysisCrossovers);
105
106      var checkedItemList = new CheckedItemList<ISymbolicExpressionTreeCrossover>();
107      checkedItemList.AddRange(list.OrderBy(op => op.Name));
108      Operators = checkedItemList;
109      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>>(Operators.CheckedItems));
110    }
111
112    public override IOperation Apply() {
113      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
114        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
115        IScope globalScope = ExecutionContext.Scope;
116        while (globalScope.Parent != null)
117          globalScope = globalScope.Parent;
118
119        globalScope.Variables.Add(new Core.Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
120      }
121      return base.Apply();
122    }
123
124    public ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
125      double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
126      if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
127      double r = random.NextDouble() * sum;
128      sum = 0;
129      int index = -1;
130      foreach (var indexedItem in Operators.CheckedItems) {
131        sum += Probabilities[indexedItem.Index];
132        if (sum > r) {
133          index = indexedItem.Index;
134          break;
135        }
136      }
137      return Operators[index].Crossover(random, parent0, parent1);
138    }
139
140    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>> e) {
141      base.Operators_ItemsReplaced(sender, e);
142      ParameterizeCrossovers();
143    }
144
145    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>> e) {
146      base.Operators_ItemsAdded(sender, e);
147      ParameterizeCrossovers();
148    }
149
150    private void ParameterizeCrossovers() {
151      foreach (IStochasticOperator op in Operators.OfType<IStochasticOperator>()) {
152        op.RandomParameter.ActualName = RandomParameter.Name;
153      }
154      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
155        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
156        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
157      }
158      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in Operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
159        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.