Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Creators/MultiSymbolicDataAnalysisExpressionCreator.cs @ 15783

Last change on this file since 15783 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 8.7 KB
RevLine 
[7037]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7037]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.Creators {
[13636]37  [StorableClass]
[7037]38  public class MultiSymbolicDataAnalysisExpressionCreator : StochasticMultiBranch<ISymbolicDataAnalysisSolutionCreator>,
39    ISymbolicDataAnalysisSolutionCreator,
40    ISymbolicExpressionTreeSizeConstraintOperator,
41  ISymbolicExpressionTreeGrammarBasedOperator {
42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
43    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
44    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
45    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
46    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
47
48    public override bool CanChangeName {
49      get { return false; }
50    }
51    protected override bool CreateChildOperation {
52      get { return true; }
53    }
54
55    #region parameter properties
56    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
57      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
58    }
59    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
60      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
61    }
62    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
63      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
64    }
65    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
66      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
67    }
68    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
69      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
70    }
71    #endregion
72
73    [StorableConstructor]
74    protected MultiSymbolicDataAnalysisExpressionCreator(bool deserializing) : base(deserializing) { }
75    protected MultiSymbolicDataAnalysisExpressionCreator(MultiSymbolicDataAnalysisExpressionCreator original, Cloner cloner) : base(original, cloner) { }
76    public override IDeepCloneable Clone(Cloner cloner) { return new MultiSymbolicDataAnalysisExpressionCreator(this, cloner); }
77    public MultiSymbolicDataAnalysisExpressionCreator()
78      : base() {
79      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
80      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
81      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
82      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
83      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
84
85      List<ISymbolicDataAnalysisSolutionCreator> list = new List<ISymbolicDataAnalysisSolutionCreator>();
[7052]86      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisSolutionCreator))) {
87        if (this.GetType().Assembly != type.Assembly) continue;
88        if (typeof(IMultiOperator<ISymbolicDataAnalysisSolutionCreator>).IsAssignableFrom(type)) continue;
89        list.Add((ISymbolicDataAnalysisSolutionCreator)Activator.CreateInstance(type));
[7037]90      }
91      CheckedItemList<ISymbolicDataAnalysisSolutionCreator> checkedItemList = new CheckedItemList<ISymbolicDataAnalysisSolutionCreator>();
92      checkedItemList.AddRange(list.OrderBy(op => op.Name));
93      Operators = checkedItemList.AsReadOnly();
94      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisSolutionCreator>>(Operators.CheckedItems));
95
96    }
97
[10507]98    public override IOperation InstrumentedApply() {
[7037]99      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
100        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
101        IScope globalScope = ExecutionContext.Scope;
102        while (globalScope.Parent != null)
103          globalScope = globalScope.Parent;
104
105        globalScope.Variables.Add(new Core.Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
106      }
[10507]107      return base.InstrumentedApply();
[7037]108    }
109
110    public ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
111      double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
112      if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
113      double r = random.NextDouble() * sum;
114      sum = 0;
115      int index = -1;
116      foreach (var indexedItem in Operators.CheckedItems) {
117        sum += Probabilities[indexedItem.Index];
118        if (sum > r) {
119          index = indexedItem.Index;
120          break;
121        }
122      }
123      return Operators[index].CreateTree(random, grammar, maxTreeLength, maxTreeDepth);
124    }
125
126    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisSolutionCreator>> e) {
127      base.Operators_ItemsReplaced(sender, e);
128      ParameterizeTreeCreators();
129    }
130
131    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisSolutionCreator>> e) {
132      base.Operators_ItemsAdded(sender, e);
133      ParameterizeTreeCreators();
134    }
135    private void ParameterizeTreeCreators() {
136      foreach (IStochasticOperator creator in Operators.OfType<IStochasticOperator>()) {
137        creator.RandomParameter.ActualName = RandomParameter.Name;
138      }
139      foreach (ISymbolicExpressionTreeCreator creator in Operators.OfType<ISymbolicExpressionTreeCreator>()) {
140        creator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
141      }
142      foreach (ISymbolicExpressionTreeSizeConstraintOperator creator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
143        creator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
144        creator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
145      }
146
147      foreach (ISymbolicExpressionTreeGrammarBasedOperator creator in Operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
148        creator.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
149      }
150    }
151
152  }
153}
Note: See TracBrowser for help on using the repository browser.