Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/MultiSymbolicDataAnalysisExpressionCrossover.cs @ 7119

Last change on this file since 7119 was 7119, checked in by bburlacu, 12 years ago

#1682: Implemented the MultiSymbolicDataAnalysisExpressionTreeCrossover

File size: 9.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35
36namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Crossovers {
37  public class MultiSymbolicDataAnalysisExpressionCrossover<T> : StochasticMultiBranch<ISymbolicDataAnalysisExpressionCrossover<T>>,
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 false; }
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 = new List<ISymbolicDataAnalysisExpressionCrossover<T>>();
100      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisExpressionCrossover<T>))) {
101        if (this.GetType().Assembly != type.Assembly) continue;
102        if (typeof(IMultiOperator<ISymbolicDataAnalysisExpressionCrossover<T>>).IsAssignableFrom(type)) 
103          continue;
104        list.Add((ISymbolicDataAnalysisExpressionCrossover<T>)Activator.CreateInstance(type));
105      }
106      var checkedItemList = new CheckedItemList<ISymbolicDataAnalysisExpressionCrossover<T>>();
107      checkedItemList.AddRange(list.OrderBy(op => op.Name));
108      Operators = checkedItemList.AsReadOnly();
109      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisExpressionCrossover<T>>>(Operators.CheckedItems));
110
111    }
112
113    public override IOperation Apply() {
114      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
115        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
116        IScope globalScope = ExecutionContext.Scope;
117        while (globalScope.Parent != null)
118          globalScope = globalScope.Parent;
119
120        globalScope.Variables.Add(new Core.Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
121      }
122      return base.Apply();
123    }
124
125    public ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
126      double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
127      if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
128      double r = random.NextDouble() * sum;
129      sum = 0;
130      int index = -1;
131      foreach (var indexedItem in Operators.CheckedItems) {
132        sum += Probabilities[indexedItem.Index];
133        if (sum > r) {
134          index = indexedItem.Index;
135          break;
136        }
137      }
138      return Operators[index].Crossover(random, parent0, parent1);
139    }
140
141    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisExpressionCrossover<T>>> e) {
142      base.Operators_ItemsReplaced(sender, e);
143      ParameterizeCrossovers();
144    }
145
146    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisExpressionCrossover<T>>> e) {
147      base.Operators_ItemsAdded(sender, e);
148      ParameterizeCrossovers();
149    }
150
151    private void ParameterizeCrossovers() {
152      foreach (IStochasticOperator op in Operators.OfType<IStochasticOperator>()) {
153        op.RandomParameter.ActualName = RandomParameter.Name;
154      }
155      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
156        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
157        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
158      }
159      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in Operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
160        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
161      }
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.