[3841] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3841] | 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.Collections.Generic;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3841] | 24 | using HeuristicLab.Core;
|
---|
[4068] | 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3841] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5574] | 27 |
|
---|
[3841] | 28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 29 | [StorableClass]
|
---|
| 30 | [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
|
---|
[5580] | 31 | public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
|
---|
[4722] | 32 | [StorableConstructor]
|
---|
| 33 | protected FullFunctionalExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
| 34 | protected FullFunctionalExpressionGrammar(FullFunctionalExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
[3841] | 35 | public FullFunctionalExpressionGrammar()
|
---|
| 36 | : base() {
|
---|
| 37 | Initialize();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[4722] | 40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 41 | return new FullFunctionalExpressionGrammar(this, cloner);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[3841] | 44 | private void Initialize() {
|
---|
| 45 | var add = new Addition();
|
---|
| 46 | var sub = new Subtraction();
|
---|
| 47 | var mul = new Multiplication();
|
---|
| 48 | var div = new Division();
|
---|
| 49 | var mean = new Average();
|
---|
| 50 | var sin = new Sine();
|
---|
| 51 | var cos = new Cosine();
|
---|
| 52 | var tan = new Tangent();
|
---|
| 53 | var log = new Logarithm();
|
---|
[5288] | 54 | var pow = new Power();
|
---|
[5384] | 55 | pow.InitialFrequency = 0.0;
|
---|
| 56 | var root = new Root();
|
---|
| 57 | root.InitialFrequency = 0.0;
|
---|
[3841] | 58 | var exp = new Exponential();
|
---|
| 59 | var @if = new IfThenElse();
|
---|
| 60 | var gt = new GreaterThan();
|
---|
| 61 | var lt = new LessThan();
|
---|
| 62 | var and = new And();
|
---|
| 63 | var or = new Or();
|
---|
| 64 | var not = new Not();
|
---|
[5373] | 65 |
|
---|
| 66 | var timeLag = new TimeLag();
|
---|
| 67 | timeLag.InitialFrequency = 0.0;
|
---|
| 68 | var integral = new Integral();
|
---|
| 69 | integral.InitialFrequency = 0.0;
|
---|
| 70 | var derivative = new Derivative();
|
---|
| 71 | derivative.InitialFrequency = 0.0;
|
---|
| 72 |
|
---|
[5467] | 73 | var variableCondition = new VariableCondition();
|
---|
| 74 | variableCondition.InitialFrequency = 0.0;
|
---|
| 75 |
|
---|
[3841] | 76 | var constant = new Constant();
|
---|
| 77 | constant.MinValue = -20;
|
---|
| 78 | constant.MaxValue = 20;
|
---|
[5574] | 79 | var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
|
---|
[4249] | 80 | var laggedVariable = new LaggedVariable();
|
---|
| 81 | laggedVariable.InitialFrequency = 0.0;
|
---|
[3841] | 82 |
|
---|
[5467] | 83 | var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, pow, root, exp, @if, gt, lt, and, or, not, timeLag, integral, derivative, constant, variableSymbol, laggedVariable, variableCondition };
|
---|
[5373] | 84 | var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not, timeLag, integral, derivative };
|
---|
| 85 |
|
---|
[5467] | 86 | var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition };
|
---|
[3993] | 87 | var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
|
---|
[4249] | 88 | var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
|
---|
[3841] | 89 |
|
---|
| 90 | foreach (var symb in allSymbols)
|
---|
| 91 | AddSymbol(symb);
|
---|
| 92 |
|
---|
| 93 | foreach (var funSymb in functionSymbols) {
|
---|
| 94 | SetMinSubtreeCount(funSymb, 1);
|
---|
| 95 | SetMaxSubtreeCount(funSymb, 3);
|
---|
| 96 | }
|
---|
| 97 | foreach (var funSymb in unaryFunctionSymbols) {
|
---|
| 98 | SetMinSubtreeCount(funSymb, 1);
|
---|
| 99 | SetMaxSubtreeCount(funSymb, 1);
|
---|
| 100 | }
|
---|
| 101 | foreach (var funSymb in binaryFunctionSymbols) {
|
---|
| 102 | SetMinSubtreeCount(funSymb, 2);
|
---|
| 103 | SetMaxSubtreeCount(funSymb, 2);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[4249] | 106 | foreach (var terminalSymbol in terminalSymbols) {
|
---|
| 107 | SetMinSubtreeCount(terminalSymbol, 0);
|
---|
| 108 | SetMaxSubtreeCount(terminalSymbol, 0);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[3841] | 111 | SetMinSubtreeCount(@if, 3);
|
---|
| 112 | SetMaxSubtreeCount(@if, 3);
|
---|
| 113 |
|
---|
[4249] | 114 |
|
---|
[3841] | 115 | // allow each symbol as child of the start symbol
|
---|
| 116 | foreach (var symb in allSymbols) {
|
---|
| 117 | SetAllowedChild(StartSymbol, symb, 0);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
| 121 | foreach (var parent in allSymbols) {
|
---|
| 122 | for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
|
---|
| 123 | foreach (var child in allSymbols) {
|
---|
| 124 | SetAllowedChild(parent, child, i);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|