- Timestamp:
- 09/16/11 11:15:41 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs
r6337 r6782 21 21 22 22 using System.Collections.Generic; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 29 30 [Item("TypeCoherentExpressionGrammar", "Represents a grammar for functional expressions in which special syntactic constraints are enforced so that boolean and real-valued expressions are not mixed.")] 30 31 public class TypeCoherentExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar { 32 private const string ArithmeticFunctionsName = "Arithmetic Functions"; 33 private const string TrigonometricFunctionsName = "Trigonometric Functions"; 34 private const string ExponentialFunctionsName = "Exponential and Logarithmic Functions"; 35 private const string RealValuedSymbolsName = "Real Valued Symbols"; 36 private const string TerminalsName = "Terminals"; 37 private const string PowerFunctionsName = "Power Functions"; 38 private const string ConditionsName = "Conditions"; 39 private const string ComparisonsName = "Comparisons"; 40 private const string BooleanOperatorsName = "Boolean Operators"; 41 private const string ConditionalSymbolsName = "ConditionalSymbols"; 42 private const string TimeSeriesSymbolsName = "Time Series Symbols"; 31 43 32 44 [StorableConstructor] … … 42 54 43 55 private void Initialize() { 56 #region symbol declaration 44 57 var add = new Addition(); 45 58 var sub = new Subtraction(); … … 52 65 var log = new Logarithm(); 53 66 var pow = new Power(); 54 pow.InitialFrequency = 0.0;55 67 var root = new Root(); 56 root.InitialFrequency = 0.0;57 68 var exp = new Exponential(); 58 69 var @if = new IfThenElse(); … … 62 73 var or = new Or(); 63 74 var not = new Not(); 75 var variableCondition = new VariableCondition(); 64 76 65 77 var timeLag = new TimeLag(); 66 timeLag.InitialFrequency = 0.0;67 78 var integral = new Integral(); 68 integral.InitialFrequency = 0.0;69 79 var derivative = new Derivative(); 70 derivative.InitialFrequency = 0.0;71 var variableCondition = new VariableCondition();72 variableCondition.InitialFrequency = 0.0;73 80 74 81 var constant = new Constant(); 75 82 constant.MinValue = -20; 76 83 constant.MaxValue = 20; 77 var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();84 var variableSymbol = new Variable(); 78 85 var laggedVariable = new LaggedVariable(); 86 #endregion 79 87 80 laggedVariable.InitialFrequency = 0.0; 81 mean.InitialFrequency = 0.0; 88 #region group symbol declaration 89 var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean }); 90 var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan }); 91 var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log }); 92 var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol }); 93 var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, terminalSymbols }); 82 94 83 /* 84 * Start = RealValueExpression 85 * 86 * RealValueExpression = 87 * "Variable" | 88 * "Constant" | 89 * BinaryOperator RealValueExpression RealValueExpression | 90 * UnaryOperator RealValueExpression | 91 * "IF" BooleanExpression RealValueExpression RealValueExpression | 92 * "VariableCondition" RealValueExpression RealValueExpression 93 * 94 * BinaryOperator = 95 * "+" | "-" | "*" | "/" | "Power" | "Root" 96 * 97 * UnaryOperator = 98 * "Sin" | "Cos" | "Tan" | "Log" | "Exp" 99 * 100 * BooleanExpression = 101 * "AND" BooleanExpression BooleanExpression | 102 * "OR" BooleanExpression BooleanExpression | 103 * "NOT" BooleanExpression | 104 * ">" RealValueExpression RealValueExpression | 105 * "<" RealValueExpression RealValueExpression 106 */ 95 var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { pow, root }); 107 96 108 var arithmeticSymbols = new GroupSymbol("Arithmetic Functions", new List<ISymbol>() { add, sub, mul, div});109 var trigonometricSymbols = new GroupSymbol("Trigonometric Functions", new List<ISymbol>() { sin, cos, tan});110 var powerSymbols = new GroupSymbol("Power Functions", new List<ISymbol> { pow, root });111 var exponentialAndLogarithmicSymbols = new GroupSymbol("Exponential and Logarithmic Functions", new List<ISymbol> { exp, log});112 var terminalSymbols = new GroupSymbol("Terminals", new List<ISymbol> { constant, variableSymbol }); 113 var realValuedSymbols = new GroupSymbol("Real valued Symbols",114 new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, powerSymbols, exponentialAndLogarithmicSymbols, terminalSymbols });97 var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition }); 98 var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt }); 99 var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not }); 100 var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols }); 101 102 var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable }); 103 #endregion 115 104 116 105 AddSymbol(realValuedSymbols); 106 AddSymbol(powerSymbols); 107 AddSymbol(conditionalSymbols); 108 AddSymbol(timeSeriesSymbols); 117 109 110 #region subtree count configuration 118 111 SetSubtreeCount(arithmeticSymbols, 2, 2); 119 112 SetSubtreeCount(trigonometricSymbols, 1, 1); … … 122 115 SetSubtreeCount(terminalSymbols, 0, 0); 123 116 117 SetSubtreeCount(@if, 3, 3); 118 SetSubtreeCount(variableCondition, 2, 2); 119 SetSubtreeCount(comparisonSymbols, 2, 2); 120 SetSubtreeCount(and, 2, 2); 121 SetSubtreeCount(or, 2, 2); 122 SetSubtreeCount(not, 1, 1); 123 124 SetSubtreeCount(timeLag, 1, 1); 125 SetSubtreeCount(integral, 1, 1); 126 SetSubtreeCount(derivative, 1, 1); 127 SetSubtreeCount(laggedVariable, 0, 0); 128 #endregion 129 130 #region alloed child symbols configuration 124 131 AddAllowedChildSymbol(StartSymbol, realValuedSymbols); 125 132 AddAllowedChildSymbol(DefunSymbol, realValuedSymbols); 133 126 134 AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols); 135 AddAllowedChildSymbol(realValuedSymbols, powerSymbols); 136 AddAllowedChildSymbol(realValuedSymbols, conditionSymbols); 137 AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols); 127 138 128 //var unaryBooleanFunctionSymbols = new List<Symbol>() { not }; 129 //var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and }; 130 //var relationalFunctionSymbols = new List<Symbol>() { gt, lt }; 131 //var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols); 139 AddAllowedChildSymbol(powerSymbols, variableSymbol, 0); 140 AddAllowedChildSymbol(powerSymbols, constant, 1); 132 141 133 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 }; 142 AddAllowedChildSymbol(@if, comparisonSymbols, 0); 143 AddAllowedChildSymbol(@if, booleanOperationSymbols, 0); 144 AddAllowedChildSymbol(@if, conditionSymbols, 1); 145 AddAllowedChildSymbol(@if, realValuedSymbols, 1); 146 AddAllowedChildSymbol(@if, powerSymbols, 1); 147 AddAllowedChildSymbol(@if, timeSeriesSymbols, 1); 148 AddAllowedChildSymbol(@if, conditionSymbols, 2); 149 AddAllowedChildSymbol(@if, realValuedSymbols, 2); 150 AddAllowedChildSymbol(@if, powerSymbols, 2); 151 AddAllowedChildSymbol(@if, timeSeriesSymbols, 2); 134 152 135 var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, timeLag, integral, derivative }; 136 var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean, pow, root, variableCondition }; 153 AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols); 154 AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols); 155 AddAllowedChildSymbol(comparisonSymbols, powerSymbols); 156 AddAllowedChildSymbol(comparisonSymbols, conditionSymbols); 157 AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols); 158 159 AddAllowedChildSymbol(variableCondition, realValuedSymbols); 160 AddAllowedChildSymbol(variableCondition, powerSymbols); 161 AddAllowedChildSymbol(variableCondition, conditionSymbols); 162 AddAllowedChildSymbol(variableCondition, timeSeriesSymbols); 137 163 138 164 165 AddAllowedChildSymbol(timeLag, realValuedSymbols); 166 AddAllowedChildSymbol(timeLag, powerSymbols); 167 AddAllowedChildSymbol(timeLag, conditionSymbols); 139 168 140 //var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable }; 141 //var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if }); 169 AddAllowedChildSymbol(integral, realValuedSymbols); 170 AddAllowedChildSymbol(integral, powerSymbols); 171 AddAllowedChildSymbol(integral, conditionSymbols); 142 172 173 AddAllowedChildSymbol(derivative, realValuedSymbols); 174 AddAllowedChildSymbol(derivative, powerSymbols); 175 AddAllowedChildSymbol(derivative, conditionSymbols); 176 #endregion 177 } 143 178 144 //foreach (var symb in allSymbols) 145 // AddSymbol(symb); 179 public void ConfigureAsDefaultRegressionGrammar() { 180 Symbols.Where(s => s is Average).First().Enabled = false; 181 Symbols.Where(s => s.Name == TrigonometricFunctionsName).First().Enabled = false; 182 Symbols.Where(s => s.Name == PowerFunctionsName).First().Enabled = false; 183 Symbols.Where(s => s.Name == ConditionalSymbolsName).First().Enabled = false; 184 Symbols.Where(s => s.Name == TimeSeriesSymbolsName).First().Enabled = false; 185 } 146 186 147 //foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) { 148 // SetSubtreeCount(unaryFun, 1, 1); 149 //} 150 //foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) { 151 // SetSubtreeCount(binaryFun, 2, 2); 152 //} 153 154 //foreach (var terminalSymbol in terminalSymbols) { 155 // SetSubtreeCount(terminalSymbol, 0, 0); 156 //} 157 158 //SetSubtreeCount(@if, 3, 3); 159 160 161 // allow only real-valued expressions as child of the start symbol 162 //foreach (var symb in realValuedSymbols) { 163 // AddAllowedChildSymbol(StartSymbol, symb); 164 // AddAllowedChildSymbol(DefunSymbol, symb); 165 //} 166 167 //foreach (var symb in unaryFunctionSymbols) { 168 // foreach (var childSymb in realValuedSymbols) { 169 // AddAllowedChildSymbol(symb, childSymb); 170 // } 171 //} 172 173 //foreach (var symb in binaryFunctionSymbols) { 174 // foreach (var childSymb in realValuedSymbols) { 175 // AddAllowedChildSymbol(symb, childSymb); 176 // } 177 //} 178 179 //foreach (var childSymb in booleanSymbols) { 180 // AddAllowedChildSymbol(@if, childSymb, 0); 181 //} 182 //foreach (var childSymb in realValuedSymbols) { 183 // AddAllowedChildSymbol(@if, childSymb, 1); 184 // AddAllowedChildSymbol(@if, childSymb, 2); 185 //} 186 187 //foreach (var symb in relationalFunctionSymbols) { 188 // foreach (var childSymb in realValuedSymbols) { 189 // AddAllowedChildSymbol(symb, childSymb); 190 // } 191 //} 192 //foreach (var symb in binaryBooleanFunctionSymbols) { 193 // foreach (var childSymb in booleanSymbols) { 194 // AddAllowedChildSymbol(symb, childSymb); 195 // } 196 //} 197 //foreach (var symb in unaryBooleanFunctionSymbols) { 198 // foreach (var childSymb in booleanSymbols) { 199 // AddAllowedChildSymbol(symb, childSymb); 200 // } 201 //} 187 public void ConfigureAsDefaultClassificationGrammar() { 188 Symbols.Where(s => s is Average).First().Enabled = false; 189 Symbols.Where(s => s.Name == TrigonometricFunctionsName).First().Enabled = false; 190 Symbols.Where(s => s.Name == ExponentialFunctionsName).First().Enabled = false; 191 Symbols.Where(s => s.Name == PowerFunctionsName).First().Enabled = false; 192 Symbols.Where(s => s.Name == TimeSeriesSymbolsName).First().Enabled = false; 202 193 } 203 194 }
Note: See TracChangeset
for help on using the changeset viewer.