Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1682: Added crossovers.

File size: 9.7 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.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 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 = (from type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisExpressionCrossover<T>))
100                  where this.GetType().Assembly == type.Assembly
101                  where !typeof(IMultiOperator<ISymbolicDataAnalysisExpressionCrossover<T>>).IsAssignableFrom(type)
102                  select (ISymbolicDataAnalysisExpressionCrossover<T>)Activator.CreateInstance(type)).ToList();
103
104      var checkedItemList = new CheckedItemList<ISymbolicDataAnalysisExpressionCrossover<T>>();
105      checkedItemList.AddRange(list.OrderBy(op => op.Name));
106      Operators = checkedItemList.AsReadOnly();
107      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisExpressionCrossover<T>>>(Operators.CheckedItems));
108      Name = "MultiCrossover";
109    }
110
111    public override IOperation Apply() {
112      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
113        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
114        IScope globalScope = ExecutionContext.Scope;
115        while (globalScope.Parent != null)
116          globalScope = globalScope.Parent;
117
118        globalScope.Variables.Add(new Core.Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
119      }
120      return base.Apply();
121    }
122
123    public ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
124      double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
125      if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
126      double r = random.NextDouble() * sum;
127      sum = 0;
128      int index = -1;
129      foreach (var indexedItem in Operators.CheckedItems) {
130        sum += Probabilities[indexedItem.Index];
131        if (sum > r) {
132          index = indexedItem.Index;
133          break;
134        }
135      }
136      return Operators[index].Crossover(random, parent0, parent1);
137    }
138
139    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisExpressionCrossover<T>>> e) {
140      base.Operators_ItemsReplaced(sender, e);
141      ParameterizeCrossovers();
142    }
143
144    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisExpressionCrossover<T>>> e) {
145      base.Operators_ItemsAdded(sender, e);
146      ParameterizeCrossovers();
147    }
148
149    private void ParameterizeCrossovers() {
150      foreach (IStochasticOperator op in Operators.OfType<IStochasticOperator>()) {
151        op.RandomParameter.ActualName = RandomParameter.Name;
152      }
153      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
154        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
155        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
156      }
157      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in Operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
158        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
159      }
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.