[7037] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 33 | using HEAL.Attic;
|
---|
[7037] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
| 35 |
|
---|
[12108] | 36 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Creators {
|
---|
[16565] | 37 | [StorableType("8E932C08-6D00-4055-9525-DBF28420DAB0")]
|
---|
[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 |
|
---|
| 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 | #endregion
|
---|
| 68 |
|
---|
| 69 | [StorableConstructor]
|
---|
[16565] | 70 | protected MultiSymbolicDataAnalysisExpressionCreator(StorableConstructorFlag _) : base(_) { }
|
---|
[7037] | 71 | protected MultiSymbolicDataAnalysisExpressionCreator(MultiSymbolicDataAnalysisExpressionCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 72 | public override IDeepCloneable Clone(Cloner cloner) { return new MultiSymbolicDataAnalysisExpressionCreator(this, cloner); }
|
---|
| 73 | public MultiSymbolicDataAnalysisExpressionCreator()
|
---|
| 74 | : base() {
|
---|
| 75 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
| 76 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 77 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
[18064] | 78 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
|
---|
[7037] | 79 |
|
---|
| 80 | List<ISymbolicDataAnalysisSolutionCreator> list = new List<ISymbolicDataAnalysisSolutionCreator>();
|
---|
[7052] | 81 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisSolutionCreator))) {
|
---|
| 82 | if (this.GetType().Assembly != type.Assembly) continue;
|
---|
| 83 | if (typeof(IMultiOperator<ISymbolicDataAnalysisSolutionCreator>).IsAssignableFrom(type)) continue;
|
---|
| 84 | list.Add((ISymbolicDataAnalysisSolutionCreator)Activator.CreateInstance(type));
|
---|
[7037] | 85 | }
|
---|
| 86 | CheckedItemList<ISymbolicDataAnalysisSolutionCreator> checkedItemList = new CheckedItemList<ISymbolicDataAnalysisSolutionCreator>();
|
---|
| 87 | checkedItemList.AddRange(list.OrderBy(op => op.Name));
|
---|
| 88 | Operators = checkedItemList.AsReadOnly();
|
---|
| 89 | Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisSolutionCreator>>(Operators.CheckedItems));
|
---|
| 90 |
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | public ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
| 95 | double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
|
---|
| 96 | if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
|
---|
| 97 | double r = random.NextDouble() * sum;
|
---|
| 98 | sum = 0;
|
---|
| 99 | int index = -1;
|
---|
| 100 | foreach (var indexedItem in Operators.CheckedItems) {
|
---|
| 101 | sum += Probabilities[indexedItem.Index];
|
---|
| 102 | if (sum > r) {
|
---|
| 103 | index = indexedItem.Index;
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | return Operators[index].CreateTree(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisSolutionCreator>> e) {
|
---|
| 111 | base.Operators_ItemsReplaced(sender, e);
|
---|
| 112 | ParameterizeTreeCreators();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicDataAnalysisSolutionCreator>> e) {
|
---|
| 116 | base.Operators_ItemsAdded(sender, e);
|
---|
| 117 | ParameterizeTreeCreators();
|
---|
| 118 | }
|
---|
| 119 | private void ParameterizeTreeCreators() {
|
---|
| 120 | foreach (IStochasticOperator creator in Operators.OfType<IStochasticOperator>()) {
|
---|
| 121 | creator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 122 | }
|
---|
| 123 | foreach (ISymbolicExpressionTreeCreator creator in Operators.OfType<ISymbolicExpressionTreeCreator>()) {
|
---|
| 124 | creator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
| 125 | }
|
---|
| 126 | foreach (ISymbolicExpressionTreeSizeConstraintOperator creator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
| 127 | creator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
|
---|
| 128 | creator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | foreach (ISymbolicExpressionTreeGrammarBasedOperator creator in Operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
| 132 | creator.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | }
|
---|
| 137 | }
|
---|