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